// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: lhRecordAppend
static int lhRecordAppend(
	lhcell *pCell,
	const void *pData,unqlite_int64 nByte
	)
{
	lhash_kv_engine *pEngine = pCell->pPage->pHash;
	const unsigned char *zPtr,*zEnd;
	lhpage *pPage = pCell->pPage;
	unsigned char *zRaw,*zRawEnd;
	unqlite_page *pOvfl,*pNew;
	sxu64 nDatalen;
	sxu32 nAvail;
	pgno iOvfl;
	int rc;
	if( pCell->nData + nByte < pCell->nData ){
		/* Overflow */
		pEngine->pIo->xErr(pEngine->pIo->pHandle,"Append operation will cause data overflow");
		return UNQLITE_LIMIT;
	}
	/* Acquire a writer lock on this page */
	rc = pEngine->pIo->xWrite(pPage->pRaw);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	if( pCell->iOvfl == 0 ){
		sxu16 iOfft = 0; /* cc warning */
		/* Local payload, check for a bigger place */
		rc = lhAllocateSpace(pPage,L_HASH_CELL_SZ + pCell->nKey + pCell->nData + nByte,&iOfft);
		if( rc != UNQLITE_OK ){
			/* Transfer the payload to an overflow page */
			rc = lhCellWriteOvflPayload(pCell,
				&pPage->pRaw->zData[pCell->iStart + L_HASH_CELL_SZ],pCell->nKey,
				(const void *)&pPage->pRaw->zData[pCell->iStart + L_HASH_CELL_SZ + pCell->nKey],pCell->nData,
				pData,nByte,
				(const void *)0);
			if( rc != UNQLITE_OK ){
				return rc;
			}
			/* Update the cell header */
			SyBigEndianPack64(&pPage->pRaw->zData[pCell->iStart + 4 /* Hash */ + 4 /* Key */],pCell->nData + nByte);
			/* Restore freespace */
			lhRestoreSpace(pPage,(sxu16)(pCell->iStart + L_HASH_CELL_SZ),(sxu16)(pCell->nKey + pCell->nData));
			/* New data size */
			pCell->nData += nByte;
		}else{
			sxu16 iOldOfft = pCell->iStart;
			sxu32 iOld = (sxu32)pCell->nData;
			SyBlob sWorker;
			SyBlobInit(&sWorker,&pEngine->sAllocator);
			/* Copy the old data */
			rc = SyBlobAppend(&sWorker,(const void *)&pPage->pRaw->zData[pCell->iStart + L_HASH_CELL_SZ + pCell->nKey],(sxu32)pCell->nData);
			if( rc == SXRET_OK ){
				/* Append the new data */
				rc = SyBlobAppend(&sWorker,pData,(sxu32)nByte);
			}
			if( rc != UNQLITE_OK ){
				SyBlobRelease(&sWorker);
				return rc;
			}
			/* Space is available, transfer the cell */
			lhMoveLocalCell(pCell,iOfft,SyBlobData(&sWorker),(unqlite_int64)SyBlobLength(&sWorker));
			/* Restore cell space */
			lhRestoreSpace(pPage,iOldOfft,(sxu16)(L_HASH_CELL_SZ + pCell->nKey + iOld));
			/* All done */
			SyBlobRelease(&sWorker);
		}
		return UNQLITE_OK;
	}
	/* Point to the overflow page which hold the data */
	rc = pEngine->pIo->xGet(pEngine->pIo->pHandle,pCell->iDataPage,&pOvfl);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Next overflow page in the chain */
	SyBigEndianUnpack64(pOvfl->zData,&iOvfl);
	/* Point to the end of the chunk */
	zRaw = &pOvfl->zData[pCell->iDataOfft];
	zRawEnd = &pOvfl->zData[pEngine->iPageSize];
	nDatalen = pCell->nData;
	nAvail = (sxu32)(zRawEnd - zRaw);
	for(;;){
		if( zRaw >= zRawEnd ){
			if( iOvfl == 0 ){
				/* Cant happen */
				pEngine->pIo->xErr(pEngine->pIo->pHandle,"Corrupt overflow page");
				return UNQLITE_CORRUPT;
			}
			rc = pEngine->pIo->xGet(pEngine->pIo->pHandle,iOvfl,&pNew);
			if( rc != UNQLITE_OK ){
				return rc;
			}
			/* Next overflow page on the chain */
			SyBigEndianUnpack64(pNew->zData,&iOvfl);
			/* Unref the previous overflow page */
			pEngine->pIo->xPageUnref(pOvfl);
			/* Point to the new chunk */
			zRaw = &pNew->zData[8];
			zRawEnd = &pNew->zData[pCell->pPage->pHash->iPageSize];
			nAvail = L_HASH_OVERFLOW_SIZE(pCell->pPage->pHash->iPageSize);
			pOvfl = pNew;
		}
		if( (sxu64)nAvail > nDatalen ){
			zRaw += nDatalen;
			break;
		}else{
			nDatalen -= nAvail;
		}
		zRaw += nAvail;
	}
	/* Start the append process */
	zPtr = (const unsigned char *)pData;
	zEnd = &zPtr[nByte];
	/* Acquire a writer lock */
	rc = pEngine->pIo->xWrite(pOvfl);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	for(;;){
		sxu32 nLen;
		if( zPtr >= zEnd ){
			break;
		}
		if( zRaw >= zRawEnd ){
			/* Acquire a new page */
			rc = lhAcquirePage(pEngine,&pNew);
			if( rc != UNQLITE_OK ){
				return rc;
			}
			rc = pEngine->pIo->xWrite(pNew);
			if( rc != UNQLITE_OK ){
				return rc;
			}
			/* Link */
			SyBigEndianPack64(pOvfl->zData,pNew->iPage);
			pEngine->pIo->xPageUnref(pOvfl);
			SyBigEndianPack64(pNew->zData,0); /* Next overflow page on the chain */
			pOvfl = pNew;
			zRaw = &pNew->zData[8];
			zRawEnd = &pNew->zData[pEngine->iPageSize];
		}
		nAvail = (sxu32)(zRawEnd-zRaw);
		nLen = (sxu32)(zEnd-zPtr);
		if( nLen > nAvail ){
			nLen = nAvail;
		}
		SyMemcpy((const void *)zPtr,(void *)zRaw,nLen);
		/* Synchronize pointers */
		zPtr += nLen;
		zRaw += nLen;
	}
	/* Unref the last overflow page */
	pEngine->pIo->xPageUnref(pOvfl);
	/* Finally, update the cell header */
	pCell->nData += nByte;
	SyBigEndianPack64(&pPage->pRaw->zData[pCell->iStart + 4 /* Hash */ + 4 /* Key */],pCell->nData);
	/* All done */
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x63165
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x80(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rdi
leaq 0x5ebff(%rip), %rsi     # 0xc1d56
callq *%rax
movl $0xfffffff9, -0x4(%rbp) # imm = 0xFFFFFFF9
jmp 0x6373f
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x6318e
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6373f
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x633c8
movw $0x0, -0x7e(%rbp)
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1a, %eax
movl %eax, %eax
movl %eax, %esi
movq -0x10(%rbp), %rax
addq 0x8(%rax), %rsi
addq -0x20(%rbp), %rsi
leaq -0x7e(%rbp), %rdx
callq 0x63d40
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x632cb
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
addl $0x1a, %eax
cltq
addq %rax, %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
addl $0x1a, %eax
movq -0x10(%rbp), %r8
addl 0x4(%r8), %eax
movl %eax, %eax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %r8
movq -0x18(%rbp), %r9
movq -0x20(%rbp), %rax
xorl %r10d, %r10d
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movb $0x0, %al
callq 0x643c0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x6325b
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6373f
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
addl $0x4, %eax
addl $0x4, %eax
cltq
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
addq -0x20(%rbp), %rsi
callq 0x61c60
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
addl $0x1a, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rdx
addq 0x8(%rdx), %rax
movzwl %cx, %esi
movzwl %ax, %edx
callq 0x66290
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
jmp 0x633bc
movq -0x10(%rbp), %rax
movw 0x28(%rax), %ax
movw %ax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0x84(%rbp)
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
leaq -0xa8(%rbp), %rdi
callq 0x58bb0
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
addl $0x1a, %eax
movq -0x10(%rbp), %rcx
addl 0x4(%rcx), %eax
movl %eax, %eax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq -0xa8(%rbp), %rdi
callq 0x5d9d0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jne 0x63353
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq -0xa8(%rbp), %rdi
callq 0x5d9d0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x63370
leaq -0xa8(%rbp), %rdi
callq 0x58c00
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6373f
movq -0x10(%rbp), %rdi
movw -0x7e(%rbp), %ax
movq -0xa0(%rbp), %rdx
movl -0x98(%rbp), %ecx
movzwl %ax, %esi
callq 0x66350
movq -0x40(%rbp), %rdi
movw -0x80(%rbp), %cx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1a, %eax
addl -0x84(%rbp), %eax
movzwl %cx, %esi
movzwl %ax, %edx
callq 0x66290
leaq -0xa8(%rbp), %rdi
callq 0x58c00
movl $0x0, -0x4(%rbp)
jmp 0x6373f
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rdi
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rsi
leaq -0x58(%rbp), %rdx
callq *%rax
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x633ff
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6373f
movq -0x58(%rbp), %rax
movq (%rax), %rdi
leaq -0x78(%rbp), %rsi
callq 0x61db0
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x38(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x110(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x6c(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jb 0x6353c
cmpq $0x0, -0x78(%rbp)
jne 0x6349a
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x80(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rdi
leaq 0x5e969(%rip), %rsi     # 0xc1df5
callq *%rax
movl $0xffffffe8, -0x4(%rbp) # imm = 0xFFFFFFE8
jmp 0x6373f
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rdi
movq -0x78(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq *%rax
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x634cd
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6373f
movq -0x60(%rbp), %rax
movq (%rax), %rdi
leaq -0x78(%rbp), %rsi
callq 0x61db0
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movq -0x60(%rbp), %rax
movq (%rax), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq (%rcx), %rcx
movslq 0x110(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
movl 0x110(%rax), %eax
subl $0x8, %eax
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x6c(%rbp), %eax
cmpq -0x68(%rbp), %rax
jbe 0x63553
movq -0x68(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x63578
movl -0x6c(%rbp), %eax
movl %eax, %ecx
movq -0x68(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x6c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x63458
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x635b1
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6373f
jmp 0x635b3
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0x635c2
jmp 0x636ec
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jb 0x6367b
movq -0x28(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x625b0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x635f1
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6373f
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x60(%rbp), %rdi
callq *%rax
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x63616
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6373f
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x61c60
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movq -0x60(%rbp), %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x61c60
movq -0x60(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x110(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x6c(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jbe 0x636ae
movl -0x6c(%rbp), %eax
movl %eax, -0xac(%rbp)
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0xac(%rbp), %edx
callq 0x5d570
movl -0xac(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl -0xac(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x635b3
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
addl $0x4, %eax
addl $0x4, %eax
cltq
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x61c60
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)