// File: /workspace/github_c_makefile_star2_O3/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 0x37fb4
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, -0x30(%rbp)
leal 0x1a(%r15), %esi
movq -0x48(%rbp), %r12
addq %r12, %rsi
leaq -0x2a(%rbp), %rdx
movq %r14, %rdi
callq 0x38687
movl %eax, %ebx
testl %eax, %eax
je 0x3803c
leaq -0x2a(%rbp), %rdx
movl $0x1a, %esi
movq %r14, %rdi
callq 0x38687
testl %eax, %eax
je 0x3803c
cmpl $0x0, 0x10(%rbp)
je 0x380ef
movl %r15d, %edx
addq %r12, %rdx
movq (%r14), %rdi
movq 0x40(%r14), %rsi
leaq -0x50(%rbp), %r8
xorl %ecx, %ecx
callq 0x38fc0
movq %r12, %r8
movl %eax, %r12d
testl %eax, %eax
jne 0x37fa2
movq -0x50(%rbp), %rdi
movl $0x1, (%rsp)
movq -0x38(%rbp), %rsi
movl %r15d, %edx
movq -0x40(%rbp), %rcx
movl -0x30(%rbp), %r9d
callq 0x37f67
movl %eax, %r12d
jmp 0x37fa2
movq %r13, %rdi
movq %r14, %rsi
callq 0x38981
testq %rax, %rax
je 0x380d0
movq %rax, %r14
movzwl -0x2a(%rbp), %eax
movw %ax, 0x28(%r14)
movl %r15d, 0x4(%r14)
movq %r12, 0x8(%r14)
movl -0x30(%rbp), %eax
movl %eax, (%r14)
cmpl $0x3ffff, %r15d         # imm = 0x3FFFF
ja 0x38086
movq %r14, %rdi
addq $0x40, %rdi
movq -0x38(%rbp), %rsi
movl %r15d, %edx
callq 0x359ad
movq %r14, %rdi
callq 0x389d8
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x37fa2
testl %ebx, %ebx
je 0x380fa
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movl %r15d, %edx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x38b94
testl %eax, %eax
je 0x3813c
movl %eax, %r12d
movq %r14, %rdi
callq 0x38ebd
jmp 0x37fa2
movq (%r13), %rax
movq (%rax), %rdi
leaq 0x3b7f7(%rip), %rsi     # 0x738d5
callq *0x80(%rax)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x37fa2
movl $0xffffffb7, %r12d      # imm = 0xFFFFFFB7
jmp 0x37fa2
movq 0x20(%r14), %rax
movzwl 0x28(%r14), %ecx
movq 0x8(%rax), %rax
movq (%rax), %rax
leaq (%rax,%rcx), %r12
addq $0x1a, %r12
movq -0x38(%rbp), %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x34893
movq -0x48(%rbp), %rdx
testq %rdx, %rdx
jle 0x3813c
movl %r15d, %eax
addq %rax, %r12
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x34893
movq %r14, %rdi
callq 0x38f5b
xorl %r12d, %r12d
jmp 0x37fa2