// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x40668, %rsp          # imm = 0x40668
movl $0x2, %edi
leaq 0x1dd(%rip), %rsi       # 0xa7c0
callq 0x1130
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
movl $0x40620, %edx          # imm = 0x40620
callq 0x10f0
movl $0xff, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x1300
movq %rax, 0x40058(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x2d1e(%rip), %rsi      # 0xd339
leaq -0x7d2(%rip), %rdx      # 0x9e50
callq 0x9cb0
leaq 0x48(%rsp), %rdi
leaq 0x2d0c(%rip), %rsi      # 0xd33f
leaq -0x6da(%rip), %rdx      # 0x9f60
callq 0x9cb0
leaq 0x48(%rsp), %rdi
leaq 0x2cfd(%rip), %rsi      # 0xd348
leaq -0x512(%rip), %rdx      # 0xa140
callq 0x9cb0
movq 0x596a(%rip), %rax      # 0xffc8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %ecx
movl $0x2, %edx
movq %rcx, %rsi
callq 0x11a0
leaq 0x48(%rsp), %rax
movq %rax, 0x40048(%rsp)
movb $0x0, 0x40660(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq $0xff, 0x30(%rsp)
leaq 0x2cab(%rip), %rdi      # 0xd354
movb $0x0, %al
callq 0x10a0
movq 0x5919(%rip), %rax      # 0xffd0
movq (%rax), %rdx
leaq 0x40(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x11c0
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
jg 0xa72e
callq 0x1050
cmpl $0x0, (%rax)
je 0xa727
movq 0x590b(%rip), %rax      # 0xfff0
movq (%rax), %rax
movq %rax, (%rsp)
callq 0x1050
movl (%rax), %edi
callq 0x11e0
movq %rax, 0x8(%rsp)
callq 0x1050
movq (%rsp), %rdi
movq 0x8(%rsp), %rdx
movl (%rax), %ecx
leaq 0x2c43(%rip), %rsi      # 0xd357
movb $0x0, %al
callq 0x1140
callq 0x1050
movl (%rax), %edi
callq 0x11d0
xorl %edi, %edi
callq 0x11d0
movl $0x0, 0x2c(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x48(%rsp), %rcx
callq 0x2cd0
movl %eax, 0x2c(%rsp)
cmpl $0x0, %eax
je 0xa763
jmp 0xa7a7
jmp 0xa765
jmp 0xa767
movl $0x3, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x10(%rsp), %esi
movq 0x18(%rsp), %rdx
leaq 0x48(%rsp), %rdi
callq 0x9560
movq 0x20(%rsp), %rsi
leaq 0x48(%rsp), %rdi
xorl %edx, %edx
callq 0xa420
leaq 0x48(%rsp), %rdi
callq 0x7430
cmpq $0x0, 0x20(%rsp)
je 0xa7b9
movq 0x20(%rsp), %rdi
callq 0x1030
jmp 0xa672
nop