// File: /workspace/github_c_makefile_star2_O1/endeav0r[P]hsvm/src/assembler.c
// Function: main
int main (int argc, char * argv[])
{
    int c;
    char * output_filename = NULL;

    while ((c = getopt(argc, argv, "o:")) != -1) {
        switch (c) {
        case 'o' :
            output_filename = optarg;
            break;
        case '?' :
            if (optopt == 'o')
                printf("argument o requires argument\n");
            else
                printf("unknown argument %c\n", c);
            return -1;
        }
    }

    if (output_filename == NULL) {
        printf("usage: %s -o filename <sources>\n", argv[0]);
        printf("\n");
        printf(" -o specifies output filename\n");
        printf(" <sources> specifices assembly files for input\n");
        return -1;
    }

    struct _token * first = NULL;
    struct _token * last = NULL;
    int source_i;

    for (source_i = optind; source_i < argc; source_i++) {
        if (first == NULL) {
            first = get_tokens(argv[source_i]);
            if (first == NULL)
                return -1;
            last = first;
        }
        else {
            last->next = get_tokens(argv[source_i]);
            if (last->next == NULL)
                return -1;
        }
        while (last->next != NULL) { last = last->next; }
    }

    unsigned char * mem;
    mem = (unsigned char *) malloc(0x10000);
    int assembled_bytes = assemble(first, mem);

    write_mem(output_filename, mem, assembled_bytes);

    free(mem);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movl %edi, %ebp
xorl %r12d, %r12d
leaq 0x35b0(%rip), %r14      # 0x6288
movq 0x62f1(%rip), %rbx      # 0x8fd0
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2280
cmpl $-0x1, %eax
je 0x2d00
cmpl $0x3f, %eax
je 0x2d6c
cmpl $0x6f, %eax
jne 0x2cdf
movq (%rbx), %r12
jmp 0x2cdf
testq %r12, %r12
je 0x2d93
movq %r12, (%rsp)
movq 0x62b4(%rip), %rax      # 0x8fc8
movslq (%rax), %r13
cmpl %ebp, %r13d
jge 0x2dcd
movslq %ebp, %rbp
xorl %r12d, %r12d
xorl %ebx, %ebx
movq (%r15,%r13,8), %rdi
callq 0x2b83
movq %rax, %r14
testq %rbx, %rbx
je 0x2d48
movq %r14, 0x10(%r12)
testq %r14, %r14
movq %rbx, %r14
jne 0x2d50
jmp 0x2dc6
movq %r14, %r12
testq %r14, %r14
je 0x2dc6
movq %r12, %rax
movq %rax, %r12
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x2d53
incq %r13
movq %r14, %rbx
cmpq %rbp, %r13
jne 0x2d28
jmp 0x2dd0
movq 0x626d(%rip), %rcx      # 0x8fe0
cmpl $0x6f, (%rcx)
jne 0x2d81
leaq 0x358f(%rip), %rdi      # 0x630e
jmp 0x2dc1
leaq 0x3503(%rip), %rdi      # 0x628b
movl %eax, %esi
xorl %eax, %eax
callq 0x2120
jmp 0x2dc6
movq (%r15), %rsi
leaq 0x3503(%rip), %rdi      # 0x62a0
xorl %eax, %eax
callq 0x2120
movl $0xa, %edi
callq 0x2050
leaq 0x350c(%rip), %rdi      # 0x62c1
callq 0x20b0
leaq 0x351e(%rip), %rdi      # 0x62df
callq 0x20b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2e01
xorl %r14d, %r14d
movl $0x10000, %edi          # imm = 0x10000
callq 0x21f0
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x25f8
movzwl %ax, %edx
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x2c51
movq %r15, %rdi
callq 0x2040
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq