xed-sys 0.6.0+xed-2024.05.20

Rust FFI bindings for Intel XED.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/* BEGIN_LEGAL 

Copyright (c) 2023 Intel Corporation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  
END_LEGAL */
/// @file xed.c


////////////////////////////////////////////////////////////////////////////

#include "xed/xed-interface.h"
#include "xed/xed-immdis.h"
#include "xed-examples-util.h"
#if defined(XED_ENCODER)
# include "xed-enc-lang.h"
#endif
#include "xed-disas-elf.h"
#include "xed-disas-macho.h"
#include "xed-disas-raw.h"
#include "xed-disas-hex.h"
#include "xed-disas-pecoff.h"
#include "xed-disas-filter.h"
#include "xed-symbol-table.h"
#include "xed-nm-symtab.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

int main(int argc, char** argv);
static int intel_asm_emit = 0;

////////////////////////////////////////////////////////////////////////////
#if defined(XED_DECODER)
static xed_uint_t disas_decode( xed_disas_info_t* di,
                                const char* decode_text,
                                xed_decoded_inst_t* xedd,
                                xed_uint64_t runtime_address)
{
    xed_uint8_t hex_decode_text[XED_MAX_INSTRUCTION_BYTES];
    xed_uint_t bytes = xed_convert_ascii_to_hex(decode_text,
                                                hex_decode_text,
                                                XED_MAX_INSTRUCTION_BYTES);
    return  disas_decode_binary(di,
                                hex_decode_text,
                                bytes,
                                xedd,
                                runtime_address);
}
#endif

#if defined(XED_DECODER) && defined(XED_ENCODER)
static unsigned int disas_decode_encode(xed_disas_info_t* di,
                                        const char* decode_text,
                                        xed_decoded_inst_t* xedd,
                                        xed_uint64_t runtime_address)
{
    xed_uint8_t hex_decode_text[XED_MAX_INSTRUCTION_BYTES];
    xed_uint_t bytes = xed_convert_ascii_to_hex( decode_text,
                                                 hex_decode_text, 
                                                 XED_MAX_INSTRUCTION_BYTES);
    return disas_decode_encode_binary(di,
                                      hex_decode_text,
                                      bytes,
                                      xedd,
                                      runtime_address);
}
#endif

static FILE*
fopen_portable(char const* const file_name,
               char const* const mode)
{
    FILE* f = 0;
    if ((file_name == 0) || (file_name[0] == 0))
        return 0;
#if defined(XED_WINDOWS) && !defined(PIN_CRT)
    errno_t error = fopen_s(&f, file_name, mode);
    if (error != 0)
        return 0;
#else
    f = fopen(file_name, mode);
#endif
    return f;
}



#if defined(XED_ENCODER)

static unsigned int disas_encode(const xed_state_t* dstate,
                                 const char* encode_text,
                                 xed_operand_enum_t* operands,
                                 xed_uint32_t* operands_value,
                                 xed_bool_t encode_force)
{
    char buf[5000];
    xed_uint8_t array[XED_MAX_INSTRUCTION_BYTES];
    unsigned int ilen = XED_MAX_INSTRUCTION_BYTES;
    unsigned int olen=0;
    ascii_encode_request_t areq;
    xed_encoder_request_t req;
    xed_error_enum_t r;
    unsigned int i;

    areq.dstate = *dstate;
    areq.command = encode_text;
    req = parse_encode_request(areq);
    for(i = 0; i < XED_MAX_INPUT_OPERNADS; i++) {
        if (operands[i] != XED_OPERAND_INVALID)
            xed3_set_generic_operand(&req, operands[i], operands_value[i]);
    }
    xed3_operand_set_encode_force(&req, encode_force);

    xed_encode_request_print(&req, buf, 5000);
    printf("Request: %s", buf);

    r = xed_encode(&req, array, ilen, &olen);
    if (r != XED_ERROR_NONE)     {
        printf("Could not encode: %s\n", encode_text);
        printf("Error code was: %s\n", xed_error_enum_t2str(r));
        xedex_derror("Dying");
    }
    else if (CLIENT_VERBOSE)   {
        char buf2[XED_HEX_BUFLEN];
        xed_print_hex_line(buf2,array, olen,XED_HEX_BUFLEN);
        printf("Encodable! %s\n", buf2);
        if (intel_asm_emit) 
            xed_print_intel_asm_emit(array,olen);
        else
            xed_print_bytes_pseudo_op(array,olen);
    }
    return olen;
}

