// File: /workspace/github_c_makefile_star2_O1/vtereshkov[P]umka-lang/src/umka_vm.c
// Function: vmAsm
int vmAsm(int ip, const Instruction *code, const DebugInfo *debugPerInstr, char *buf, int size)
{
    const Instruction *instr = &code[ip];
    const DebugInfo *debug = &debugPerInstr[ip];

    char opcodeBuf[DEFAULT_STR_LEN + 1];
    snprintf(opcodeBuf, DEFAULT_STR_LEN + 1, "%s%s", instr->inlineOpcode == OP_SWAP ? "SWAP; " : "", opcodeSpelling[instr->opcode]);
    int chars = snprintf(buf, size, "%09d %6d %28s", ip, debug->line, opcodeBuf);

    if (instr->tokKind != TOK_NONE)
        chars += snprintf(buf + chars, nonneg(size - chars), " %s", lexSpelling(instr->tokKind));

    if (instr->type)
    {
        char typeBuf[DEFAULT_STR_LEN + 1];
        chars += snprintf(buf + chars, nonneg(size - chars), " %s", typeSpelling(instr->type, typeBuf));
    }

    if (instr->typeKind != TYPE_NONE && (!instr->type || instr->opcode == OP_ASSERT_RANGE))
        chars += snprintf(buf + chars, nonneg(size - chars), " %s", typeKindSpelling(instr->typeKind));

    switch (instr->opcode)
    {
        case OP_PUSH:
        {
            if (instr->typeKind == TYPE_PTR || instr->inlineOpcode == OP_DEREF)
                chars += snprintf(buf + chars, nonneg(size - chars), " %p", instr->operand.ptrVal);
            else if (instr->typeKind == TYPE_REAL)
                chars += snprintf(buf + chars, nonneg(size - chars), " %lg", instr->operand.realVal);
            else
                chars += snprintf(buf + chars, nonneg(size - chars), " %lld", (long long int)instr->operand.intVal);
            break;
        }
        case OP_PUSH_REG:
        case OP_POP_REG:                chars += snprintf(buf + chars, nonneg(size - chars), " %s",  regSpelling[instr->operand.intVal]); break;
        case OP_PUSH_ZERO:
        case OP_PUSH_LOCAL_PTR:
        case OP_PUSH_LOCAL:
        case OP_POP:
        case OP_ZERO:
        case OP_ASSIGN:
        case OP_ASSIGN_PARAM:
        case OP_CHANGE_REF_CNT_LOCAL:
        case OP_GET_FIELD_PTR:
        case OP_GOTO:
        case OP_GOTO_IF:
        case OP_GOTO_IF_NOT:
        case OP_CALL_INDIRECT:
        case OP_RETURN:                 chars += snprintf(buf + chars, nonneg(size - chars), " %lld",  (long long int)instr->operand.intVal); break;
        case OP_CALL:
        {
            const char *fnName = debugPerInstr[instr->operand.intVal].fnName;
            chars += snprintf(buf + chars, nonneg(size - chars), " %s (%lld)", fnName, (long long int)instr->operand.intVal);
            break;
        }
        case OP_PUSH_LOCAL_PTR_ZERO:
        case OP_GET_ARRAY_PTR:          chars += snprintf(buf + chars, nonneg(size - chars), " %d %d", (int)instr->operand.int32Val[0], (int)instr->operand.int32Val[1]); break;
        case OP_CHANGE_REF_CNT_GLOBAL:
        case OP_ENTER_FRAME:
        case OP_CALL_EXTERN:            chars += snprintf(buf + chars, nonneg(size - chars), " %p",    instr->operand.ptrVal); break;
        case OP_CALL_BUILTIN:           chars += snprintf(buf + chars, nonneg(size - chars), " %s",    builtinSpelling[instr->operand.builtinVal]); break;

        default: break;
    }

    if (instr->inlineOpcode == OP_DEREF)
        chars += snprintf(buf + chars, nonneg(size - chars), "; DEREF");

    return chars;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %r8d, %ebx
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r12
movslq %edi, %r14
movq %r14, %r13
shlq $0x5, %r13
leaq (%rsi,%r13), %rax
movq %rax, 0x18(%rsp)
cmpl $0xb, 0x4(%rsi,%r13)
leaq 0x3735f(%rip), %rax     # 0x7c09e
leaq 0x33430(%rip), %rcx     # 0x78176
cmoveq %rax, %rcx
movl (%rsi,%r13), %eax
leaq 0x37213(%rip), %rdx     # 0x7bf68
movslq (%rdx,%rax,4), %r8
addq %rdx, %r8
leaq 0x2e602(%rip), %rdx     # 0x73365
leaq 0x20(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x3170
movslq %ebx, %rsi
leaq (%r14,%r14,2), %rax
movq %rbp, 0x10(%rsp)
movl 0x10(%rbp,%rax,8), %r8d
leaq 0x37319(%rip), %rdx     # 0x7c0a5
movq %r15, %rdi
movl %r14d, %ecx
leaq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0x3170
movl %eax, %ebp
movl 0x8(%r12,%r13), %edi
testl %edi, %edi
movq %r15, 0x8(%rsp)
je 0x44de4
movslq %ebp, %r12
addq %r15, %r12
movl %ebx, %r13d
subl %ebp, %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
callq 0x1b411
leaq 0x33f44(%rip), %rdx     # 0x78d16
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rcx
xorl %eax, %eax
callq 0x3170
addl %eax, %ebp
movq %rbx, %r15
movq 0x18(%rsp), %rbx
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x44e39
movslq %ebp, %r14
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %r12
movl %r15d, %r13d
subl %r14d, %r13d
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
leaq 0x120(%rsp), %rsi
callq 0x1ee46
leaq 0x33ef2(%rip), %rdx     # 0x78d16
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rcx
xorl %eax, %eax
callq 0x3170
movl %eax, %ebp
addl %r14d, %ebp
movl 0xc(%rbx), %edi
testl %edi, %edi
je 0x44e88
cmpq $0x0, 0x10(%rbx)
je 0x44e4c
cmpl $0x1b, (%rbx)
jne 0x44e88
movslq %ebp, %r14
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %r12
movl %r15d, %r13d
subl %r14d, %r13d
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
callq 0x1fe46
leaq 0x33ea3(%rip), %rdx     # 0x78d16
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rcx
xorl %eax, %eax
callq 0x3170
movl %eax, %ebp
addl %r14d, %ebp
movl (%rbx), %eax
decl %eax
cmpl $0x25, %eax
ja 0x44ff5
leaq 0x35298(%rip), %rcx     # 0x7a134
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq 0x8(%rsp), %r12
jmpq *%rax
movslq %ebp, %rdi
addq %r12, %rdi
movl %r15d, %esi
subl %ebp, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
movq 0x18(%rbx), %rcx
leaq 0x37436(%rip), %rdx     # 0x7c2fd
jmp 0x44fa7
movslq %ebp, %rdi
addq %r12, %rdi
movl %r15d, %esi
subl %ebp, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
movq 0x18(%rbx), %rax
leaq 0x37123(%rip), %rdx     # 0x7c00c
movslq (%rdx,%rax,4), %rcx
addq %rdx, %rcx
jmp 0x44f3f
movslq %ebp, %rdi
addq %r12, %rdi
movl %r15d, %esi
subl %ebp, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
movl 0x18(%rbx), %ecx
movl 0x1c(%rbx), %r8d
leaq 0x371b1(%rip), %rdx     # 0x7c0c3
xorl %eax, %eax
callq 0x3170
jmp 0x44fae
movslq %ebp, %rax
movl %r15d, %esi
subl %eax, %esi
xorl %ecx, %ecx
testl %esi, %esi
cmovlel %ecx, %esi
leaq (%r12,%rax), %rdi
movl 0x18(%rbx), %eax
leaq 0x49cc5(%rip), %rcx     # 0x8ec00
movq (%rcx,%rax,8), %rcx
leaq 0x33dd0(%rip), %rdx     # 0x78d16
jmp 0x44fa7
movq 0x18(%rbx), %r8
leaq (%r8,%r8,2), %rax
movq 0x10(%rsp), %rcx
movq 0x8(%rcx,%rax,8), %rcx
movslq %ebp, %rdi
addq %r12, %rdi
movl %r15d, %esi
subl %ebp, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
leaq 0x37145(%rip), %rdx     # 0x7c0b8
xorl %eax, %eax
callq 0x3170
jmp 0x44fae
movl 0xc(%rbx), %eax
cmpl $0x10, %eax
je 0x44f8a
cmpl $0xd, 0x4(%rbx)
jne 0x44ffc
movslq %ebp, %rdi
addq %r12, %rdi
movl %r15d, %esi
subl %ebp, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
movq 0x18(%rbx), %rcx
leaq 0x3734e(%rip), %rdx     # 0x7c2f5
xorl %eax, %eax
callq 0x3170
addl %eax, %ebp
cmpl $0xd, 0x4(%rbx)
jne 0x44fe1
movslq %ebp, %r14
addq %r14, %r12
subl %r14d, %r15d
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
leaq 0x370fb(%rip), %rdx     # 0x7c0ca
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x3170
movl %eax, %ebp
addl %r14d, %ebp
movl %ebp, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r12
jmp 0x44fb0
movslq %ebp, %rdi
addq %r12, %rdi
movl %r15d, %ecx
subl %ebp, %ecx
xorl %esi, %esi
testl %ecx, %ecx
cmovgl %ecx, %esi
cmpl $0xf, %eax
jne 0x44ebc
movsd 0x18(%rbx), %xmm0
leaq 0x37090(%rip), %rdx     # 0x7c0b3
movb $0x1, %al
callq 0x3170
jmp 0x44fae