// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movq 0x37773(%rip), %rax     # 0x3cff0
andl $0x0, (%rax)
leaq 0x1c1aa(%rip), %rax     # 0x21a31
movq %rax, 0x10(%rsp)
leaq 0x1c1a5(%rip), %r15     # 0x21a38
movq 0x37736(%rip), %r12     # 0x3cfd0
pushq $0x10
popq %r14
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x41c0
cmpl $0x41, %eax
je 0x58d5
cmpl $0x6f, %eax
je 0x58c5
cmpl $0x72, %eax
jne 0x58de
movq (%r12), %rdi
callq 0x208c2
jmp 0x589e
movq (%r12), %rdi
xorl %esi, %esi
movl %r14d, %edx
callq 0x4160
jmp 0x589e
movb $0x1, 0x38880(%rip)     # 0x3e15c
jmp 0x589e
cmpl $-0x1, %eax
je 0x58f7
cmpl $0x61, %eax
jne 0x5d35
movq (%r12), %rax
movq %rax, 0x10(%rsp)
jmp 0x589e
movq 0x376ca(%rip), %r14     # 0x3cfc8
movslq (%r14), %rax
movq (%rbx,%rax,8), %rdi
testq %rdi, %rdi
je 0x5d35
cmpb $0x2d, (%rdi)
je 0x5d35
xorl %esi, %esi
xorl %eax, %eax
callq 0x4180
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
je 0x5d12
pushq $0x1
popq %rdi
movl $0x100000, %esi         # imm = 0x100000
callq 0x4120
testq %rax, %rax
je 0x5d1b
movq %rax, %r15
movl $0x100000, %edx         # imm = 0x100000
movl 0xc(%rsp), %edi
movq %rax, %rsi
callq 0x40f0
testl %eax, %eax
jle 0x5d24
movslq (%r14), %rcx
movq (%rbx,%rcx,8), %rdx
leaq 0x1c0e3(%rip), %rdi     # 0x21a50
movl %eax, %esi
xorl %eax, %eax
callq 0x40b0
callq 0x2082a
movslq (%r14), %rax
leaq (%rbx,%rax,8), %r14
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x5cf8
cmpb $0x0, (%rax)
je 0x5cf8
andl $0x0, 0x387b5(%rip)     # 0x3e154
andl $0x0, 0x387b2(%rip)     # 0x3e158
andl $0x0, 0x38753(%rip)     # 0x3e100
cmpb $0x0, 0x1(%rax)
jne 0x5d35
leaq 0x8(%r14), %r13
movzbl (%rax), %eax
leal -0x63(%rax), %ecx
cmpl $0x9, %ecx
jbe 0x5a35
cmpl $0x43, %eax
je 0x5b69
cmpl $0x46, %eax
je 0x5bb2
cmpl $0x54, %eax
je 0x5b5c
cmpl $0x78, %eax
jne 0x5d35
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x5d35
cmpq $0x0, 0x18(%r14)
je 0x5d35
addq $0x18, %r14
xorl %esi, %esi
pushq $0x10
popq %rbp
movl %ebp, %edx
callq 0x4160
movq %rax, %r12
movq (%r14), %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x4160
leal (%rax,%r12), %edx
movq %r15, %rdi
movl %r12d, %esi
callq 0x535e
jmp 0x5982
leaq 0x1b5d4(%rip), %rdx     # 0x21010
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x5d35
addq $0x10, %r14
xorl %esi, %esi
pushq $0x10
popq %rdx
callq 0x4160
movq %rax, %rbp
movq %r15, %rdi
callq 0x4b7b
movq 0x386ce(%rip), %rdi     # 0x3e140
movl %ebp, %esi
callq 0x4c08
movl %eax, %r12d
movq %r15, %rdi
movl %eax, %esi
pushq $0x1
popq %rdx
movl %ebp, %ecx
callq 0x51f3
testl %r12d, %r12d
setne %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5982
movslq %r12d, %rdi
jmp 0x5b9b
movq %r15, %rdi
callq 0x4b7b
leaq 0x38678(%rip), %rdi     # 0x3e130
callq 0x4fd6
jmp 0x5b61
movq %r15, %rdi
callq 0x4b7b
leaq 0x3865f(%rip), %rdi     # 0x3e130
callq 0x5014
jmp 0x5b61
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x5d35
addq $0x10, %r14
xorl %esi, %esi
pushq $0x10
popq %rdx
callq 0x4160
movq %rax, %r12
movq %r15, %rdi
callq 0x4b7b
movq 0x38640(%rip), %rdi     # 0x3e148
movl %r12d, %esi
callq 0x4c08
movl %eax, %ebp
movq %r15, %rdi
movl %eax, %esi
pushq $0x2
popq %rdx
movl %r12d, %ecx
callq 0x51f3
testl %ebp, %ebp
setne %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5982
movslq %ebp, %rbp
addq %r15, %rbp
movq %rbp, %rdi
pushq $0x4
popq %rsi
movl %eax, %edx
callq 0x535e
movq %rbp, %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x5494
jmp 0x5982
callq 0x571f
movq %r13, %r14
jmp 0x5982
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x5d35
addq $0x10, %r14
xorl %esi, %esi
pushq $0x10
popq %rdx
callq 0x4160
movq %rax, %r12
movq %r15, %rdi
movl %r12d, %esi
pushq $0x1
popq %rdx
pushq $-0x1
popq %rcx
callq 0x51f3
movslq %r12d, %rdi
addq %r15, %rdi
pushq $0x6
popq %rsi
movl %eax, %edx
movq 0x10(%rsp), %rcx
callq 0x55b6
jmp 0x5982
movq %r15, %rdi
callq 0x4b7b
leaq 0x3856f(%rip), %r14     # 0x3e130
movq %r14, %rdi
callq 0x4fd6
movq %r14, %rdi
callq 0x5014
movq 0x373e8(%rip), %rax     # 0x3cfc0
movq (%rax), %rsi
leaq 0x1be8e(%rip), %rdi     # 0x21a70
callq 0x40c0
xorl %r12d, %r12d
movq 0x10(%rsp), %r14
cmpl $0x50, %r12d
je 0x5c63
andl $0x0, 0x38558(%rip)     # 0x3e154
andl $0x0, 0x38555(%rip)     # 0x3e158
movq 0x38536(%rip), %rdi     # 0x3e140
movl %r12d, %esi
callq 0x4c08
movl %eax, %ebp
movq %r15, %rdi
movl %eax, %esi
pushq $0x1
popq %rdx
movl %r12d, %ecx
callq 0x51f3
testl %ebp, %ebp
setne %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5c5e
movslq %ebp, %rdi
addq %r15, %rdi
pushq $0x6
popq %rsi
movl %eax, %edx
movq %r14, %rcx
callq 0x55b6
movq 0x37371(%rip), %rax     # 0x3cfc0
movq (%rax), %rsi
leaq 0x1be40(%rip), %rdi     # 0x21a99
callq 0x40c0
incl %r12d
jmp 0x5bef
movq 0x37356(%rip), %rax     # 0x3cfc0
movq (%rax), %rsi
leaq 0x1be13(%rip), %rdi     # 0x21a87
callq 0x40c0
pushq $0x1
popq %r12
pushq $0x2
popq %r14
cmpl $0x22, %r12d
je 0x5b61
movq 0x384b6(%rip), %rdi     # 0x3e148
movl %r12d, %esi
callq 0x4c08
movl %eax, %ebp
movq %r15, %rdi
movl %eax, %esi
movl %r14d, %edx
movl %r12d, %ecx
callq 0x51f3
testl %ebp, %ebp
setne %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5cf3
movslq %ebp, %rbp
addq %r15, %rbp
movq %rbp, %rdi
pushq $0x4
popq %rsi
movl %eax, %edx
callq 0x535e
movq %rbp, %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x5494
movq 0x372dc(%rip), %rax     # 0x3cfc0
movq (%rax), %rsi
leaq 0x1bdab(%rip), %rdi     # 0x21a99
callq 0x40c0
incl %r12d
jmp 0x5c81
movl 0xc(%rsp), %edi
callq 0x40e0
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq (%r14), %rax
movq (%rbx,%rax,8), %rdi
jmp 0x5d2b
leaq 0x1bd1e(%rip), %rdi     # 0x21a40
jmp 0x5d2b
leaq 0x1bd1e(%rip), %rdi     # 0x21a49
callq 0x41a0
pushq $0x1
popq %rax
jmp 0x5d03
movq %rbx, %rdi
callq 0x583c
nopl (%rax)