// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: unqliteVmLoadCollection
static int unqliteVmLoadCollection(
	unqlite_vm *pVm,    /* Target VM */
	const char *zName,  /* Collection name */
	sxu32 nByte,        /* zName length */
	int iFlag,          /* Control flag */
	unqlite_col **ppOut /* OUT: in-memory collection */
	)
{
	unqlite_kv_methods *pMethods;
	unqlite_kv_engine *pEngine;
	unqlite_kv_cursor *pCursor;
	unqlite *pDb = pVm->pDb;
	unqlite_col *pCol = 0; /* cc warning */
	int rc = SXERR_MEM;
	char *zDup = 0;
	/* Point to the underlying KV store */
	pEngine = unqlitePagerGetKvEngine(pVm->pDb);
	pMethods = pEngine->pIo->pMethods;
	/* Allocate a new cursor */
	rc = unqliteInitCursor(pDb,&pCursor);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	if( (iFlag & UNQLITE_VM_COLLECTION_CREATE) == 0 ){
		/* Seek to the desired location */
		rc = pMethods->xSeek(pCursor,(const void *)zName,(unqlite_int64)nByte,UNQLITE_CURSOR_MATCH_EXACT);
		if( rc != UNQLITE_OK ){
			unqliteGenErrorFormat(pDb,"Collection '%.*s' not defined in the underlying database",nByte,zName);
			unqliteReleaseCursor(pDb,pCursor);
			return rc;
		}
	}
	/* Allocate a new instance */
	pCol = (unqlite_col *)SyMemBackendPoolAlloc(&pVm->sAlloc,sizeof(unqlite_col));
	if( pCol == 0 ){
		unqliteGenOutofMem(pDb);
		rc = UNQLITE_NOMEM;
		goto fail;
	}
	SyZero(pCol,sizeof(unqlite_col));
	/* Fill in the structure */
	SyBlobInit(&pCol->sWorker,&pVm->sAlloc);
	SyBlobInit(&pCol->sHeader,&pVm->sAlloc);
	pCol->pVm = pVm;
	pCol->pCursor = pCursor;
	/* Duplicate collection name */
	zDup = SyMemBackendStrDup(&pVm->sAlloc,zName,nByte);
	if( zDup == 0 ){
		unqliteGenOutofMem(pDb);
		rc = UNQLITE_NOMEM;
		goto fail;
	}
	pCol->nRecSize = 64; /* Must be a power of two */
	pCol->apRecord = (unqlite_col_record **)SyMemBackendAlloc(&pVm->sAlloc,pCol->nRecSize * sizeof(unqlite_col_record *));
	if( pCol->apRecord == 0 ){
		unqliteGenOutofMem(pDb);
		rc = UNQLITE_NOMEM;
		goto fail;
	}
	/* Zero the table */
	SyZero((void *)pCol->apRecord,pCol->nRecSize * sizeof(unqlite_col_record *));
	SyStringInitFromBuf(&pCol->sName,zDup,nByte);
	jx9MemObjInit(pVm->pJx9Vm,&pCol->sSchema);
	if( iFlag & UNQLITE_VM_COLLECTION_CREATE ){
		/* Create a new collection */
		if( pMethods->xReplace == 0 ){
			/* Read-only KV engine: Generate an error message and return */
			unqliteGenErrorFormat(pDb,
				"Cannot create new collection '%z' due to a read-only Key/Value storage engine",
				&pCol->sName
			);
			rc = UNQLITE_ABORT; /* Abort VM execution */
			goto fail;
		}
		/* Write the collection header */
		rc = CollectionSetHeader(pEngine,pCol,0,0,0);
		if( rc != UNQLITE_OK ){
			rc = UNQLITE_ABORT; /* Abort VM execution */
			goto fail;
		}
	}else{
		/* Read the collection header */
		rc = CollectionLoadHeader(pCol);
		if( rc != UNQLITE_OK ){
			unqliteGenErrorFormat(pDb,"Corrupt collection '%z' header",&pCol->sName);
			goto fail;
		}
	}
	/* Finally install the collection */
	unqliteVmInstallCollection(pVm,pCol);
	/* All done */
	if( ppOut ){
		*ppOut = pCol;
	}
	return UNQLITE_OK;
fail:
	unqliteReleaseCursor(pDb,pCursor);
	if( zDup ){
		SyMemBackendFree(&pVm->sAlloc,zDup);
	}
	if( pCol ){
		if( pCol->apRecord ){
			SyMemBackendFree(&pVm->sAlloc,(void *)pCol->apRecord);
		}
		SyBlobRelease(&pCol->sHeader);
		SyBlobRelease(&pCol->sWorker);
		jx9MemObjRelease(&pCol->sSchema);
		SyMemBackendPoolFree(&pVm->sAlloc,pCol);
	}
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, -0x70(%rbp)
movl %ecx, %r12d
movl %edx, -0x38(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdi, %r13
movq (%rdi), %r15
movq 0xd8(%r15), %rax
movq 0x10(%rax), %rbx
movq (%rbx), %rax
movq 0x8(%rax), %r14
leaq -0x50(%rbp), %rsi
movq %r15, %rdi
callq 0x2af93
testl %eax, %eax
jne 0x35570
movq %r14, -0x60(%rbp)
movq %rbx, -0x68(%rbp)
movq %r13, -0x48(%rbp)
testb $0x1, %r12b
movl %r12d, %ebx
jne 0x35249
movq -0x50(%rbp), %r14
pushq $0x1
popq %rcx
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movl -0x38(%rbp), %edx
movq -0x60(%rbp), %rax
callq *0x50(%rax)
testl %eax, %eax
je 0x35249
leaq 0x2ed50(%rip), %rsi     # 0x63f74
movq %r15, %rdi
movl -0x38(%rbp), %edx
movl %eax, %ebx
movq -0x30(%rbp), %rcx
xorl %eax, %eax
callq 0x32836
movq %r15, %rdi
movq %r14, %rsi
callq 0x2b0d0
movl %ebx, %eax
jmp 0x35570
movq -0x48(%rbp), %r13
leaq 0x8(%r13), %r14
movq %r14, %rdi
movl $0x140, %esi            # imm = 0x140
callq 0x279f7
movq %rax, %r12
testq %rax, %rax
je 0x3544d
movq %r12, %rdi
movl $0x140, %esi            # imm = 0x140
callq 0x27aea
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%r12)
movq %r14, 0x68(%r12)
andl $0x0, 0x80(%r12)
movups %xmm0, 0x90(%r12)
movq %r14, 0x88(%r12)
andl $0x0, 0xa0(%r12)
movq %r13, (%r12)
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq %rax, 0x118(%r12)
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movl -0x38(%rbp), %edx
callq 0x35732
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x3546b
movl $0x40, 0xdc(%r12)
movq %r14, %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x2ca19
movq %rax, 0xc8(%r12)
testq %rax, %rax
movq %r15, %r13
je 0x35489
movl 0xdc(%r12), %esi
shll $0x3, %esi
movq %rax, %rdi
callq 0x27aea
leaq 0x8(%r12), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq %rax, 0x8(%r12)
movl -0x38(%rbp), %eax
movl %eax, 0x10(%r12)
movq -0x48(%rbp), %rax
movq 0xe0(%rax), %rdi
leaq 0x20(%r12), %r15
movq %r15, %rsi
callq 0x35772
testb $0x1, %bl
jne 0x35498
movq %r14, %rbx
leaq 0x88(%r12), %r14
movq %r14, %rdi
callq 0x31b1e
movq %r12, -0x38(%rbp)
movq 0x118(%r12), %rdi
movq (%rdi), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
leaq -0xa7fa(%rip), %rsi     # 0x2ab89
movq %r14, %rdx
callq *0xa0(%rax)
testl %eax, %eax
jne 0x354cb
movq -0x38(%rbp), %rcx
movl 0x98(%rcx), %edx
pushq $-0x18
popq %rax
cmpl $0x16, %edx
jb 0x354cb
addq $0x90, %rcx
movq (%rcx), %rsi
movzwl (%rsi), %ecx
rolw $0x8, %cx
cmpw $0x611e, %cx            # imm = 0x611E
jne 0x354cb
movq %rsi, %r13
leaq 0x2(%rsi), %rdi
movq -0x38(%rbp), %r12
movl %edx, -0x30(%rbp)
leaq 0xa8(%r12), %rsi
callq 0x2f685
leaq 0xa(%r13), %rdi
leaq 0xb8(%r12), %rsi
callq 0x2f685
leaq 0x12(%r13), %rdi
leaq -0x74(%rbp), %r14
movq %r14, %rsi
callq 0x2f679
movl (%r14), %edi
movq %r12, %rsi
addq $0xe0, %rsi
callq 0x33d2b
movl -0x30(%rbp), %esi
addq $0x16, %r13
movl 0x90(%r12), %eax
movl %r13d, %ecx
subl %eax, %ecx
movl %ecx, 0x60(%r12)
cmpl $0x16, %esi
je 0x35445
addl $-0x16, %esi
movq %r13, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x35fc0
movq %rbx, %r14
jmp 0x3557f
movq %r14, %rbx
movq %r15, %r13
movq %r15, %rdi
callq 0x32825
pushq $-0x1
popq %r14
xorl %r15d, %r15d
movq -0x50(%rbp), %rsi
jmp 0x3550d
movq %r14, %rbx
movq %r15, %r13
movq %r15, %rdi
callq 0x32825
pushq $-0x1
popq %r14
xorl %r15d, %r15d
movq -0x40(%rbp), %rsi
jmp 0x3550d
movq %r14, %rbx
movq %r13, %rdi
callq 0x32825
pushq $-0x1
jmp 0x35503
movq -0x60(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x354e9
movq -0x68(%rbp), %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x357a9
testl %eax, %eax
movq -0x30(%rbp), %r15
je 0x3557f
movq %r14, %rbx
pushq $-0xa
popq %r14
jmp 0x35480
movl %eax, %r14d
leaq 0x2eb26(%rip), %rsi     # 0x63ffb
movq %r13, %rdi
movq -0x58(%rbp), %rdx
xorl %eax, %eax
callq 0x32836
movq -0x38(%rbp), %r12
jmp 0x35505
movq %r14, %rbx
leaq 0x2eaba(%rip), %rsi     # 0x63fad
movq %r13, %rdi
movq -0x58(%rbp), %rdx
xorl %eax, %eax
callq 0x32836
pushq $-0xa
popq %r14
movq -0x40(%rbp), %rsi
movq -0x30(%rbp), %r15
movq %r13, %rdi
callq 0x2b0d0
testq %r15, %r15
je 0x35525
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2d420
testq %r12, %r12
je 0x3556d
movq 0xc8(%r12), %rsi
testq %rsi, %rsi
je 0x3553f
movq %rbx, %rdi
callq 0x2d420
leaq 0x88(%r12), %rdi
callq 0x2a72f
leaq 0x68(%r12), %rdi
callq 0x2a72f
movq %r12, %rdi
addq $0x20, %rdi
callq 0x296af
movq %rbx, %rdi
movq %r12, %rsi
callq 0x27bf8
movl %r14d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r12), %rdi
movl 0x10(%r12), %esi
callq 0x356e0
movl %eax, 0x18(%r12)
movq -0x48(%rbp), %rsi
movl 0xdc(%rsi), %r15d
leal -0x1(%r15), %ecx
andl %eax, %ecx
movq 0xc8(%rsi), %rax
movq (%rax,%rcx,8), %rdx
movq %rdx, 0x130(%r12)
testq %rdx, %rdx
je 0x355c3
movq %r12, 0x138(%rdx)
movq %r14, -0x30(%rbp)
movq %r12, (%rax,%rcx,8)
movq 0xd0(%rsi), %rax
testq %rax, %rax
je 0x355e6
movq %rax, 0x120(%r12)
movq %r12, 0x128(%rax)
movq %r12, 0xd0(%rsi)
movl 0xd8(%rsi), %eax
incl %eax
movl %eax, 0xd8(%rsi)
leal (,%r15,4), %ecx
cmpl %ecx, %eax
setb %cl
cmpl $0x2710, %eax           # imm = 0x2710
setae %al
orb %cl, %al
jne 0x356c9
movl %r15d, %r14d
shll $0x4, %r14d
movq -0x30(%rbp), %rdi
movl %r14d, %esi
callq 0x2ca19
movq -0x48(%rbp), %rcx
testq %rax, %rax
je 0x356c9
movq %rax, %rbx
movq %r12, -0x38(%rbp)
leaq 0xd0(%rcx), %r12
addl %r15d, %r15d
movq %rcx, %r13
movq %rax, %rdi
movl %r14d, %esi
callq 0x27aea
movl 0xd8(%r13), %eax
leal -0x1(%r15), %ecx
xorps %xmm0, %xmm0
movq -0x30(%rbp), %rdi
subl $0x1, %eax
jb 0x356a7
movq (%r12), %r12
movups %xmm0, 0x130(%r12)
movl 0x18(%r12), %edx
andl %ecx, %edx
movq (%rbx,%rdx,8), %rsi
movq %rsi, 0x130(%r12)
testq %rsi, %rsi
je 0x3569a
movq %r12, 0x138(%rsi)
movq %r12, (%rbx,%rdx,8)
addq $0x120, %r12            # imm = 0x120
jmp 0x35669
movq -0x48(%rbp), %r14
movq 0xc8(%r14), %rsi
callq 0x2d420
movq %rbx, 0xc8(%r14)
movl %r15d, 0xdc(%r14)
movq -0x38(%rbp), %r12
xorl %eax, %eax
movq -0x70(%rbp), %rcx
testq %rcx, %rcx
je 0x35570
movq %r12, (%rcx)
jmp 0x35570