// File: /workspace/github_c_makefile_star2_O3/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 0x1a55e
movq %rsi, %r15
movl %edi, %r12d
movq 0x8(%rsi), %rdi
leaq 0x27b5(%rip), %rsi      # 0x1cb6f
callq 0x6180
testq %rax, %rax
je 0x1a567
movq %rax, %r14
movq %rax, %rdi
callq 0xe757
movq %rax, %rbx
movq %r14, %rdi
callq 0x60c0
testl %eax, %eax
je 0x1a3f4
movq 0x8(%r15), %rsi
leaq 0x2c2a(%rip), %rdi      # 0x1d017
xorl %eax, %eax
callq 0x6100
movq %rbx, %rdi
callq 0x1059e
movq %rax, -0x30(%rbp)
cmpl $0x1, (%rax)
je 0x1a57b
xorl %eax, %eax
callq 0x75bc
movq %rax, %r14
cmpl $0x2, %r12d
je 0x1a453
movl %r12d, %r13d
movl $0x3, %ebx
movq -0x8(%r15,%rbx,8), %rdi
callq 0x65cc
testq %rax, %rax
je 0x1a537
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
callq 0x76a6
testq %r12, %r12
je 0x1a453
leaq 0x1(%rbx), %rax
cmpq %r13, %rbx
movq %rax, %rbx
jb 0x1a421
movq -0x30(%rbp), %rbx
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x1373b
movq %rax, %r15
movl (%rax), %eax
cmpq $0x3, %rax
ja 0x1a592
leaq 0x2b5b(%rip), %rcx      # 0x1cfd4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x4(%r15), %esi
leaq 0x2c5c(%rip), %rdi      # 0x1d0e9
xorl %eax, %eax
callq 0x6100
jmp 0x1a4b4
leaq 0x2be7(%rip), %rdi      # 0x1d084
jmp 0x1a4af
leaq 0x2c0c(%rip), %rdi      # 0x1d0b2
jmp 0x1a4af
leaq 0x2b94(%rip), %rdi      # 0x1d043
callq 0x6070
movq %r15, %rdi
callq 0x6030
leaq -0x1f70(%rbp), %rsi
movl $0x0, (%rsi)
movq 0x8(%rbx), %rdi
callq 0x1512c
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x15bb8
leaq 0x2c4a(%rip), %rdi      # 0x1d131
movl %eax, %esi
xorl %eax, %eax
callq 0x6100
movq %r15, %rdi
movq %r14, %rsi
callq 0x163e8
leaq 0x2c42(%rip), %rdi      # 0x1d144
movl %eax, %esi
xorl %eax, %eax
callq 0x6100
movq %r15, %rdi
callq 0x149b8
movq %rbx, %rdi
callq 0xf839
movq %r14, %rdi
callq 0x7d9e
xorl %eax, %eax
addq $0x1f48, %rsp           # imm = 0x1F48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x7d9e
movq -0x30(%rbp), %rdi
callq 0xf839
leaq 0x2c09(%rip), %rdi      # 0x1d158
callq 0x6070
movl $0x1, %edi
callq 0x61a0
leaq 0x2a7f(%rip), %rdi      # 0x1cfe4
jmp 0x1a54f
movq 0x8(%r15), %rsi
leaq 0x2a89(%rip), %rdi      # 0x1cffb
xorl %eax, %eax
callq 0x6100
jmp 0x1a554
movq -0x30(%rbp), %rbx
movq 0x10(%rbx), %rdi
callq 0xf770
movq %rbx, %rdi
callq 0xf839
jmp 0x1a554
movq %r15, %rdi
callq 0x6030
movq %rbx, %rdi
callq 0xf839
movq %r14, %rdi
callq 0x7d9e
leaq 0xa53(%rip), %rdi       # 0x1b004
leaq 0x2b4a(%rip), %rsi      # 0x1d102
leaq 0x2705(%rip), %rcx      # 0x1ccc4
movl $0x76, %edx
xorl %eax, %eax
callq 0x6100
leaq 0xa5a(%rip), %rdi       # 0x1b02c
leaq 0x2b34(%rip), %rsi      # 0x1d10d
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 0xc0b(%rip), %rdi       # 0x1b216
callq 0x6070
testl %r14d, %r14d
jle 0x1a554
movl %r14d, %r15d
leaq 0xa09(%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 0x1a626
jmp 0x1a554