// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x72460
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x72539
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x10, %rsi
callq 0x6e750
movl $0x0, -0x4(%rbp)
jmp 0x72804
movq -0x10(%rbp), %rax
movq (%rax), %rdi
addq $0x8, %rdi
movl $0x70, %esi
callq 0x541f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x72565
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x72804
movq -0x28(%rbp), %rdi
movl $0x70, %esi
callq 0x54280
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0xe0(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x10, %rsi
callq 0x6da90
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x10, %rsi
callq 0x6e750
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0xdc(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
andq %rcx, %rax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rax
movl -0x2c(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x72618
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rax
movl -0x2c(%rbp), %edx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x58(%rax)
movq -0x28(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rax
movl -0x2c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
cmpq $0x0, 0xd0(%rax)
jne 0x7264d
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xd0(%rax)
jmp 0x72682
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rax
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xd0(%rax)
movq -0x10(%rbp), %rax
movl 0xd8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xd8(%rax)
movq -0x10(%rbp), %rax
movl 0xd8(%rax), %eax
movq -0x10(%rbp), %rcx
imull $0x3, 0xdc(%rcx), %ecx
cmpl %ecx, %eax
jb 0x727fd
movq -0x10(%rbp), %rax
cmpl $0x186a0, 0xd8(%rax)    # imm = 0x186A0
jae 0x727fd
movq -0x10(%rbp), %rax
movl 0xdc(%rax), %eax
shll %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
addq $0x8, %rdi
movl -0x30(%rbp), %eax
shlq $0x3, %rax
movl %eax, %esi
callq 0x5bf70
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x727fb
movq -0x40(%rbp), %rdi
movl -0x30(%rbp), %eax
shlq $0x3, %rax
movl %eax, %esi
callq 0x54280
movl $0x0, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xd8(%rcx), %eax
jb 0x72739
jmp 0x727c4
movq -0x38(%rbp), %rax
movq $0x0, 0x58(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x30(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
andq %rcx, %rax
movl %eax, -0x2c(%rbp)
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x7279b
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %edx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x58(%rax)
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x72725
movq -0x10(%rbp), %rax
movq (%rax), %rdi
addq $0x8, %rdi
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rsi
callq 0x5d6b0
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xc8(%rax)
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xdc(%rax)
jmp 0x727fd
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)