// File: /workspace/github_c_makefile_star2_O2/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 0x170(%rip), %rsi       # 0x557d
pushq $0x2
popq %rbp
movl %ebp, %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 0x130c
movq %rax, 0x40010(%rbx)
leaq 0x2d64(%rip), %rsi      # 0x81a9
leaq -0x4d0(%rip), %rdx      # 0x4f7c
movq %rbx, %rdi
callq 0x4ec9
leaq 0x2d54(%rip), %rsi      # 0x81af
leaq -0x440(%rip), %rdx      # 0x5022
movq %rbx, %rdi
callq 0x4ec9
leaq 0x2d47(%rip), %rsi      # 0x81b8
leaq -0x32d(%rip), %rdx      # 0x514b
movq %rbx, %rdi
callq 0x4ec9
movq 0x6b41(%rip), %rax      # 0xbfc8
movq (%rax), %rdi
xorl %esi, %esi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x11b0
leaq 0x2d28(%rip), %r14      # 0x81c4
movq 0x6b2d(%rip), %rbp      # 0xbfd0
leaq 0x8(%rsp), %r15
leaq 0x10(%rsp), %r12
movq %rsp, %r13
movq %rbx, 0x40018(%rsp)
movb $0x0, 0x40630(%rsp)
andq $0x0, 0x8(%rsp)
movq $0xff, 0x10(%rsp)
movq %r14, %rdi
xorl %eax, %eax
callq 0x10b0
movq (%rbp), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x11d0
testq %rax, %rax
jle 0x553c
andq $0x0, (%rsp)
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x2060
testl %eax, %eax
jne 0x552e
movq (%rsp), %rdx
movq %rbx, %rdi
pushq $0x3
popq %rsi
callq 0x4ca7
movq (%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x52fd
movq %rbx, %rdi
callq 0x40c7
movq (%rsp), %rdi
callq 0x1030
jmp 0x54b0
callq 0x1050
movl (%rax), %edi
testl %edi, %edi
jne 0x554e
xorl %edi, %edi
callq 0x11e0
movq %rax, %rbx
movq 0x6a98(%rip), %rax      # 0xbff0
movq (%rax), %r14
callq 0x11f0
movl (%rbx), %ecx
leaq 0x2c5e(%rip), %rsi      # 0x81c7
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movl (%rbx), %edi
callq 0x11e0