// File: /workspace/github_c_makefile_star2_O2/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, %r14
movl %edi, %ebp
xorl %r12d, %r12d
leaq 0x3640(%rip), %r15      # 0x6288
movq 0x6381(%rip), %rbx      # 0x8fd0
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2280
cmpl $-0x1, %eax
je 0x2c70
cmpl $0x3f, %eax
je 0x2cd6
cmpl $0x6f, %eax
jne 0x2c4f
movq (%rbx), %r12
jmp 0x2c4f
testq %r12, %r12
je 0x2cfe
movq %r12, (%rsp)
movq 0x6344(%rip), %rax      # 0x8fc8
movslq (%rax), %r12
movslq %ebp, %r13
xorl %r15d, %r15d
pushq $-0x1
popq %rbp
xorl %ebx, %ebx
cmpq %r13, %r12
jge 0x2d43
movq (%r14,%r12,8), %rdi
callq 0x2afb
testq %r15, %r15
je 0x2cb7
movq %rax, 0x10(%rbx)
testq %rax, %rax
movq %r15, %rax
jne 0x2cbf
jmp 0x2d32
movq %rax, %rbx
testq %rax, %rax
je 0x2d32
movq %rbx, %rcx
movq %rcx, %rbx
movq 0x10(%rcx), %rcx
testq %rcx, %rcx
jne 0x2cc2
incq %r12
movq %rax, %r15
jmp 0x2c92
movq 0x6303(%rip), %rax      # 0x8fe0
cmpl $0x6f, (%rax)
jne 0x2ceb
leaq 0x3625(%rip), %rdi      # 0x630e
jmp 0x2d2a
leaq 0x3599(%rip), %rdi      # 0x628b
pushq $0x3f
popq %rsi
xorl %eax, %eax
callq 0x2120
jmp 0x2d2f
movq (%r14), %rsi
leaq 0x3598(%rip), %rdi      # 0x62a0
xorl %eax, %eax
callq 0x2120
pushq $0xa
popq %rdi
callq 0x2050
leaq 0x35a3(%rip), %rdi      # 0x62c1
callq 0x20b0
leaq 0x35b5(%rip), %rdi      # 0x62df
callq 0x20b0
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10000, %edi          # imm = 0x10000
callq 0x21f0
movq %rax, %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x2520
movzwl %ax, %edx
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x2bc5
movq %r14, %rdi
callq 0x2040
xorl %ebp, %ebp
jmp 0x2d32
nop