// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %rsi, 0x8(%rsp)
movslq %edi, %r15
leaq (,%r15,8), %rdi
callq 0x4120
movq %rax, %r13
xorl %r12d, %r12d
movl $0x0, %ebp
cmpl $0x2, %r15d
jb 0x615a
movl $0x1, %ebx
movq 0x1143e(%rip), %rax     # 0x174f8
movq %rax, 0x20(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
movq %r15, 0x18(%rsp)
movq %r13, %r14
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %r13
testq %r12, %r12
jne 0x6118
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x40e0
testl %eax, %eax
je 0x6125
leaq 0x113fe(%rip), %r12     # 0x174f0
xorl %ecx, %ecx
movq %rcx, %r15
cmpq $0x5, %rcx
je 0x6130
movq 0x20(%r12), %rsi
movq %r13, %rdi
callq 0x40e0
addq $0x18, %r12
leaq 0x1(%r15), %rcx
testl %eax, %eax
jne 0x60f4
jmp 0x6133
movq %r13, 0x10(%r12)
xorl %r12d, %r12d
movq %r14, %r13
jmp 0x614e
movb $0x1, %al
leaq 0x113c2(%rip), %r12     # 0x174f0
jmp 0x613f
xorl %r12d, %r12d
cmpq $0x5, %r15
setb %al
movq 0x18(%rsp), %r15
testb %al, %al
movq %r14, %r13
jne 0x614e
movq %rbx, (%r13,%rbp,8)
incq %rbp
incq %rbx
cmpq %r15, %rbx
jne 0x60c9
testq %r12, %r12
jne 0x625f
testq %rbp, %rbp
je 0x6269
leaq 0x10(%rsp), %r15
movq $0x0, (%r15)
xorl %ebx, %ebx
xorl %r12d, %r12d
movq 0x8(%rsp), %r14
movq (%r13,%rbx,8), %rax
movq (%r14,%rax,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x4738
addq %rax, %r12
incq %rbx
cmpq %rbx, %rbp
jne 0x6182
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x456c
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x5a4c
testb %al, %al
je 0x622d
leaq 0x3f58(%rip), %rdx      # 0xa124
leaq 0x3f86(%rip), %rcx      # 0xa159
leaq 0x3fb5(%rip), %r8       # 0xa18f
xorl %ebp, %ebp
xorl %edi, %edi
xorl %esi, %esi
callq 0x7f6b
movq %rax, %r14
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x800d
leaq 0x3ede(%rip), %rdi      # 0xa0da
leaq 0x3e46(%rip), %rsi      # 0xa049
callq 0x4150
testq %rax, %rax
je 0x6275
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x7940
movq %r15, %rdi
callq 0x4090
movq %r14, %rdi
callq 0x80ed
jmp 0x623c
leaq 0x28(%rsp), %rdi
callq 0x4647
movl $0x1, %ebp
movq 0x10(%rsp), %rdi
callq 0x4030
movq %r13, %rdi
callq 0x4030
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x4291
jmp 0x626e
callq 0x42d4
movl $0x1, %ebp
jmp 0x6246
callq 0x42af
movl $0x1, %ebp
jmp 0x6223