// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/src/pk_var.c
// Function: newFunction
Function* newFunction(PKVM* vm, const char* name, int length, Script* owner,
                      bool is_native, const char* docstring) {

  Function* func = ALLOCATE(vm, Function);
  varInitObject(&func->_super, vm, OBJ_FUNC);

  vmPushTempRef(vm, &func->_super); // func

  if (owner == NULL) {
    ASSERT(is_native, OOPS);
    func->name = name;
    func->owner = NULL;
    func->is_native = is_native;

  } else {
    pkFunctionBufferWrite(&owner->functions, vm, func);
    uint32_t name_index = scriptAddName(owner, vm, name, length);

    func->name = owner->names.data[name_index]->data;
    func->owner = owner;
    func->arity = -2; // -1 means variadic args.
    func->is_native = is_native;
  }

  if (is_native) {
    func->native = NULL;

  } else {
    Fn* fn = ALLOCATE(vm, Fn);
    pkByteBufferInit(&fn->opcodes);
    pkUintBufferInit(&fn->oplines);
    fn->stack_size = 0;
    func->fn = fn;
  }

  // Both native and script (TODO:) functions support docstring.
  func->docstring = docstring;

  vmPopTempRef(vm); // func
  return func;
}

// Assembly:
subq $0x48, %rsp
movb %r8b, %al
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movl %edx, 0x34(%rsp)
movq %rcx, 0x28(%rsp)
andb $0x1, %al
movb %al, 0x27(%rsp)
movq %r9, 0x18(%rsp)
movq 0x40(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x40, %ecx
movq %rdx, %rsi
callq 0x8f90
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl $0x5, %edx
callq 0x3d00
movq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x9420
cmpq $0x0, 0x28(%rsp)
jne 0x4b5c
jmp 0x4aed
testb $0x1, 0x27(%rsp)
jne 0x4b2e
movq 0x2e4fd(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x2564b(%rip), %rsi     # 0x2a150
leaq 0x25758(%rip), %rdx     # 0x2a264
leaq 0x25780(%rip), %rcx     # 0x2a293
leaq 0x25682(%rip), %r8      # 0x2a19c
movl $0x1a4, %r9d            # imm = 0x1A4
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x4b30
movq 0x38(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x10(%rsp), %rax
movq $0x0, 0x18(%rax)
movb 0x27(%rsp), %cl
movq 0x10(%rsp), %rax
andb $0x1, %cl
movb %cl, 0x30(%rax)
jmp 0x4bd7
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
movq 0x40(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x3970
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl 0x34(%rsp), %ecx
callq 0x4c60
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rax
movq 0x60(%rax), %rax
movl 0xc(%rsp), %ecx
movq (%rax,%rcx,8), %rcx
addq $0x1c, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x10(%rsp), %rax
movl $0xfffffffe, 0x20(%rax) # imm = 0xFFFFFFFE
movb 0x27(%rsp), %cl
movq 0x10(%rsp), %rax
andb $0x1, %cl
movb %cl, 0x30(%rax)
testb $0x1, 0x27(%rsp)
je 0x4bed
movq 0x10(%rsp), %rax
movq $0x0, 0x38(%rax)
jmp 0x4c35
movq 0x40(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x28, %ecx
movq %rdx, %rsi
callq 0x8f90
movq %rax, (%rsp)
movq (%rsp), %rdi
callq 0x30d0
movq (%rsp), %rdi
addq $0x10, %rdi
callq 0x2e80
movq (%rsp), %rax
movl $0x0, 0x20(%rax)
movq (%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x38(%rax)
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x40(%rsp), %rdi
callq 0x95c0
movq 0x10(%rsp), %rax
addq $0x48, %rsp
retq
nopw (%rax,%rax)