// File: /workspace/github_c_makefile_star2_O2/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, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, %r13
movzbl (%rdi), %r14d
movl %r14d, %edi
callq 0x2a18
movq %rax, %rbx
movzbl 0x1(%r13), %edi
callq 0x29fc
movq %rax, %r12
movzbl 0x2(%r13), %edi
callq 0x29fc
movq %rax, %rbp
movzbl 0x3(%r13), %edi
callq 0x29fc
movq %rax, %rcx
pushq $-0x1
popq %rax
testq %rbx, %rbx
je 0x2d5a
leal -0x10(%r14), %edx
cmpl $0x51, %edx
ja 0x2d69
leaq 0x36e2(%rip), %rsi      # 0x6280
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
movzwl 0x2(%r13), %eax
rolw $0x8, %ax
movzwl %ax, %ecx
leaq 0x38f2(%rip), %rdx      # 0x64ac
leaq 0x10(%rsp), %r14
pushq $0x8
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x2140
leaq 0x38dc(%rip), %rdx      # 0x64af
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x2140
jmp 0x2d5a
testq %r12, %r12
sete %dl
testq %rbp, %rbp
sete %sil
orb %dl, %sil
testq %rcx, %rcx
sete %dl
orb %sil, %dl
jne 0x2d5a
movq %rcx, (%rsp)
leaq 0x3878(%rip), %rdx      # 0x648f
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rcx
movq %r12, %r8
movq %rbp, %r9
jmp 0x2d53
testq %r12, %r12
je 0x2d5a
movzwl 0x2(%r13), %eax
rolw $0x8, %ax
movzwl %ax, %ecx
leaq 0x3854(%rip), %rdx      # 0x649d
leaq 0x10(%rsp), %r14
pushq $0x8
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x2140
leaq 0x3840(%rip), %rdx      # 0x64a2
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rcx
movq %r12, %r8
movq %r14, %r9
jmp 0x2d53
testq %r12, %r12
je 0x2d5a
movzwl 0x2(%r13), %eax
rolw $0x8, %ax
movzwl %ax, %ecx
leaq 0x3809(%rip), %rdx      # 0x649d
leaq 0x10(%rsp), %rdi
pushq $0x8
popq %rsi
xorl %eax, %eax
callq 0x2140
cmpb $0x34, (%r13)
jne 0x2d39
leaq 0x37ed(%rip), %rdx      # 0x64a2
leaq 0x10(%rsp), %r8
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rcx
movq %r12, %r9
jmp 0x2d53
testq %r12, %r12
sete %cl
testq %rbp, %rbp
sete %dl
orb %cl, %dl
jne 0x2d5a
leaq 0x37be(%rip), %rdx      # 0x64a2
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rcx
movq %r12, %r8
movq %rbp, %r9
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2140
testq %r12, %r12
je 0x2d5a
leaq 0x3799(%rip), %rdx      # 0x64af
movq %r15, %rdi
movq 0x8(%rsp), %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 0x3762(%rip), %rdx      # 0x64a2
leaq 0x10(%rsp), %r9
movq %r15, %rdi
movq 0x8(%rsp), %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, %r14d
jne 0x2d5a
leaq 0x3721(%rip), %rdx      # 0x649a
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rcx
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2140
nopl (%rax)