// File: /workspace/github_c_makefile_star2_O1/endeav0r[P]hvm/src/instruction.c
// Function: ins_str
int ins_str (const struct _instruction * ins, char * buf, size_t buf_size)
{
    const char * opcode    = ins_opcode_str(ins->opcode);
    const char * operand_0 = ins_reg_str(ins->operand_0);
    const char * operand_1 = ins_reg_str(ins->operand_1);
    const char * operand_2 = ins_reg_str(ins->operand_2);
    char lval[8];

    if (opcode == NULL)
        return -1;

    switch (ins->opcode) {
    case OP_ADD :
    case OP_SUB :
    case OP_MUL :
    case OP_DIV :
    case OP_MOD :
    case OP_AND :
    case OP_OR  :
    case OP_XOR :
        if ((operand_0 == NULL) || (operand_1 == NULL) || (operand_2 == NULL))
            return -1;
        return snprintf(buf, buf_size, "%s %s, %s, %s",
                        opcode, operand_0, operand_1, operand_2);

    case OP_ADDLVAL :
    case OP_SUBLVAL :
    case OP_MULLVAL :
    case OP_DIVLVAL :
    case OP_MODLVAL :
    case OP_ANDLVAL :
    case OP_ORLVAL  :
    case OP_XORLVAL :
        if (operand_0 == NULL)
            return -1;
        snprintf(lval, 8, "%04x", ntohs(ins->lval));
        return snprintf(buf, buf_size, "%s %s, %s", opcode, operand_0, lval);

    case OP_MOV    :
    case OP_CMP    :
    case OP_LOADR  :
    case OP_STORR  :
    case OP_LOADBR :
    case OP_STORBR :
        if ((operand_0 == NULL) || (operand_1 == NULL))
            return -1;
        return snprintf(buf, buf_size, "%s %s, %s", opcode, operand_0, operand_1);

    case OP_JMP      :
    case OP_JE       :
    case OP_JNE      :
    case OP_JL       :
    case OP_JLE      :
    case OP_JG       :
    case OP_JGE      :
    case OP_CALL     :
    case OP_PUSHLVAL :
        snprintf(lval, 8, "%x", ntohs(ins->lval));
        return snprintf(buf, buf_size, "%s %s", opcode, lval);

    case OP_LOAD    :
    case OP_STOR    :
    case OP_LOADB   :
    case OP_STORB   :
    case OP_MOVLVAL :
    case OP_CMPLVAL :
        if (operand_0 == NULL)
            return -1;
        snprintf(lval, 8, "%04x", ntohs(ins->lval));
        if (ins->opcode == OP_STOR) 
            return snprintf(buf, buf_size, "%s %s, %s", opcode, lval, operand_0);
        else
            return snprintf(buf, buf_size, "%s %s, %s", opcode, operand_0, lval);

    case OP_CALLR :
    case OP_IN    :
    case OP_OUT   :
    case OP_PUSH  :
    case OP_POP   :
        if (operand_0 == NULL)
            return -1;
        return snprintf(buf, buf_size, "%s %s", opcode, operand_0);

    case OP_RET     :
    case OP_HLT     :
    case OP_SYSCALL :
    case OP_NOP     :
        return snprintf(buf, buf_size, "%s", opcode);
    }

    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r13
movzbl (%rdi), %ebp
movl %ebp, %edi
callq 0x2a64
movq %rax, %rbx
movzbl 0x1(%r13), %eax
leaq 0x38cd(%rip), %rcx      # 0x6478
cmpq $0xa, %rax
ja 0x2df8
leaq 0x36c4(%rip), %rdx      # 0x6280
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
xorl %edx, %edx
movq %rcx, %r12
jmp 0x2c26
leaq 0x38b1(%rip), %r12      # 0x6484
jmp 0x2c24
leaq 0x38b4(%rip), %r12      # 0x6490
jmp 0x2c24
leaq 0x3899(%rip), %r12      # 0x647e
jmp 0x2c24
leaq 0x3893(%rip), %r12      # 0x6481
jmp 0x2c24
leaq 0x38a1(%rip), %r12      # 0x6498
jmp 0x2c24
leaq 0x387b(%rip), %r12      # 0x647b
jmp 0x2c24
leaq 0x387e(%rip), %r12      # 0x6487
jmp 0x2c24
leaq 0x3878(%rip), %r12      # 0x648a
jmp 0x2c24
leaq 0x3872(%rip), %r12      # 0x648d
jmp 0x2c24
leaq 0x3870(%rip), %r12      # 0x6494
xorl %edx, %edx
movzbl 0x2(%r13), %eax
cmpq $0xa, %rax
ja 0x2e02
leaq 0x3670(%rip), %rsi      # 0x62ac
movslq (%rsi,%rax,4), %rax
addq %rsi, %rax
jmpq *%rax
xorl %esi, %esi
movq %rcx, %r9
jmp 0x2ca6
leaq 0x3831(%rip), %r9       # 0x6484
jmp 0x2ca4
leaq 0x3834(%rip), %r9       # 0x6490
jmp 0x2ca4
leaq 0x3819(%rip), %r9       # 0x647e
jmp 0x2ca4
leaq 0x3813(%rip), %r9       # 0x6481
jmp 0x2ca4
leaq 0x3821(%rip), %r9       # 0x6498
jmp 0x2ca4
leaq 0x37fb(%rip), %r9       # 0x647b
jmp 0x2ca4
leaq 0x37fe(%rip), %r9       # 0x6487
jmp 0x2ca4
leaq 0x37f8(%rip), %r9       # 0x648a
jmp 0x2ca4
leaq 0x37f2(%rip), %r9       # 0x648d
jmp 0x2ca4
leaq 0x37f0(%rip), %r9       # 0x6494
xorl %esi, %esi
movzbl 0x3(%r13), %eax
cmpq $0xa, %rax
ja 0x2e0d
leaq 0x361c(%rip), %rdi      # 0x62d8
movslq (%rdi,%rax,4), %rax
addq %rdi, %rax
jmpq *%rax
leaq 0x37af(%rip), %rcx      # 0x647b
jmp 0x2d1d
leaq 0x37af(%rip), %rcx      # 0x6484
jmp 0x2d1d
leaq 0x37b2(%rip), %rcx      # 0x6490
jmp 0x2d1d
leaq 0x3797(%rip), %rcx      # 0x647e
jmp 0x2d1d
leaq 0x3791(%rip), %rcx      # 0x6481
jmp 0x2d1d
leaq 0x379f(%rip), %rcx      # 0x6498
jmp 0x2d1d
leaq 0x3785(%rip), %rcx      # 0x6487
jmp 0x2d1d
leaq 0x377f(%rip), %rcx      # 0x648a
jmp 0x2d1d
leaq 0x3779(%rip), %rcx      # 0x648d
jmp 0x2d1d
leaq 0x3777(%rip), %rcx      # 0x6494
xorl %edi, %edi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %rbx, %rbx
je 0x2edc
leal -0x10(%rbp), %r8d
cmpl $0x51, %r8d
ja 0x2eeb
leaq 0x35c2(%rip), %r10      # 0x6304
movslq (%r10,%r8,4), %r8
addq %r10, %r8
jmpq *%r8
movzwl 0x2(%r13), %eax
rolw $0x8, %ax
movzwl %ax, %ecx
leaq 0x37d1(%rip), %rdx      # 0x6530
leaq 0x10(%rsp), %r12
movl $0x8, %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x2140
leaq 0x37b9(%rip), %rdx      # 0x6533
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2140
jmp 0x2edc
orb %dil, %sil
orb %sil, %dl
jne 0x2edc
movq %rcx, (%rsp)
leaq 0x376a(%rip), %rdx      # 0x6513
jmp 0x2ec9
testb %dl, %dl
jne 0x2edc
movzwl 0x2(%r13), %eax
rolw $0x8, %ax
movzwl %ax, %ecx
leaq 0x3758(%rip), %rdx      # 0x6521
leaq 0x10(%rsp), %r13
movl $0x8, %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0x2140
leaq 0x3742(%rip), %rdx      # 0x6526
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rcx
movq %r12, %r8
movq %r13, %r9
jmp 0x2ed5
movb $0x1, %dl
xorl %r12d, %r12d
jmp 0x2c26
movb $0x1, %sil
xorl %r9d, %r9d
jmp 0x2ca6
movb $0x1, %dil
xorl %ecx, %ecx
jmp 0x2d1f
testb %dl, %dl
jne 0x2edc
movzwl 0x2(%r13), %eax
rolw $0x8, %ax
movzwl %ax, %ecx
leaq 0x36ef(%rip), %rdx      # 0x6521
leaq 0x10(%rsp), %rdi
movl $0x8, %esi
xorl %eax, %eax
callq 0x2140
cmpb $0x34, (%r13)
jne 0x2ebd
leaq 0x36d5(%rip), %rdx      # 0x6526
leaq 0x10(%rsp), %r8
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rcx
movq %r12, %r9
jmp 0x2ed5
orb %sil, %dl
jne 0x2edc
leaq 0x36b6(%rip), %rdx      # 0x6526
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rcx
movq %r12, %r8
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2140
testb %dl, %dl
jne 0x2edc
leaq 0x3697(%rip), %rdx      # 0x6533
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rcx
movq %r12, %r8
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2140
leaq 0x3662(%rip), %rdx      # 0x6526
leaq 0x10(%rsp), %r9
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2140
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x90, %ebp
jne 0x2edc
leaq 0x3624(%rip), %rdx      # 0x651e
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rcx
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2140