// File: /workspace/github_c_makefile_star2_O2/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, %rbx
callq 0x5a8b
movb $0x1, (%rax)
leaq 0x6225(%rip), %rsi      # 0x1b830
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
callq 0xe6bc
movq %rax, 0x30(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x15874
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x15874
xorl %r12d, %r12d
movq 0xd99a(%rip), %r15      # 0x22fd8
testb $0x1, %r12b
leaq 0x61ef(%rip), %rdi      # 0x1b838
leaq 0x61ed(%rip), %rax      # 0x1b83d
cmovneq %rax, %rdi
xorl %eax, %eax
callq 0x2150
movq (%r15), %rdi
callq 0x21c0
cmpb $0xa, %al
je 0x1567c
movzbl %al, %esi
cmpl $0xff, %esi
je 0x1567c
movq %r13, %rdi
callq 0x15923
jmp 0x1565b
movq %r13, %rdi
xorl %esi, %esi
callq 0x15923
movq 0x48(%rsp), %rbp
movq %rbp, %rbx
testq %rbp, %rbp
je 0x15814
movsbq (%rbx), %r14
testq %r14, %r14
je 0x156b5
callq 0x23c0
movq (%rax), %rax
incq %rbx
testb $0x20, 0x1(%rax,%r14,2)
jne 0x15697
jmp 0x156bf
testb $0x1, %r12b
je 0x157d4
cmpl $0x0, 0x28(%rsp)
leaq 0x20(%rsp), %rbx
je 0x156db
movq %rbx, %rdi
pushq $0xa
popq %rsi
callq 0x15923
movq 0x48(%rsp), %rbp
movl 0x50(%rsp), %edx
decl %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1592b
movq %r13, %rdi
callq 0x1587b
movq %rbx, %rdi
xorl %esi, %esi
callq 0x15923
leaq 0x60(%rsp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x58(%rsp)
movups 0x58(%rsp), %xmm0
movups 0x68(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0xc60c
cmpl $0x1, %eax
sete %r12b
jne 0x1576e
movl 0x28(%rsp), %eax
testl %eax, %eax
je 0x1583e
movq 0x20(%rsp), %rcx
decl %eax
cmpb $0x0, (%rcx,%rax)
jne 0x1583e
movl %eax, 0x28(%rsp)
jmp 0x1563e
movl %eax, %ebp
movq %rbx, %rdi
callq 0x1587b
testl %ebp, %ebp
jne 0x1563e
movq 0x38(%rsp), %r14
movq %r14, %rdi
movq 0x30(%rsp), %rsi
leaq 0x2af3(%rip), %rdx      # 0x18287
callq 0xe9cb
movq %rax, %rbp
movq %r14, %rdi
movq %rax, %rsi
callq 0x282e
movq %rax, %rbx
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x9248
movq %r14, %rdi
movq %rbp, %rsi
callq 0x5ae8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5ae8
jmp 0x1563e
movq %r13, %rdi
callq 0x1587b
xorl %r12d, %r12d
cmpl $0x0, 0x28(%rsp)
je 0x1563e
movq 0xd807(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x28f5(%rip), %rsi      # 0x180f0
leaq 0x2a02(%rip), %rdx      # 0x18204
leaq 0x6039(%rip), %rcx      # 0x1b842
leaq 0x6037(%rip), %r8       # 0x1b847
pushq $0x61
jmp 0x15866
movq 0xd7dd(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x28cb(%rip), %rsi      # 0x180f0
leaq 0x29d8(%rip), %rdx      # 0x18204
leaq 0x601f(%rip), %rcx      # 0x1b852
leaq 0x600d(%rip), %r8       # 0x1b847
pushq $0x32
jmp 0x15866
movq 0xd7b3(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x28a1(%rip), %rsi      # 0x180f0
leaq 0x29ae(%rip), %rdx      # 0x18204
leaq 0x5fe5(%rip), %rcx      # 0x1b842
leaq 0x5fe3(%rip), %r8       # 0x1b847
pushq $0x6f
popq %r9
xorl %eax, %eax
callq 0x2250
ud2
nopl (%rax)