// File: /workspace/github_c_makefile_star2_O2/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 0x35737(%rip), %rax     # 0x6ea06
leaq 0x31e40(%rip), %rcx     # 0x6b116
cmoveq %rax, %rcx
movl (%rsi,%r13), %eax
leaq 0x355eb(%rip), %rdx     # 0x6e8d0
movslq (%rdx,%rax,4), %r8
addq %rdx, %r8
leaq 0x2d072(%rip), %rdx     # 0x66365
leaq 0x20(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x3170
movslq %ebx, %rsi
imulq $0x18, %r14, %rax
movq %rbp, 0x10(%rsp)
movl 0x10(%rbp,%rax), %r8d
leaq 0x356f1(%rip), %rdx     # 0x6ea0d
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 0x39374
movslq %ebp, %r12
addq %r15, %r12
movl %ebx, %r13d
subl %ebp, %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
callq 0x118c5
leaq 0x32994(%rip), %rdx     # 0x6bcf6
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 0x393c9
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 0x15180
leaq 0x32942(%rip), %rdx     # 0x6bcf6
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 0x3941a
cmpq $0x0, 0x10(%rbx)
je 0x393de
movl (%rbx), %eax
cmpl $0x1b, %eax
jne 0x3941c
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 0x1601e
leaq 0x328f1(%rip), %rdx     # 0x6bcf6
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rcx
xorl %eax, %eax
callq 0x3170
movl %eax, %ebp
addl %r14d, %ebp
movl (%rbx), %eax
movq 0x8(%rsp), %r12
decl %eax
cmpl $0x25, %eax
ja 0x39542
leaq 0x33a01(%rip), %rcx     # 0x6ce34
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
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 0x3580c(%rip), %rdx     # 0x6ec65
jmp 0x39539
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 0x354f9(%rip), %rdx     # 0x6e974
movslq (%rdx,%rax,4), %rcx
addq %rdx, %rcx
jmp 0x394d1
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 0x35587(%rip), %rdx     # 0x6ea2b
xorl %eax, %eax
callq 0x3170
jmp 0x39540
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 0x41733(%rip), %rcx     # 0x7ac00
movq (%rcx,%rax,8), %rcx
leaq 0x3281e(%rip), %rdx     # 0x6bcf6
jmp 0x39539
movq 0x18(%rbx), %r8
imulq $0x18, %r8, %rax
movq 0x10(%rsp), %rcx
movq 0x8(%rcx,%rax), %rcx
movslq %ebp, %rdi
addq %r12, %rdi
movl %r15d, %esi
subl %ebp, %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
leaq 0x3551b(%rip), %rdx     # 0x6ea20
xorl %eax, %eax
callq 0x3170
jmp 0x39540
movl 0xc(%rbx), %eax
cmpl $0x10, %eax
je 0x3951c
cmpl $0xd, 0x4(%rbx)
jne 0x39587
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 0x35724(%rip), %rdx     # 0x6ec5d
xorl %eax, %eax
callq 0x3170
addl %eax, %ebp
cmpl $0xd, 0x4(%rbx)
jne 0x39573
movslq %ebp, %r14
addq %r14, %r12
subl %r14d, %r15d
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
leaq 0x354d1(%rip), %rdx     # 0x6ea32
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
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 0x3944e
movsd 0x18(%rbx), %xmm0
leaq 0x3546d(%rip), %rdx     # 0x6ea1b
movb $0x1, %al
callq 0x3170
jmp 0x39540