// File: /workspace/github_c_makefile_star2_O3/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 0x3630(%rip), %r14      # 0x6288
movq 0x6371(%rip), %rbx      # 0x8fd0
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2280
cmpl $-0x1, %eax
je 0x2c80
cmpl $0x3f, %eax
je 0x2cef
cmpl $0x6f, %eax
jne 0x2c5f
movq (%rbx), %r12
jmp 0x2c5f
testq %r12, %r12
je 0x2d19
movq %r12, (%rsp)
movq 0x6334(%rip), %rax      # 0x8fc8
movslq (%rax), %r13
cmpl %ebp, %r13d
jge 0x2d53
movslq %ebp, %rbp
xorl %r12d, %r12d
xorl %ebx, %ebx
movq (%r15,%r13,8), %rdi
callq 0x2b03
movq %rax, %r14
testq %rbx, %rbx
je 0x2ccb
movq %r14, 0x10(%r12)
testq %r14, %r14
movq %rbx, %r14
jne 0x2cd3
jmp 0x2d4c
movq %r14, %r12
testq %r14, %r14
je 0x2d4c
movq %r12, %rax
movq %rax, %r12
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x2cd6
incq %r13
movq %r14, %rbx
cmpq %rbp, %r13
jne 0x2ca8
jmp 0x2d56
movq 0x62ea(%rip), %rax      # 0x8fe0
cmpl $0x6f, (%rax)
jne 0x2d04
leaq 0x360c(%rip), %rdi      # 0x630e
jmp 0x2d47
leaq 0x3580(%rip), %rdi      # 0x628b
movl $0x3f, %esi
xorl %eax, %eax
callq 0x2120
jmp 0x2d4c
movq (%r15), %rsi
leaq 0x357d(%rip), %rdi      # 0x62a0
xorl %eax, %eax
callq 0x2120
movl $0xa, %edi
callq 0x2050
leaq 0x3586(%rip), %rdi      # 0x62c1
callq 0x20b0
leaq 0x3598(%rip), %rdi      # 0x62df
callq 0x20b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2d87
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 0x2bd1
movq %r15, %rdi
callq 0x2040
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop