// File: /workspace/github_c_makefile_star2_O1/mikebdp2[P]AtomDis/main.c
// Function: main
int main (int argc, char *argv[])
{
    int            c;
    int            opt_off = 0;
    const char    *opt_addrformat = "%04x: ";
    char         **arg;
    int            fdmem;
    uint8_t       *data;
    bios_tables_t *tabs;
    int            off, start, len, last;

    opterr = 0;
    while ( (c = getopt (argc, argv, "o:r:a:A")) != -1)
	switch (c) {
	case 'o':
	    opt_off = strtol (optarg, NULL, 16);
	    break;
	case 'r':
	    index_load_registers (optarg);
	    break;
	case 'a':
	    opt_addrformat = optarg;
	    break;
	case 'A':
	    opt_reg_addresses = 1;
	    break;
	default:
	    usage (argv);
	}

    if (! argv[optind] || argv[optind][0] == '-')
	usage (argv);

    if ( (fdmem = open (argv[optind], O_RDONLY)) == -1) {
	perror (argv[optind]);
	return 1;
    }
    
#if USE_MMAP 
    if ( (data = mmap (NULL, MMAP_SIZE, PROT_READ, MAP_PRIVATE, fdmem, opt_off))
	 == (void *) -1) {
	perror ("mmap()");
	return 1;
    }
#else
    if (! (data = calloc (1, MMAP_SIZE)) ) {
	perror ("calloc()");
	return 1;
    }
    if ( (len = read (fdmem, data, MMAP_SIZE)) <= 0) {
	perror ("read()");
	return 1;
    }
    printf ("Read %x bytes of data from %s\n\n", len, argv[optind]);
    init_data_dumpers ();
#endif

    for (arg = &argv[optind+1]; *arg && **arg; arg++) {
	last_reg_index  = INDEX_NONE;
	last_reg_offset = 0;
	addrtypes_shift[D_REG] = 0;
	if (arg[0][1])
	    usage (argv);
	switch (arg[0][0]) {
	case 'i':
	    tabs = get_pointers (data);
	    do_info (tabs);
	    break;
	case 'l':
	    tabs = get_pointers (data);
	    do_list (tabs);
	    break;
	case 'x':
	    if (!arg[1] || !arg[2])
		usage (argv);
	    start = strtol (arg[1], NULL, 16);
	    len   = strtol (arg[2], NULL, 16);
	    arg  += 2;
	    do_dump (data, start, start+len);
	    break;
	case 'd':
	    if (!arg[1])
		usage (argv);
	    start = strtol (arg[1], NULL, 16);
	    arg++;
	    tabs  = get_pointers (data);
	    off   = get_table_offset(tabs->MasterDataTables, start);
	    len   = do_tableinfo (data, off, INDEX_DATA_TABLE, start);
	    if (off && len) {
		do_dump (data + off, 4, len);
		do_data (data + off, 0, start);
	    }
	    break;
	case 'c':
	    if (!arg[1])
		usage (argv);
	    start = strtol (arg[1], NULL, 16);
	    arg++;
	    tabs  = get_pointers (data);
	    off   = get_table_offset(tabs->MasterCommandTables, start);
	    len   = do_tableinfo (data, off, INDEX_COMMAND_TABLE, start);
	    if (off && len)
		do_diss (data + off, 6, len, opt_addrformat);
	    break;
	case 'C':
	    if (!arg[1])
		usage (argv);
	    off   = strtol (arg[1], NULL, 16);
	    arg++;
	    len   = do_tableinfo (data, off, INDEX_COMMAND_TABLE, -1);
	    do_diss (data + off, 6, len, opt_addrformat);
	    break;
	case 'T':
	    do_test (data);
	    break;
	case 'F':
	    tabs = get_pointers (data);
	    do_info (tabs);
	    do_list (tabs);
	    fputs ("\n*** Command Tables:\n\n", stdout);
	    last = 0;
	    for (start = 0; start < sizeof (ATOM_MASTER_LIST_OF_COMMAND_TABLES)
		 / sizeof (uint16_t); start++) {
		last_reg_index  = INDEX_NONE;
		last_reg_offset = 0;
		off   = get_table_offset(tabs->MasterCommandTables, start);
		len   = do_tableinfo (data, off, INDEX_COMMAND_TABLE, start);
		if (off && len) {
		    do_diss (data + off, 6, len, opt_addrformat);
		    fputs ("\n", stdout);
		}
	    }
	    fputs ("\n*** Data Tables:\n\n", stdout);
	    last = 0;
	    /* Data table #0 is reseverd (SET_DATA_BLOCK 0 == BIOS start */
	    for (start = 1; start < sizeof (ATOM_MASTER_LIST_OF_DATA_TABLES)
		 / sizeof (uint16_t); start++) {
		off   = get_table_offset(tabs->MasterDataTables, start);
		len   = do_tableinfo (data, off, INDEX_DATA_TABLE, start);
		if (off && len) {
		    do_dump (data + off, 4, len);
		    do_data (data + off, 0, start);
		    fputs ("\n", stdout);
		}
	    }
	    break;
	default:
	    usage (argv);
	}
    }

#if USE_MMAP
    munmap (data, MMAP_SIZE);
#endif
    close  (fdmem);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movq 0x3661b(%rip), %rax     # 0x3bff0
movl $0x0, (%rax)
leaq 0x1d04f(%rip), %r12     # 0x22a31
leaq 0x1d04f(%rip), %r15     # 0x22a38
movq 0x365e0(%rip), %r14     # 0x3bfd0
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x41c0
cmpl $0x6e, %eax
jle 0x5a21
cmpl $0x72, %eax
je 0x5a2f
cmpl $0x6f, %eax
jne 0x5f34
movq (%r14), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x4160
jmp 0x59f0
cmpl $0x41, %eax
jne 0x5a39
movb $0x1, 0x3772f(%rip)     # 0x3d15c
jmp 0x59f0
movq (%r14), %rdi
callq 0x21527
jmp 0x59f0
cmpl $-0x1, %eax
je 0x5a4c
cmpl $0x61, %eax
jne 0x5f34
movq (%r14), %r12
jmp 0x59f0
movq 0x36575(%rip), %r14     # 0x3bfc8
movslq (%r14), %rax
movq (%rbx,%rax,8), %rdi
testq %rdi, %rdi
je 0x5f4c
cmpb $0x2d, (%rdi)
je 0x5f4c
xorl %esi, %esi
xorl %eax, %eax
callq 0x4180
cmpl $-0x1, %eax
je 0x5f0e
movl %eax, %ebp
movl $0x1, %edi
movl $0x100000, %esi         # imm = 0x100000
callq 0x4120
testq %rax, %rax
je 0x5f17
movq %rax, %r15
movl $0x100000, %edx         # imm = 0x100000
movl %ebp, %edi
movq %rax, %rsi
callq 0x40f0
testl %eax, %eax
jle 0x5f20
movl %ebp, 0x1c(%rsp)
movq %r12, 0x10(%rsp)
movslq (%r14), %rcx
movq (%rbx,%rcx,8), %rdx
leaq 0x1cf87(%rip), %rdi     # 0x22a50
xorl %r12d, %r12d
movl %eax, %esi
xorl %eax, %eax
callq 0x40b0
callq 0x21470
movslq (%r14), %rcx
movq 0x8(%rbx,%rcx,8), %rax
testq %rax, %rax
je 0x5ef3
leaq (%rbx,%rcx,8), %r13
leaq (%rbx,%rcx,8), %r14
addq $0x8, %r14
cmpb $0x0, (%rax)
je 0x5ef3
movl %r12d, 0x3764d(%rip)    # 0x3d154
movl %r12d, 0x3764a(%rip)    # 0x3d158
movl %r12d, 0x375eb(%rip)    # 0x3d100
cmpb $0x0, 0x1(%rax)
jne 0x5f3c
movzbl (%rax), %eax
cmpl $0x62, %eax
jle 0x5baf
leal -0x63(%rax), %ecx
cmpl $0x9, %ecx
ja 0x5bd4
leaq 0x1c4d2(%rip), %rdx     # 0x22010
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x10(%r13), %rdi
testq %rdi, %rdi
je 0x5f64
addq $0x10, %r13
xorl %esi, %esi
movl $0x10, %edx
callq 0x4160
movq %rax, %r14
movq %r15, %rdi
callq 0x4d1b
movq 0x375ca(%rip), %rdi     # 0x3d140
movl %r14d, %esi
callq 0x4d89
movl %eax, %ebp
movq %r15, %rdi
movl %eax, %esi
movl $0x1, %edx
movl %r14d, %ecx
callq 0x5309
testl %ebp, %ebp
setne %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5d63
movslq %ebp, %rdi
jmp 0x5d4f
cmpl $0x43, %eax
je 0x5d17
cmpl $0x46, %eax
je 0x5d6b
cmpl $0x54, %eax
jne 0x5f54
callq 0x5867
jmp 0x5edf
cmpl $0x78, %eax
jne 0x5f54
movq 0x10(%r13), %rdi
testq %rdi, %rdi
je 0x5f44
cmpq $0x0, 0x18(%r13)
je 0x5f44
addq $0x18, %r13
xorl %esi, %esi
movl $0x10, %edx
callq 0x4160
movq %rax, %r14
movq (%r13), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x4160
leal (%rax,%r14), %edx
movq %r15, %rdi
movl %r14d, %esi
callq 0x546e
jmp 0x5d63
movq 0x10(%r13), %rdi
testq %rdi, %rdi
je 0x5f5c
addq $0x10, %r13
xorl %esi, %esi
movl $0x10, %edx
callq 0x4160
movq %rax, %rbp
movq %r15, %rdi
callq 0x4d1b
movq 0x374ed(%rip), %rdi     # 0x3d148
movl %ebp, %esi
callq 0x4d89
movl %eax, %r14d
movq %r15, %rdi
movl %eax, %esi
movl $0x2, %edx
movl %ebp, %ecx
callq 0x5309
testl %r14d, %r14d
setne %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5d63
movslq %r14d, %r14
addq %r15, %r14
movq %r14, %rdi
movl $0x4, %esi
movl %eax, %edx
callq 0x546e
movq %r14, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x55af
jmp 0x5d63
movq %r15, %rdi
callq 0x4d1b
leaq 0x3746f(%rip), %rdi     # 0x3d130
callq 0x5136
jmp 0x5edf
movq %r15, %rdi
callq 0x4d1b
movq 0x362e6(%rip), %rax     # 0x3bfc0
movq (%rax), %rdi
movq 0x37454(%rip), %rax     # 0x3d138
movzbl 0x2(%rax), %edx
movzbl 0x3(%rax), %ecx
movzwl 0x10(%rax), %r8d
addq 0x37438(%rip), %r8      # 0x3d130
movzwl 0x18(%rax), %r9d
movzwl 0x1a(%rax), %eax
movl %eax, (%rsp)
leaq 0x1c6b7(%rip), %rsi     # 0x223c2
xorl %eax, %eax
callq 0x4150
jmp 0x5edf
movq 0x10(%r13), %rdi
testq %rdi, %rdi
je 0x5f6c
addq $0x10, %r13
xorl %esi, %esi
movl $0x10, %edx
callq 0x4160
movq %rax, %r14
movq %r15, %rdi
movl %r14d, %esi
movl $0x1, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x5309
movslq %r14d, %rdi
addq %r15, %rdi
movl $0x6, %esi
movl %eax, %edx
movq 0x10(%rsp), %rcx
callq 0x56db
movq %r13, %r14
jmp 0x5edf
movq %r14, 0x20(%rsp)
movq %r15, %rdi
callq 0x4d1b
movq 0x36241(%rip), %r14     # 0x3bfc0
movq (%r14), %rdi
movq 0x373af(%rip), %rax     # 0x3d138
movzbl 0x2(%rax), %edx
movzbl 0x3(%rax), %ecx
movzwl 0x10(%rax), %r8d
addq 0x37393(%rip), %r8      # 0x3d130
movzwl 0x18(%rax), %r9d
movzwl 0x1a(%rax), %eax
movl %eax, (%rsp)
xorl %ebp, %ebp
leaq 0x1c610(%rip), %rsi     # 0x223c2
xorl %eax, %eax
callq 0x4150
leaq 0x37370(%rip), %rdi     # 0x3d130
callq 0x5136
movq (%r14), %rsi
leaq 0x1cca1(%rip), %rdi     # 0x22a70
callq 0x40c0
movq 0x10(%rsp), %r13
movl %r12d, 0x37374(%rip)    # 0x3d154
movl %r12d, 0x37371(%rip)    # 0x3d158
movq 0x37352(%rip), %rdi     # 0x3d140
movl %ebp, %esi
callq 0x4d89
movl %eax, %r14d
movq %r15, %rdi
movl %eax, %esi
movl $0x1, %edx
movl %ebp, %ecx
callq 0x5309
testl %r14d, %r14d
setne %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5e46
movslq %r14d, %rdi
addq %r15, %rdi
movl $0x6, %esi
movl %eax, %edx
movq %r13, %rcx
callq 0x56db
movq 0x36189(%rip), %rax     # 0x3bfc0
movq (%rax), %rsi
leaq 0x1cc58(%rip), %rdi     # 0x22a99
callq 0x40c0
incl %ebp
cmpl $0x50, %ebp
jne 0x5dd9
movq 0x3616c(%rip), %rax     # 0x3bfc0
movq (%rax), %rsi
leaq 0x1cc29(%rip), %rdi     # 0x22a87
callq 0x40c0
movl $0x1, %ebp
movq 0x372d9(%rip), %rdi     # 0x3d148
movl %ebp, %esi
callq 0x4d89
movl %eax, %r14d
movq %r15, %rdi
movl %eax, %esi
movl $0x2, %edx
movl %ebp, %ecx
callq 0x5309
testl %r14d, %r14d
setne %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5ed3
movslq %r14d, %r14
addq %r15, %r14
movq %r14, %rdi
movl $0x4, %esi
movl %eax, %edx
callq 0x546e
movq %r14, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x55af
movq 0x360fc(%rip), %rax     # 0x3bfc0
movq (%rax), %rsi
leaq 0x1cbcb(%rip), %rdi     # 0x22a99
callq 0x40c0
incl %ebp
cmpl $0x22, %ebp
jne 0x5e68
movq 0x20(%rsp), %r14
movq %r14, %r13
addq $0x8, %r14
movq 0x8(%r13), %rax
testq %rax, %rax
jne 0x5af7
movl 0x1c(%rsp), %edi
callq 0x40e0
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq (%r14), %rax
movq (%rbx,%rax,8), %rdi
jmp 0x5f27
leaq 0x1cb22(%rip), %rdi     # 0x22a40
jmp 0x5f27
leaq 0x1cb22(%rip), %rdi     # 0x22a49
callq 0x41a0
movl $0x1, %r12d
jmp 0x5efc
movq %rbx, %rdi
callq 0x4366
movq %rbx, %rdi
callq 0x4255
movq %rbx, %rdi
callq 0x42f1
movq %rbx, %rdi
callq 0x433f
movq %rbx, %rdi
callq 0x4318
movq %rbx, %rdi
callq 0x42ca
movq %rbx, %rdi
callq 0x42a3
movq %rbx, %rdi
callq 0x427c