// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9VmCallUserFunction
JX9_PRIVATE sxi32 jx9VmCallUserFunction(
	jx9_vm *pVm,       /* Target VM */
	jx9_value *pFunc,  /* Callback name */
	int nArg,          /* Total number of given arguments */
	jx9_value **apArg, /* Callback arguments */
	jx9_value *pResult /* Store callback return value here. NULL otherwise */
	)
{
	jx9_value *aStack;
	VmInstr aInstr[2];
	int i;
	if((pFunc->iFlags & (MEMOBJ_STRING)) == 0 ){
		/* Don't bother processing, it's invalid anyway */
		if( pResult ){
			/* Assume a null return value */
			jx9MemObjRelease(pResult);
		}
		return SXERR_INVALID;
	}
	/* Create a new operand stack */
	aStack = VmNewOperandStack(&(*pVm), 1+nArg);
	if( aStack == 0 ){
		jx9VmThrowError(&(*pVm), 0, JX9_CTX_ERR, 
			"JX9 is running out of memory while invoking user callback");
		if( pResult ){
			/* Assume a null return value */
			jx9MemObjRelease(pResult);
		}
		return SXERR_MEM;
	}
	/* Fill the operand stack with the given arguments */
	for( i = 0 ; i < nArg ; i++ ){
		jx9MemObjLoad(apArg[i], &aStack[i]);
		aStack[i].nIdx = apArg[i]->nIdx;
	}
	/* Push the function name */
	jx9MemObjLoad(pFunc, &aStack[i]);
	aStack[i].nIdx = SXU32_HIGH; /* Mark as constant */
	/* Emit the CALL istruction */
	aInstr[0].iOp = JX9_OP_CALL;
	aInstr[0].iP1 = nArg; /* Total number of given arguments */
	aInstr[0].iP2 = 0;
	aInstr[0].p3  = 0;
	/* Emit the DONE instruction */
	aInstr[1].iOp = JX9_OP_DONE;
	aInstr[1].iP1 = 1;   /* Extract function return value if available */
	aInstr[1].iP2 = 0;
	aInstr[1].p3  = 0;
	/* Execute the function body (if available) */
	VmByteCodeExec(&(*pVm), aInstr, aStack, nArg, pResult);
	/* Clean up the mess left behind */
	SyMemBackendFree(&pVm->sAllocator, aStack);
	return JX9_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r14
testb $0x1, 0x8(%rsi)
jne 0x408c0
movl $0xfffffff7, %r13d      # imm = 0xFFFFFFF7
jmp 0x40946
movq %rcx, %r15
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r12
leal 0x1(%rbx), %esi
callq 0x40a72
testq %rax, %rax
je 0x4092a
movq %r13, -0x38(%rbp)
movq %r12, -0x48(%rbp)
xorl %r13d, %r13d
testl %ebx, %ebx
movq %rbx, -0x40(%rbp)
jle 0x40955
movq %r14, -0x30(%rbp)
movl %ebx, %r12d
xorl %r14d, %r14d
movq %rax, %rbx
movq %rax, %r13
movq (%r15,%r14,8), %rdi
movq %r13, %rsi
callq 0x40cc7
movq (%r15,%r14,8), %rax
movl 0x38(%rax), %eax
movl %eax, 0x38(%r13)
incq %r14
addq $0x40, %r13
cmpq %r14, %r12
jne 0x408fb
movq -0x30(%rbp), %r14
xorl %r13d, %r13d
movq %rbx, %rax
jmp 0x40958
leaq 0x337ea(%rip), %rcx     # 0x7411b
movq %r12, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x40afa
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testq %r14, %r14
je 0x409bd
movq %r14, %rdi
callq 0x30465
jmp 0x409bd
xorl %r12d, %r12d
shlq $0x6, %r12
movq %rax, %rsi
addq %r12, %rsi
movq -0x38(%rbp), %rdi
movq %rax, %r15
callq 0x40cc7
movl $0xffffffff, 0x38(%r15,%r12) # imm = 0xFFFFFFFF
leaq -0x80(%rbp), %rsi
movb $0x10, (%rsi)
movq -0x40(%rbp), %rcx
movl %ecx, 0x4(%rsi)
movl $0x0, 0x8(%rsi)
movq %r13, 0x10(%rsi)
movb $0x1, 0x18(%rsi)
movq $0x1, 0x1c(%rsi)
movq %r13, 0x28(%rsi)
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
movq %r14, %r8
callq 0x40d1d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3490d
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq