// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: CollectionCacheInstallRecord
static int CollectionCacheInstallRecord(
	unqlite_col *pCol, /* Target collection */
	jx9_int64 nId,     /* Unique record ID */
	jx9_value *pValue  /* JSON value */
	)
{
	unqlite_col_record *pRecord;
	sxu32 iBucket;
	/* Fetch the record first */
	pRecord = CollectionCacheFetchRecord(pCol,nId);
	if( pRecord ){
		/* Record already installed, overwrite its old value  */
		jx9MemObjStore(pValue,&pRecord->sValue);
		return UNQLITE_OK;
	}
	/* Allocate a new instance */
	pRecord = (unqlite_col_record *)SyMemBackendPoolAlloc(&pCol->pVm->sAlloc,sizeof(unqlite_col_record));
	if( pRecord == 0 ){
		return UNQLITE_NOMEM;
	}
	/* Zero the structure */
	SyZero(pRecord,sizeof(unqlite_col_record));
	/* Fill in the structure */
	jx9MemObjInit(pCol->pVm->pJx9Vm,&pRecord->sValue);
	jx9MemObjStore(pValue,&pRecord->sValue);
	pRecord->nId = nId;
	pRecord->pCol = pCol;
	/* Install in the corresponding bucket */
	iBucket = COL_RECORD_HASH(nId) & (pCol->nRecSize - 1);
	pRecord->pNextCol = pCol->apRecord[iBucket];
	if( pCol->apRecord[iBucket] ){
		pCol->apRecord[iBucket]->pPrevCol = pRecord;
	}
	pCol->apRecord[iBucket] = pRecord;
	/* Link */
	MACRO_LD_PUSH(pCol->pList,pRecord);
	pCol->nRec++;
	if( (pCol->nRec >= pCol->nRecSize * 3) && pCol->nRec < 100000 ){
		/* Allocate a new larger table */
		sxu32 nNewSize = pCol->nRecSize << 1;
		unqlite_col_record *pEntry;
		unqlite_col_record **apNew;
		sxu32 n;
		
		apNew = (unqlite_col_record **)SyMemBackendAlloc(&pCol->pVm->sAlloc, nNewSize * sizeof(unqlite_col_record *));
		if( apNew ){
			/* Zero the new table */
			SyZero((void *)apNew, nNewSize * sizeof(unqlite_col_record *));
			/* Rehash all entries */
			n = 0;
			pEntry = pCol->pList;
			for(;;){
				/* Loop one */
				if( n >= pCol->nRec ){
					break;
				}
				pEntry->pNextCol = pEntry->pPrevCol = 0;
				/* Install in the new bucket */
				iBucket = COL_RECORD_HASH(pEntry->nId) & (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(&pCol->pVm->sAlloc,(void *)pCol->apRecord);
			pCol->apRecord = apNew;
			pCol->nRecSize = nNewSize;
		}
	}
	/* All done */
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x0, 0xd8(%rdi)
je 0x406b8
movq 0xc8(%rbx), %rax
movl 0xdc(%rbx), %ecx
decl %ecx
movl %r14d, %edx
andl %ecx, %edx
movq (%rax,%rdx,8), %rsi
testq %rsi, %rsi
je 0x406b8
cmpq %r14, 0x8(%rsi)
je 0x40881
movq 0x50(%rsi), %rsi
jmp 0x406a3
movq (%rbx), %rdi
addq $0x8, %rdi
movl $0x70, %esi
callq 0x2e481
testq %rax, %rax
je 0x40872
movq %rax, %r12
movq $-0x4, %rax
movl $0x0, 0x4(%r12,%rax)
addq $0x4, %rax
cmpq $0x6c, %rax
jb 0x406dc
movq (%rbx), %rax
movq 0xe0(%rax), %rax
movq %r12, %rsi
addq $0x10, %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r12)
movups %xmm0, 0x10(%r12)
movups %xmm0, 0x40(%r12)
movq %rax, 0x20(%r12)
movups %xmm0, 0x30(%r12)
movq %rax, 0x28(%r12)
movl $0x20, 0x18(%r12)
movq %r15, %rdi
callq 0x3e19f
movq %r14, 0x8(%r12)
movq %rbx, (%r12)
movl 0xdc(%rbx), %r13d
leal -0x1(%r13), %eax
andl %eax, %r14d
movq 0xc8(%rbx), %rax
movq (%rax,%r14,8), %rcx
movq %rcx, 0x50(%r12)
testq %rcx, %rcx
je 0x40766
movq %r12, 0x58(%rcx)
movq %r12, (%rax,%r14,8)
movq 0xd0(%rbx), %rax
testq %rax, %rax
je 0x4077f
movq %rax, 0x60(%r12)
movq %r12, 0x68(%rax)
movq %r12, 0xd0(%rbx)
movl 0xd8(%rbx), %eax
incl %eax
movl %eax, 0xd8(%rbx)
leal (%r13,%r13,2), %ecx
cmpl %ecx, %eax
setb %cl
cmpl $0x186a0, %eax          # imm = 0x186A0
setae %al
orb %cl, %al
jne 0x40872
movq (%rbx), %rdi
addq $0x8, %rdi
movl %r13d, %r15d
shll $0x4, %r15d
movl %r15d, %esi
callq 0x33d86
testq %rax, %rax
je 0x40872
movq %rax, %r14
testl %r15d, %r15d
je 0x4080d
movl %r15d, %ecx
movq %r14, %rax
addq %rcx, %rax
movq %r14, %rcx
movb $0x0, (%rcx)
incq %rcx
cmpq %rax, %rcx
jae 0x4080d
movb $0x0, (%rcx)
incq %rcx
cmpq %rax, %rcx
jae 0x4080d
movb $0x0, (%rcx)
incq %rcx
cmpq %rax, %rcx
jae 0x4080d
movb $0x0, (%rcx)
incq %rcx
cmpq %rax, %rcx
jb 0x407e1
addl %r13d, %r13d
movl 0xd8(%rbx), %eax
testl %eax, %eax
je 0x40851
leaq 0xd0(%rbx), %rcx
leal -0x1(%r13), %edx
xorps %xmm0, %xmm0
movq (%rcx), %rcx
movups %xmm0, 0x50(%rcx)
movl 0x8(%rcx), %esi
andl %edx, %esi
movq (%r14,%rsi,8), %rdi
movq %rdi, 0x50(%rcx)
testq %rdi, %rdi
je 0x40845
movq %rcx, 0x58(%rdi)
movq %rcx, (%r14,%rsi,8)
addq $0x60, %rcx
decl %eax
jne 0x40828
movq (%rbx), %rdi
movq 0xc8(%rbx), %rsi
addq $0x8, %rdi
callq 0x3490d
movq %r14, 0xc8(%rbx)
movl %r13d, 0xdc(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x10, %rsi
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3e19f