// File: /workspace/github_c_makefile_star2_O1/alexj136[P]minijit/src/icodegen.c
// Function: icodegen_Expr
ICodeOperationVector *icodegen_Expr(Prog *prog, Expr *expr, int *next_label,
        int *func_labels) {

    BEGIN_CODE_GENERATION;

    if(Expr_isInt(expr)) {

        // Just load the literal value into the accumulator
        INSERT_OPERATION(LOADIMM    , Int_value(expr)   , ACCUMULATOR   );
    }
    else if(Expr_isAdd(expr) || Expr_isSub(expr)) {

        // Evaluate the RHS
        if(Expr_isAdd(expr)) {
            INSERT_EXPR_CODE(Add_rhs(expr));
        }
        else /* if(Expr_isSub(expr)) */ {
            INSERT_EXPR_CODE(Sub_rhs(expr));
        }

        // Push the RHS result on the stack
        INSERT_OPERATION(STORE      , ACCUMULATOR       , STACK_POINTER );
        INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
        INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );

        // Evaluate the LHS
        INSERT_EXPR_CODE(Add_lhs(expr));

        // Pop the RHS into temporary register
        INSERT_OPERATION(LOADIMM    , 1                 , TEMPORARY     );
        INSERT_OPERATION(SUB        , STACK_POINTER     , TEMPORARY     );
        INSERT_OPERATION(LOAD       , STACK_POINTER     , TEMPORARY     );

        // Do the operation
        if(Expr_isAdd(expr)) {
            INSERT_OPERATION(ADD        , ACCUMULATOR       , TEMPORARY     );
        }
        else /* if(Expr_isSub(expr)) */ {
            INSERT_OPERATION(SUB        , ACCUMULATOR       , TEMPORARY     );
        }
    }
    else if(Expr_isCall(expr)) {

        // Determine the label number to jump to in the call
        int callee_label = func_labels[Call_name(expr)];
        int num_args     = Call_num_args(expr);
        int num_vars     = Func_num_vars(Prog_func(prog, Call_name(expr)));
        int idx;

        // Push the current frame pointer on the stack
        INSERT_OPERATION(STORE      , FRAME_POINTER     , STACK_POINTER );
        INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
        INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );

        // Push the current return address on the stack
        INSERT_OPERATION(STORE      , RETURN_ADDRESS    , STACK_POINTER );
        INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
        INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );

        // Add zeros to the stack for the non-argument variables in the callee
        for(idx = num_vars - 1; idx >= num_args; idx--) {

            INSERT_OPERATION(LOADIMM    , 0                 , ACCUMULATOR   );
            INSERT_OPERATION(STORE      , ACCUMULATOR       , STACK_POINTER );
            INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
            INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );
        }

        // For each argument expression, evaluate and push the result on the
        // stack
        for(idx = num_args - 1; idx >= 0; idx--) {

            // Evaluate the idxth argument expression
            INSERT_EXPR_CODE(Call_arg(expr, idx));

            // Push the result on the stack
            INSERT_OPERATION(STORE      , ACCUMULATOR       , STACK_POINTER );
            INSERT_OPERATION(LOADIMM    , 1                 , ACCUMULATOR   );
            INSERT_OPERATION(ADD        , STACK_POINTER     , ACCUMULATOR   );
        }

        // Set the frame pointer for the callee (one less than the stack
        // pointer)
        INSERT_OPERATION(MOVE       , STACK_POINTER     , FRAME_POINTER );
        INSERT_OPERATION(LOADIMM    , 1                 , TEMPORARY     );
        INSERT_OPERATION(SUB        , FRAME_POINTER     , TEMPORARY     );

        // Jump to the function
        INSERT_OPERATION(JUMPLINK   , callee_label      , 0             );

        // Pop the arguments and variables off the stack
        INSERT_OPERATION(LOADIMM    , num_vars          , TEMPORARY     );
        INSERT_OPERATION(SUB        , STACK_POINTER     , TEMPORARY     );

        // Restore the return address
        INSERT_OPERATION(LOADIMM    , 1                 , TEMPORARY     );
        INSERT_OPERATION(SUB        , STACK_POINTER     , TEMPORARY     );
        INSERT_OPERATION(LOAD       , STACK_POINTER     , RETURN_ADDRESS);

        // Restore the frame pointer
        INSERT_OPERATION(LOADIMM    , 1                 , TEMPORARY     );
        INSERT_OPERATION(SUB        , STACK_POINTER     , TEMPORARY     );
        INSERT_OPERATION(LOAD       , STACK_POINTER     , FRAME_POINTER );
    }
    else if(Expr_isVar(expr)) {

        // Calculate the address from which to load
        INSERT_OPERATION(LOADIMM    , Var_name(expr)    , ACCUMULATOR   );
        INSERT_OPERATION(MOVE       , FRAME_POINTER     , TEMPORARY     );
        INSERT_OPERATION(SUB        , TEMPORARY         , ACCUMULATOR   );

        // Perform the load operation with the calculated address
        INSERT_OPERATION(LOAD       , TEMPORARY         , ACCUMULATOR   );
    }
    else {
        ERROR("Expr type not recognised.");
    }

    RETURN_GENERATED_CODE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f68, %rsp           # imm = 0x1F68
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r12
xorl %eax, %eax
callq 0x14232
movq %rax, %rbx
movl (%r14), %eax
cmpq $0x4, %rax
ja 0x15b61
leaq 0x711f(%rip), %rcx      # 0x1c724
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%r14), %rsi
movq %r12, %rdi
movq %r13, %rdx
movq %r15, %rcx
callq 0x155c7
movq %r12, -0x30(%rbp)
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1440b
movq %r12, %rdi
callq 0x149e4
movl $0x2, %edi
movl $0x4, %esi
xorl %edx, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x5, %edi
movl $0x1, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x3, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movq 0x8(%r14), %rsi
movq -0x30(%rbp), %rdi
movq %r13, %rdx
movq %r15, %rcx
callq 0x155c7
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1440b
movq %r15, %rdi
callq 0x149e4
movl $0x5, %edi
movl $0x1, %esi
movl $0x5, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x4, %edi
xorl %esi, %esi
movl $0x5, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x1, %edi
xorl %esi, %esi
movl $0x5, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
cmpl $0x1, (%r14)
jne 0x15b2d
movl $0x3, %edi
jmp 0x15b32
movl 0x4(%r14), %esi
movl $0x5, %edi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
xorl %edi, %edi
movl $0x1, %esi
movl $0x5, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x4, %edi
movl $0x5, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x1, %edi
movl $0x5, %esi
jmp 0x15793
movl 0x4(%r14), %esi
movl $0x5, %edi
movl $0x4, %edx
jmp 0x15b3c
movq %r13, -0x40(%rbp)
movslq 0x4(%r14), %rax
movq %r15, %r13
movl (%r15,%rax,4), %eax
movl %eax, -0x38(%rbp)
movq 0x18(%r14), %rdi
callq 0x9eba
movq %r12, %rcx
movl %eax, %r12d
movq %rcx, -0x30(%rbp)
movq (%rcx), %rdi
movq %r14, -0x48(%rbp)
movl 0x4(%r14), %esi
callq 0x883f
movq 0x8(%rax), %rdi
callq 0x75ec
movl %eax, %r14d
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x5, %edi
movl $0x1, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x3, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x5, %edi
movl $0x1, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x3, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
cmpl %r12d, %r14d
jle 0x1590f
movl %r14d, %r15d
decl %r15d
movl $0x5, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x2, %edi
movl $0x4, %esi
xorl %edx, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x5, %edi
movl $0x1, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x3, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x13d40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1431c
cmpl %r12d, %r15d
jg 0x15894
movl %r14d, -0x34(%rbp)
testl %r12d, %r12d
movq %r13, %r15
movq -0x40(%rbp), %r13
movq %rbx, %r14
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rbx
jle 0x159d8
incl %r12d
leal -0x2(%r12), %esi
movq 0x18(%rax), %rdi
callq 0xa14b
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x155c7
movq %r15, %r13
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x1440b
movq %r15, %rdi
movq %r13, %r15
movq -0x40(%rbp), %r13
callq 0x149e4
movl $0x2, %edi
movl $0x4, %esi
xorl %edx, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x5, %edi
movl $0x1, %esi
movl $0x4, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x3, %edi
xorl %esi, %esi
movl $0x4, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movq -0x48(%rbp), %rax
decl %r12d
cmpl $0x1, %r12d
jg 0x15931
xorl %edi, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x5, %edi
movl $0x1, %esi
movl $0x5, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x4, %edi
movl $0x1, %esi
movl $0x5, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x8, %edi
movl -0x38(%rbp), %esi
xorl %edx, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x5, %edi
movl -0x34(%rbp), %esi
movl $0x5, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x4, %edi
xorl %esi, %esi
movl $0x5, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x5, %edi
movl $0x1, %esi
movl $0x5, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x4, %edi
xorl %esi, %esi
movl $0x5, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x1, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x5, %edi
movl $0x1, %esi
movl $0x5, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x4, %edi
xorl %esi, %esi
movl $0x5, %edx
callq 0x13d40
movq %r14, %rdi
movq %rax, %rsi
callq 0x1431c
movl $0x1, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x13d40
movq %rax, %rsi
movq %r14, %rbx
jmp 0x15b44
movl $0x4, %edi
movl $0x4, %esi
movl $0x5, %edx
callq 0x13d40
movq %rax, %rsi
movq %rbx, %rdi
callq 0x1431c
movq %rbx, %rax
addq $0x1f68, %rsp           # imm = 0x1F68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x549c(%rip), %rdi      # 0x1b004
leaq 0x6bc9(%rip), %rsi      # 0x1c738
leaq 0x6bdf(%rip), %rcx      # 0x1c755
movl $0x105, %edx            # imm = 0x105
xorl %eax, %eax
callq 0x6100
leaq 0x54a3(%rip), %rdi      # 0x1b02c
leaq 0x5846(%rip), %rsi      # 0x1b3d6
xorl %eax, %eax
callq 0x6100
leaq -0x1f90(%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 0x5654(%rip), %rdi      # 0x1b216
callq 0x6070
testl %r14d, %r14d
jle 0x15bef
movl %r14d, %r15d
leaq 0x5456(%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 0x15bd9
movl $0x1, %edi
callq 0x61a0
nopl (%rax)