// File: /workspace/github_c_makefile_star2_O2/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, %rax
movq %rax, 0x10(%rsp)
leaq (,%rax,8), %rdi
callq 0x4120
movq %rax, (%rsp)
pushq $0x1
popq %rbp
xorl %ebx, %ebx
xorl %r13d, %r13d
cmpq 0x10(%rsp), %rbp
jae 0x5370
movq (%r14,%rbp,8), %r15
testq %rbx, %rbx
jne 0x5365
leaq 0x1019f(%rip), %rbx     # 0x154d8
pushq $0x7
popq %r12
decq %r12
je 0x5358
movq 0x20(%rbx), %rsi
addq $0x18, %rbx
movq %r15, %rdi
callq 0x40e0
testl %eax, %eax
jne 0x533d
jmp 0x536b
movq (%rsp), %rax
movq %rbp, (%rax,%r13,8)
incq %r13
jmp 0x5369
movq %r15, 0x10(%rbx)
xorl %ebx, %ebx
incq %rbp
jmp 0x5322
testq %rbx, %rbx
jne 0x5476
testq %r13, %r13
je 0x5493
leaq 0x8(%rsp), %r15
andq $0x0, (%r15)
xorl %r12d, %r12d
xorl %ebx, %ebx
movq (%rsp), %rbp
cmpq %rbx, %r13
je 0x53bb
movq (%rbp,%rbx,8), %rax
movq (%r14,%rax,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x4414
addq %rax, %r12
incq %rbx
jmp 0x5394
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x427c
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x4dd1
testb %al, %al
je 0x5445
leaq 0x2d44(%rip), %rdx      # 0x8124
leaq 0x2d72(%rip), %rcx      # 0x8159
leaq 0x2da1(%rip), %r8       # 0x818f
xorl %ebp, %ebp
xorl %edi, %edi
xorl %esi, %esi
callq 0x6a78
movq %rax, %r14
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x6b08
leaq 0x2cca(%rip), %rdi      # 0x80da
leaq 0x2c32(%rip), %rsi      # 0x8049
callq 0x4150
testq %rax, %rax
je 0x54b4
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x64ec
movq %r15, %rdi
callq 0x4090
movq %r14, %rdi
callq 0x6be8
jmp 0x5452
leaq 0x18(%rsp), %rdi
callq 0x433b
pushq $0x1
popq %rbp
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
movq 0xfb7b(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x2d16(%rip), %rsi      # 0x81a0
xorl %eax, %eax
callq 0x40f0
jmp 0x54af
movq 0xfb5e(%rip), %rax      # 0x14ff8
movq (%rax), %rcx
leaq 0x2d22(%rip), %rdi      # 0x81c6
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x4170
pushq $0x1
popq %rbp
jmp 0x545c
movq 0xfb3d(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x2d22(%rip), %rsi      # 0x81e7
leaq 0x2c0e(%rip), %rdx      # 0x80da
xorl %eax, %eax
callq 0x40f0
pushq $0x1
popq %rbp
jmp 0x543b