// File: /workspace/github_c_makefile_star2_O2/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 0x1935b
movq %rsi, %r15
movl %edi, %r12d
movq 0x8(%rsi), %rdi
leaq 0x29d1(%rip), %rsi      # 0x1bbaf
callq 0x6180
testq %rax, %rax
je 0x19364
movq %rax, %r14
movq %rax, %rdi
callq 0xdfa1
movq %rax, %rbx
movq %r14, %rdi
callq 0x60c0
testl %eax, %eax
je 0x19218
movq 0x8(%r15), %rsi
leaq 0x2e46(%rip), %rdi      # 0x1c057
xorl %eax, %eax
callq 0x6100
movq %rbx, %rdi
callq 0xfccf
movq %rax, -0x30(%rbp)
cmpl $0x1, (%rax)
je 0x19378
xorl %eax, %eax
callq 0x7503
movq %rax, %r14
movl %r12d, %r12d
pushq $0x1
popq %r13
pushq $0x2
popq %rbx
movl %r13d, %eax
testl %eax, %eax
je 0x19272
cmpq %r12, %rbx
jae 0x19272
movq (%r15,%rbx,8), %rdi
callq 0x65c4
testq %rax, %rax
je 0x1926b
movq %r14, %rdi
movq %rax, %rsi
callq 0x75e5
movl %r13d, %eax
jmp 0x1926d
xorl %eax, %eax
incq %rbx
jmp 0x19244
testl %eax, %eax
je 0x1938f
movq -0x30(%rbp), %rbx
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x12ade
movq %rax, %r15
movl (%rax), %eax
cmpq $0x3, %rax
ja 0x193b4
leaq 0x2d74(%rip), %rcx      # 0x1c014
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x4(%r15), %esi
leaq 0x2e75(%rip), %rdi      # 0x1c129
xorl %eax, %eax
callq 0x6100
jmp 0x192db
leaq 0x2e00(%rip), %rdi      # 0x1c0c4
jmp 0x192d6
leaq 0x2e25(%rip), %rdi      # 0x1c0f2
jmp 0x192d6
leaq 0x2dad(%rip), %rdi      # 0x1c083
callq 0x6070
movq %r15, %rdi
callq 0x6030
leaq -0x1f70(%rbp), %rsi
andl $0x0, (%rsi)
movq 0x8(%rbx), %rdi
callq 0x1439c
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x14d10
leaq 0x2e66(%rip), %rdi      # 0x1c171
movl %eax, %esi
xorl %eax, %eax
callq 0x6100
movq %r15, %rdi
movq %r14, %rsi
callq 0x154e0
leaq 0x2e5e(%rip), %rdi      # 0x1c184
movl %eax, %esi
xorl %eax, %eax
callq 0x6100
movq %r15, %rdi
callq 0x13c6e
movq %rbx, %rdi
callq 0xef9c
movq %r14, %rdi
callq 0x7c77
xorl %eax, %eax
addq $0x1f48, %rsp           # imm = 0x1F48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2cc2(%rip), %rdi      # 0x1c024
jmp 0x193a7
movq 0x8(%r15), %rsi
leaq 0x2ccc(%rip), %rdi      # 0x1c03b
xorl %eax, %eax
callq 0x6100
jmp 0x193ac
movq -0x30(%rbp), %rbx
movq 0x10(%rbx), %rdi
callq 0xeeea
movq %rbx, %rdi
callq 0xef9c
jmp 0x193ac
movq %r14, %rdi
callq 0x7c77
movq -0x30(%rbp), %rdi
callq 0xef9c
leaq 0x2df1(%rip), %rdi      # 0x1c198
callq 0x6070
pushq $0x1
popq %rdi
callq 0x61a0
movq %r15, %rdi
callq 0x6030
movq %rbx, %rdi
callq 0xef9c
movq %r14, %rdi
callq 0x7c77
leaq 0xc31(%rip), %rdi       # 0x1a004
leaq 0x2d68(%rip), %rsi      # 0x1c142
leaq 0x2923(%rip), %rcx      # 0x1bd04
xorl %r12d, %r12d
pushq $0x76
popq %rdx
xorl %eax, %eax
callq 0x6100
leaq 0xc37(%rip), %rbx       # 0x1a02c
leaq 0x2d51(%rip), %rsi      # 0x1c14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6100
leaq -0x1f70(%rbp), %r15
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x60e0
movl %eax, %r14d
movq %r15, %rdi
movl %eax, %esi
callq 0x60b0
movq %rax, %r15
leaq 0xde5(%rip), %rdi       # 0x1a216
callq 0x6070
testl %r14d, %r14d
cmovlel %r12d, %r14d
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x193ac
movq (%r15,%r12,8), %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6100
incq %r12
jmp 0x19440