// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/src/pk_vm.c
// Function: growStack
static inline void growStack(PKVM* vm, int size) {
  Fiber* fiber = vm->fiber;
  ASSERT(fiber->stack_size <= size, OOPS);
  int new_size = utilPowerOf2Ceil(size);

  Var* old_rbp = fiber->stack; //< Old stack base pointer.
  fiber->stack = (Var*)vmRealloc(vm, fiber->stack,
                                 sizeof(Var) * fiber->stack_size,
                                 sizeof(Var) * new_size);
  fiber->stack_size = new_size;

  // If the old stack base pointer is the same as the current, that means the
  // stack hasn't been moved by the reallocation. In that case we're done.
  if (old_rbp == fiber->stack) return;

  // If we reached here that means the stack is moved by the reallocation and
  // we have to update all the pointers that pointing to the old stack slots.

  //
  //                                     '        '
  //             '        '              '        '
  //             '        '              |        | <new_rsp
  //    old_rsp> |        |              |        |
  //             |        |       .----> | value  | <new_ptr
  //             |        |       |      |        |
  //    old_ptr> | value  | ------'      |________| <new_rbp
  //             |        | ^            new stack
  //    old_rbp> |________| | height
  //             old stack
  //
  //            new_ptr = new_rbp      + height
  //                    = fiber->stack + ( old_ptr  - old_rbp )
#define MAP_PTR(old_ptr) (fiber->stack + ((old_ptr) - old_rbp))

  // Update the stack top pointer and the return pointer.
  fiber->sp = MAP_PTR(fiber->sp);
  fiber->ret = MAP_PTR(fiber->ret);

  // Update the stack base pointer of the call frames.
  for (int i = 0; i < fiber->frame_count; i++) {
    CallFrame* frame = fiber->frames + i;
    frame->rbp = MAP_PTR(frame->rbp);
  }
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl %esi, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movq 0x5f0(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movl 0x28(%rax), %eax
cmpl 0x2c(%rsp), %eax
jle 0x121b6
movq 0x20e75(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x17fc3(%rip), %rsi     # 0x2a150
leaq 0x180d0(%rip), %rdx     # 0x2a264
leaq 0x18893(%rip), %rcx     # 0x2aa2e
leaq 0x1849a(%rip), %r8      # 0x2a63c
movl $0x1eb, %r9d            # imm = 0x1EB
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x121b8
movl 0x2c(%rsp), %edi
callq 0x13ec0
movl %eax, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rsi
movq 0x20(%rsp), %rax
movslq 0x28(%rax), %rdx
shlq $0x3, %rdx
movslq 0x1c(%rsp), %rcx
shlq $0x3, %rcx
callq 0x8f90
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x20(%rax)
movl 0x1c(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x28(%rax)
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %rcx
cmpq 0x20(%rcx), %rax
jne 0x12229
jmp 0x122ed
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rcx
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rsp), %rdx
subq %rdx, %rax
sarq $0x3, %rax
shlq $0x3, %rax
addq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rcx
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x10(%rsp), %rdx
subq %rdx, %rax
sarq $0x3, %rax
shlq $0x3, %rax
addq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x38(%rax)
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x4c(%rcx), %eax
jge 0x122ed
movq 0x20(%rsp), %rax
movq 0x40(%rax), %rax
movslq 0xc(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rcx
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdx
subq %rdx, %rax
sarq $0x3, %rax
shlq $0x3, %rax
addq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x10(%rax)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x1228d
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)