// File: /workspace/github_c_makefile_star2_O2/alexj136[P]minijit/src/icode_interpreter.c
// Function: ICodeInterpreterState_init
ICodeInterpreterState *ICodeInterpreterState_init(ICodeOperationVector *code,
        IntRefVector *initial_stack) {

    int idx;
    int min_label = INT_MAX;
    int max_label = 0;
    int max_reg   = TEMPORARY;

    // Perform a scan of the icode to determine the maximum and minimum label
    // numbers, so that we need not allocate a huge amount of memory for label
    // numbers not needed. Also determine how much memory we need to allocate
    // for registers.
    for(idx = 0; idx < ICodeOperationVector_size(code); idx++) {
        ICodeOperation *next = ICodeOperationVector_get(code, idx);

        // Check for new maximum register
        if((next->opc == MOVE)
                || (next->opc == LOAD)
                || (next->opc == STORE)
                || (next->opc == ADD)
                || (next->opc == SUB)) {

            if(next->arg1 > max_reg) {
                max_reg = next->arg1;
            }
            if(next->arg2 > max_reg) {
                max_reg = next->arg2;
            }
        }

        // Check for new maximum register (arg1 is not a register in LOADIMM)
        else if(next->opc == LOADIMM) {
            if(next->arg2 > max_reg) {
                max_reg = next->arg2;
            }
        }

        // No registers in a JUMP
        else if(next->opc == JUMP) {
            // Nothing to do here
        }

        else if(next->opc == JUMPCOND) {
        }

        // No explicit registers in a JUMPLINK (implicit RETURN_ADDRESS need not
        // be accounted for since it is less in its index than the minimum
        // size allowed for the registers array)
        else if(next->opc == JUMPLINK) {
            // Nothing to do here
        }

        // arg2 is not a register in JUMPADDR
        else if(next->opc == JUMPADDR) {
            if(next->arg1 > max_reg) {
                max_reg = next->arg1;
            }
        }

        // Check if we have the new minimum/maximum label
        else if(next->opc == LABEL) {
            if(next->arg1 < 0) {
                ERROR("Negative label number in an ICodeOperationVector");
            }
            if(next->arg1 > max_label) {
                max_label = next->arg1;
            }
            if(next->arg1 < min_label) {
                min_label = next->arg1;
            }
        }

        // No registers in a HALT
        else if(next->opc == HALT) {
            // Nothing to do here
        }

        else {
            ERROR("Unrecognised Opcode in ICodeOperation");
        }
    }

    ICodeInterpreterState *state = challoc(sizeof(ICodeInterpreterState));
    state->code      = code;
    state->stack     = challoc(sizeof(int) * ICodeInterpreterState_STACK_SIZE);
    state->registers = challoc(sizeof(int) * (max_reg + 1));
    state->labels    = challoc(sizeof(int) * (max_label - min_label + 1));
    state->min_label = min_label;

    // Populate the labels array. The labels array maps label names to indexes
    // into the ICodeOperationVector.
    for(idx = 0; idx < ICodeOperationVector_size(code); idx++) {
        ICodeOperation *next = ICodeOperationVector_get(code, idx);
        if(next->opc == LABEL) {
            (state->labels)[(next->arg1) - min_label] = idx;
        }
    }

    // Initialise the main registers
    // Stack pointer points to the next free space on the stack
    (state->registers)[STACK_POINTER]   = IntRefVector_size(initial_stack);
    // Frame pointer points to the first variable in the frame
    (state->registers)[FRAME_POINTER]   = IntRefVector_size(initial_stack) - 1;
    (state->registers)[RETURN_ADDRESS]  = 0;
    (state->registers)[PROGRAM_COUNTER] = 0;

    // Initialise the stack
    for(idx = 0; idx < IntRefVector_size(initial_stack); idx++) {
        (state->stack)[idx] =
                IntRef_value(IntRefVector_get(initial_stack, idx));
    }

    return state;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f58, %rsp           # imm = 0x1F58
movq %rsi, %rbx
movq %rdi, %r14
movl $0x7fffffff, -0x2c(%rbp) # imm = 0x7FFFFFFF
movq $0x0, -0x38(%rbp)
pushq $0x5
popq %r13
leaq 0x6a1e(%rip), %r12      # 0x1b794
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x1352e
cmpl %eax, %r15d
jge 0x14de9
movq %r14, %rdi
movl %r15d, %esi
callq 0x137b2
movl (%rax), %ecx
cmpq $0xb, %rcx
ja 0x14ed1
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
movl 0x4(%rax), %ecx
cmpl %r13d, %ecx
cmovgl %ecx, %r13d
movl 0x8(%rax), %eax
cmpl %r13d, %eax
cmovgl %eax, %r13d
incl %r15d
jmp 0x14d79
movl 0x4(%rax), %eax
testl %eax, %eax
js 0x14f61
movq -0x38(%rbp), %rcx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movq %rcx, -0x38(%rbp)
movl -0x2c(%rbp), %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, -0x2c(%rbp)
jmp 0x14dba
movl 0x4(%rax), %eax
jmp 0x14db3
pushq $0x28
popq %rdi
callq 0x62ec
movq %rax, %r15
movq %r14, (%rax)
movl $0x61a80, %edi          # imm = 0x61A80
callq 0x62ec
movq %rax, 0x8(%r15)
leal 0x4(,%r13,4), %edi
callq 0x62ec
movq %rax, 0x10(%r15)
movl -0x2c(%rbp), %r13d
movq -0x38(%rbp), %rax
subl %r13d, %eax
leal 0x4(,%rax,4), %edi
callq 0x62ec
movq %rax, 0x18(%r15)
movl %r13d, 0x20(%r15)
xorl %r12d, %r12d
movslq %r13d, %r13
movq %r14, %rdi
callq 0x1352e
cmpl %eax, %r12d
jge 0x14e6c
movq %r14, %rdi
movl %r12d, %esi
callq 0x137b2
cmpl $0xa, (%rax)
jne 0x14e67
movq 0x18(%r15), %rcx
movslq 0x4(%rax), %rax
subq %r13, %rax
movl %r12d, (%rcx,%rax,4)
incl %r12d
jmp 0x14e3b
movq %rbx, %rdi
callq 0x7537
movq 0x10(%r15), %rcx
movl %eax, (%rcx)
movq %rbx, %rdi
callq 0x7537
decl %eax
movq 0x10(%r15), %rcx
movl %eax, 0x4(%rcx)
andq $0x0, 0x8(%rcx)
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x7537
cltq
cmpq %rax, %r14
jge 0x14ebc
movq %rbx, %rdi
movl %r14d, %esi
callq 0x77bb
movl (%rax), %eax
movq 0x8(%r15), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
jmp 0x14e93
movq %r15, %rax
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x512c(%rip), %rdi      # 0x1a004
leaq 0x6915(%rip), %rsi      # 0x1b7f4
leaq 0x6926(%rip), %rcx      # 0x1b80c
xorl %r12d, %r12d
pushq $0x68
popq %rdx
xorl %eax, %eax
callq 0x6100
leaq 0x5132(%rip), %rbx      # 0x1a02c
leaq 0x6957(%rip), %rsi      # 0x1b858
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 0x52e0(%rip), %rdi      # 0x1a216
callq 0x6070
testl %r14d, %r14d
cmovlel %r12d, %r14d
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x14fed
movq (%r15,%r12,8), %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6100
incq %r12
jmp 0x14f45
leaq 0x509c(%rip), %rdi      # 0x1a004
leaq 0x6885(%rip), %rsi      # 0x1b7f4
leaq 0x6896(%rip), %rcx      # 0x1b80c
xorl %r12d, %r12d
pushq $0x58
popq %rdx
xorl %eax, %eax
callq 0x6100
leaq 0x50a2(%rip), %rbx      # 0x1a02c
leaq 0x6896(%rip), %rsi      # 0x1b827
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 0x5250(%rip), %rdi      # 0x1a216
callq 0x6070
testl %r14d, %r14d
cmovlel %r12d, %r14d
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x14fed
movq (%r15,%r12,8), %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x6100
incq %r12
jmp 0x14fd5
pushq $0x1
popq %rdi
callq 0x61a0