// File: /workspace/github_c_makefile_star2_O2/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 0x12d73
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r13
leaq 0x88c4(%rip), %rcx      # 0x1b450
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rbx), %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x12b5c
movq %rax, %r12
movq 0x10(%rbx), %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x12b5c
movq %rax, %r14
cmpl $0x0, (%r12)
je 0x12c8a
movq %r14, %rdi
callq 0x6030
jmp 0x12c9b
movslq 0x4(%rbx), %rax
movl (%r15,%rax,4), %edi
jmp 0x12d10
movl 0x4(%rbx), %edi
jmp 0x12d10
movq (%r13), %rdi
movl 0x4(%rbx), %esi
callq 0x86d9
testq %rax, %rax
je 0x12cb0
movq %rax, %r12
movl 0x4(%rax), %r14d
movq 0x18(%rbx), %rdi
callq 0x9b87
cmpl %eax, %r14d
jne 0x12ccc
movq %r13, -0x30(%rbp)
movq %r12, -0x38(%rbp)
movq 0x8(%r12), %rdi
callq 0x7537
leal (,%rax,4), %edi
callq 0x62ec
movq %rax, %r13
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
callq 0x9b87
cltq
cmpq %rax, %r14
jge 0x12d26
movq 0x18(%rbx), %rdi
movl %r14d, %esi
callq 0x9e11
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x12b5c
movq %rax, %r12
movl 0x4(%rax), %eax
movl %eax, (%r13,%r14,4)
cmpl $0x0, (%r12)
jne 0x12d6b
movq %r12, %rdi
callq 0x6030
incq %r14
jmp 0x12c38
cmpl $0x0, (%r14)
je 0x12ce8
movq %r12, %rdi
callq 0x6030
movq %r14, %r12
movq %r12, %rax
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x20(%rbx), %edi
movl 0x24(%rbx), %esi
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12aae
movl 0x20(%rbx), %edi
movl 0x24(%rbx), %esi
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12a96
movl 0x4(%r12), %r15d
movl 0x4(%r14), %r13d
movq %r12, %rdi
callq 0x6030
movq %r14, %rdi
callq 0x6030
movl %r13d, %edi
negl %edi
cmpl $0x2, (%rbx)
cmovnel %r13d, %edi
addl %r15d, %edi
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12a7e
movq -0x38(%rbp), %r14
movslq 0x4(%r14), %rbx
movq 0x8(%r14), %rdi
callq 0x7537
cltq
cmpq %rax, %rbx
jge 0x12d49
andl $0x0, (%r13,%rbx,4)
incq %rbx
jmp 0x12d2e
movq -0x30(%rbp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x12e09
movq %rax, %rbx
movq %r13, %rdi
callq 0x6030
movq %rbx, %rax
jmp 0x12c9e
movq %r13, %rdi
jmp 0x12bc7
leaq 0x728a(%rip), %rdi      # 0x1a004
leaq 0x86f3(%rip), %rsi      # 0x1b474
leaq 0x870d(%rip), %rcx      # 0x1b495
xorl %r12d, %r12d
movl $0xf3, %edx
xorl %eax, %eax
callq 0x6100
leaq 0x728e(%rip), %rbx      # 0x1a02c
leaq 0x7641(%rip), %rsi      # 0x1a3e6
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6100
leaq -0x1f80(%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 0x743c(%rip), %rdi      # 0x1a216
callq 0x6070
testl %r14d, %r14d
cmovlel %r12d, %r14d
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x12e01
movq (%r15,%r12,8), %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6100
incq %r12
jmp 0x12de9
pushq $0x1
popq %rdi
callq 0x61a0