static void no_comments(char* buf) {
    size_t i, len = strlen(buf);
    for(i=0;i<len;i++) {
        if (buf[i] == ';' || buf[i] == '#' ||
            buf[i] == '\r' || buf[i] == '\n')
        {
            buf[i]= 0; // stomp on it
            return;
        }
    }
}

static void xed_assemble(const xed_state_t* dstate,
                         const char* encode_file_name)
{
#define ASM_BUF_SIZE 1024
    const xed_int_t bsize = ASM_BUF_SIZE;
    char buf[ASM_BUF_SIZE];
    FILE* f;
    if ((encode_file_name == 0) || (encode_file_name[0] == 0)) {
        printf("illegal 'encode_file_name' file name\n");
        xedex_derror("Dying");
    }
    f = fopen_portable(encode_file_name, "r");
    if (!f) {
        printf("Could not open %s\n", encode_file_name);
        xedex_derror("Dying");
    }

    while(!feof(f) && fgets(buf, bsize, f))
    {
        unsigned int olen=0;
        ascii_encode_request_t areq;
        xed_encoder_request_t req;
        xed_uint8_t array[XED_MAX_INSTRUCTION_BYTES];
        const unsigned int ilen = XED_MAX_INSTRUCTION_BYTES;
        xed_error_enum_t r;
        unsigned int i;
        
        printf("; %s\n",buf);
        no_comments(buf);
        if (strlen(buf) == 0)
            continue;
        areq.dstate = *dstate;
        areq.command = buf;
        req = parse_encode_request(areq);
 
        r = xed_encode(&req, array, ilen, &olen);
        if (r != XED_ERROR_NONE)     {
            printf("Could not encode: %s\n", buf);
            printf("Error code was: %s\n", xed_error_enum_t2str(r));
            xedex_derror("Dying");
        }
        printf("      .byte ");
        for(i=0;i<olen;i++) {
            if (i > 0)
                printf(", ");
            printf("0x%02x",array[i]);
        }
        printf("\n");
    }
    fclose(f);
}
#endif

static void emit_version(void) {
    printf("%s\n", xed_get_copyright());
    printf("XED version: [%s]\n\n", xed_get_version());
}

