// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movl %edx, 0x2c(%rsp)
movq %rcx, 0x20(%rsp)
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
cmpl $-0x1, 0x20(%rax)
jge 0x10bd0
movq 0x2245b(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x195a9(%rip), %rsi     # 0x2a150
leaq 0x19bcc(%rip), %rdx     # 0x2a77a
leaq 0x19bff(%rip), %rcx     # 0x2a7b4
leaq 0x19a80(%rip), %r8      # 0x2a63c
movl $0x148, %r9d            # imm = 0x148
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x10bd2
movl 0x2c(%rsp), %eax
movq 0x30(%rsp), %rcx
movq 0x18(%rcx), %rcx
cmpl 0x20(%rcx), %eax
je 0x10ca4
leaq 0x14(%rsp), %rdi
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x20(%rax), %edx
leaq 0x1cf71(%rip), %rsi     # 0x2db71
movb $0x0, %al
callq 0x2350
movq 0x38(%rsp), %rax
cmpq $0x0, 0x5f0(%rax)
je 0x10c9a
jmp 0x10c1c
jmp 0x10c1e
movq 0x38(%rsp), %rax
movq 0x5f0(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x10c6b
movq 0x223c0(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x1950e(%rip), %rsi     # 0x2a150
leaq 0x1961b(%rip), %rdx     # 0x2a264
leaq 0x19b64(%rip), %rcx     # 0x2a7b4
leaq 0x199e5(%rip), %r8      # 0x2a63c
movl $0x14c, %r9d            # imm = 0x14C
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x10c6d
movq 0x38(%rsp), %rdi
leaq 0x14(%rsp), %rdx
leaq 0x19b45(%rip), %rsi     # 0x2a7c3
movb $0x0, %al
callq 0x4ed0
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rcx, 0x58(%rax)
jmp 0x10c9a
movb $0x0, 0x47(%rsp)
jmp 0x1116c
movq 0x30(%rsp), %rax
cmpl $0x0, 0x10(%rax)
je 0x10f14
movq 0x30(%rsp), %rax
movl 0x10(%rax), %eax
movq %rax, 0x8(%rsp)
subq $0x3, %rax
ja 0x10edf
movq 0x8(%rsp), %rax
leaq 0x1991e(%rip), %rcx     # 0x2a5f4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x10ce1
movq 0x22310(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x194e8(%rip), %rsi     # 0x2a1da
leaq 0x19abb(%rip), %rdx     # 0x2a7b4
leaq 0x1993c(%rip), %rcx     # 0x2a63c
movl $0x151, %r8d            # imm = 0x151
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x10d16
movq 0x38(%rsp), %rax
cmpq $0x0, 0x5f0(%rax)
je 0x10da3
jmp 0x10d27
jmp 0x10d29
movq 0x38(%rsp), %rax
movq 0x5f0(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x10d76
movq 0x222b5(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x19403(%rip), %rsi     # 0x2a150
leaq 0x19510(%rip), %rdx     # 0x2a264
leaq 0x19a59(%rip), %rcx     # 0x2a7b4
leaq 0x198da(%rip), %r8      # 0x2a63c
movl $0x153, %r9d            # imm = 0x153
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x10d78
movq 0x38(%rsp), %rdi
leaq 0x19a5f(%rip), %rsi     # 0x2a7e3
movl $0x23, %edx
callq 0x2760
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rcx, 0x58(%rax)
jmp 0x10da3
movb $0x0, 0x47(%rsp)
jmp 0x1116c
jmp 0x10daf
movq 0x38(%rsp), %rax
cmpq $0x0, 0x5f0(%rax)
je 0x10e3c
jmp 0x10dc0
jmp 0x10dc2
movq 0x38(%rsp), %rax
movq 0x5f0(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x10e0f
movq 0x2221c(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x1936a(%rip), %rsi     # 0x2a150
leaq 0x19477(%rip), %rdx     # 0x2a264
leaq 0x199c0(%rip), %rcx     # 0x2a7b4
leaq 0x19841(%rip), %r8      # 0x2a63c
movl $0x156, %r9d            # imm = 0x156
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x10e11
movq 0x38(%rsp), %rdi
leaq 0x199ea(%rip), %rsi     # 0x2a807
movl $0x40, %edx
callq 0x2760
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rcx, 0x58(%rax)
jmp 0x10e3c
movb $0x0, 0x47(%rsp)
jmp 0x1116c
jmp 0x10e48
movq 0x38(%rsp), %rax
cmpq $0x0, 0x5f0(%rax)
je 0x10ed5
jmp 0x10e59
jmp 0x10e5b
movq 0x38(%rsp), %rax
movq 0x5f0(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x10ea8
movq 0x22183(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x192d1(%rip), %rsi     # 0x2a150
leaq 0x193de(%rip), %rdx     # 0x2a264
leaq 0x19927(%rip), %rcx     # 0x2a7b4
leaq 0x197a8(%rip), %r8      # 0x2a63c
movl $0x158, %r9d            # imm = 0x158
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x10eaa
movq 0x38(%rsp), %rdi
leaq 0x19992(%rip), %rsi     # 0x2a848
movl $0x1b, %edx
callq 0x2760
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rcx, 0x58(%rax)
jmp 0x10ed5
movb $0x0, 0x47(%rsp)
jmp 0x1116c
jmp 0x10ee1
movq 0x22110(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x192e8(%rip), %rsi     # 0x2a1da
leaq 0x198bb(%rip), %rdx     # 0x2a7b4
leaq 0x1973c(%rip), %rcx     # 0x2a63c
movl $0x15a, %r8d            # imm = 0x15A
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x10f16
movq 0x30(%rsp), %rax
cmpq $0x0, 0x20(%rax)
je 0x10f3d
movq 0x30(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x30(%rsp), %rcx
movq 0x20(%rcx), %rcx
addq $0x8, %rcx
cmpq %rcx, %rax
je 0x10f77
movq 0x220b4(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x19202(%rip), %rsi     # 0x2a150
leaq 0x1930f(%rip), %rdx     # 0x2a264
leaq 0x19858(%rip), %rcx     # 0x2a7b4
leaq 0x196d9(%rip), %r8      # 0x2a63c
movl $0x15d, %r9d            # imm = 0x15D
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x10f79
jmp 0x10f7b
movq 0x30(%rsp), %rax
movq 0x38(%rax), %rax
addq $0x8, %rax
movq 0x30(%rsp), %rcx
cmpq 0x30(%rcx), %rax
je 0x10fcd
movq 0x2205e(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x191ac(%rip), %rsi     # 0x2a150
leaq 0x192b9(%rip), %rdx     # 0x2a264
leaq 0x19802(%rip), %rcx     # 0x2a7b4
leaq 0x19683(%rip), %r8      # 0x2a63c
movl $0x15e, %r9d            # imm = 0x15E
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x10fcf
jmp 0x10fd1
movq 0x30(%rsp), %rax
cmpl $0x1, 0x4c(%rax)
je 0x11016
movq 0x22015(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x19163(%rip), %rsi     # 0x2a150
leaq 0x19270(%rip), %rdx     # 0x2a264
leaq 0x197b9(%rip), %rcx     # 0x2a7b4
leaq 0x1963a(%rip), %r8      # 0x2a63c
movl $0x164, %r9d            # imm = 0x164
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x11018
jmp 0x1101a
movq 0x30(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x10(%rax), %rax
movq 0x30(%rsp), %rcx
cmpq 0x38(%rcx), %rax
je 0x1106c
movq 0x21fbf(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x1910d(%rip), %rsi     # 0x2a150
leaq 0x1921a(%rip), %rdx     # 0x2a264
leaq 0x19763(%rip), %rcx     # 0x2a7b4
leaq 0x195e4(%rip), %r8      # 0x2a63c
movl $0x165, %r9d            # imm = 0x165
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x1106e
jmp 0x11070
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x30(%rsp), %rcx
movslq 0x28(%rcx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq 0x30(%rsp), %rcx
movq 0x30(%rcx), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movslq 0x2c(%rsp), %rcx
cmpq %rcx, %rax
jge 0x110dd
movq 0x21f4e(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x1909c(%rip), %rsi     # 0x2a150
leaq 0x191a9(%rip), %rdx     # 0x2a264
leaq 0x196f2(%rip), %rcx     # 0x2a7b4
leaq 0x19573(%rip), %r8      # 0x2a63c
movl $0x166, %r9d            # imm = 0x166
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x110df
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x11126
movq 0x20(%rsp), %rax
movslq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdx
movq 0x30(%rsp), %rax
movq 0x38(%rax), %rax
movl 0x10(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x110e7
movl 0x2c(%rsp), %edx
movq 0x30(%rsp), %rax
movq 0x30(%rax), %rcx
movslq %edx, %rdx
shlq $0x3, %rdx
addq %rdx, %rcx
movq %rcx, 0x30(%rax)
movq 0x38(%rsp), %rax
movq 0x5f0(%rax), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x50(%rax)
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x5f0(%rax)
movb $0x1, 0x47(%rsp)
movb 0x47(%rsp), %al
andb $0x1, %al
addq $0x48, %rsp
retq
nopw (%rax,%rax)