// File: /workspace/github_c_makefile_star2_O3/jtuu[P]quesler/src/compiler.c
// Function: main
int main(int argc, const char* argv[]) {
    int ret = 0;

    size_t* input_file_indices = malloc(argc * sizeof(size_t));
    size_t num_input_files = 0;

    ArgOption* expecting_opt = NULL;

    for (size_t i = 1; i < (size_t) argc; i++) {
        if (expecting_opt != NULL) {
            expecting_opt->value = argv[i];
            expecting_opt = NULL;
        } else {
            bool is_opt = false;
            for (size_t j = 0; j < NUM_OPTS; j++) {
                if (strcmp(argv[i], options[j].option) == 0) {
                    expecting_opt = &options[j];
                    is_opt = true;
                    break;
                }
            }

            if (!is_opt) {
                input_file_indices[num_input_files++] = i;
            }
        }
    }

    if (expecting_opt != NULL) {
        fprintf(stderr, "error: missing value for option \"%s\"\n", expecting_opt->name);
        ret = 1;
        goto end;
    }

    if (num_input_files < 1) {
        fprintf(stderr, "error: no input files specified\n");
        ret = 1;
        goto end;
    }

    char* source = NULL;
    size_t read_size = 0;
    for (size_t i = 0; i < num_input_files; i++) {
        read_size += read_file_ex(argv[input_file_indices[i]], (uint8_t**) &source, read_size, read_size, false);
    }

    Chunk chunk;
    init_chunk(&chunk);

    if (!compile(source, &chunk)) {
        free_chunk(&chunk);
        ret = 1;
        goto end1;
    }

    long quest_number = strtol(option_quest_number.value, NULL, 10);
    long quest_language = strtol(option_quest_language.value, NULL, 10);

    QstBin* bin = create_bin(
        (uint32_t) quest_number,
        (uint32_t) quest_language,
        option_quest_name.value,
        option_short_description.value,
        option_long_description.value);
    chunk_to_bin(&chunk, bin);

    FILE *output_file = fopen(option_output_file.value, "wb");
    if (!output_file) {
        fprintf(stderr, "error: failed to open output file \"%s\"\n", option_output_file.value);
        ret = 1;
        goto end2;
    }

    write_bin(output_file, bin);

    fclose(output_file);

end2:
    free_bin(bin);

end1:
    free(source);

end:
    free(input_file_indices);
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
movslq %edi, %rbx
leaq (,%rbx,8), %rdi
callq 0x4120
movq %rax, (%rsp)
movq %rbx, 0x10(%rsp)
cmpl $0x2, %ebx
jb 0x619c
movl $0x1, %ebx
xorl %r13d, %r13d
leaq 0x114a9(%rip), %rbp     # 0x174f0
xorl %r15d, %r15d
movq (%r14,%rbx,8), %r12
testq %r15, %r15
jne 0x6086
xorl %r15d, %r15d
movq 0x8(%r15,%rbp), %rsi
movq %r12, %rdi
callq 0x40e0
testl %eax, %eax
je 0x6081
addq $0x18, %r15
cmpq $0x90, %r15
jne 0x6056
movq (%rsp), %rax
movq %rbx, (%rax,%r13,8)
incq %r13
jmp 0x608a
addq %rbp, %r15
jmp 0x608d
movq %r12, 0x10(%r15)
xorl %r15d, %r15d
incq %rbx
cmpq 0x10(%rsp), %rbx
jne 0x604a
testq %r15, %r15
jne 0x61a3
testq %r13, %r13
je 0x619c
leaq 0x8(%rsp), %r15
movq $0x0, (%r15)
xorl %ebx, %ebx
xorl %r12d, %r12d
movq (%rsp), %rbp
movq (%rbp,%rbx,8), %rax
movq (%r14,%rax,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x4720
addq %rax, %r12
incq %rbx
cmpq %rbx, %r13
jne 0x60be
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x454c
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x5a2b
testb %al, %al
je 0x6169
leaq 0x4040(%rip), %rdx      # 0xa148
leaq 0x406e(%rip), %rcx      # 0xa17d
leaq 0x409d(%rip), %r8       # 0xa1b3
xorl %ebp, %ebp
xorl %edi, %edi
xorl %esi, %esi
callq 0x7dbf
movq %rax, %r14
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x7e61
leaq 0x3fc6(%rip), %rdi      # 0xa0fe
leaq 0x3f0a(%rip), %rsi      # 0xa049
callq 0x4150
testq %rax, %rax
je 0x61b2
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x77d4
movq %r15, %rdi
callq 0x4090
movq %r14, %rdi
callq 0x7f41
jmp 0x6178
leaq 0x18(%rsp), %rdi
callq 0x462e
movl $0x1, %ebp
movq 0x8(%rsp), %rdi
callq 0x4030
movq (%rsp), %rdi
callq 0x4030
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x42bd
jmp 0x61ab
movq %r15, %rdi
callq 0x427a
movl $0x1, %ebp
jmp 0x6182
callq 0x4298
movl $0x1, %ebp
jmp 0x615f