// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: lhMapInstallBucket
static int lhMapInstallBucket(lhash_kv_engine *pEngine,pgno iLogic,pgno iReal)
{
	lhash_bmap_rec *pRec;
	sxu32 iBucket;
	/* Allocate a new instance */
	pRec = (lhash_bmap_rec *)SyMemBackendPoolAlloc(&pEngine->sAllocator,sizeof(lhash_bmap_rec));
	if( pRec == 0 ){
		return UNQLITE_NOMEM;
	}
	/* Zero the structure */
	SyZero(pRec,sizeof(lhash_bmap_rec));
	/* Fill in the structure */
	pRec->iLogic = iLogic;
	pRec->iReal = iReal;
	iBucket = iLogic & (pEngine->nBuckSize - 1);
	pRec->pNextCol = pEngine->apMap[iBucket];
	if( pEngine->apMap[iBucket] ){
		pEngine->apMap[iBucket]->pPrevCol = pRec;
	}
	pEngine->apMap[iBucket] = pRec;
	/* Link */
	if( pEngine->pFirst == 0 ){
		pEngine->pFirst = pEngine->pList = pRec;
	}else{
		MACRO_LD_PUSH(pEngine->pList,pRec);
	}
	pEngine->nBuckRec++;
	if( (pEngine->nBuckRec >= pEngine->nBuckSize * 3) && pEngine->nBuckRec < 100000 ){
		/* Allocate a new larger table */
		sxu32 nNewSize = pEngine->nBuckSize << 1;
		lhash_bmap_rec *pEntry;
		lhash_bmap_rec **apNew;
		sxu32 n;
		
		apNew = (lhash_bmap_rec **)SyMemBackendAlloc(&pEngine->sAllocator, nNewSize * sizeof(lhash_bmap_rec *));
		if( apNew ){
			/* Zero the new table */
			SyZero((void *)apNew, nNewSize * sizeof(lhash_bmap_rec *));
			/* Rehash all entries */
			n = 0;
			pEntry = pEngine->pList;
			for(;;){
				/* Loop one */
				if( n >= pEngine->nBuckRec ){
					break;
				}
				pEntry->pNextCol = pEntry->pPrevCol = 0;
				/* Install in the new bucket */
				iBucket = pEntry->iLogic & (nNewSize - 1);
				pEntry->pNextCol = apNew[iBucket];
				if( apNew[iBucket] ){
					apNew[iBucket]->pPrevCol = pEntry;
				}
				apNew[iBucket] = pEntry;
				/* Point to the next entry */
				pEntry = pEntry->pNext;
				n++;
			}
			/* Release the old table and reflect the change */
			SyMemBackendFree(&pEngine->sAllocator,(void *)pEngine->apMap);
			pEngine->apMap = apNew;
			pEngine->nBuckSize  = nNewSize;
		}
	}
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x8(%rdi), %r14
pushq $0x30
popq %rsi
movq %r14, %rdi
callq 0x279f7
testq %rax, %rax
je 0x2f7e5
movq %rax, %r15
movq %r14, -0x30(%rbp)
pushq $0x30
popq %rsi
movq %rax, %rdi
callq 0x27aea
movq %r12, (%r15)
movq %r13, 0x8(%r15)
movl 0xe4(%rbx), %r14d
leal -0x1(%r14), %eax
andl %eax, %r12d
movq 0xd8(%rbx), %rax
movq (%rax,%r12,8), %rcx
movq %rcx, 0x20(%r15)
testq %rcx, %rcx
je 0x2f7be
movq %r15, 0x28(%rcx)
movq %r15, (%rax,%r12,8)
leaq 0xe8(%rbx), %rax
cmpq $0x0, 0xf0(%rbx)
je 0x2f7ee
movq (%rax), %rcx
testq %rcx, %rcx
je 0x2f7fb
movq %rcx, 0x10(%r15)
addq $0x18, %rcx
jmp 0x2f7f8
pushq $-0x1
popq %r15
jmp 0x2f8ae
movq %rax, %rcx
leaq 0xf0(%rbx), %rax
movq %r15, (%rcx)
movq %r15, (%rax)
movl 0xe0(%rbx), %eax
incl %eax
movl %eax, 0xe0(%rbx)
leal (%r14,%r14,2), %ecx
cmpl %ecx, %eax
setb %cl
cmpl $0x186a0, %eax          # imm = 0x186A0
setae %al
xorl %r15d, %r15d
orb %cl, %al
jne 0x2f8ae
movl %r14d, %r13d
shll $0x4, %r13d
movq -0x30(%rbp), %rdi
movl %r13d, %esi
callq 0x2ca19
testq %rax, %rax
je 0x2f8ae
movq %rax, %r12
addl %r14d, %r14d
movq %rax, %rdi
movl %r13d, %esi
callq 0x27aea
leaq 0xe8(%rbx), %rax
movl 0xe0(%rbx), %ecx
leal -0x1(%r14), %edx
xorps %xmm0, %xmm0
subl $0x1, %ecx
jb 0x2f890
movq (%rax), %rax
movups %xmm0, 0x20(%rax)
movl (%rax), %esi
andl %edx, %esi
movq (%r12,%rsi,8), %rdi
movq %rdi, 0x20(%rax)
testq %rdi, %rdi
je 0x2f886
movq %rax, 0x28(%rdi)
movq %rax, (%r12,%rsi,8)
addq $0x10, %rax
jmp 0x2f865
movq 0xd8(%rbx), %rsi
movq -0x30(%rbp), %rdi
callq 0x2d420
movq %r12, 0xd8(%rbx)
movl %r14d, 0xe4(%rbx)
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq