// File: /workspace/github_c_makefile_star2_O1/alexj136[P]minijit/src/main.c
// Function: main
int main(int argc, char *argv[]) {

    /* =========================================================================
     * Check that an argument file was supplied. Quit if not.
     */
    if(argc < 2) {
        puts("No arguments provided.");
        exit(EXIT_FAILURE);
    }

    /* =========================================================================
     * We now know a file argument was supplied - try to open it, and quit if it
     * fails.
     */
	FILE *file = fopen(argv[1], "r");

	if (!file) {
        printf("Could not open file: '%s'.\n", argv[1]);
        exit(EXIT_FAILURE);
	}

    /* =========================================================================
     * We now know that the file opened successfully, so perform lexical
     * analysis on its contents.
     */
    LexerResult *lr = lex_file(file);

    /* =========================================================================
     * Close the file now that lexical analysis has concluded. Print a warning
     * if closing fails, but continue with the pipeline anyway.
     */
    int close_status = fclose(file);
    if(close_status != 0) {
        printf("Warning: file '%s' did not close properly.\n", argv[1]);
    }

    /* =========================================================================
     * Do the parse. We don't need to check if the lexer failed because the
     * parser can deal with token lists that contain errors, and return any
     * lexical errors along with parse errors.
     *     If parsing fails, print the errors and exit.
     */
    ParseResult *pr = parse(lr);

    if(pr->type == parseFail) {
        ParseErrorVector_print(pr->errors);
        ParseResult_free(pr);
        exit(EXIT_FAILURE);
    }

    /* =========================================================================
     * We got the correct number of arguments, so now parse the command-line
     * arguments. If parsing fails, quit with an appropriate error message.
     */
    IntRefVector *prog_args = IntRefVector_init();
    int argv_idx      = 2;
    bool no_errors    = true;
    while((argv_idx < argc) && no_errors) {

        IntRef *arg_result = str_to_int(argv[argv_idx]);

        if(arg_result == NULL) {
            no_errors = false;
        }
        else {
            IntRefVector_append(prog_args, arg_result);
        }

        argv_idx++;
    }

    if(!no_errors) {
        IntRefVector_free_elems(prog_args);
        ParseResult_free(pr);
        printf("Could not parse command line arguments.\n");
        exit(EXIT_FAILURE);
    }

    /* =========================================================================
     * We now have everything we need to run the program, so run the program and
     * print the result.
     */
    InterpretResult *res = interpret_Prog(pr->prog, prog_args);
    if(res->type == iFailIncorrectNumArgs) {
        puts("Runtime Error: Function call with incorrect number of "
                "arguments.");
    }
    else if(res->type == iFailFunctionNotFound) {
        puts("Runtime Error: Call to an undefined function.");
    }
    else if(res->type == iFailEndWithoutReturn) {
        puts("Runtime Error: Reached end of function without return.");
    }
    else if(res->type == iSuccess) {
        printf("INTERPRETER RESULT = %d\n", res->result);
    }
    else {
        free(res);
        ParseResult_free(pr);
        IntRefVector_free_elems(prog_args);
        ERROR("InterpretResult type not recognised");
    }
    free(res);

    /* =========================================================================
     * Generate and run icode and vmcode for the given program, with the given
     * args.
     */
    int next_label = 0;
    ICodeOperationVector *icodevec = icodegen_Prog(pr->prog, &next_label);
    int icode_result = ICodeOperationVector_execute(icodevec, prog_args);
    printf("ICODE RESULT = %d\n", icode_result);
    int vmcode_result = VMCode_execute(icodevec, prog_args);
    printf("VMCODE RESULT = %d\n", vmcode_result);
    ICodeOperationVector_free_elems(icodevec);

    /* =========================================================================
     * Deallocate the remaining heap objects.
     */
    ParseResult_free(pr);
    IntRefVector_free_elems(prog_args);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f48, %rsp           # imm = 0x1F48
cmpl $0x1, %edi
jle 0x1a55b
movq %rsi, %r15
movl %edi, %r12d
movq 0x8(%rsi), %rdi
leaq 0x27a9(%rip), %rsi      # 0x1cb7f
callq 0x6180
testq %rax, %rax
je 0x1a564
movq %rax, %r14
movq %rax, %rdi
callq 0xe78d
movq %rax, %rbx
movq %r14, %rdi
callq 0x60c0
testl %eax, %eax
je 0x1a410
movq 0x8(%r15), %rsi
leaq 0x2c1e(%rip), %rdi      # 0x1d027
xorl %eax, %eax
callq 0x6100
movq %rbx, %rdi
callq 0x105d1
movq %rax, %rbx
cmpl $0x1, (%rax)
je 0x1a578
xorl %eax, %eax
callq 0x75b4
movq %rax, %r14
cmpl $0x2, %r12d
je 0x1a47b
movq %rbx, -0x30(%rbp)
movl %r12d, %r13d
movl $0x3, %ebx
movq -0x8(%r15,%rbx,8), %rdi
callq 0x65cc
movq %rax, %r12
testq %rax, %rax
je 0x1a45d
movq %r14, %rdi
movq %r12, %rsi
callq 0x769e
testq %r12, %r12
je 0x1a46e
leaq 0x1(%rbx), %rax
cmpq %r13, %rbx
movq %rax, %rbx
jb 0x1a440
testq %r12, %r12
movq -0x30(%rbp), %rbx
je 0x1a58b
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x1373b
movq %rax, %r15
movl (%rax), %eax
cmpq $0x3, %rax
ja 0x1a5b1
leaq 0x2b47(%rip), %rcx      # 0x1cfe4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x4(%r15), %esi
leaq 0x2c48(%rip), %rdi      # 0x1d0f9
xorl %eax, %eax
callq 0x6100
jmp 0x1a4d8
leaq 0x2bd3(%rip), %rdi      # 0x1d094
jmp 0x1a4d3
leaq 0x2bf8(%rip), %rdi      # 0x1d0c2
jmp 0x1a4d3
leaq 0x2b80(%rip), %rdi      # 0x1d053
callq 0x6070
movq %r15, %rdi
callq 0x6030
leaq -0x1f70(%rbp), %rsi
movl $0x0, (%rsi)
movq 0x8(%rbx), %rdi
callq 0x15170
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x15bfc
leaq 0x2c36(%rip), %rdi      # 0x1d141
movl %eax, %esi
xorl %eax, %eax
callq 0x6100
movq %r15, %rdi
movq %r14, %rsi
callq 0x1642c
leaq 0x2c2e(%rip), %rdi      # 0x1d154
movl %eax, %esi
xorl %eax, %eax
callq 0x6100
movq %r15, %rdi
callq 0x14a0a
movq %rbx, %rdi
callq 0xf85c
movq %r14, %rdi
callq 0x7d8c
xorl %eax, %eax
addq $0x1f48, %rsp           # imm = 0x1F48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2a92(%rip), %rdi      # 0x1cff4
jmp 0x1a5a2
movq 0x8(%r15), %rsi
leaq 0x2a9c(%rip), %rdi      # 0x1d00b
xorl %eax, %eax
callq 0x6100
jmp 0x1a5a7
movq 0x10(%rbx), %rdi
callq 0xf793
movq %rbx, %rdi
callq 0xf85c
jmp 0x1a5a7
movq %r14, %rdi
callq 0x7d8c
movq %rbx, %rdi
callq 0xf85c
leaq 0x2bc6(%rip), %rdi      # 0x1d168
callq 0x6070
movl $0x1, %edi
callq 0x61a0
movq %r15, %rdi
callq 0x6030
movq %rbx, %rdi
callq 0xf85c
movq %r14, %rdi
callq 0x7d8c
leaq 0xa34(%rip), %rdi       # 0x1b004
leaq 0x2b3b(%rip), %rsi      # 0x1d112
leaq 0x26f6(%rip), %rcx      # 0x1ccd4
movl $0x76, %edx
xorl %eax, %eax
callq 0x6100
leaq 0xa3b(%rip), %rdi       # 0x1b02c
leaq 0x2b25(%rip), %rsi      # 0x1d11d
xorl %eax, %eax
callq 0x6100
leaq -0x1f70(%rbp), %rbx
movq %rbx, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x60e0
movl %eax, %r14d
movq %rbx, %rdi
movl %eax, %esi
callq 0x60b0
movq %rax, %rbx
leaq 0xbec(%rip), %rdi       # 0x1b216
callq 0x6070
testl %r14d, %r14d
jle 0x1a5a7
movl %r14d, %r15d
leaq 0x9ea(%rip), %r14       # 0x1b02c
xorl %r12d, %r12d
movq (%rbx,%r12,8), %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x6100
incq %r12
cmpq %r12, %r15
jne 0x1a645
jmp 0x1a5a7