// File: /workspace/github_c_makefile_star2_O3/atomlang[P]atomlang/cli/repl.c
// Function: repl
int repl(PKVM* vm, const PkCompileOptions* options) {

  // Set repl_mode of the user_data.
  VmUserData* user_data = (VmUserData*)pkGetUserData(vm);
  user_data->repl_mode = true;

  // The main module that'll be used to compile and execute the input source.
  PkHandle* module = pkNewModule(vm, "$(REPL)");

  // A buffer to store lines read from stdin.
  ByteBuffer lines;
  byteBufferInit(&lines);

  // A buffer to store a line read from stdin.
  ByteBuffer line;
  byteBufferInit(&line);

  // Will be set to true if the compilation failed with unexpected EOF to add
  // more lines to the [lines] buffer.
  bool need_more_lines = false;

  bool done = false;
  do {

    // Print the input listening line.
    if (!need_more_lines) {
      printf(">>> ");
    } else {
      printf("... ");
    }

    // Read a line from stdin and add the line to the lines buffer.
    readLine(&line);
    bool is_empty = is_str_empty((const char*)line.data);

    // If the line is empty, we don't have to compile it.
    if (is_empty && !need_more_lines) {
      byteBufferClear(&line);
      ASSERT(lines.count == 0, OOPS);
      continue;
    }

    if (lines.count != 0) byteBufferWrite(&lines, '\n');
    byteBufferAddString(&lines, (const char*)line.data, line.count - 1);
    byteBufferClear(&line);
    byteBufferWrite(&lines, '\0');

    // Compile the buffer to the module.
    PkStringPtr source_ptr = { (const char*)lines.data, NULL, NULL };
    PkResult result = pkCompileModule(vm, module, source_ptr, options);

    if (result == PK_RESULT_UNEXPECTED_EOF) {
      ASSERT(lines.count > 0 && lines.data[lines.count - 1] == '\0', OOPS);
      lines.count -= 1; // Remove the null byte to append a new string.
      need_more_lines = true;
      continue;
    }

    // We're buffering the lines for unexpected EOF, if we reached here that
    // means it's either successfully compiled or compilation error. Clean the
    // buffer for the next iteration.
    need_more_lines = false;
    byteBufferClear(&lines);

    if (result != PK_RESULT_SUCCESS) continue;

    // Compiled source would be the "main" function of the module. Run it.
    PkHandle* _main = pkGetFunction(vm, module, PK_IMPLICIT_MAIN_NAME);
    PkHandle* fiber = pkNewFiber(vm, _main);
    result = pkRunFiber(vm, fiber, 0, NULL);
    pkReleaseHandle(vm, _main);
    pkReleaseHandle(vm, fiber);

  } while (!done);

  byteBufferClear(&lines);
  pkReleaseHandle(vm, module);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, 0x40(%rsp)
movq %rdi, %r14
callq 0xb4ff
movb $0x1, (%rax)
leaq 0x5b9e(%rip), %rsi      # 0x1f831
movq %r14, %rdi
callq 0x12c98
movq %rax, 0x38(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x19f08
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x19f08
xorl %r12d, %r12d
movq 0xf317(%rip), %r15      # 0x28fd8
testb $0x1, %r12b
leaq 0x5b6d(%rip), %rdi      # 0x1f839
leaq 0x5b6b(%rip), %rax      # 0x1f83e
cmovneq %rax, %rdi
xorl %eax, %eax
callq 0x2150
movq (%r15), %rdi
callq 0x21c0
cmpb $0xa, %al
je 0x19cff
movzbl %al, %esi
cmpl $0xff, %esi
je 0x19cff
movq %r13, %rdi
callq 0x19fb4
jmp 0x19cde
movq %r13, %rdi
xorl %esi, %esi
callq 0x19fb4
movq 0x48(%rsp), %rbp
testq %rbp, %rbp
je 0x19e76
movb (%rbp), %bl
testb %bl, %bl
je 0x19d3e
callq 0x23c0
movq (%rax), %rax
leaq 0x1(%rbp), %rcx
movsbq %bl, %rdx
testb $0x20, 0x1(%rax,%rdx,2)
je 0x19d48
movb (%rcx), %bl
incq %rcx
testb %bl, %bl
jne 0x19d2a
testb $0x1, %r12b
je 0x19dfa
cmpl $0x0, 0x30(%rsp)
leaq 0x28(%rsp), %rbx
je 0x19d66
movq %rbx, %rdi
movl $0xa, %esi
callq 0x19fb4
movq 0x48(%rsp), %rbp
movl 0x50(%rsp), %edx
decl %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x19fe0
movq %r13, %rdi
callq 0x19f0f
movq %rbx, %rdi
xorl %esi, %esi
callq 0x19fb4
leaq 0x60(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x28(%rsp), %rax
movq %rax, 0x58(%rsp)
movups 0x58(%rsp), %xmm0
movups 0x68(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x10ef3
cmpl $0x1, %eax
sete %r12b
jne 0x19e15
movl 0x30(%rsp), %eax
testl %eax, %eax
je 0x19ea4
movq 0x28(%rsp), %rcx
decl %eax
cmpb $0x0, (%rcx,%rax)
jne 0x19ea4
movl %eax, 0x30(%rsp)
jmp 0x19cc1
movq %r13, %rdi
callq 0x19f0f
xorl %r12d, %r12d
cmpl $0x0, 0x30(%rsp)
je 0x19cc1
jmp 0x19ed2
movl %eax, %ebp
movq %rbx, %rdi
callq 0x19f0f
testl %ebp, %ebp
jne 0x19cc1
movq %r14, %rdi
movq 0x38(%rsp), %rsi
leaq 0x2441(%rip), %rdx      # 0x1c277
callq 0x12e93
movq %rax, %rbp
movq %r14, %rdi
movq %rax, %rsi
callq 0x815c
movq %rax, %rbx
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xdb49
movq %r14, %rdi
movq %rbp, %rsi
callq 0xb581
movq %r14, %rdi
movq %rbx, %rsi
callq 0xb581
jmp 0x19cc1
movq 0xf17b(%rip), %rax      # 0x28ff8
movq (%rax), %rdi
leaq 0x2259(%rip), %rsi      # 0x1c0e0
leaq 0x2366(%rip), %rdx      # 0x1c1f4
leaq 0x59be(%rip), %rcx      # 0x1f853
leaq 0x59ac(%rip), %r8       # 0x1f848
movl $0x32, %r9d
jmp 0x19efe
movq 0xf14d(%rip), %rax      # 0x28ff8
movq (%rax), %rdi
leaq 0x222b(%rip), %rsi      # 0x1c0e0
leaq 0x2338(%rip), %rdx      # 0x1c1f4
leaq 0x5980(%rip), %rcx      # 0x1f843
leaq 0x597e(%rip), %r8       # 0x1f848
movl $0x6f, %r9d
jmp 0x19efe
movq 0xf11f(%rip), %rax      # 0x28ff8
movq (%rax), %rdi
leaq 0x21fd(%rip), %rsi      # 0x1c0e0
leaq 0x230a(%rip), %rdx      # 0x1c1f4
leaq 0x5952(%rip), %rcx      # 0x1f843
leaq 0x5950(%rip), %r8       # 0x1f848
movl $0x61, %r9d
xorl %eax, %eax
callq 0x2250
ud2
nop