// File: /workspace/github_c_makefile_star2_O2/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 0x3eaa
movq %rdi, %rbx
decl %eax
imulq $0x18, %rax, %rax
addq %rdi, %rax
addq $0x40020, %rax          # imm = 0x40020
movq %rax, 0x20(%rsp)
leaq 0xf(%rsp), %r13
leaq 0x309a(%rip), %r15      # 0x6d80
leaq 0x3163(%rip), %r14      # 0x6e50
movb $0x0, 0xf(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3ee8
movq %rbx, %rdi
callq 0x3f7c
movzbl 0xf(%rsp), %r12d
cmpq $0x18, %r12
ja 0x3ec9
movq %rax, %rbp
movslq (%r15,%r12,4), %rax
addq %r15, %rax
jmpq *%rax
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
imulq $0x11, %r12, %rcx
addq %r14, %rcx
leaq 0x4245(%rip), %rdi      # 0x7f7c
xorl %eax, %eax
callq 0x10b0
jmp 0x3ced
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3fd4
movq %r13, %r15
movq (%rbp), %r13
movq 0x8(%rbp), %rbp
imulq $0x11, %r12, %r12
addq %r14, %r12
movl 0x10(%rsp), %edi
movq 0x18(%rsp), %rsi
callq 0x3b97
leaq 0x420c(%rip), %rdi      # 0x7f88
movq %r13, %rsi
movq %r15, %r13
leaq 0x2ff7(%rip), %r15      # 0x6d80
movq %rbp, %rdx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x10b0
jmp 0x3ced
andw $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4071
movzwl 0x10(%rsp), %r8d
movq 0x20(%rsp), %rdx
movq (%rdx), %rax
leaq (%rax,%r8), %rcx
movq %rcx, (%rdx)
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
imulq $0x11, %r12, %rcx
addq %r14, %rcx
movzbl (%rax,%r8), %eax
imulq $0x11, %rax, %r9
addq %r14, %r9
leaq 0x41da(%rip), %rdi      # 0x7fc2
jmp 0x3e9e
movb $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3ee8
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
imulq $0x11, %r12, %rcx
addq %r14, %rcx
movzbl 0x10(%rsp), %r8d
leaq 0x417c(%rip), %rdi      # 0x7f97
jmp 0x3e4b
movb $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3ee8
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
movzbl 0x10(%rsp), %r8d
leaq 0x4169(%rip), %rdi      # 0x7fad
leaq 0x319d(%rip), %rcx      # 0x6fe8
xorl %eax, %eax
callq 0x10b0
jmp 0x3ced
andw $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4071
movzwl 0x10(%rsp), %r8d
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
subq %r8, %rax
movq %rax, (%rcx)
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
movzbl (%rax), %eax
imulq $0x11, %rax, %r9
addq %r14, %r9
leaq 0x414c(%rip), %rdi      # 0x7fe3
leaq 0x3139(%rip), %rcx      # 0x6fd7
xorl %eax, %eax
callq 0x10b0
jmp 0x3ced
leaq 0x409f(%rip), %rdi      # 0x7f50
leaq 0x40aa(%rip), %rsi      # 0x7f62
leaq 0x40a8(%rip), %rcx      # 0x7f67
movl $0xb4, %edx
callq 0x10f0
leaq 0x2e23(%rip), %rdi      # 0x6cf3
leaq 0x408b(%rip), %rsi      # 0x7f62
leaq 0x4089(%rip), %rcx      # 0x7f67
movl $0x10d, %edx            # imm = 0x10D
callq 0x10f0