// File: /workspace/github_c_makefile_star2_O3/gaultier[P]lox-c/vm.c
// Function: vm_repl
void vm_repl(void) {
    signal(SIGINT, repl_sig_quit);

    Vm vm = {.globals = ht_init(UINT8_MAX, NULL)};
    fn_define_native(&vm, "clock", fn_native_clock);
    fn_define_native(&vm, "readLine", fn_native_read_line);
    fn_define_native(&vm, "parseNumber", fn_native_parse_number);

    // Make sure stdout in unbuffered, otherwise the repl experience is
    // suboptimal
    setvbuf(stdout, (char*)NULL, _IONBF, 0);

    while (true) {
        vm.stack_top = vm.stack;
        vm.frame_len = 0;

        char* source = NULL;
        ssize_t source_len = 0;
        size_t line_cap = UINT8_MAX;

        printf("> ");
        if ((source_len = getline(&source, &line_cap, stdin)) <= 0) {
            if (errno) {
                fprintf(stderr,
                        "Could not read from stdin: errno=%s error=%d\n",
                        strerror(errno), errno);
                exit(errno);
            } else {
                exit(0);
            }
        }

        Result result = RES_OK;

        ObjFunction* fn = NULL;
        if ((result = parser_compile(source, (size_t)source_len, &fn, &vm)) !=
            RES_OK) {
            goto cleanup;
        }

        LOG("parsing successful%s\n", "");

        stack_push(&vm, OBJ_VAL(fn));
        fn_call(&vm, fn, 0);

        vm_run_bytecode(&vm);

    cleanup:
        if (fn) {
            free(fn);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x40638, %rsp          # imm = 0x40638
leaq 0x207(%rip), %rsi       # 0x6259
movl $0x2, %edi
callq 0x1130
leaq 0x18(%rsp), %rbx
movl $0x40620, %edx          # imm = 0x40620
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1100
movl $0xff, %edi
xorl %esi, %esi
callq 0x1d8c
movq %rax, 0x40010(%rbx)
leaq 0x315f(%rip), %rsi      # 0x91e9
leaq -0x2e2(%rip), %rdx      # 0x5daf
movq %rbx, %rdi
callq 0x5c84
leaq 0x314f(%rip), %rsi      # 0x91ef
leaq -0x278(%rip), %rdx      # 0x5e2f
movq %rbx, %rdi
callq 0x5c84
leaq 0x3142(%rip), %rsi      # 0x91f8
leaq -0x1f3(%rip), %rdx      # 0x5eca
movq %rbx, %rdi
callq 0x5c84
movq 0x6efc(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
xorl %esi, %esi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x11b0
movq %rbx, 0x40000(%rbx)
movb $0x0, 0x40618(%rbx)
leaq 0x8(%rsp), %r14
movq $0x0, (%r14)
leaq 0x10(%rsp), %r15
movq $0xff, (%r15)
leaq 0x30fa(%rip), %rdi      # 0x9204
xorl %eax, %eax
callq 0x10b0
movq 0x6eb8(%rip), %rbp      # 0xcfd0
movq (%rbp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x11d0
testq %rax, %rax
jle 0x61f9
movq %rsp, %r14
leaq 0x30ca(%rip), %r15      # 0x9204
leaq 0x8(%rsp), %r12
leaq 0x10(%rsp), %r13
movq $0x0, (%rsp)
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2c64
testl %eax, %eax
jne 0x61a7
movq 0x40018(%rsp), %rax
movq %rax, %rcx
subq %rbx, %rcx
cmpq $0x3fff0, %rcx          # imm = 0x3FFF0
jae 0x623a
movq (%rsp), %rsi
movl $0x3, (%rax)
movq %rsi, 0x8(%rax)
addq $0x10, 0x40018(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5fbe
movq %rbx, %rdi
callq 0x4dac
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x61b5
callq 0x1030
movq %rbx, 0x40018(%rsp)
movb $0x0, 0x40630(%rsp)
movq $0x0, 0x8(%rsp)
movq $0xff, 0x10(%rsp)
movq %r15, %rdi
xorl %eax, %eax
callq 0x10b0
movq (%rbp), %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x11d0
testq %rax, %rax
jg 0x6144
callq 0x1050
movl (%rax), %edi
testl %edi, %edi
jne 0x620b
xorl %edi, %edi
callq 0x11e0
movq %rax, %rbx
movq 0x6ddb(%rip), %rax      # 0xcff0
movq (%rax), %r14
callq 0x11f0
movl (%rbx), %ecx
leaq 0x2fe1(%rip), %rsi      # 0x9207
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movl (%rbx), %edi
callq 0x11e0
leaq 0x3111(%rip), %rdi      # 0x9352
leaq 0x2d5a(%rip), %rsi      # 0x8fa2
leaq 0x3121(%rip), %rcx      # 0x9370
movl $0x6f, %edx
callq 0x10f0