// File: /workspace/github_c_makefile_star2_O0/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
subq $0x3f10, %rsp           # imm = 0x3F10
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x7fffffff, -0x18(%rbp) # imm = 0x7FFFFFFF
movl $0x0, -0x1c(%rbp)
movl $0x5, -0x20(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x3efc(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1a540
movl %eax, %ecx
movl -0x3efc(%rbp), %eax
cmpl %ecx, %eax
jge 0x1cd8b
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x1a900
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1cade
movq -0x28(%rbp), %rax
cmpl $0x1, (%rax)
je 0x1cade
movq -0x28(%rbp), %rax
cmpl $0x2, (%rax)
je 0x1cade
movq -0x28(%rbp), %rax
cmpl $0x3, (%rax)
je 0x1cade
movq -0x28(%rbp), %rax
cmpl $0x4, (%rax)
jne 0x1cb0f
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x20(%rbp), %eax
jle 0x1caf4
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x20(%rbp), %eax
jle 0x1cb0a
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x20(%rbp)
jmp 0x1cd7b
movq -0x28(%rbp), %rax
cmpl $0x5, (%rax)
jne 0x1cb33
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x20(%rbp), %eax
jle 0x1cb2e
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x20(%rbp)
jmp 0x1cd79
movq -0x28(%rbp), %rax
cmpl $0x6, (%rax)
jne 0x1cb41
jmp 0x1cd77
movq -0x28(%rbp), %rax
cmpl $0x7, (%rax)
jne 0x1cb4f
jmp 0x1cd75
movq -0x28(%rbp), %rax
cmpl $0x8, (%rax)
jne 0x1cb5d
jmp 0x1cd73
movq -0x28(%rbp), %rax
cmpl $0x9, (%rax)
jne 0x1cb81
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x20(%rbp), %eax
jle 0x1cb7c
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x20(%rbp)
jmp 0x1cd71
movq -0x28(%rbp), %rax
cmpl $0xa, (%rax)
jne 0x1cc96
movq -0x28(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jge 0x1cc65
jmp 0x1cb9e
leaq 0x745f(%rip), %rdi      # 0x24004
leaq 0x8b6a(%rip), %rsi      # 0x25716
movl $0x58, %edx
leaq 0x8b76(%rip), %rcx      # 0x2572e
movb $0x0, %al
callq 0x60f0
leaq 0x7466(%rip), %rdi      # 0x2402c
leaq 0x8b7c(%rip), %rsi      # 0x25749
movb $0x0, %al
callq 0x60f0
leaq -0x1f70(%rbp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x60d0
movl %eax, -0x1f74(%rbp)
leaq -0x1f70(%rbp), %rdi
movl -0x1f74(%rbp), %esi
callq 0x60a0
movq %rax, -0x1f80(%rbp)
leaq 0x7440(%rip), %rdi      # 0x2404b
movb $0x0, %al
callq 0x60f0
movl $0x0, -0x1f84(%rbp)
movl -0x1f84(%rbp), %eax
cmpl -0x1f74(%rbp), %eax
jge 0x1cc5b
movq -0x1f80(%rbp), %rax
movslq -0x1f84(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x73e9(%rip), %rdi      # 0x2402c
movb $0x0, %al
callq 0x60f0
movl -0x1f84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1f84(%rbp)
jmp 0x1cc1c
movl $0x1, %edi
callq 0x61a0
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x1c(%rbp), %eax
jle 0x1cc7b
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x18(%rbp), %eax
jge 0x1cc91
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x18(%rbp)
jmp 0x1cd6f
movq -0x28(%rbp), %rax
cmpl $0xb, (%rax)
jne 0x1cca4
jmp 0x1cd6d
jmp 0x1cca6
leaq 0x7357(%rip), %rdi      # 0x24004
leaq 0x8a62(%rip), %rsi      # 0x25716
movl $0x68, %edx
leaq 0x8a6e(%rip), %rcx      # 0x2572e
movb $0x0, %al
callq 0x60f0
leaq 0x735e(%rip), %rdi      # 0x2402c
leaq 0x8aa5(%rip), %rsi      # 0x2577a
movb $0x0, %al
callq 0x60f0
leaq -0x3ed0(%rbp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x60d0
movl %eax, -0x3ed4(%rbp)
leaq -0x3ed0(%rbp), %rdi
movl -0x3ed4(%rbp), %esi
callq 0x60a0
movq %rax, -0x3ee0(%rbp)
leaq 0x7338(%rip), %rdi      # 0x2404b
movb $0x0, %al
callq 0x60f0
movl $0x0, -0x3ee4(%rbp)
movl -0x3ee4(%rbp), %eax
cmpl -0x3ed4(%rbp), %eax
jge 0x1cd63
movq -0x3ee0(%rbp), %rax
movslq -0x3ee4(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x72e1(%rip), %rdi      # 0x2402c
movb $0x0, %al
callq 0x60f0
movl -0x3ee4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3ee4(%rbp)
jmp 0x1cd24
movl $0x1, %edi
callq 0x61a0
jmp 0x1cd6f
jmp 0x1cd71
jmp 0x1cd73
jmp 0x1cd75
jmp 0x1cd77
jmp 0x1cd79
jmp 0x1cd7b
jmp 0x1cd7d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1ca7f
movl $0x28, %edi
callq 0x62f0
movq %rax, -0x3ef0(%rbp)
movq -0x8(%rbp), %rcx
movq -0x3ef0(%rbp), %rax
movq %rcx, (%rax)
movl $0x61a80, %edi          # imm = 0x61A80
callq 0x62f0
movq %rax, %rcx
movq -0x3ef0(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x20(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
movl %eax, %edi
callq 0x62f0
movq %rax, %rcx
movq -0x3ef0(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %eax
subl -0x18(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
movl %eax, %edi
callq 0x62f0
movq %rax, %rcx
movq -0x3ef0(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x18(%rbp), %ecx
movq -0x3ef0(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x3f00(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1a540
movl %eax, %ecx
movl -0x3f00(%rbp), %eax
cmpl %ecx, %eax
jge 0x1ce86
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x1a900
movq %rax, -0x3ef8(%rbp)
movq -0x3ef8(%rbp), %rax
cmpl $0xa, (%rax)
jne 0x1ce79
movl -0x14(%rbp), %edx
movq -0x3ef0(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x3ef8(%rbp), %rcx
movl 0x4(%rcx), %ecx
subl -0x18(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1ce7b
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1ce1b
movq -0x10(%rbp), %rdi
callq 0x7e20
movl %eax, %ecx
movq -0x3ef0(%rbp), %rax
movq 0x10(%rax), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
callq 0x7e20
movl %eax, %ecx
subl $0x1, %ecx
movq -0x3ef0(%rbp), %rax
movq 0x10(%rax), %rax
movl %ecx, 0x4(%rax)
movq -0x3ef0(%rbp), %rax
movq 0x10(%rax), %rax
movl $0x0, 0x8(%rax)
movq -0x3ef0(%rbp), %rax
movq 0x10(%rax), %rax
movl $0x0, 0xc(%rax)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x3f04(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7e20
movl %eax, %ecx
movl -0x3f04(%rbp), %eax
cmpl %ecx, %eax
jge 0x1cf2e
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x81e0
movl (%rax), %edx
movq -0x3ef0(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x14(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1cee5
movq -0x3ef0(%rbp), %rax
addq $0x3f10, %rsp           # imm = 0x3F10
popq %rbp
retq
nop