// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movslq -0x8(%rbp), %rdi
shlq $0x3, %rdi
callq 0x4130
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x1, -0x38(%rbp)
movq -0x38(%rbp), %rax
movslq -0x8(%rbp), %rcx
cmpq %rcx, %rax
jae 0x58fd
cmpq $0x0, -0x30(%rbp)
je 0x5862
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq $0x0, -0x30(%rbp)
jmp 0x58ea
movb $0x0, -0x39(%rbp)
movq $0x0, -0x48(%rbp)
cmpq $0x6, -0x48(%rbp)
jae 0x58c7
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x13c68(%rip), %rax     # 0x194f0
imulq $0x18, -0x48(%rbp), %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
callq 0x40f0
cmpl $0x0, %eax
jne 0x58b7
leaq 0x13c4b(%rip), %rax     # 0x194f0
imulq $0x18, -0x48(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movb $0x1, -0x39(%rbp)
jmp 0x58c7
jmp 0x58b9
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x586e
testb $0x1, -0x39(%rbp)
jne 0x58e8
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x28(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x58ea
jmp 0x58ec
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x5829
cmpq $0x0, -0x30(%rbp)
je 0x592f
movq 0x136ed(%rip), %rax     # 0x18ff8
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rdx
leaq 0x6868(%rip), %rsi      # 0xc184
movb $0x0, %al
callq 0x4100
movl $0x1, -0x14(%rbp)
jmp 0x5af0
cmpq $0x1, -0x28(%rbp)
jae 0x595a
movq 0x136bb(%rip), %rax     # 0x18ff8
movq (%rax), %rdi
leaq 0x6863(%rip), %rsi      # 0xc1aa
movb $0x0, %al
callq 0x4100
movl $0x1, -0x14(%rbp)
jmp 0x5af0
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x59ba
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movq -0x60(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x58(%rbp), %rdx
movq -0x58(%rbp), %rcx
leaq -0x50(%rbp), %rsi
xorl %r8d, %r8d
callq 0x45d0
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x5972
leaq -0xb0(%rbp), %rdi
callq 0x4280
movq -0x50(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x5450
testb $0x1, %al
jne 0x59f2
leaq -0xb0(%rbp), %rdi
callq 0x4430
movl $0x1, -0x14(%rbp)
jmp 0x5ae7
movq 0xb54f(%rip), %rdi      # 0x10f48
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x4110
movq %rax, -0xb8(%rbp)
movq 0xb54b(%rip), %rdi      # 0x10f60
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x4110
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rax
movl %eax, %edi
movq -0xc0(%rbp), %rax
movl %eax, %esi
movq 0xb535(%rip), %rdx      # 0x10f78
movq 0xb546(%rip), %rcx      # 0x10f90
movq 0xb557(%rip), %r8       # 0x10fa8
callq 0x88c0
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x89d0
movq 0xb4b9(%rip), %rdi      # 0x10f30
leaq 0x65af(%rip), %rsi      # 0xc02d
callq 0x4150
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
jne 0x5abc
movq 0x1355d(%rip), %rax     # 0x18ff8
movq (%rax), %rdi
movq 0xb48b(%rip), %rdx      # 0x10f30
leaq 0x671f(%rip), %rsi      # 0xc1cb
movb $0x0, %al
callq 0x4100
movl $0x1, -0x14(%rbp)
jmp 0x5adb
movq -0xd0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x7eb0
movq -0xd0(%rbp), %rdi
callq 0x4080
movq -0xc8(%rbp), %rdi
callq 0x8b90
movq -0x50(%rbp), %rdi
callq 0x4030
movq -0x20(%rbp), %rdi
callq 0x4030
movl -0x14(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)