// File: /workspace/github_c_makefile_star2_O1/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: lhStoreCell
static int lhStoreCell(
	lhpage *pPage, /* Target page */
	const void *pKey,sxu32 nKeyLen, /* Payload: Key */
	const void *pData,unqlite_int64 nDataLen, /* Payload: Data */
	sxu32 nHash, /* Hash of the key */
	int auto_append /* Auto append a slave page if full */
	)
{
	lhash_kv_engine *pEngine = pPage->pHash;
	int iNeedOvfl = 0; /* Need overflow page for this cell and its payload*/
	lhcell *pCell;
	sxu16 nOfft;
	int rc;
	/* Acquire a writer lock on this page first */
	rc = pEngine->pIo->xWrite(pPage->pRaw);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Check for a free block  */
	rc = lhAllocateSpace(pPage,L_HASH_CELL_SZ+nKeyLen+nDataLen,&nOfft);
	if( rc != UNQLITE_OK ){
		/* Check for a free block to hold a single cell only (without payload) */
		rc = lhAllocateSpace(pPage,L_HASH_CELL_SZ,&nOfft);
		if( rc != UNQLITE_OK ){
			if( !auto_append ){
				/* A split must be done */
				return UNQLITE_FULL;
			}else{
				/* Store this record in a slave page */
				rc = lhSlaveStore(pPage,pKey,nKeyLen,pData,nDataLen,nHash);
				return rc;
			}
		}
		iNeedOvfl = 1;
	}
	/* Allocate a new cell instance */
	pCell = lhNewCell(pEngine,pPage);
	if( pCell == 0 ){
		pEngine->pIo->xErr(pEngine->pIo->pHandle,"KV store is running out of memory");
		return UNQLITE_NOMEM;
	}
	/* Fill-in the structure */
	pCell->iStart = nOfft;
	pCell->nKey = nKeyLen;
	pCell->nData = (sxu64)nDataLen;
	pCell->nHash = nHash;
	if( nKeyLen < 262144 /* 256 KB */ ){
		/* Keep the key in-memory for fast lookup */
		SyBlobAppend(&pCell->sKey,pKey,nKeyLen);
	}
	/* Link the cell */
	rc = lhInstallCell(pCell);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Write the payload */
	if( iNeedOvfl ){
		rc = lhCellWriteOvflPayload(pCell,pKey,nKeyLen,pData,nDataLen,(const void *)0);
		if( rc != UNQLITE_OK ){
			lhCellDiscard(pCell);
			return rc;
		}
	}else{
		lhCellWriteLocalPayload(pCell,pKey,nKeyLen,pData,nDataLen);
	}
	/* Finally, Write the cell header */
	lhCellWriteHeader(pCell);
	/* All done */
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %ebx
movq %r8, -0x48(%rbp)
movq %rcx, -0x40(%rbp)
movl %edx, %r15d
movq %rsi, -0x38(%rbp)
movq %rdi, %r14
movq (%rdi), %r13
movq (%r13), %rax
movq 0x8(%rdi), %rdi
callq *0x28(%rax)
movl %eax, %r12d
testl %eax, %eax
je 0x387e6
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, -0x50(%rbp)
movl %ebx, -0x30(%rbp)
leal 0x1a(%r15), %esi
movq -0x48(%rbp), %rbx
addq %rbx, %rsi
leaq -0x2a(%rbp), %rdx
movq %r14, %rdi
callq 0x38f12
movl %eax, %r12d
testl %eax, %eax
je 0x3886f
leaq -0x2a(%rbp), %rdx
movl $0x1a, %esi
movq %r14, %rdi
callq 0x38f12
testl %eax, %eax
je 0x3886f
cmpl $0x0, 0x10(%rbp)
je 0x3892a
movl %r15d, %esi
addq %rbx, %rsi
leaq -0x58(%rbp), %rcx
movq %r14, %rdi
xorl %edx, %edx
callq 0x398df
movl %eax, %r12d
testl %eax, %eax
jne 0x387d4
movq -0x58(%rbp), %rdi
movl $0x1, (%rsp)
movq -0x38(%rbp), %rsi
movl %r15d, %edx
movq -0x40(%rbp), %rcx
movq %rbx, %r8
movl -0x30(%rbp), %r9d
callq 0x38799
movl %eax, %r12d
jmp 0x387d4
movq -0x50(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x3921c
testq %rax, %rax
je 0x3890b
movq %rax, %r14
movl %r12d, %r13d
movzwl -0x2a(%rbp), %eax
movw %ax, 0x28(%r14)
movl %r15d, 0x4(%r14)
movq %rbx, 0x8(%r14)
movl -0x30(%rbp), %eax
movl %eax, (%r14)
cmpl $0x3ffff, %r15d         # imm = 0x3FFFF
ja 0x388c0
movq %r14, %rdi
addq $0x40, %rdi
movq -0x38(%rbp), %rsi
movl %r15d, %edx
callq 0x360ae
movq %r14, %rdi
callq 0x39277
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x387d4
movq %r14, %rdi
testl %r13d, %r13d
je 0x38935
movq -0x38(%rbp), %rsi
movl %r15d, %edx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x39433
testl %eax, %eax
je 0x38949
movl %eax, %r12d
movq %r14, %rdi
callq 0x39775
jmp 0x387d4
movq (%r13), %rax
movq (%rax), %rdi
leaq 0x3cfb4(%rip), %rsi     # 0x758cd
callq *0x80(%rax)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x387d4
movl $0xffffffb7, %r12d      # imm = 0xFFFFFFB7
jmp 0x387d4
movq -0x38(%rbp), %rsi
movl %r15d, %edx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
callq 0x3981b
movq %r14, %rdi
callq 0x3987a
xorl %r12d, %r12d
jmp 0x387d4