// File: /workspace/github_c_makefile_star2_O3/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
callq 0x1420e
testl %eax, %eax
jle 0x15cb5
movl $0x5, %r13d
movl $0x7fffffff, -0x2c(%rbp) # imm = 0x7FFFFFFF
xorl %r15d, %r15d
leaq 0x6b2c(%rip), %r12      # 0x1c754
movq $0x0, -0x38(%rbp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x144a1
movl (%rax), %ecx
cmpq $0xb, %rcx
ja 0x15dba
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
movq %r14, %rdi
callq 0x1420e
cmpl %eax, %r15d
jl 0x15c30
jmp 0x15ca0
movl 0x4(%rax), %eax
testl %eax, %eax
js 0x15e51
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 0x15c64
movl 0x4(%rax), %eax
jmp 0x15c5d
leal 0x4(,%r13,4), %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %r13d
movq -0x38(%rbp), %r12
jmp 0x15cc5
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movl $0x18, -0x30(%rbp)
movl $0x28, %edi
callq 0x62ec
movq %rax, %r15
movq %r14, (%rax)
movl $0x61a80, %edi          # imm = 0x61A80
callq 0x62ec
movq %rax, 0x8(%r15)
movl -0x30(%rbp), %edi
callq 0x62ec
movq %rax, 0x10(%r15)
subl %r13d, %r12d
leal 0x4(,%r12,4), %edi
callq 0x62ec
movq %rax, 0x18(%r15)
movl %r13d, 0x20(%r15)
movq %r14, %rdi
callq 0x1420e
testl %eax, %eax
jle 0x15d48
xorl %r12d, %r12d
movslq %r13d, %r13
movq %r14, %rdi
movl %r12d, %esi
callq 0x144a1
cmpl $0xa, (%rax)
jne 0x15d38
movq 0x18(%r15), %rcx
movslq 0x4(%rax), %rax
subq %r13, %rax
movl %r12d, (%rcx,%rax,4)
incl %r12d
movq %r14, %rdi
callq 0x1420e
cmpl %eax, %r12d
jl 0x15d19
movq %rbx, %rdi
callq 0x75f4
movq 0x10(%r15), %rcx
movl %eax, (%rcx)
movq %rbx, %rdi
callq 0x75f4
decl %eax
movq 0x10(%r15), %rcx
movl %eax, 0x4(%rcx)
movq $0x0, 0x8(%rcx)
movq %rbx, %rdi
callq 0x75f4
testl %eax, %eax
jle 0x15da5
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x7887
movl (%rax), %eax
movq 0x8(%r15), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
movq %rbx, %rdi
callq 0x75f4
cltq
cmpq %rax, %r14
jl 0x15d7e
movq %r15, %rax
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5243(%rip), %rdi      # 0x1b004
leaq 0x69ec(%rip), %rsi      # 0x1c7b4
leaq 0x69fd(%rip), %rcx      # 0x1c7cc
movl $0x68, %edx
xorl %eax, %eax
callq 0x6100
leaq 0x524a(%rip), %rdi      # 0x1b02c
leaq 0x6a2f(%rip), %rsi      # 0x1c818
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 0x53fb(%rip), %rdi      # 0x1b216
callq 0x6070
testl %r14d, %r14d
jle 0x15edf
movl %r14d, %r15d
leaq 0x51f9(%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 0x15e36
jmp 0x15edf
leaq 0x51ac(%rip), %rdi      # 0x1b004
leaq 0x6955(%rip), %rsi      # 0x1c7b4
leaq 0x6966(%rip), %rcx      # 0x1c7cc
movl $0x58, %edx
xorl %eax, %eax
callq 0x6100
leaq 0x51b3(%rip), %rdi      # 0x1b02c
leaq 0x6967(%rip), %rsi      # 0x1c7e7
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 0x5364(%rip), %rdi      # 0x1b216
callq 0x6070
testl %r14d, %r14d
jle 0x15edf
movl %r14d, %r15d
leaq 0x5166(%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 0x15ec9
movl $0x1, %edi
callq 0x61a0