// File: /workspace/github_c_makefile_star2_O3/alexj136[P]minijit/src/interpreter.c
// Function: interpret_Expr
InterpretResult *interpret_Expr(Prog *prog, Expr *expr, int *store) {
    if(Expr_isInt(expr)) {
        return InterpretSuccess_init(Int_value(expr));
    }
    else if(Expr_isAdd(expr) || Expr_isSub(expr)) {
        InterpretResult *lhs;
        InterpretResult *rhs;

        if(Expr_isAdd(expr)) {
            lhs = interpret_Expr(prog, Add_lhs(expr), store);
            rhs = interpret_Expr(prog, Add_rhs(expr), store);
        }
        else /* if(Expr_isSub(expr)) */ {
            lhs = interpret_Expr(prog, Sub_lhs(expr), store);
            rhs = interpret_Expr(prog, Sub_rhs(expr), store);
        }

        if((lhs->type == iSuccess) && (rhs->type == iSuccess)) {
            int lhs_val = lhs->result;
            int rhs_val = rhs->result;

            free(lhs);
            free(rhs);

            return InterpretSuccess_init(
                    lhs_val + (Expr_isSub(expr) ? -rhs_val : rhs_val));
        }

        else if(lhs->type != iSuccess) {
            free(rhs);
            return lhs;
        }

        else /* if(rhs->type != iSuccess) */ {
            free(lhs);
            return rhs;
        }
    }
    else if(Expr_isCall(expr)) {

        Func *callee = Prog_func(prog, Call_name(expr));

        if(callee == NULL) {
            return InterpretFailFunctionNotFound_init(Expr_src_line_no(expr),
                    Expr_src_char_no(expr));
        }

        if(Func_num_args(callee) != Call_num_args(expr)) {
            return InterpretFailIncorrectNumArgs_init(Expr_src_line_no(expr),
                    Expr_src_char_no(expr));
        }

        int *callee_store = challoc(sizeof(int) * Func_num_vars(callee));
        bool failed_arg_eval = false;
        InterpretResult *arg_res;

        // Evaluate each argument expression. Stop early if we encounter an
        // error.
        int idx;
        for(idx = 0; idx < Call_num_args(expr); idx++) {

            arg_res = interpret_Expr(prog, Call_arg(expr, idx), store);
            callee_store[idx] = arg_res->result;

            if(arg_res->type != iSuccess) {
                failed_arg_eval = true;
                break;
            }

            free(arg_res);
        }

        // If an error was encountered in argument evaluation, clean up and
        // return an error as the result of the entire call
        if(failed_arg_eval) {
            free(callee_store);
            return arg_res;
        }

        // Argument evaluation succeeded, so write zero in place for the initial
        // values of the other variables in the callee's store
        for(idx = Func_num_args(callee); idx < Func_num_vars(callee); idx++) {
            callee_store[idx] = 0;
        }

        // Make the call, free the argument array, and return the call result
        InterpretResult *call_res = interpret_Func(prog, callee, callee_store);
        free(callee_store);
        return call_res;
    }
    else if(Expr_isVar(expr)) {
        return InterpretSuccess_init(store[Var_name(expr)]);
    }
    else {
        ERROR("Expr type not recognised.");
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f58, %rsp           # imm = 0x1F58
movl (%rsi), %eax
cmpq $0x4, %rax
ja 0x13a25
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x8c2d(%rip), %rcx      # 0x1c420
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rbx), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x137c3
movq %rax, %r12
movq 0x10(%rbx), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x137c3
movq %rax, %r14
cmpl $0x0, (%r12)
je 0x13972
movq %r14, %rdi
callq 0x6030
jmp 0x139c2
movslq 0x4(%rbx), %rax
movl (%r15,%rax,4), %ebx
jmp 0x13845
movl 0x4(%rbx), %ebx
movl $0x14, %edi
callq 0x62ec
movq %rax, %r12
movl $0x0, (%rax)
movl %ebx, 0x4(%rax)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x8(%r12)
movl $0xffffffff, 0x10(%r12) # imm = 0xFFFFFFFF
jmp 0x139c2
movq (%r14), %rdi
movl 0x4(%rbx), %esi
callq 0x8853
testq %rax, %rax
je 0x13985
movq %rax, %r13
movl 0x4(%rax), %r12d
movq 0x18(%rbx), %rdi
callq 0x9edd
cmpl %eax, %r12d
jne 0x1399f
movq %r13, -0x38(%rbp)
movq 0x8(%r13), %rdi
callq 0x75f4
leal (,%rax,4), %edi
callq 0x62ec
movq %rax, -0x30(%rbp)
movq 0x18(%rbx), %rdi
callq 0x9edd
testl %eax, %eax
jle 0x13920
xorl %r13d, %r13d
movq 0x18(%rbx), %rdi
movl %r13d, %esi
callq 0xa170
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x137c3
movq %rax, %r12
movl 0x4(%rax), %eax
movq -0x30(%rbp), %rcx
movl %eax, (%rcx,%r13,4)
cmpl $0x0, (%r12)
jne 0x13a1c
movq %r12, %rdi
callq 0x6030
incq %r13
movq 0x18(%rbx), %rdi
callq 0x9edd
cltq
cmpq %rax, %r13
jl 0x138d2
movq -0x38(%rbp), %r15
movslq 0x4(%r15), %rbx
movq 0x8(%r15), %rdi
callq 0x75f4
cmpl %eax, %ebx
movq -0x30(%rbp), %r12
jge 0x13954
movl $0x0, (%r12,%rbx,4)
incq %rbx
movq 0x8(%r15), %rdi
callq 0x75f4
cltq
cmpq %rax, %rbx
jl 0x13939
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x13abd
movq %rax, %rbx
movq %r12, %rdi
callq 0x6030
movq %rbx, %rax
jmp 0x139c5
cmpl $0x0, (%r14)
je 0x139d7
movq %r12, %rdi
callq 0x6030
movq %r14, %r12
jmp 0x139c2
movq 0x20(%rbx), %rbx
movl $0x14, %edi
callq 0x62ec
movq %rax, %r12
movq $0x2, (%rax)
jmp 0x139b7
movq 0x20(%rbx), %rbx
movl $0x14, %edi
callq 0x62ec
movq %rax, %r12
movq $0x1, (%rax)
movl $0x0, 0x8(%rax)
movq %rbx, 0xc(%rax)
movq %r12, %rax
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%r12), %r15d
movl 0x4(%r14), %r13d
movq %r12, %rdi
callq 0x6030
movq %r14, %rdi
callq 0x6030
movl %r13d, %r14d
negl %r14d
cmpl $0x2, (%rbx)
cmovnel %r13d, %r14d
addl %r15d, %r14d
movl $0x14, %edi
callq 0x62ec
movq %rax, %r12
movl $0x0, (%rax)
movl %r14d, 0x4(%rax)
jmp 0x1385b
movq -0x30(%rbp), %rdi
jmp 0x1382e
leaq 0x75d8(%rip), %rdi      # 0x1b004
leaq 0x8a01(%rip), %rsi      # 0x1c434
leaq 0x8a1b(%rip), %rcx      # 0x1c455
movl $0xf3, %edx
xorl %eax, %eax
callq 0x6100
leaq 0x75df(%rip), %rdi      # 0x1b02c
leaq 0x7982(%rip), %rsi      # 0x1b3d6
xorl %eax, %eax
callq 0x6100
leaq -0x1f80(%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 0x7790(%rip), %rdi      # 0x1b216
callq 0x6070
testl %r14d, %r14d
jle 0x13ab3
movl %r14d, %r15d
leaq 0x7592(%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 0x13a9d
movl $0x1, %edi
callq 0x61a0