// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq 0x78(%rsp), %rax
movsbl 0x40618(%rax), %eax
cmpl $0x0, %eax
jle 0x6ccf
jmp 0x6cee
leaq 0x640a(%rip), %rdi      # 0xd0e0
leaq 0x6415(%rip), %rsi      # 0xd0f2
movl $0xb4, %edx
leaq 0x640e(%rip), %rcx      # 0xd0f7
callq 0x10e0
movq 0x78(%rsp), %rax
addq $0x40018, %rax          # imm = 0x40018
movq 0x78(%rsp), %rcx
movsbl 0x40618(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x70(%rsp)
jmp 0x6d19
jmp 0x6d1b
movb $0x0, 0x6f(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x6f(%rsp), %rsi
callq 0x70a0
movq 0x78(%rsp), %rdi
callq 0x71c0
movq %rax, 0x60(%rsp)
movzbl 0x6f(%rsp), %eax
movq %rax, 0x20(%rsp)
subq $0x18, %rax
ja 0x7065
movq 0x20(%rsp), %rax
leaq 0x51b2(%rip), %rcx      # 0xbf10
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x60(%rsp), %rax
movq (%rax), %rsi
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rdx
movzbl 0x6f(%rsp), %eax
leaq 0x525c(%rip), %rcx      # 0xbfe0
imulq $0x11, %rax, %rax
addq %rax, %rcx
leaq 0x637a(%rip), %rdi      # 0xd10c
movb $0x0, %al
callq 0x10a0
jmp 0x7084
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10f0
movq 0x78(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x7260
movl %eax, 0x4c(%rsp)
cmpl $0x0, 0x4c(%rsp)
je 0x6dd9
movl 0x4c(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x7089
jmp 0x6ddb
movq 0x60(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x10(%rsp)
movzbl 0x6f(%rsp), %eax
movl %eax, %ecx
leaq 0x51dc(%rip), %rax      # 0xbfe0
imulq $0x11, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movl 0x50(%rsp), %edi
movq 0x58(%rsp), %rsi
callq 0x6ad0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %rax, %r8
leaq 0x62e1(%rip), %rdi      # 0xd118
movb $0x0, %al
callq 0x10a0
jmp 0x7084
movb $0x0, 0x4b(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x4b(%rsp), %rsi
callq 0x70a0
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x44(%rsp)
je 0x6e72
movl 0x44(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x7089
jmp 0x6e74
movq 0x60(%rsp), %rax
movq (%rax), %rsi
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rdx
movzbl 0x6f(%rsp), %eax
leaq 0x514f(%rip), %rcx      # 0xbfe0
imulq $0x11, %rax, %rax
addq %rax, %rcx
movzbl 0x4b(%rsp), %r8d
leaq 0x6282(%rip), %rdi      # 0xd127
movb $0x0, %al
callq 0x10a0
jmp 0x7084
movb $0x0, 0x43(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x43(%rsp), %rsi
callq 0x70a0
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
je 0x6ee0
movl 0x3c(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x7089
jmp 0x6ee2
movq 0x60(%rsp), %rax
movq (%rax), %rsi
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rdx
movzbl 0x6f(%rsp), %eax
leaq 0x50e1(%rip), %rcx      # 0xbfe0
imulq $0x11, %rax, %rax
addq %rax, %rcx
movzbl 0x43(%rsp), %r8d
leaq 0x622a(%rip), %rdi      # 0xd13d
movb $0x0, %al
callq 0x10a0
jmp 0x7084
movw $0x0, 0x3a(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x3a(%rsp), %rsi
callq 0x73a0
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x6f50
movl 0x34(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x7089
jmp 0x6f52
movzwl 0x3a(%rsp), %edx
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rsp), %rax
movq (%rax), %rsi
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rdx
movzbl 0x6f(%rsp), %eax
leaq 0x5059(%rip), %rcx      # 0xbfe0
imulq $0x11, %rax, %rax
addq %rax, %rcx
movzwl 0x3a(%rsp), %r8d
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
leaq 0x5039(%rip), %r9       # 0xbfe0
imulq $0x11, %rax, %rax
addq %rax, %r9
leaq 0x619d(%rip), %rdi      # 0xd152
movb $0x0, %al
callq 0x10a0
jmp 0x7084
movw $0x0, 0x32(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x32(%rsp), %rsi
callq 0x73a0
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0x6ff2
movl 0x2c(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x7089
jmp 0x6ff4
movzwl 0x32(%rsp), %edx
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rcx
movslq %edx, %rsi
xorl %edx, %edx
subq %rsi, %rdx
addq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rsp), %rax
movq (%rax), %rsi
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rdx
movzbl 0x6f(%rsp), %eax
leaq 0x4fb2(%rip), %rcx      # 0xbfe0
imulq $0x11, %rax, %rax
addq %rax, %rcx
movzwl 0x32(%rsp), %r8d
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
leaq 0x4f92(%rip), %r9       # 0xbfe0
imulq $0x11, %rax, %rax
addq %rax, %r9
leaq 0x6117(%rip), %rdi      # 0xd173
movb $0x0, %al
callq 0x10a0
jmp 0x7084
leaq 0x4e2f(%rip), %rdi      # 0xbe9b
leaq 0x607f(%rip), %rsi      # 0xd0f2
movl $0x10d, %edx            # imm = 0x10D
leaq 0x6078(%rip), %rcx      # 0xd0f7
callq 0x10e0
jmp 0x6d17
movl 0x84(%rsp), %eax
addq $0x88, %rsp
retq
nopl (%rax,%rax)