// File: /workspace/github_c_makefile_star2_O3/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 0x4b8c
movq %rdi, %rbx
decl %eax
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rax
addq $0x40020, %rax          # imm = 0x40020
movq %rax, 0x20(%rsp)
leaq 0xf(%rsp), %r12
leaq 0x341d(%rip), %r15      # 0x7dc0
leaq 0x34e6(%rip), %r14      # 0x7e90
movb $0x0, 0xf(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4bca
movq %rbx, %rdi
callq 0x4c5f
movzbl 0xf(%rsp), %ebp
cmpq $0x18, %rbp
ja 0x4bab
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 0x45c5(%rip), %rdi      # 0x8fbc
movq %rbp, %rcx
xorl %eax, %eax
callq 0x10b0
jmp 0x49aa
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4cb9
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 0x4850
leaq 0x4585(%rip), %rdi      # 0x8fc8
movq %r12, %rsi
movq %r15, %r12
leaq 0x3370(%rip), %r15      # 0x7dc0
movq %r13, %rdx
movq %rbp, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x10b0
jmp 0x49aa
movw $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4d56
movzwl 0x10(%rsp), %r8d
movq 0x20(%rsp), %rdx
movq (%rdx), %rax
leaq (%rax,%r8), %rcx
movq %rcx, (%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 0x4549(%rip), %rdi      # 0x9002
movq %rbp, %rcx
jmp 0x4b80
movb $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4bca
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movl %ebp, %eax
shll $0x4, %eax
addq %r14, %rbp
addq %rax, %rbp
movzbl 0x10(%rsp), %r8d
leaq 0x44e4(%rip), %rdi      # 0x8fd7
movq %rbp, %rcx
jmp 0x4b26
movb $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4bca
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movzbl 0x10(%rsp), %r8d
leaq 0x44ce(%rip), %rdi      # 0x8fed
leaq 0x3502(%rip), %rcx      # 0x8028
xorl %eax, %eax
callq 0x10b0
jmp 0x49aa
movw $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4d56
movzwl 0x10(%rsp), %r8d
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
subq %r8, %rax
movq %rax, (%rcx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movzbl (%rax), %r9d
movl %r9d, %eax
shll $0x4, %eax
addq %r14, %r9
addq %rax, %r9
leaq 0x44aa(%rip), %rdi      # 0x9023
leaq 0x3497(%rip), %rcx      # 0x8017
xorl %eax, %eax
callq 0x10b0
jmp 0x49aa
leaq 0x43fd(%rip), %rdi      # 0x8f90
leaq 0x4408(%rip), %rsi      # 0x8fa2
leaq 0x4406(%rip), %rcx      # 0x8fa7
movl $0xb4, %edx
callq 0x10f0
leaq 0x3171(%rip), %rdi      # 0x7d23
leaq 0x43e9(%rip), %rsi      # 0x8fa2
leaq 0x43e7(%rip), %rcx      # 0x8fa7
movl $0x10d, %edx            # imm = 0x10D
callq 0x10f0