// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/src/pk_var.c
// Function: newFiber
Fiber* newFiber(PKVM* vm, Function* fn) {
  Fiber* fiber = ALLOCATE(vm, Fiber);
  memset(fiber, 0, sizeof(Fiber));
  varInitObject(&fiber->_super, vm, OBJ_FIBER);

  fiber->state = FIBER_NEW;
  fiber->func = fn;

  if (fn->is_native) {
    // For native functions, we're only using stack for parameters,
    // there won't be any locals or temps (which are belongs to the
    // native "C" stack).
    int stack_size = utilPowerOf2Ceil(fn->arity + 1);
    fiber->stack = ALLOCATE_ARRAY(vm, Var, stack_size);
    fiber->stack_size = stack_size;
    fiber->ret = fiber->stack;
    fiber->sp = fiber->stack + 1;

  } else {
    // Allocate stack.
    int stack_size = utilPowerOf2Ceil(fn->fn->stack_size + 1);
    if (stack_size < MIN_STACK_SIZE) stack_size = MIN_STACK_SIZE;
    fiber->stack = ALLOCATE_ARRAY(vm, Var, stack_size);
    fiber->stack_size = stack_size;
    fiber->ret = fiber->stack;
    fiber->sp = fiber->stack + 1;

    // Allocate call frames.
    fiber->frame_capacity = INITIAL_CALL_FRAMES;
    fiber->frames = ALLOCATE_ARRAY(vm, CallFrame, fiber->frame_capacity);
    fiber->frame_count = 1;

    // Initialize the first frame.
    fiber->frames[0].fn = fn;
    fiber->frames[0].ip = fn->fn->opcodes.data;
    fiber->frames[0].rbp = fiber->ret;
  }

  // Initialize the return value to null (doesn't really have to do that here
  // but if we're trying to debut it may crash when dumping the return value).
  *fiber->ret = VAR_NULL;

  return fiber;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq 0x20(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x60, %ecx
movq %rdx, %rsi
callq 0x8f90
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movl $0x60, %edx
callq 0x2180
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl $0x6, %edx
callq 0x3d00
movq 0x10(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x18(%rsp), %rax
testb $0x1, 0x30(%rax)
je 0x2c36
movq 0x18(%rsp), %rax
movl 0x20(%rax), %edi
addl $0x1, %edi
callq 0x13ec0
movl %eax, 0xc(%rsp)
movq 0x20(%rsp), %rdi
movslq 0xc(%rsp), %rcx
shlq $0x3, %rcx
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x8f90
movq %rax, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x20(%rax)
movl 0xc(%rsp), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x28(%rax)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x38(%rax)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rcx
addq $0x8, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x30(%rax)
jmp 0x2d3c
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movl 0x20(%rax), %edi
addl $0x1, %edi
callq 0x13ec0
movl %eax, 0x8(%rsp)
cmpl $0x80, 0x8(%rsp)
jge 0x2c60
movl $0x80, 0x8(%rsp)
movq 0x20(%rsp), %rdi
movslq 0x8(%rsp), %rcx
shlq $0x3, %rcx
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x8f90
movq %rax, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x20(%rax)
movl 0x8(%rsp), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x28(%rax)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x38(%rax)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rcx
addq $0x8, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x10(%rsp), %rax
movl $0x4, 0x48(%rax)
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rax
movslq 0x48(%rax), %rax
imulq $0x18, %rax, %rcx
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x8f90
movq %rax, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0x4c(%rax)
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rax
movq 0x40(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax), %rcx
movq 0x10(%rsp), %rax
movq 0x40(%rax), %rax
movq %rcx, (%rax)
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rcx
movq 0x10(%rsp), %rax
movq 0x40(%rax), %rax
movq %rcx, 0x10(%rax)
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rax
movabsq $0x7ffc000000000000, %rcx # imm = 0x7FFC000000000000
movq %rcx, (%rax)
movq 0x10(%rsp), %rax
addq $0x28, %rsp
retq
nopl (%rax)