static void usage(char* prog) {
    unsigned int i;
    static const char* usage_msg[] = {
      "One of the following is required:",
#if defined(__APPLE__)
      "\t-i input_file             (decode macho-format file)",
#elif defined(XED_ELF_READER)
      "\t-i input_file             (decode elf-format file)",
#elif defined(_WIN32)
      "\t-i input_file             (decode pecoff-format file)",
#endif
      "\t-ir raw_input_file        (decode a raw unformatted binary file)",
      "\t-ih hex_input_file        (decode a raw unformatted ASCII hex file)",
      "\t-d hex-string             (decode a sequence of bytes, must be last)",
      "\t-j                        (just decode one instruction when using -d)",
      "\t-F prefix                 (decode ascii hex bytes after prefix)",
      "\t                          (running in filter mode from stdin)",
#if defined(XED_ENCODER)
      "\t-ide input_file           (decode/encode file)",
      "\t-e instruction            (encode, must be last)",
      "\t-f                        (encode force, skip encoder chip check)",
      "\t-ie file-to-assemble      (assemble the contents of the file)",
      "\t-de hex-string            (decode-then-encode, must be last)",
#endif
      "",
      "Optional arguments:",
      "",
      "\t-v N          (0=quiet, 1=errors, 2=useful-info, 3=trace,",
      "\t               5=very verbose)",
      "\t-xv N         (XED engine verbosity, 0...99)",
      "",
      "\t-chip-check CHIP   (count instructions that are not valid for CHIP)",
      "\t-chip-check-list   (list the valid chips)",
      "",
      "\t-s section    (target section for file disassembly,",
      "\t               PECOFF and ELF formats only)",
      "",
      "\t-n N          (number of instructions to decode. Default 100M,",
      "\t               accepts K/M/G qualifiers)",
      " ",
      "\t-b addr       (Base address offset, for DLLs/shared libraries.",
      "\t               Use 0x for hex addresses)",
      "\t-as addr      (Address to start disassembling.",
      "\t               Use 0x for hex addresses)",
      "\t-ae addr      (Address to end   disassembling.",
      "\t               Use 0x for hex addresses)",
      "\t-no-resync    (Disable symbol-based resynchronization algorithm",
      "\t               for disassembly)",
      "\t-ast          (Show the AVX/SSE transition classfication)",
      "\t-histo        (Histogram decode times)",
      "",
      "\t-I            (Intel syntax for disassembly)",
      "\t-A            (ATT SYSV syntax for disassembly)",
      "\t-isa-set      (Emit the XED \"ISA set\" in dissasembly)",
      "\t-xml          (XML formatting)",
      "\t-uc           (upper case hex formatting)",
      "\t-pmd          (positive memory displacement formatting)",
      "\t-nwm          (Format AVX512 without curly braces for writemasks, include k0)",
      "\t-emit         (Output __emit statements for the Intel compiler)",
      "\t-S file       Read symbol table in \"nm\" format from file",
#if defined(XED_DWARF) 
      "\t-line         (Emit line number information, if present)",
#endif
      "\t-dot FN       (Emit a register dependence graph file in dot format.",
      "\t               Best used with -as ADDR -ae ADDR to limit graph size.)",
      "",
      "\t-r            (for REAL_16 mode, 16b addressing (20b addresses),",
      "\t               16b default data size)",
      "\t-r32          (for REAL_32 mode, 16b addressing (20b addresses),",
      "\t               32b default data size)",
      "\t-16           (for LEGACY_16 mode, 16b addressing,",
      "\t               16b default data size)",
      "\t-32           (for LEGACY_32 mode, 32b addressing,",
      "\t               32b default data size -- default)",
      "\t-64           (for LONG_64 mode w/64b addressing",
      "\t               Optional on windows/linux)",
#if defined(XED_MPX)
      "\t-mpx          (Turn on MPX mode for disassembly, default is off)",
#endif
#if defined(XED_CET)
      "\t-cet          (Turn on CET mode for disassembly, default is off)",
#endif
      "\t-s32          (32b stack addressing, default, not in LONG_64 mode)",
      "\t-s16          (16b stack addressing, not in LONG_64 mode)",
      "\t-set OP VAL   (Set a XED operands to some integer value, repeatable)",

#if defined(XED_USING_DEBUG_HELP)
      "",
      "\t-sp           (Search path for windows symbols)",
#endif
      "\t-version      (The version message)",
      "\t-help         (This help message)",
      " ",
      0
    };      

    emit_version();
    printf("Usage: %s [options]\n", prog);
    for(i=0; usage_msg[i]  ; i++)
        printf("%s\n", usage_msg[i]);
}

 

static char const* remove_spaces(char const*  s) { //frees original string
    xed_uint32_t i=0,c=0;
    char* p=0;
    
    if (s == 0)
        return 0;
    
    while(s[i]) {
        if (s[i] != ' ')
            c++;
        i++;
    }
    c++; // add the null
    p = (char*)malloc(c);
    assert(p!=0);
    i=0;
    c=0;
    while(s[i]) {
        if (s[i] != ' ')
            p[c++] = s[i];
        i++;
    }
    p[c]=0;
    free((void*)s);

    return p;
}


static void
test_argc(int i, int argc)
{
    if (i+1 >= argc)
        xedex_derror("Need more arguments. Use \"xed -help\" for usage.");
}




static void list_chips(void)
{
    xed_chip_enum_t c = XED_CHIP_INVALID;
    int i=0;
    for( ; c < XED_CHIP_LAST;  i++ ) {
        if (i > 0 && (i % 3) == 0)
            printf("\n");
        printf("%-25s ", xed_chip_enum_t2str(c));
        c = (xed_chip_enum_t)(c + 1);
    }
    printf("\n");
}


