// File: /workspace/github_c_makefile_star2_O2/endeav0r[P]hsvm/src/disassembler.c
// Function: main
int main (int argc, char * argv[])
{
    int c;
    unsigned int offset = 0;

    while ((c = getopt(argc, argv, "o:")) != -1) {
        switch (c) {
        case 'o' :
            offset = strtoul(optarg, NULL, 0);
            break;
        }
    }

    if (optind == argc) {
        fprintf(stderr, "missing name of file to disassembler\n");
        return -1;
    }

    uint8_t * buf;
    size_t buf_size;

    buf = disassembler_load(argv[optind], &buf_size);
    if (buf == NULL)
        return -1;

    size_t buf_i = offset;
    char dis_str[64];
    int error;

    while (buf_i < buf_size) {
        if (buf_i <= buf_size - 4) {
            error = ins_str((struct _instruction *) &(buf[buf_i]), dis_str, 64);
            if (error > 0) {
                // destination for jump instructions
                if (is_branch(&(buf[buf_i]))) {
                    printf("%04x %s (%04x)\n", (unsigned int) buf_i, dis_str,
                           branch_abs(&(buf[buf_i]), buf_i + 4));
                }
                else {
                    printf("%04x %s\n", (unsigned int) buf_i, dis_str);
                }
                buf_i += 4;
                continue;
            }
        }
        if (buf_i <= buf_size - 2) {
            printf("%04x %02x%02x\n", (unsigned int) buf_i, buf[buf_i], buf[buf_i + 1]);
            buf_i += 2;
        }
        else
            buf_i++;
    }

    free(buf);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movl %edi, %ebp
xorl %r14d, %r14d
leaq 0x2ba7(%rip), %r15      # 0x401c
movq 0x5b54(%rip), %r12      # 0x6fd0
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1260
cmpl $-0x1, %eax
je 0x14a5
cmpl $0x6f, %eax
jne 0x147c
movq (%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1270
movq %rax, %r14
jmp 0x147c
movq 0x5b1c(%rip), %rax      # 0x6fc8
movslq (%rax), %rax
cmpl %ebp, %eax
je 0x158e
movq (%rbx,%rax,8), %rdi
leaq 0x8(%rsp), %rsi
callq 0x13ac
testq %rax, %rax
je 0x15aa
movq %rax, %rbx
movl %r14d, %r14d
movq 0x8(%rsp), %r15
leaq -0x4(%r15), %r13
leaq -0x2(%r15), %r12
cmpq %r15, %r14
jae 0x1582
cmpq %r13, %r14
ja 0x1539
leaq (%rbx,%r14), %rbp
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
pushq $0x40
popq %rdx
callq 0x16ef
testl %eax, %eax
jle 0x1539
movb (%rbp), %al
andb $-0x8, %al
cmpb $0x20, %al
jne 0x1561
movzwl 0x2(%rbp), %eax
rolw $0x8, %ax
addl %r14d, %eax
addl $0x4, %eax
movzwl %ax, %ecx
leaq 0x2b1d(%rip), %rdi      # 0x4045
movl %r14d, %esi
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
jmp 0x1577
pushq $0x1
popq %rax
cmpq %r12, %r14
ja 0x157a
movzbl (%rbx,%r14), %edx
movzbl 0x1(%rbx,%r14), %ecx
leaq 0x2b0b(%rip), %rdi      # 0x405e
movl %r14d, %esi
xorl %eax, %eax
callq 0x1110
pushq $0x2
jmp 0x1579
leaq 0x2aed(%rip), %rdi      # 0x4055
movl %r14d, %esi
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
pushq $0x4
popq %rax
addq %rax, %r14
jmp 0x14e1
movq %rbx, %rdi
callq 0x1040
xorl %eax, %eax
jmp 0x15ad
movq 0x5a5b(%rip), %rax      # 0x6ff0
movq (%rax), %rcx
leaq 0x2a80(%rip), %rdi      # 0x401f
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x1290
pushq $-0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq