// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x5aee(%rip), %rdx      # 0x701c
callq 0x1270
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x155f
movl -0x14(%rbp), %eax
subl $0x6f, %eax
jne 0x155d
jmp 0x1545
movq 0x8a8c(%rip), %rax      # 0x9fd8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1280
movl %eax, -0x18(%rbp)
jmp 0x1520
movq 0x8a6a(%rip), %rax      # 0x9fd0
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jne 0x1591
movq 0x8a84(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x5aa1(%rip), %rsi      # 0x701f
movb $0x0, %al
callq 0x11a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16fa
movq -0x10(%rbp), %rax
movq 0x8a34(%rip), %rcx      # 0x9fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x28(%rbp), %rsi
callq 0x13b0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x15c3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16fa
movl -0x18(%rbp), %eax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x16ea
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x4, %rcx
cmpq %rcx, %rax
ja 0x168d
movq -0x20(%rbp), %rdi
addq -0x30(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movl $0x40, %edx
callq 0x1af0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jle 0x168b
movq -0x20(%rbp), %rdi
addq -0x30(%rbp), %rdi
callq 0x1480
cmpl $0x0, %eax
je 0x1662
movq -0x30(%rbp), %rax
movl %eax, -0x84(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rdi
addq -0x30(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x4, %rsi
callq 0x14c0
movl -0x84(%rbp), %esi
movq -0x80(%rbp), %rdx
movzwl %ax, %ecx
leaq 0x59ec(%rip), %rdi      # 0x7045
movb $0x0, %al
callq 0x1120
jmp 0x167a
movq -0x30(%rbp), %rax
movl %eax, %esi
leaq -0x70(%rbp), %rdx
leaq 0x59e2(%rip), %rdi      # 0x7055
movb $0x0, %al
callq 0x1120
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
jmp 0x15ca
jmp 0x168d
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x2, %rcx
cmpq %rcx, %rax
ja 0x16d9
movq -0x30(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movzbl (%rax,%rcx), %edx
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movzbl 0x1(%rax,%rcx), %ecx
leaq 0x599a(%rip), %rdi      # 0x705e
movb $0x0, %al
callq 0x1120
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
jmp 0x16e5
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x15ca
movq -0x20(%rbp), %rdi
callq 0x1040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)