// File: /workspace/github_c_makefile_star2_O2/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 0x9514
movq %rdi, %rbx
cmpl %edx, %eax
jne 0x942e
movl 0x10(%rsi), %eax
cmpq $0x3, %rax
ja 0x95a7
leaq 0xf207(%rip), %rdi      # 0x185a8
movslq (%rdi,%rax,4), %rax
addq %rdi, %rax
jmpq *%rax
movq 0x20(%rsi), %r8
testq %r8, %r8
je 0x9576
movq 0x30(%rsi), %rax
leaq 0x8(%r8), %rdi
cmpq %rdi, %rax
jne 0x9576
movq 0x38(%rsi), %r9
leaq 0x8(%r9), %rdi
cmpq %rax, %rdi
jne 0x95d5
cmpl $0x1, 0x4c(%rsi)
jne 0x9606
movq 0x40(%rsi), %r10
cmpq %r9, 0x10(%r10)
jne 0x9637
movslq 0x28(%rsi), %r9
leaq (%r8,%r9,8), %r9
subq %rax, %r9
sarq $0x3, %r9
movslq %edx, %r8
cmpq %r8, %r9
jl 0x9668
xorl %r9d, %r9d
testl %edx, %edx
cmovlel %r9d, %edx
cmpq %r9, %rdx
je 0x94f6
movq (%rcx,%r9,8), %r10
movq (%r10), %r10
movq %r10, (%rdi,%r9,8)
incq %r9
jmp 0x9415
leaq 0x125ec(%rip), %rsi     # 0x1ba21
leaq 0x4(%rsp), %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x2360
movq 0x5f0(%rbx), %rax
testq %rax, %rax
je 0x94ee
cmpq $0x0, 0x58(%rax)
jne 0x9545
leaq 0xf302(%rip), %rsi      # 0x18767
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3a8d
jmp 0x94e3
movq 0x5f0(%rbx), %rax
testq %rax, %rax
je 0x94ee
cmpq $0x0, 0x58(%rax)
jne 0x9699
leaq 0xf317(%rip), %rsi      # 0x187ab
pushq $0x40
jmp 0x94da
movq 0x5f0(%rbx), %rax
testq %rax, %rax
je 0x94ee
cmpq $0x0, 0x58(%rax)
jne 0x96c7
leaq 0xf336(%rip), %rsi      # 0x187ec
pushq $0x1b
jmp 0x94da
movq 0x5f0(%rbx), %rax
testq %rax, %rax
je 0x94ee
cmpq $0x0, 0x58(%rax)
jne 0x96f5
leaq 0xf2af(%rip), %rsi      # 0x18787
pushq $0x23
popq %rdx
movq %rbx, %rdi
callq 0x261f
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
retq
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 0x94f0
movq 0x19add(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xebcb(%rip), %rsi      # 0x180f0
leaq 0xf1f2(%rip), %rdx      # 0x1871e
leaq 0xf225(%rip), %rcx      # 0x18758
leaq 0xf0a6(%rip), %r8       # 0x185e0
movl $0x148, %r9d            # imm = 0x148
jmp 0x9721
movq 0x19aac(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xeb9a(%rip), %rsi      # 0x180f0
leaq 0xeca7(%rip), %rdx      # 0x18204
leaq 0xf1f4(%rip), %rcx      # 0x18758
leaq 0xf075(%rip), %r8       # 0x185e0
movl $0x14c, %r9d            # imm = 0x14C
jmp 0x9721
movq 0x19a7b(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xeb69(%rip), %rsi      # 0x180f0
leaq 0xec76(%rip), %rdx      # 0x18204
leaq 0xf1c3(%rip), %rcx      # 0x18758
leaq 0xf044(%rip), %r8       # 0x185e0
movl $0x15d, %r9d            # imm = 0x15D
jmp 0x9721
movq 0x19a4a(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xebc2(%rip), %rsi      # 0x1817a
leaq 0xf199(%rip), %rdx      # 0x18758
leaq 0xf01a(%rip), %rcx      # 0x185e0
movl $0x15a, %r8d            # imm = 0x15A
xorl %eax, %eax
callq 0x2250
ud2
movq 0x19a1c(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xeb0a(%rip), %rsi      # 0x180f0
leaq 0xec17(%rip), %rdx      # 0x18204
leaq 0xf164(%rip), %rcx      # 0x18758
leaq 0xefe5(%rip), %r8       # 0x185e0
movl $0x15e, %r9d            # imm = 0x15E
jmp 0x9721
movq 0x199eb(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xead9(%rip), %rsi      # 0x180f0
leaq 0xebe6(%rip), %rdx      # 0x18204
leaq 0xf133(%rip), %rcx      # 0x18758
leaq 0xefb4(%rip), %r8       # 0x185e0
movl $0x164, %r9d            # imm = 0x164
jmp 0x9721
movq 0x199ba(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xeaa8(%rip), %rsi      # 0x180f0
leaq 0xebb5(%rip), %rdx      # 0x18204
leaq 0xf102(%rip), %rcx      # 0x18758
leaq 0xef83(%rip), %r8       # 0x185e0
movl $0x165, %r9d            # imm = 0x165
jmp 0x9721
movq 0x19989(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xea77(%rip), %rsi      # 0x180f0
leaq 0xeb84(%rip), %rdx      # 0x18204
leaq 0xf0d1(%rip), %rcx      # 0x18758
leaq 0xef52(%rip), %r8       # 0x185e0
movl $0x166, %r9d            # imm = 0x166
jmp 0x9721
movq 0x19958(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xea46(%rip), %rsi      # 0x180f0
leaq 0xeb53(%rip), %rdx      # 0x18204
leaq 0xf0a0(%rip), %rcx      # 0x18758
leaq 0xef21(%rip), %r8       # 0x185e0
movl $0x156, %r9d            # imm = 0x156
jmp 0x9721
movq 0x1992a(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xea18(%rip), %rsi      # 0x180f0
leaq 0xeb25(%rip), %rdx      # 0x18204
leaq 0xf072(%rip), %rcx      # 0x18758
leaq 0xeef3(%rip), %r8       # 0x185e0
movl $0x158, %r9d            # imm = 0x158
jmp 0x9721
movq 0x198fc(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xe9ea(%rip), %rsi      # 0x180f0
leaq 0xeaf7(%rip), %rdx      # 0x18204
leaq 0xf044(%rip), %rcx      # 0x18758
leaq 0xeec5(%rip), %r8       # 0x185e0
movl $0x153, %r9d            # imm = 0x153
xorl %eax, %eax
callq 0x2250
ud2