// File: /workspace/github_c_makefile_star2_O3/atomlang[P]atomlang/src/pk_vm.c
// Function: vmPrepareFiber
bool vmPrepareFiber(PKVM* vm, Fiber* fiber, int argc, Var** argv) {
  ASSERT(fiber->func->arity >= -1, OOPS " (Forget to initialize arity.)");

  if (argc != fiber->func->arity) {
    char buff[STR_INT_BUFF_SIZE]; sprintf(buff, "%d", fiber->func->arity);
    _ERR_FAIL(stringFormat(vm, "Expected exactly $ argument(s).", buff));
  }

  if (fiber->state != FIBER_NEW) {
    switch (fiber->state) {
      case FIBER_NEW: UNREACHABLE();
      case FIBER_RUNNING:
        _ERR_FAIL(newString(vm, "The fiber has already been running."));
      case FIBER_YIELDED:
        _ERR_FAIL(newString(vm, "Cannot run a fiber which is yielded, use "
          "fiber_resume() instead."));
      case FIBER_DONE:
        _ERR_FAIL(newString(vm, "The fiber has done running."));
    }
    UNREACHABLE();
  }

  ASSERT(fiber->stack != NULL && fiber->sp == fiber->stack + 1, OOPS);
  ASSERT(fiber->ret + 1 == fiber->sp, OOPS);

  // Pass the function arguments.

  // Assert we have the first frame (to push the arguments). And assert we have
  // enough stack space for parameters.
  ASSERT(fiber->frame_count == 1, OOPS);
  ASSERT(fiber->frames[0].rbp == fiber->ret, OOPS);
  ASSERT((fiber->stack + fiber->stack_size) - fiber->sp >= argc, OOPS);

  // ARG1 is fiber, function arguments are ARG(2), ARG(3), ... ARG(argc).
  // And ret[0] is the return value, parameters starts at ret[1], ...
  for (int i = 0; i < argc; i++) {
    fiber->ret[1 + i] = *argv[i]; // +1: ret[0] is return value.
  }
  fiber->sp += argc; // Parameters.

  // Set the new fiber as the vm's fiber.
  fiber->caller = vm->fiber;
  vm->fiber = fiber;

  // On success return true.
  return true;
}

// Assembly:
pushq %rbx
subq $0x10, %rsp
movq 0x18(%rsi), %rax
movl 0x20(%rax), %eax
cmpl $-0x2, %eax
jle 0xdd96
movq %rdi, %rbx
cmpl %edx, %eax
jne 0xdcc8
movl 0x10(%rsi), %eax
cmpq $0x3, %rax
ja 0xdda5
leaq 0xe978(%rip), %rdi      # 0x1c598
movslq (%rdi,%rax,4), %rax
addq %rdi, %rax
jmpq *%rax
movq 0x20(%rsi), %r8
testq %r8, %r8
je 0xdda0
movq 0x30(%rsi), %rax
leaq 0x8(%r8), %rdi
cmpq %rdi, %rax
jne 0xdda0
movq 0x38(%rsi), %r9
leaq 0x8(%r9), %rdi
cmpq %rax, %rdi
jne 0xddaa
cmpl $0x1, 0x4c(%rsi)
jne 0xddaf
movq 0x40(%rsi), %r10
cmpq %r9, 0x10(%r10)
jne 0xddb4
movslq 0x28(%rsi), %r9
leaq (%r8,%r9,8), %r9
subq %rax, %r9
sarq $0x3, %r9
movslq %edx, %r8
cmpq %r8, %r9
jl 0xddb9
testl %edx, %edx
jle 0xdca7
movl %edx, %edx
xorl %r9d, %r9d
movq (%rcx,%r9,8), %r10
movq (%r10), %r10
movq %r10, (%rdi,%r9,8)
incq %r9
cmpq %r9, %rdx
jne 0xdc94
leaq (%rax,%r8,8), %rax
movq %rax, 0x30(%rsi)
movq 0x5f0(%rbx), %rax
movq %rax, 0x50(%rsi)
movq %rsi, 0x5f0(%rbx)
movb $0x1, %al
jmp 0xdd90
leaq 0x11d52(%rip), %rsi     # 0x1fa21
leaq 0x4(%rsp), %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x2360
movq 0x5f0(%rbx), %rax
testq %rax, %rax
je 0xdd8e
cmpq $0x0, 0x58(%rax)
jne 0xdd9b
leaq 0xea58(%rip), %rsi      # 0x1c757
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x94f4
jmp 0xdd83
movq 0x5f0(%rbx), %rax
testq %rax, %rax
je 0xdd8e
cmpq $0x0, 0x58(%rax)
jne 0xddbe
leaq 0xea6d(%rip), %rsi      # 0x1c79b
movq %rbx, %rdi
movl $0x40, %edx
jmp 0xdd7e
movq 0x5f0(%rbx), %rax
testq %rax, %rax
je 0xdd8e
cmpq $0x0, 0x58(%rax)
jne 0xddc3
leaq 0xea8a(%rip), %rsi      # 0x1c7dc
movq %rbx, %rdi
movl $0x1b, %edx
jmp 0xdd7e
movq 0x5f0(%rbx), %rax
testq %rax, %rax
je 0xdd8e
cmpq $0x0, 0x58(%rax)
jne 0xddc8
leaq 0xea01(%rip), %rsi      # 0x1c777
movq %rbx, %rdi
movl $0x23, %edx
callq 0x7f0c
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
retq
callq 0x4397
callq 0x43cd
callq 0x457d
callq 0x45b3
callq 0x44a5
callq 0x44db
callq 0x4511
callq 0x4547
callq 0x4403
callq 0x446f
callq 0x4439