// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: lhRecordOverwrite
static int lhRecordOverwrite(
	lhcell *pCell,
	const void *pData,unqlite_int64 nByte
	)
{
	lhash_kv_engine *pEngine = pCell->pPage->pHash;
	unsigned char *zRaw,*zRawEnd,*zPayload;
	const unsigned char *zPtr,*zEnd;
	unqlite_page *pOvfl,*pOld,*pNew;
	lhpage *pPage = pCell->pPage;
	sxu32 nAvail;
	pgno iOvfl;
	int rc;
	/* Acquire a writer lock on this page */
	rc = pEngine->pIo->xWrite(pPage->pRaw);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	if( pCell->iOvfl == 0 ){
		/* Local payload, try to deal with the free space issues */
		zPayload = &pPage->pRaw->zData[pCell->iStart + L_HASH_CELL_SZ + pCell->nKey];
		if( pCell->nData == (sxu64)nByte ){
			/* Best scenario, simply a memcpy operation */
			SyMemcpy(pData,(void *)zPayload,(sxu32)nByte);
		}else if( (sxu64)nByte < pCell->nData ){
			/* Shorter data, not so ugly */
			SyMemcpy(pData,(void *)zPayload,(sxu32)nByte);
			/* Update the cell header */
			SyBigEndianPack64(&pPage->pRaw->zData[pCell->iStart + 4 /* Hash */ + 4 /* Key */],nByte);
			/* Restore freespace */
			lhRestoreSpace(pPage,(sxu16)(pCell->iStart + L_HASH_CELL_SZ + pCell->nKey + nByte),(sxu16)(pCell->nData - nByte));
			/* New data size */
			pCell->nData = (sxu64)nByte;
		}else{
			sxu16 iOfft = 0; /* cc warning */
			/* Check if another chunk is available for this cell */
			rc = lhAllocateSpace(pPage,L_HASH_CELL_SZ + pCell->nKey + 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,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 */],(sxu64)nByte);
				/* Restore freespace */
				lhRestoreSpace(pPage,(sxu16)(pCell->iStart + L_HASH_CELL_SZ),(sxu16)(pCell->nKey + pCell->nData));
				/* New data size */
				pCell->nData = (sxu64)nByte;
			}else{
				sxu16 iOldOfft = pCell->iStart;
				sxu32 iOld = (sxu32)pCell->nData;
				/* Space is available, transfer the cell */
				lhMoveLocalCell(pCell,iOfft,pData,nByte);
				/* Restore cell space */
				lhRestoreSpace(pPage,iOldOfft,(sxu16)(L_HASH_CELL_SZ + pCell->nKey + iOld));
			}
		}
		return UNQLITE_OK;
	}
	/* Point to the overflow page */
	rc = pEngine->pIo->xGet(pEngine->pIo->pHandle,pCell->iDataPage,&pOvfl);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Relase all old overflow pages first */
	SyBigEndianUnpack64(pOvfl->zData,&iOvfl);
	pOld = pOvfl;
	for(;;){
		if( iOvfl == 0 ){
			/* No more overflow pages on the chain */
			break;
		}
		/* Point to the target page */
		if( UNQLITE_OK != pEngine->pIo->xGet(pEngine->pIo->pHandle,iOvfl,&pOld) ){
			/* Not so fatal if something goes wrong here */
			break;
		}
		/* Next overflow page to be released */
		SyBigEndianUnpack64(pOld->zData,&iOvfl);
		if( pOld != pOvfl ){ /* xx: chm is maniac */
			/* Restore the page to the free list */
			lhRestorePage(pEngine,pOld);
			/* Unref */
			pEngine->pIo->xPageUnref(pOld);
		}
	}
	/* Point to the data offset */
	zRaw = &pOvfl->zData[pCell->iDataOfft];
	zRawEnd = &pOvfl->zData[pEngine->iPageSize];
	/* The data to be stored */
	zPtr = (const unsigned char *)pData;
	zEnd = &zPtr[nByte];
	/* Start the overwrite process */
	/* Acquire a writer lock */
	rc = pEngine->pIo->xWrite(pOvfl);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	SyBigEndianPack64(pOvfl->zData,0);
	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 = (sxu64)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 $0x90, %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, -0x70(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x70(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x637b4
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x63ce5
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x63a06
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x28(%rcx), %ecx
addl $0x1a, %ecx
movq -0x10(%rbp), %rdx
addl 0x4(%rdx), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x20(%rbp), %rax
jne 0x6380f
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rax
movl %eax, %edx
callq 0x5d570
jmp 0x639fa
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x638a2
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rax
movl %eax, %edx
callq 0x5d570
movq -0x70(%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 -0x20(%rbp), %rsi
callq 0x61c60
movq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
addl $0x1a, %eax
movq -0x10(%rbp), %rcx
addl 0x4(%rcx), %eax
movl %eax, %eax
addq -0x20(%rbp), %rax
movw %ax, %cx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x20(%rbp), %rax
movzwl %cx, %esi
movzwl %ax, %edx
callq 0x66290
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x639f8
movw $0x0, -0x86(%rbp)
movq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1a, %eax
movl %eax, %eax
movl %eax, %esi
addq -0x20(%rbp), %rsi
leaq -0x86(%rbp), %rdx
callq 0x63d40
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x63998
movq -0x10(%rbp), %rdi
movq -0x70(%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 -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
movb $0x0, %al
callq 0x643c0
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x63937
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x63ce5
movq -0x70(%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 -0x20(%rbp), %rsi
callq 0x61c60
movq -0x70(%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
movq %rcx, 0x8(%rax)
jmp 0x639f6
movq -0x10(%rbp), %rax
movw 0x28(%rax), %ax
movw %ax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0x8c(%rbp)
movq -0x10(%rbp), %rdi
movw -0x86(%rbp), %ax
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movzwl %ax, %esi
callq 0x66350
movq -0x70(%rbp), %rdi
movw -0x88(%rbp), %cx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1a, %eax
addl -0x8c(%rbp), %eax
movzwl %cx, %esi
movzwl %ax, %edx
callq 0x66290
jmp 0x639f8
jmp 0x639fa
movl $0x0, -0x4(%rbp)
jmp 0x63ce5
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, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x63a46
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x63ce5
movq -0x58(%rbp), %rax
movq (%rax), %rdi
leaq -0x80(%rbp), %rsi
callq 0x61db0
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x63a67
jmp 0x63aca
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rdi
movq -0x80(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq *%rax
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x63a90
jmp 0x63aca
movq -0x60(%rbp), %rax
movq (%rax), %rdi
leaq -0x80(%rbp), %rsi
callq 0x61db0
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0x63ac8
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x66460
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rax
movq -0x60(%rbp), %rdi
callq *%rax
jmp 0x63a5e
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x38(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x110(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x63b3b
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x63ce5
movq -0x58(%rbp), %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x61c60
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jb 0x63b5a
jmp 0x63c96
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0x63c25
movq -0x28(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x625b0
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x63b92
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x63ce5
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x68(%rbp), %rdi
callq *%rax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x63bc0
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x63ce5
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movq -0x68(%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 -0x68(%rbp), %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x61c60
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x68(%rbp), %rax
movq (%rax), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x110(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x74(%rbp)
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x74(%rbp), %eax
jbe 0x63c58
movl -0x74(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x90(%rbp), %edx
callq 0x5d570
movl -0x90(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl -0x90(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0x63b4b
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
movq %rcx, 0x8(%rax)
movq -0x70(%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 $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)