// File: /workspace/github_c_makefile_star2_O1/gaultier[P]lox-c/vm.c
// Function: vm_dump
Result vm_dump(Vm* vm) {
    assert(vm->frame_len > 0);
    CallFrame* const frame = &vm->frames[vm->frame_len - 1];

    while (true) {
        LOG("frame ip=%zu opcodes_len=%zu vm opcode[0]=%s frame opcode=%s\n",
            frame->ip - frame->fn->chunk.opcodes - 1,
            buf_size(frame->fn->chunk.opcodes),
            opcode_str[frame->fn->chunk.opcodes[0]], opcode_str[*frame->ip]);
        uint8_t opcode = 0;
        read_u8(vm, &opcode);

        const Location* const loc = get_location(vm);

        switch (opcode) {
            // 0 operand
            case OP_RETURN:
            case OP_NEGATE:
            case OP_ADD:
            case OP_SUBTRACT:
            case OP_MULTIPLY:
            case OP_DIVIDE:
            case OP_NIL:
            case OP_TRUE:
            case OP_FALSE:
            case OP_NOT:
            case OP_EQUAL:
            case OP_LESS:
            case OP_GREATER:
            case OP_PRINT:
            case OP_POP:
                printf("%zu:%zu:%s\n", loc->line, loc->column,
                       opcode_str[opcode]);
                break;

            // 1 u8 operand: index in constants
            case OP_CONSTANT:
            case OP_DEFINE_GLOBAL:
            case OP_GET_GLOBAL:
            case OP_SET_GLOBAL: {
                Value v = {0};
                RETURN_IF_ERR(read_constant(vm, &v));
                printf("%zu:%zu:%s:%s\n", loc->line, loc->column,
                       opcode_str[opcode], value_to_str_debug(v));
                break;
            }

            // 1 u8 operand: index in stack
            case OP_GET_LOCAL:
            case OP_SET_LOCAL: {
                uint8_t stack_i = 0;
                RETURN_IF_ERR(read_u8(vm, &stack_i));
                printf("%zu:%zu:%s stack[%d]\n", loc->line, loc->column,
                       opcode_str[opcode], stack_i);
                break;
            }
            // 1 u8 operand: number
            case OP_CALL: {
                uint8_t count = 0;
                RETURN_IF_ERR(read_u8(vm, &count));
                printf("%zu:%zu:%s count:%d\n", loc->line, loc->column,
                       opcode_str[opcode], count);
                break;
            }

            // 1  u16 operand
            case OP_JUMP_IF_FALSE:
            case OP_JUMP: {
                uint16_t offset = 0;
                RETURN_IF_ERR(read_u16(vm, &offset));
                // TODO: bound check
                frame->ip += offset;

                printf("%zu:%zu:%s offset=%hu target=%s\n", loc->line,
                       loc->column, opcode_str[opcode], offset,
                       opcode_str[*frame->ip]);
                break;
            }
            case OP_LOOP: {
                uint16_t offset = 0;
                RETURN_IF_ERR(read_u16(vm, &offset));
                frame->ip -= offset;

                printf("%zu:%zu:%s offset=%d target=%s\n", loc->line,
                       loc->column, opcode_str[opcode], offset,
                       opcode_str[*frame->ip]);
                break;
            }

            default:
                assert(false);
        }
    }
    return RES_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movsbq 0x40618(%rdi), %rax
testq %rax, %rax
jle 0x4eb4
movq %rdi, %rbx
decl %eax
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rax
addq $0x40018, %rax          # imm = 0x40018
movq %rax, 0x20(%rsp)
leaq 0xf(%rsp), %r12
leaq 0x30d1(%rip), %r15      # 0x7d90
leaq 0x319a(%rip), %r14      # 0x7e60
movb $0x0, 0xf(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4ef2
movq %rbx, %rdi
callq 0x4f87
movzbl 0xf(%rsp), %ebp
cmpq $0x18, %rbp
ja 0x4ed3
movq %rax, %r13
movslq (%r15,%rbp,4), %rax
addq %r15, %rax
jmpq *%rax
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movl %ebp, %eax
shll $0x4, %eax
addq %r14, %rbp
addq %rax, %rbp
leaq 0x4279(%rip), %rdi      # 0x8f8c
movq %rbp, %rcx
xorl %eax, %eax
callq 0x10b0
jmp 0x4cc6
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4fe1
movq %r12, %r15
movq (%r13), %r12
movq 0x8(%r13), %r13
movl %ebp, %eax
shll $0x4, %eax
addq %r14, %rbp
addq %rax, %rbp
movl 0x10(%rsp), %edi
movq 0x18(%rsp), %rsi
callq 0x4b65
leaq 0x4239(%rip), %rdi      # 0x8f98
movq %r12, %rsi
movq %r15, %r12
leaq 0x3024(%rip), %r15      # 0x7d90
movq %r13, %rdx
movq %rbp, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x10b0
jmp 0x4cc6
movw $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x5089
movzwl 0x10(%rsp), %r8d
movq 0x20(%rsp), %rdx
movq 0x8(%rdx), %rax
leaq (%rax,%r8), %rcx
movq %rcx, 0x8(%rdx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movl %ebp, %ecx
shll $0x4, %ecx
addq %r14, %rbp
addq %rcx, %rbp
movzbl (%rax,%r8), %r9d
movl %r9d, %eax
shll $0x4, %eax
addq %r14, %r9
addq %rax, %r9
leaq 0x41fb(%rip), %rdi      # 0x8fd2
jmp 0x4ea5
movb $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4ef2
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movl %ebp, %eax
shll $0x4, %eax
addq %r14, %rbp
addq %rax, %rbp
movzbl 0x10(%rsp), %r8d
leaq 0x4199(%rip), %rdi      # 0x8fa7
jmp 0x4e42
movb $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4ef2
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movl %ebp, %eax
shll $0x4, %eax
addq %r14, %rbp
addq %rax, %rbp
movzbl 0x10(%rsp), %r8d
leaq 0x417b(%rip), %rdi      # 0x8fbd
movq %rbp, %rcx
xorl %eax, %eax
callq 0x10b0
jmp 0x4cc6
movw $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x5089
movzwl 0x10(%rsp), %r8d
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rax
subq %r8, %rax
movq %rax, 0x8(%rcx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movl %ebp, %ecx
shll $0x4, %ecx
addq %r14, %rbp
addq %rcx, %rbp
movzbl (%rax), %r9d
movl %r9d, %eax
shll $0x4, %eax
addq %r14, %r9
addq %rax, %r9
leaq 0x414e(%rip), %rdi      # 0x8ff3
movq %rbp, %rcx
xorl %eax, %eax
callq 0x10b0
jmp 0x4cc6
leaq 0x40a5(%rip), %rdi      # 0x8f60
leaq 0x40b0(%rip), %rsi      # 0x8f72
leaq 0x40ae(%rip), %rcx      # 0x8f77
movl $0xb4, %edx
callq 0x10f0
leaq 0x2e19(%rip), %rdi      # 0x7cf3
leaq 0x4091(%rip), %rsi      # 0x8f72
leaq 0x408f(%rip), %rcx      # 0x8f77
movl $0x10d, %edx            # imm = 0x10D
callq 0x10f0