// File: /workspace/github_c_makefile_star2_O3/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 0x3983d(%rip), %rax     # 0x7b10e
leaq 0x3589e(%rip), %rcx     # 0x77176
cmoveq %rax, %rcx
movl (%rsi,%r13), %eax
leaq 0x396f1(%rip), %rdx     # 0x7afd8
movslq (%rdx,%rax,4), %r8
addq %rdx, %r8
leaq 0x30a70(%rip), %rdx     # 0x72365
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 0x397f7(%rip), %rdx     # 0x7b115
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 0x41976
movslq %ebp, %r12
addq %r15, %r12
movl %ebx, %r13d
subl %ebp, %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
callq 0x1636f
leaq 0x363b2(%rip), %rdx     # 0x77d16
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 0x419cb
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 0x19e8a
leaq 0x36360(%rip), %rdx     # 0x77d16
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 0x41a1c
cmpq $0x0, 0x10(%rbx)
je 0x419e0
movl (%rbx), %eax
cmpl $0x1b, %eax
jne 0x41a1e
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 0x1adb4
leaq 0x3630f(%rip), %rdx     # 0x77d16
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 0x41b44
leaq 0x37737(%rip), %rcx     # 0x7916c
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 0x39912(%rip), %rdx     # 0x7b36d
jmp 0x41b3b
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 0x395ff(%rip), %rdx     # 0x7b07c
movslq (%rdx,%rax,4), %rcx
addq %rdx, %rcx
jmp 0x41ad3
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 0x3968d(%rip), %rdx     # 0x7b133
xorl %eax, %eax
callq 0x3170
jmp 0x41b42
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 0x45131(%rip), %rcx     # 0x86c00
movq (%rcx,%rax,8), %rcx
leaq 0x3623c(%rip), %rdx     # 0x77d16
jmp 0x41b3b
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 0x39621(%rip), %rdx     # 0x7b128
xorl %eax, %eax
callq 0x3170
jmp 0x41b42
movl 0xc(%rbx), %eax
cmpl $0x10, %eax
je 0x41b1e
cmpl $0xd, 0x4(%rbx)
jne 0x41b89
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 0x3982a(%rip), %rdx     # 0x7b365
xorl %eax, %eax
callq 0x3170
addl %eax, %ebp
cmpl $0xd, 0x4(%rbx)
jne 0x41b75
movslq %ebp, %r14
addq %r14, %r12
subl %r14d, %r15d
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
leaq 0x395d7(%rip), %rdx     # 0x7b13a
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 0x41a50
movsd 0x18(%rbx), %xmm0
leaq 0x39573(%rip), %rdx     # 0x7b123
movb $0x1, %al
callq 0x3170
jmp 0x41b42