// File: /workspace/github_c_makefile_star2_O1/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       # 0x66a5
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 0x1ddc
movq %rax, 0x40010(%rbx)
leaq 0x2ce3(%rip), %rsi      # 0x91b9
leaq -0x2e2(%rip), %rdx      # 0x61fb
movq %rbx, %rdi
callq 0x60d0
leaq 0x2cd3(%rip), %rsi      # 0x91bf
leaq -0x278(%rip), %rdx      # 0x627b
movq %rbx, %rdi
callq 0x60d0
leaq 0x2cc6(%rip), %rsi      # 0x91c8
leaq -0x1f3(%rip), %rdx      # 0x6316
movq %rbx, %rdi
callq 0x60d0
movq 0x6ab0(%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 0x2c7e(%rip), %rdi      # 0x91d4
xorl %eax, %eax
callq 0x10b0
movq 0x6a6c(%rip), %rbp      # 0xcfd0
movq (%rbp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x11d0
testq %rax, %rax
jle 0x6645
movq %rsp, %r14
leaq 0x2c4e(%rip), %r15      # 0x91d4
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 0x2d40
testl %eax, %eax
jne 0x65f3
movq 0x40018(%rsp), %rax
movq %rax, %rcx
subq %rbx, %rcx
cmpq $0x3fff0, %rcx          # imm = 0x3FFF0
jae 0x6686
movq (%rsp), %rsi
movl $0x3, (%rax)
movq %rsi, 0x8(%rax)
addq $0x10, 0x40018(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0x640a
movq %rbx, %rdi
callq 0x50df
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x6601
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 0x6590
callq 0x1050
movl (%rax), %edi
testl %edi, %edi
jne 0x6657
xorl %edi, %edi
callq 0x11e0
movq %rax, %rbx
movq 0x698f(%rip), %rax      # 0xcff0
movq (%rax), %r14
callq 0x11f0
movl (%rbx), %ecx
leaq 0x2b65(%rip), %rsi      # 0x91d7
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movl (%rbx), %edi
callq 0x11e0
leaq 0x2c95(%rip), %rdi      # 0x9322
leaq 0x28de(%rip), %rsi      # 0x8f72
leaq 0x2ca5(%rip), %rcx      # 0x9340
movl $0x6f, %edx
callq 0x10f0