int
main(int argc, char** argv)
{
    xed_bool_t sixty_four_bit = 0;
    xed_bool_t mpx_mode = 0;
    xed_bool_t cet_mode = 0;
    xed_bool_t decode_only = 1;
    char* input_file_name = 0;
    char* symbol_search_path = 0;
    char const* decode_text=0;
    char const* encode_text=0;
    xed_state_t dstate;
    xed_bool_t encode = 0;
    xed_bool_t encode_force = 0;
    xed_uint_t ninst = 100*1000*1000; // FIXME: should use maxint...
    //perf_tail is for skipping first insts in performance measure mode
    unsigned int perf_tail = 0;         
    xed_bool_t decode_encode = 0;
    int i,j;
    unsigned int loop_decode = 0;
    xed_bool_t decode_raw = 0;
    xed_bool_t decode_hex = 0;
    xed_bool_t assemble  = 0;
    char* target_section = 0;
    xed_bool_t use_binary_mode = 1;
    xed_bool_t emit_isa_set = 0;
    xed_uint64_t addr_start = 0;
    xed_uint64_t addr_end = 0;
    xed_uint64_t fake_base = 0;
    xed_bool_t xml_format =0;
    xed_bool_t resync = 0;
    xed_bool_t ast = 0;
    xed_bool_t histo = 0;
    xed_bool_t line_numbers = 0;
    xed_chip_enum_t xed_chip = XED_CHIP_INVALID;
    xed_operand_enum_t operands[XED_MAX_INPUT_OPERNADS] = {XED_OPERAND_INVALID};
    xed_uint32_t operands_value[XED_MAX_INPUT_OPERNADS] = {0};
    xed_uint8_t operands_index = 0;
    xed_bool_t filter = 0;
    xed_bool_t just_decode_first_pattern=0;
#if defined(XED_LINUX)
    char *prefix = NULL;
#endif

    char* dot_output_file_name = 0;
    xed_bool_t dot = 0;
    xed_decoded_inst_t xedd;
    xed_uint_t retval_okay = 1;
    unsigned int obytes=0;
#if defined(XED_DECODER)
    xed_disas_info_t decode_info;
#endif
#if defined(XED_LINUX)
    char *nm_symtab_fn = NULL;
#endif
    
    /* I have this here to test the functionality, if you are happy with
     * the XED formatting options, then you do not need to set this or call
     * xed_format_set_options() */

    xed_format_options_t format_options;
    memset(&format_options,0,sizeof(xed_format_options_t));
#if defined(XED_NO_HEX_BEFORE_SYMBOLIC_NAMES)
    format_options.hex_address_before_symbolic_name=0;
#else
    format_options.hex_address_before_symbolic_name=1;
#endif
    format_options.write_mask_curly_k0 = 1;
    format_options.lowercase_hex = 1;    

    xed_example_utils_init();

    xed_state_init(&dstate,
                   XED_MACHINE_MODE_LEGACY_32,
                   XED_ADDRESS_WIDTH_32b,  /* 2nd parameter ignored */
                   XED_ADDRESS_WIDTH_32b);

    resync = 1;
    client_verbose = 3;
    xed_set_verbosity( client_verbose );
    for( i=1; i < argc ; i++ )    {
#if defined(XED_LINUX)
        if (strcmp(argv[i], "-F") == 0) {
            test_argc(i, argc);
            filter = 1;
            prefix = argv[++i];
            continue;
        } else if (strcmp(argv[i], "-S") == 0) {
            test_argc(i, argc);
            nm_symtab_fn = argv[++i];
            continue;
        }
#endif
        if (strcmp(argv[i], "-no-resync") ==0)   {
            resync = 0;
            continue;
        }
        if (strcmp(argv[i], "-ast") ==0)   {
            ast = 1;
            continue;
        }
        if (strcmp(argv[i], "-histo") ==0)   {
            histo = 1;
            continue;
        }
        else if (strcmp(argv[i],"-d")==0)         {
            test_argc(i,argc);
            for(j=i+1; j< argc;j++) 
                decode_text = xedex_append_string(decode_text,argv[j]);
            break; // leave the i=1...argc loop
        }
        else if (strcmp(argv[i],"-j")==0) {
            just_decode_first_pattern=1;
            continue;
        }
        else if (strcmp(argv[i],"-i")==0)        {
            test_argc(i,argc);
            input_file_name = argv[i+1];
            i++;
        }
#if defined(XED_USING_DEBUG_HELP)
        else if (strcmp(argv[i],"-sp")==0)        {
            test_argc(i,argc);
            symbol_search_path = argv[i+1];
            i++;
        }
#endif
        else if (strcmp(argv[i],"-s")==0)        {
            test_argc(i,argc);
            target_section = argv[i+1];
            i++;
        }
        else if (strcmp(argv[i],"-xml")==0)      {
            format_options.xml_a = 1;
            format_options.xml_f = 1;
            xml_format = 1;
        }
        else if (strcmp(argv[i],"-pmd")==0)      {
             format_options.positive_memory_displacements=1;
        }
        else if (strcmp(argv[i],"-uc")==0)      {
            format_options.lowercase_hex = 0; // use uppercase hex
        }
        else if (strcmp(argv[i],"-nwm")==0)      {
            format_options.write_mask_curly_k0 = 0;
        }
#if defined(XED_DWARF) 
        else if (strcmp(argv[i],"-line")==0)      {
            line_numbers = 1;
        }
#endif
        else if (strcmp(argv[i],"-dot")==0)      {
            test_argc(i,argc);
            dot_output_file_name = argv[i+1];
            dot = 1;
            i++;
        }
        else if (strcmp(argv[i],"-ir")==0)        {
            test_argc(i,argc);
            input_file_name = argv[i+1];
            decode_raw = 1;
            i++;
        }
        else if (strcmp(argv[i],"-ih")==0)        {
            test_argc(i,argc);
            input_file_name = argv[i+1];
            decode_hex = 1;
            i++;
        }
#if defined(XED_ENCODER)
        else if (strcmp(argv[i],"-f") == 0) {
            encode_force = 1;
            continue;
        }
        else if (strcmp(argv[i],"-e") == 0) {
            encode = 1;
            test_argc(i,argc);
            // merge the rest of the args in to the encode_text string.
            for( j = i+1; j< argc; j++ )  {
                encode_text = xedex_append_string(encode_text, argv[j]);
                encode_text = xedex_append_string(encode_text, " ");
            }
            break;  // leave the loop
        }
        else if (strcmp(argv[i],"-de")==0)        {
            test_argc(i,argc);
            decode_encode = 1;
            for(j=i+1; j< argc;j++) 
                decode_text = xedex_append_string(decode_text,argv[j]);
            break; // leave the i=1...argc loop
        }
        else if (strcmp(argv[i],"-ie")==0)        {
            test_argc(i,argc);
            input_file_name = argv[i+1];
            assemble = 1;
            i++;
        }
        else if (strcmp(argv[i],"-ide")==0)        {
            test_argc(i,argc);
            input_file_name = argv[i+1];
            decode_only = 0;
            i++;
        }
#endif
        else if (strcmp(argv[i],"-n") ==0)         {
            test_argc(i,argc);
            ninst = XED_STATIC_CAST(xed_uint_t,
                xed_atoi_general(argv[i+1],1000));
            i++;
        }
        else if (strcmp(argv[i],"-perftail") ==0)         {
            // undocumented. not an interesting knob for most users.
            test_argc(i,argc);
            perf_tail = XED_STATIC_CAST(unsigned int,
                xed_atoi_general(argv[i+1],1000));
            i++;
        }
        else if (strcmp(argv[i],"-b") ==0)         {
            test_argc(i,argc);
            fake_base = (xed_uint64_t)xed_atoi_general(argv[i+1],1000);
            printf("ASSUMED BASE = " XED_FMT_LX "\n",fake_base);
            i++;
        }
        else if (strcmp(argv[i],"-as") == 0 || strcmp(argv[i],"-sa") == 0)    {
            test_argc(i,argc);
            addr_start = XED_STATIC_CAST(xed_uint64_t,
                                         xed_atoi_general(argv[i+1],1000));
            i++;
        }
        else if (strcmp(argv[i],"-ae") == 0 || strcmp(argv[i],"-ea") == 0)    {
            test_argc(i,argc);
            addr_end = XED_STATIC_CAST(xed_uint64_t,
                                       xed_atoi_general(argv[i+1],1000));
            i++;
        }

        else if (strcmp(argv[i],"-loop") ==0)         {
            test_argc(i,argc);
            loop_decode = XED_STATIC_CAST(unsigned int,
                                          xed_atoi_general(argv[i+1],1000));
            i++;
        }
        else if (strcmp(argv[i],"-v") ==0)         {
            test_argc(i,argc);
            client_verbose = XED_STATIC_CAST(int,xed_atoi_general(argv[i+1],1000));
            xed_set_verbosity(client_verbose);

            i++;
        }
        else if (strcmp(argv[i],"-xv") ==0)        {
            int xed_engine_verbose;
            test_argc(i,argc);
            xed_engine_verbose = XED_STATIC_CAST(xed_int_t,
                                                 xed_atoi_general(argv[i+1],1000));
            xed_set_verbosity(xed_engine_verbose);
            i++;
        }
        else if (strcmp(argv[i],"-chip-check")==0)        {
            test_argc(i,argc);
            xed_chip = str2xed_chip_enum_t(argv[i+1]);
            printf("Setting chip to %s\n", xed_chip_enum_t2str(xed_chip));
            if (xed_chip == XED_CHIP_INVALID) {
                printf("Invalid chip name specified. Use -chip-check-list to "
                       "see the valid chip names.\n");
                exit(1);
            }
            i++;
        }
        else if (strcmp(argv[i],"-chip-check-list")==0)        {
            list_chips();
            exit(0);
        }
        else if (strcmp(argv[i],"-A")==0)        {
            global_syntax = XED_SYNTAX_ATT;
        }
        else if (strcmp(argv[i],"-I")==0)        {
            global_syntax = XED_SYNTAX_INTEL;
        }
        else if (strcmp(argv[i],"-X")==0)        { // undocumented
            global_syntax = XED_SYNTAX_XED;
        }
        else if (strcmp(argv[i],"-isa-set")==0)   {
            emit_isa_set = 1;
        }
        else if (strcmp(argv[i],"-r32")==0)         {
            sixty_four_bit = 0;
            dstate.mmode = XED_MACHINE_MODE_REAL_32;
            dstate.stack_addr_width = XED_ADDRESS_WIDTH_16b;
            use_binary_mode = 0;
        }
        else if (strcmp(argv[i],"-r")==0)         {
            sixty_four_bit = 0;
            dstate.mmode = XED_MACHINE_MODE_REAL_16;
            dstate.stack_addr_width = XED_ADDRESS_WIDTH_16b;
            use_binary_mode = 0;
        }
        else if (strcmp(argv[i],"-16")==0)         {
            sixty_four_bit = 0;
            dstate.mmode = XED_MACHINE_MODE_LEGACY_16;
            use_binary_mode = 0;
        }
        else if (strcmp(argv[i],"-32")==0) { // default
            sixty_four_bit = 0;
            dstate.mmode = XED_MACHINE_MODE_LEGACY_32;
            use_binary_mode = 0;
        }
        else if (strcmp(argv[i],"-64")==0)         {
            sixty_four_bit = 1;
            dstate.mmode = XED_MACHINE_MODE_LONG_64;
            use_binary_mode = 0;
        }
#if defined(XED_MPX)
        else if (strcmp(argv[i],"-mpx")==0)         {
            mpx_mode = 1;
        }
#endif
#if defined(XED_CET)
        else if (strcmp(argv[i],"-cet")==0)         {
            cet_mode = 1;
        }
#endif
        else if (strcmp(argv[i],"-s32")==0) {
            dstate.stack_addr_width = XED_ADDRESS_WIDTH_32b;
            use_binary_mode = 0;
        }
        else if (strcmp(argv[i],"-s16")==0) {
            dstate.stack_addr_width = XED_ADDRESS_WIDTH_16b;
            use_binary_mode = 0;
        }
        else if (strcmp(argv[i],"-set")==0) {
            test_argc(i+1,argc); // need 2 args
            if (operands_index >= XED_MAX_INPUT_OPERNADS) {
                printf("ERROR: too many -set operands, max is %d\n", XED_MAX_INPUT_OPERNADS);
                exit(1);
            }

            operands[operands_index] = str2xed_operand_enum_t(argv[i+1]);
            operands_value[operands_index] = XED_STATIC_CAST(xed_uint32_t,
                                            xed_atoi_general(argv[i+2],1000));
            operands_index++;
            i += 2;
        }
#if 0
        else if (strcmp(argv[i],"-ti") ==0)        {
            client_verbose = 5;
            xed_set_verbosity(5);
            test_immdis();
            exit(1);
        }
#endif
        else if (strcmp(argv[i],"-emit") ==0) {
            intel_asm_emit = 1;
        }
        else if (strcmp(argv[i],"-version") == 0 ) {
            emit_version();
            exit(0);
        }
        else   {
            usage(argv[0]);
            exit(1);
        }
    }
    if (!encode)     {
        if (input_file_name == 0 &&
            (decode_text == 0 ||
             strlen(decode_text) == 0) && !filter)
        {
            printf("ERROR: required argument(s) were missing\n");
            usage(argv[0]);
            exit(1);
        }
    }

#if defined(XED_LINUX)
    if ((nm_symtab_fn != 0) && (nm_symtab_fn[0] != 0)) {
        if (!filter) {
            printf("ERROR: -S only support with -F for now\n");
            exit(1);
        }
        xed_read_nm_symtab(nm_symtab_fn);
    }
#endif

    if (CLIENT_VERBOSE2)
        printf("Initializing XED tables...\n");

    xed_tables_init();

    if (CLIENT_VERBOSE2)
        printf("Done initialing XED tables.\n");

    if (decode_text) {
        decode_text = remove_spaces(decode_text);
        assert(decode_text);
    }
    
#if defined(XED_DECODER)
    xed_format_set_options(format_options);
#endif

    if (CLIENT_VERBOSE1) 
        printf("#XED version: [%s]\n", xed_get_version());

    retval_okay = 1;
    obytes=0;

#if defined(XED_DECODER)
    xed_disas_info_init(&decode_info);
    decode_info.input_file_name  = input_file_name;
    decode_info.symbol_search_path = symbol_search_path;
    decode_info.dstate           = dstate;
    decode_info.ninst            = ninst;
    decode_info.decode_only      = decode_only;
    decode_info.sixty_four_bit   = sixty_four_bit;
    decode_info.target_section   = target_section;
    decode_info.use_binary_mode  = use_binary_mode;
    decode_info.addr_start       = addr_start;
    decode_info.addr_end         = addr_end;
    decode_info.xml_format       = xml_format;
    decode_info.fake_base        = fake_base;
    decode_info.resync           = resync;
    decode_info.line_numbers     = line_numbers;
    decode_info.perf_tail_start  = perf_tail;
    decode_info.ast              = ast;
    decode_info.histo            = histo;
    decode_info.chip             = xed_chip;
    decode_info.mpx_mode         = mpx_mode;
    decode_info.cet_mode         = cet_mode;
    decode_info.emit_isa_set     = emit_isa_set;
    decode_info.format_options   = format_options;
    decode_info.encode_force     = encode_force;
    decode_info.dot_graph_output = 0;
    memcpy(decode_info.operands, operands, sizeof(decode_info.operands));
    memcpy(decode_info.operands_value, operands_value, sizeof(decode_info.operands_value));
    
    if (dot)
    {
        decode_info.dot_graph_output = fopen_portable(dot_output_file_name,"w");
        if (!decode_info.dot_graph_output) {
            printf("Could not open %s\n", dot_output_file_name);
            xedex_derror("Dying");
        }
    }
    
    init_xedd(&xedd, &decode_info);
    
#endif

#if defined(XED_LINUX)
    if (filter)
    {
#if defined(XED_DECODER)
        retval_okay = disas_filter(&xedd, prefix, &decode_info);
#endif
    } else
#endif

    if (assemble)
    {
#if defined(XED_ENCODER)
        xed_assemble(&dstate, input_file_name);
#endif
    }
    else if (decode_encode)
    {
#if defined(XED_DECODER) && defined(XED_ENCODER)
        assert(decode_text);
        obytes = disas_decode_encode(&decode_info,
                                     decode_text,
                                     &xedd,
                                     fake_base);
#endif
        retval_okay = (obytes != 0) ? 1 : 0;
    }
    else if (encode)
    {
#if defined(XED_ENCODER)
        assert(encode_text != 0);
        obytes = disas_encode(&dstate,
                              encode_text,
                              operands,
                              operands_value,
                              encode_force);
#endif
    }
    else if (decode_text && strlen(decode_text) != 0)
    {
#if defined(XED_DECODER)
        if (loop_decode)
        {
            unsigned int k;
            for(k=0;k<loop_decode;k++) {
                retval_okay = disas_decode(&decode_info,
                                           decode_text,
                                           &xedd,
                                           fake_base);
                init_xedd(&xedd, &decode_info);
            }
        }
        else
        {
            char const* p = decode_text;
            // 2 bytes per nibble
            unsigned int remaining = (unsigned int)strlen(decode_text) / 2; 
            do {
                unsigned int len;
                retval_okay = disas_decode(&decode_info, p, &xedd, fake_base);
                len = xed_decoded_inst_get_length(&xedd);
                p+=len*2;
                if (len < remaining) {
                    remaining -= len;
                    init_xedd(&xedd, &decode_info);
                }
                else {
                    remaining = 0;
                }
            }
            while(just_decode_first_pattern==0 && retval_okay && remaining > 0);
        }
#endif
    }
    else
    {
#if defined(XED_DECODER)
        if (xml_format) {
            printf("<?xml version=\"1.0\"?>\n");
            printf("<XEDDISASM>\n");
            printf("<XEDFORMAT>1</XEDFORMAT>\n");
        }
        if (decode_raw) {
            xed_disas_raw(&decode_info);
        }
        else if (decode_hex) {
            xed_disas_hex(&decode_info);
        }
        else
        {
#if defined(__APPLE__)
            xed_disas_macho(&decode_info);
#elif defined(XED_ELF_READER)
            xed_disas_elf(&decode_info);
#elif defined(_WIN32)
            xed_disas_pecoff(&decode_info);
#else
            xedex_derror("No PECOFF, ELF or MACHO support compiled in");
#endif
            printf("# Total Errors: " XED_FMT_LU "\n", decode_info.errors);
            if (decode_info.chip)
                printf("# Total Chip Check Errors: " XED_FMT_LU "\n",
                       decode_info.errors_chip_check);
        }
#endif // XED_DECODER
    }
    
    if (xml_format) 
        printf("</XEDDISASM>\n");


    if (retval_okay==0) 
        exit(1);
    if (decode_info.dot_graph_output)
        fclose(decode_info.dot_graph_output);
    if (decode_text)
        free((void*)decode_text);
#if defined(XED_ENCODER)
    if (encode_text)
        free((void*)encode_text);
#else
    (void) encode_text;
#endif
    (void) obytes;
    return 0;
#if !defined(XED_DECODER)
    // pacify the compiler for encoder-only builds:
    (void) xedd;
    (void) sixty_four_bit;
    (void) decode_only;
    (void) symbol_search_path;
    (void) ninst;
    (void) perf_tail;
    (void) loop_decode;
    (void) decode_raw;
    (void) decode_hex;
    (void) target_section;
    (void) addr_start;
    (void) addr_end;
    (void) resync;
    (void) ast;
    (void) histo;
    (void) line_numbers;
    (void) dot_output_file_name;
    (void) dot;
    (void) use_binary_mode;
    (void) emit_isa_set;
#endif
}
 

////////////////////////////////////////////////////////////////////////////