// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: lh_record_insert
static int lh_record_insert(
	  unqlite_kv_engine *pKv,         /* KV store */
	  const void *pKey,sxu32 nKeyLen, /* Payload: Key */
	  const void *pData,unqlite_int64 nDataLen, /* Payload: data */
	  int is_append /* True for an append operation */
	  )
{
	lhash_kv_engine *pEngine = (lhash_kv_engine *)pKv;
	lhash_bmap_rec *pRec;
	unqlite_page *pRaw;
	lhpage *pPage;
	lhcell *pCell;
	pgno iBucket;
	sxu32 nHash;
	int iCnt;
	int rc;

	/* Acquire the first page (DB hash Header) so that everything gets loaded autmatically */
	rc = pEngine->pIo->xGet(pEngine->pIo->pHandle,1,0);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	iCnt = 0;
	/* Compute the hash of the key first */
	nHash = pEngine->xHash(pKey,(sxu32)nKeyLen);
retry:
	/* Extract the logical bucket number */
	iBucket = nHash & (pEngine->nmax_split_nucket - 1);
	if( iBucket >= pEngine->split_bucket + pEngine->max_split_bucket ){
		/* Low mask */
		iBucket = nHash & (pEngine->max_split_bucket - 1);
	}
	/* Map the logical bucket number to real page number */
	pRec = lhMapFindBucket(pEngine,iBucket);
	if( pRec == 0 ){
		/* Request a new page */
		rc = lhAcquirePage(pEngine,&pRaw);
		if( rc != UNQLITE_OK ){
			return rc;
		}
		/* Initialize the page */
		pPage = lhNewPage(pEngine,pRaw,0);
		if( pPage == 0 ){
			return UNQLITE_NOMEM;
		}
		/* Mark as an empty page */
		rc = lhSetEmptyPage(pPage);
		if( rc != UNQLITE_OK ){
			pEngine->pIo->xPageUnref(pRaw); /* pPage will be released during this call */
			return rc;
		}
		/* Store the cell */
		rc = lhStoreCell(pPage,pKey,nKeyLen,pData,nDataLen,nHash,1);
		if( rc == UNQLITE_OK ){
			/* Install and write the logical map record */
			rc = lhMapWriteRecord(pEngine,iBucket,pRaw->iPage);
		}
		pEngine->pIo->xPageUnref(pRaw);
		return rc;
	}else{
		/* Load the page */
		rc = lhLoadPage(pEngine,pRec->iReal,0,&pPage,0);
		if( rc != UNQLITE_OK ){
			/* IO error, unlikely scenario */
			return rc;
		}
		/* Do not add this page to the hot dirty list */
		pEngine->pIo->xDontMkHot(pPage->pRaw);
		/* Lookup for the cell */
		pCell = lhFindCell(pPage,pKey,(sxu32)nKeyLen,nHash);
		if( pCell == 0 ){
			/* Create the record */
			rc = lhRecordInstall(pPage,nHash,pKey,nKeyLen,pData,nDataLen);
			if( rc == SXERR_RETRY && iCnt++ < 2 ){
				rc = UNQLITE_OK;
				goto retry;
			}
		}else{
			if( is_append ){
				/* Append operation */
				rc = lhRecordAppend(pCell,pData,nDataLen);
			}else{
				/* Overwrite old value */
				rc = lhRecordOverwrite(pCell,pData,nDataLen);
			}
		}
		pEngine->pIo->xPageUnref(pPage->pRaw);
	}
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rdi
movl $0x1, %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x622be
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x62512
movl $0x0, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq 0xc0(%rax), %rax
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq *%rax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x40(%rbp), %rcx
movq 0x130(%rcx), %rcx
subq $0x1, %rcx
andq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x40(%rbp), %rcx
movq 0x120(%rcx), %rcx
movq -0x40(%rbp), %rdx
addq 0x128(%rdx), %rcx
cmpq %rcx, %rax
jb 0x6232d
movl -0x6c(%rbp), %eax
movq -0x40(%rbp), %rcx
movq 0x128(%rcx), %rcx
subq $0x1, %rcx
andq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x62520
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x62420
movq -0x40(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x625b0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x6236a
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x62512
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x626e0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x62392
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x62512
movq -0x58(%rbp), %rdi
callq 0x627c0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x623c0
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rax
movq -0x50(%rbp), %rdi
callq *%rax
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x62512
movq -0x58(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movl -0x6c(%rbp), %r9d
movl $0x1, (%rsp)
callq 0x628e0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jne 0x62404
movq -0x40(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x62ae0
movl %eax, -0x74(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rax
movq -0x50(%rbp), %rdi
callq *%rax
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x62512
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq -0x58(%rbp), %rcx
xorl %r8d, %r8d
callq 0x62dc0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x62450
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x62512
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x40(%rax), %rax
movq -0x58(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movq -0x58(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x6c(%rbp), %ecx
callq 0x62f10
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x624c5
movq -0x58(%rbp), %rdi
movl -0x6c(%rbp), %esi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %r9
callq 0x63040
movl %eax, -0x74(%rbp)
cmpl $-0x21, -0x74(%rbp)
jne 0x624c3
movl -0x70(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x70(%rbp)
cmpl $0x2, %eax
jge 0x624c3
movl $0x0, -0x74(%rbp)
jmp 0x622dc
jmp 0x624f7
cmpl $0x0, -0x34(%rbp)
je 0x624e1
movq -0x60(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x630f0
movl %eax, -0x74(%rbp)
jmp 0x624f5
movq -0x60(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x63750
movl %eax, -0x74(%rbp)
jmp 0x624f7
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rax
movq -0x58(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop