// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzbl (%rax), %edi
callq 0x2d80
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %edi
callq 0x2cb0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %edi
callq 0x2cb0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %edi
callq 0x2cb0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x30fa
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3379
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
addl $-0x10, %eax
movl %eax, %ecx
movq %rcx, -0x50(%rbp)
subl $0x80, %eax
ja 0x3372
movq -0x50(%rbp), %rax
leaq 0x6250(%rip), %rcx      # 0x9370
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpq $0x0, -0x30(%rbp)
je 0x313e
cmpq $0x0, -0x38(%rbp)
je 0x313e
cmpq $0x0, -0x40(%rbp)
jne 0x314a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3379
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
movq -0x40(%rbp), %rax
leaq 0x64a6(%rip), %rdx      # 0x960f
movq %rax, (%rsp)
movb $0x0, %al
callq 0x2140
movl %eax, -0x4(%rbp)
jmp 0x3379
cmpq $0x0, -0x30(%rbp)
jne 0x318f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3379
leaq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x2240
movq -0x58(%rbp), %rdi
movzwl %ax, %ecx
movl $0x8, %esi
leaq 0x6466(%rip), %rdx      # 0x961d
movb $0x0, %al
callq 0x2140
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
leaq -0x48(%rbp), %r9
leaq 0x6449(%rip), %rdx      # 0x9622
movb $0x0, %al
callq 0x2140
movl %eax, -0x4(%rbp)
jmp 0x3379
cmpq $0x0, -0x30(%rbp)
je 0x31f6
cmpq $0x0, -0x38(%rbp)
jne 0x3202
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3379
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
leaq 0x6405(%rip), %rdx      # 0x9622
movb $0x0, %al
callq 0x2140
movl %eax, -0x4(%rbp)
jmp 0x3379
leaq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x2240
movq -0x60(%rbp), %rdi
movzwl %ax, %ecx
movl $0x8, %esi
leaq 0x63d8(%rip), %rdx      # 0x962c
movb $0x0, %al
callq 0x2140
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
leaq -0x48(%rbp), %r8
leaq 0x63bd(%rip), %rdx      # 0x962f
movb $0x0, %al
callq 0x2140
movl %eax, -0x4(%rbp)
jmp 0x3379
cmpq $0x0, -0x30(%rbp)
jne 0x3294
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3379
leaq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x2240
movq -0x68(%rbp), %rdi
movzwl %ax, %ecx
movl $0x8, %esi
leaq 0x6361(%rip), %rdx      # 0x961d
movb $0x0, %al
callq 0x2140
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x34, %eax
jne 0x32f9
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
leaq -0x48(%rbp), %r8
movq -0x30(%rbp), %r9
leaq 0x6338(%rip), %rdx      # 0x9622
movb $0x0, %al
callq 0x2140
movl %eax, -0x4(%rbp)
jmp 0x3379
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
leaq -0x48(%rbp), %r9
leaq 0x630e(%rip), %rdx      # 0x9622
movb $0x0, %al
callq 0x2140
movl %eax, -0x4(%rbp)
jmp 0x3379
cmpq $0x0, -0x30(%rbp)
jne 0x3330
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3379
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
leaq 0x62e8(%rip), %rdx      # 0x962f
movb $0x0, %al
callq 0x2140
movl %eax, -0x4(%rbp)
jmp 0x3379
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
leaq 0x62b4(%rip), %rdx      # 0x961a
movb $0x0, %al
callq 0x2140
movl %eax, -0x4(%rbp)
jmp 0x3379
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)