// File: /workspace/github_c_makefile_star2_O1/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 0x3b83(%rip), %r15      # 0x501c
movq 0x6b30(%rip), %r12      # 0x7fd0
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1260
cmpl $-0x1, %eax
je 0x14c9
cmpl $0x6f, %eax
jne 0x14a0
movq (%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1270
movq %rax, %r14
jmp 0x14a0
movq 0x6af8(%rip), %rax      # 0x7fc8
movslq (%rax), %rax
cmpl %ebp, %eax
je 0x15c1
movq (%rbx,%rax,8), %rdi
leaq 0x8(%rsp), %rsi
callq 0x13cc
testq %rax, %rax
je 0x15c6
movq %rax, %rbx
movl %r14d, %r14d
movq 0x8(%rsp), %rbp
cmpq %r14, %rbp
jbe 0x15b5
leaq -0x4(%rbp), %r15
leaq -0x2(%rbp), %r12
cmpq %r15, %r14
ja 0x1561
leaq (%rbx,%r14), %r13
movl $0x40, %edx
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x170f
testl %eax, %eax
jle 0x1561
movb (%r13), %al
andb $-0x8, %al
cmpb $0x20, %al
jne 0x158e
movzwl 0x2(%r13), %eax
rolw $0x8, %ax
addl %r14d, %eax
addl $0x4, %eax
movzwl %ax, %ecx
leaq 0x3af5(%rip), %rdi      # 0x5045
movl %r14d, %esi
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
jmp 0x15a4
movl $0x1, %eax
cmpq %r12, %r14
ja 0x15a9
movzbl (%rbx,%r14), %edx
movzbl 0x1(%rbx,%r14), %ecx
leaq 0x3ae1(%rip), %rdi      # 0x505e
movl %r14d, %esi
xorl %eax, %eax
callq 0x1110
movl $0x2, %eax
jmp 0x15a9
leaq 0x3ac0(%rip), %rdi      # 0x5055
movl %r14d, %esi
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
movl $0x4, %eax
addq %rax, %r14
cmpq %rbp, %r14
jb 0x150e
movq %rbx, %rdi
callq 0x1040
xorl %eax, %eax
jmp 0x15cb
callq 0x12c0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop