// File: /workspace/github_c_makefile_star2_O2/alexj136[P]minijit/src/ncodegen.c
// Function: ncode_gen
NCodeLinkable *ncode_gen(ICodeOperationVector *ops) {

    // Allocate space to store the generated ncode. Allocate enough space such
    // that if every instruction were the longest type, there would be exactly
    // enough space for the generated code.
    int ncode_buf_size = sizeof(byte) * ((ICodeOperationVector_size(ops) *
            JUMPLINK_x86_64_size) + x86_64_preamble_size);
    byte *ncode_buf = challoc(ncode_buf_size);
    int ncode_buf_alloc_idx = 0;
    int icode_idx;
    LinkTargetVector *link_targets = LinkTargetVector_init();
    IntRefVector *label_map = IntRefVector_init();

    // Include the preamble
    write_x86_64_preamble(ncode_buf + ncode_buf_alloc_idx, NULL, NULL);
    ncode_buf_alloc_idx += x86_64_preamble_size;

    // Loop writes the instructions in place and records the link locations to
    // be filled in later
    for(icode_idx = 0; icode_idx < ICodeOperationVector_size(ops);
            icode_idx++) {

        ICodeOperation *cur_instr = ICodeOperationVector_get(ops, icode_idx);
        int opc = cur_instr->opc;
        int arg1 = cur_instr->arg1;
        int arg2 = cur_instr->arg2;

        if(opc == MOVE) {
            write_x86_64_MOVE(ncode_buf + ncode_buf_alloc_idx, arg1, arg2);
            ncode_buf_alloc_idx += MOVE_x86_64_size;
        }
        else if(opc == LOAD) {
            write_x86_64_LOAD(ncode_buf + ncode_buf_alloc_idx, arg1, arg2);
            ncode_buf_alloc_idx += LOAD_x86_64_size;
        }
        else if(opc == STORE) {
            write_x86_64_STORE(ncode_buf + ncode_buf_alloc_idx, arg1, arg2);
            ncode_buf_alloc_idx += STORE_x86_64_size;
        }
        else if(opc == ADD) {
            write_x86_64_ADD(ncode_buf + ncode_buf_alloc_idx, arg1, arg2);
            ncode_buf_alloc_idx += ADD_x86_64_size;
        }
        else if(opc == SUB) {
            write_x86_64_SUB(ncode_buf + ncode_buf_alloc_idx, arg1, arg2);
            ncode_buf_alloc_idx += SUB_x86_64_size;
        }
        else if(opc == LOADIMM) {
            write_x86_64_LOADIMM(ncode_buf + ncode_buf_alloc_idx, arg1, arg2);
            ncode_buf_alloc_idx += LOADIMM_x86_64_size;
        }
        else if(opc == JUMP) {
            write_x86_64_JUMP(ncode_buf + ncode_buf_alloc_idx, NULL);
            LinkTargetVector_append(link_targets, LinkTarget_init(
                    ncode_buf_alloc_idx + JUMP_x86_64_address_offset, arg1));
            ncode_buf_alloc_idx += JUMP_x86_64_size;
        }
        else if(opc == JUMPCOND) {
            write_x86_64_JUMPCOND(ncode_buf + ncode_buf_alloc_idx, NULL, arg2);
            LinkTargetVector_append(link_targets, LinkTarget_init(
                    ncode_buf_alloc_idx + JUMPCOND_x86_64_address_offset,
                    arg1));
            ncode_buf_alloc_idx += JUMPCOND_x86_64_size;
        }
        else if(opc == JUMPLINK) {
            write_x86_64_JUMPLINK(ncode_buf + ncode_buf_alloc_idx, NULL);
            LinkTargetVector_append(link_targets, LinkTarget_init(
                    ncode_buf_alloc_idx + JUMPLINK_x86_64_address_offset,
                    arg1));
            ncode_buf_alloc_idx += JUMPLINK_x86_64_size;
        }
        else if(opc == JUMPADDR) {
            write_x86_64_JUMPADDR(ncode_buf + ncode_buf_alloc_idx, arg1);
            ncode_buf_alloc_idx += JUMPADDR_x86_64_size;
        }
        else if(opc == LABEL) {
            IntRefVector_append(label_map, IntRef_init(ncode_buf_alloc_idx));
        }
        else if(opc == HALT) {
            write_x86_64_HALT(ncode_buf + ncode_buf_alloc_idx);
            ncode_buf_alloc_idx += HALT_x86_64_size;
        }
    }

    return NCodeLinkable_init(ncode_buf, ncode_buf_size, link_targets,
            label_map);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
callq 0x1352e
imull $0x16, %eax, %edi
addl $0x2e, %edi
movl %edi, -0x3c(%rbp)
callq 0x62ec
movq %rax, %r14
callq 0x1643b
movq %rax, -0x38(%rbp)
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x7503
movq %rax, -0x48(%rbp)
movq %r14, -0x30(%rbp)
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1609a
pushq $0x2e
popq %r15
movq %rbx, %rdi
callq 0x1352e
cmpl %eax, %r13d
jge 0x174fc
movq %rbx, %rdi
movl %r13d, %esi
callq 0x137b2
movl (%rax), %ecx
cmpq $0xb, %rcx
ja 0x174f4
movl 0x4(%rax), %r12d
movl 0x8(%rax), %edx
leaq 0x46d8(%rip), %rsi      # 0x1ba84
movslq (%rsi,%rcx,4), %rax
addq %rsi, %rax
movslq %r15d, %r14
jmpq *%rax
addq -0x30(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x161d1
jmp 0x17423
movq -0x30(%rbp), %rax
leaq (%rax,%r14), %rdi
callq 0x163bb
incl %r14d
movl %r14d, %r15d
jmp 0x174f4
addq -0x30(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x1620f
jmp 0x17423
addq -0x30(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x1639e
jmp 0x17423
addq -0x30(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x1624d
jmp 0x1746a
addq -0x30(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x161f0
addl $0x2, %r15d
jmp 0x174f4
addq -0x30(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x162bc
leal 0xb(%r15), %edi
movl %r12d, %esi
callq 0x17302
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1651d
addl $0x15, %r15d
jmp 0x174f4
addq -0x30(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x1622c
addl $0x3, %r15d
jmp 0x174f4
addq -0x30(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x1619a
addl $0x5, %r15d
jmp 0x174f4
addq -0x30(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x1626c
leal 0x2(%r15), %edi
movl %r12d, %esi
callq 0x17302
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1651d
addl $0xc, %r15d
jmp 0x174f4
movl %r15d, %edi
callq 0x6630
movq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x75e5
jmp 0x174f4
addq -0x30(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x16338
leal 0xc(%r15), %edi
movl %r12d, %esi
callq 0x17302
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x1651d
addl $0x16, %r15d
incl %r13d
jmp 0x17376
movq -0x30(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x38(%rbp), %rdx
movq -0x48(%rbp), %rcx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x16404