// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x8(%rdi), %r13
movq %r13, %rdi
movl $0x30, %esi
callq 0x2e481
testq %rax, %rax
je 0x370e3
movq $-0x4, %rcx
movl $0x0, 0x4(%rax,%rcx)
addq $0x4, %rcx
cmpq $0x2c, %rcx
jb 0x3707d
movq %r15, (%rax)
movq %r12, 0x8(%rax)
movl 0xe4(%rbx), %r14d
leal -0x1(%r14), %ecx
andl %ecx, %r15d
movq 0xd8(%rbx), %rcx
movq (%rcx,%r15,8), %rdx
movq %rdx, 0x20(%rax)
testq %rdx, %rdx
je 0x370bc
movq %rax, 0x28(%rdx)
movq %rax, (%rcx,%r15,8)
leaq 0xe8(%rbx), %rcx
cmpq $0x0, 0xf0(%rbx)
je 0x370ee
movq (%rcx), %rdx
testq %rdx, %rdx
je 0x370fb
movq %rdx, 0x10(%rax)
addq $0x18, %rdx
jmp 0x370f8
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x371e8
movq %rcx, %rdx
leaq 0xf0(%rbx), %rcx
movq %rax, (%rdx)
movq %rax, (%rcx)
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 0x371e8
movq %r13, %rdi
movl %r14d, %r13d
shll $0x4, %r13d
movq %rdi, -0x30(%rbp)
movl %r13d, %esi
callq 0x33d86
testq %rax, %rax
je 0x371e8
movq %rax, %r12
testl %r13d, %r13d
je 0x37187
movl %r13d, %ecx
movq %r12, %rax
addq %rcx, %rax
movq %r12, %rcx
movb $0x0, (%rcx)
incq %rcx
cmpq %rax, %rcx
jae 0x37187
movb $0x0, (%rcx)
incq %rcx
cmpq %rax, %rcx
jae 0x37187
movb $0x0, (%rcx)
incq %rcx
cmpq %rax, %rcx
jae 0x37187
movb $0x0, (%rcx)
incq %rcx
cmpq %rax, %rcx
jb 0x3715b
addl %r14d, %r14d
movl 0xe0(%rbx), %eax
testl %eax, %eax
movq -0x30(%rbp), %rdi
je 0x371ce
leaq 0xe8(%rbx), %rcx
leal -0x1(%r14), %edx
xorps %xmm0, %xmm0
movq (%rcx), %rcx
movups %xmm0, 0x20(%rcx)
movl (%rcx), %esi
andl %edx, %esi
movq (%r12,%rsi,8), %r8
movq %r8, 0x20(%rcx)
testq %r8, %r8
je 0x371c2
movq %rcx, 0x28(%r8)
movq %rcx, (%r12,%rsi,8)
addq $0x10, %rcx
decl %eax
jne 0x371a6
movq 0xd8(%rbx), %rsi
callq 0x3490d
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