// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/src/pk_vm.c
// Function: reuseCallFrame
static inline void reuseCallFrame(PKVM* vm, const Function* fn) {

  ASSERT(!fn->is_native, "Native function shouldn't use call frames.");
  ASSERT(fn->arity >= 0, OOPS);
  ASSERT(vm->fiber->frame_count > 0, OOPS);

  Fiber* fb = vm->fiber;

  CallFrame* frame = fb->frames + fb->frame_count - 1;
  frame->fn = fn;
  frame->ip = fn->fn->opcodes.data;

  ASSERT(*frame->rbp == VAR_NULL, OOPS);

  // Move all the argument(s) to the base of the current frame.
  Var* arg = fb->sp - fn->arity;
  Var* target = frame->rbp + 1;
  for (; arg < fb->sp; arg++, target++) {
    *target = *arg;
  }

  // At this point target points to the stack pointer of the next call.
  fb->sp = target;

  // Grow the stack if needed (least probably).
  int needed = fn->fn->stack_size + (int)(vm->fiber->sp - vm->fiber->stack);
  if (vm->fiber->stack_size <= needed) growStack(vm, needed);
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq 0x28(%rsp), %rax
testb $0x1, 0x30(%rax)
je 0x11f23
movq 0x21108(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x18256(%rip), %rsi     # 0x2a150
leaq 0x18af4(%rip), %rdx     # 0x2a9f5
leaq 0x18b30(%rip), %rcx     # 0x2aa38
leaq 0x1872d(%rip), %r8      # 0x2a63c
movl $0x22e, %r9d            # imm = 0x22E
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x11f25
jmp 0x11f27
movq 0x28(%rsp), %rax
cmpl $0x0, 0x20(%rax)
jge 0x11f6c
movq 0x210bf(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x1820d(%rip), %rsi     # 0x2a150
leaq 0x1831a(%rip), %rdx     # 0x2a264
leaq 0x18ae7(%rip), %rcx     # 0x2aa38
leaq 0x186e4(%rip), %r8      # 0x2a63c
movl $0x22f, %r9d            # imm = 0x22F
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x11f6e
jmp 0x11f70
movq 0x30(%rsp), %rax
movq 0x5f0(%rax), %rax
cmpl $0x0, 0x4c(%rax)
jg 0x11fbc
movq 0x2106f(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x181bd(%rip), %rsi     # 0x2a150
leaq 0x182ca(%rip), %rdx     # 0x2a264
leaq 0x18a97(%rip), %rcx     # 0x2aa38
leaq 0x18694(%rip), %r8      # 0x2a63c
movl $0x230, %r9d            # imm = 0x230
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x11fbe
movq 0x30(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x20(%rsp), %rcx
movslq 0x4c(%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
addq $-0x18, %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax), %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movabsq $0x7ffc000000000000, %rcx # imm = 0x7FFC000000000000
cmpq %rcx, (%rax)
je 0x12065
movq 0x20fc6(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x18114(%rip), %rsi     # 0x2a150
leaq 0x18221(%rip), %rdx     # 0x2a264
leaq 0x189ee(%rip), %rcx     # 0x2aa38
leaq 0x185eb(%rip), %r8      # 0x2a63c
movl $0x238, %r9d            # imm = 0x238
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x12067
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x28(%rsp), %rcx
movslq 0x20(%rcx), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %rcx
cmpq 0x30(%rcx), %rax
jae 0x120da
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rsp), %rax
movq %rcx, (%rax)
movq 0x10(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
jmp 0x1209c
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movl 0x20(%rax), %eax
movq 0x30(%rsp), %rcx
movq 0x5f0(%rcx), %rcx
movq 0x30(%rcx), %rcx
movq 0x30(%rsp), %rdx
movq 0x5f0(%rdx), %rdx
movq 0x20(%rdx), %rdx
subq %rdx, %rcx
sarq $0x3, %rcx
addl %ecx, %eax
movl %eax, 0x4(%rsp)
movq 0x30(%rsp), %rax
movq 0x5f0(%rax), %rax
movl 0x28(%rax), %eax
cmpl 0x4(%rsp), %eax
jg 0x12144
movq 0x30(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x12150
addq $0x38, %rsp
retq
nopl (%rax)