// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %r8, -0x58(%rbp)
movl %ecx, %r12d
movl %edx, -0x38(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdi, %rbx
movq (%rdi), %r15
movq 0xd8(%r15), %rax
movq 0x10(%rax), %r13
movq (%r13), %rax
movq 0x8(%rax), %r14
leaq -0x48(%rbp), %rsi
movq %r15, %rdi
callq 0x327a8
testl %eax, %eax
jne 0x3e320
movq %r14, -0x40(%rbp)
movq %r13, -0x50(%rbp)
testb $0x1, %r12b
movl %r12d, %r13d
jne 0x3df6d
movq -0x48(%rbp), %r14
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movl -0x38(%rbp), %edx
movl $0x1, %ecx
movq -0x40(%rbp), %rax
callq *0x50(%rax)
testl %eax, %eax
je 0x3df6d
movq %rax, %r12
leaq 0x3805b(%rip), %rsi     # 0x75f8c
movq %r15, %rdi
movl -0x38(%rbp), %edx
movq -0x30(%rbp), %rcx
xorl %eax, %eax
callq 0x3ac51
movq 0xd8(%r15), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0xb0(%rax), %rax
testq %rax, %rax
je 0x3df65
movq %r14, %rdi
callq *%rax
movq %r15, %rdi
jmp 0x3e315
leaq 0x8(%rbx), %r12
movq %r12, %rdi
movl $0x140, %esi            # imm = 0x140
callq 0x2e951
movq %rax, %r14
testq %rax, %rax
je 0x3e272
movq $-0x4, %rax
movl $0x0, 0x4(%r14,%rax)
addq $0x4, %rax
cmpq $0x13c, %rax            # imm = 0x13C
jb 0x3df91
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%r14)
movq %r12, 0x68(%r14)
xorl %eax, %eax
movl %eax, 0x80(%r14)
movups %xmm0, 0x90(%r14)
movq %r12, 0x88(%r14)
movl %eax, 0xa0(%r14)
movq %rbx, (%r14)
movq -0x48(%rbp), %rax
movq %rax, 0x118(%r14)
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movl -0x38(%rbp), %edx
callq 0x3e425
testq %rax, %rax
je 0x3e272
movq %rax, -0x30(%rbp)
movl $0x40, 0xdc(%r14)
movq %r12, %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x343ed
movq %rax, 0xc8(%r14)
testq %rax, %rax
je 0x3e32f
movl 0xdc(%r14), %ecx
shll $0x3, %ecx
testl %ecx, %ecx
je 0x3e060
addq %rax, %rcx
movb $0x0, (%rax)
incq %rax
cmpq %rcx, %rax
jae 0x3e060
movb $0x0, (%rax)
incq %rax
cmpq %rcx, %rax
jae 0x3e060
movb $0x0, (%rax)
incq %rax
cmpq %rcx, %rax
jae 0x3e060
movb $0x0, (%rax)
incq %rax
cmpq %rcx, %rax
jb 0x3e034
leaq 0x8(%r14), %rdx
movq -0x30(%rbp), %rax
movq %rax, 0x8(%r14)
movl -0x38(%rbp), %eax
movl %eax, 0x10(%r14)
movq 0xe0(%rbx), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%r14)
movups %xmm0, 0x50(%r14)
movups %xmm0, 0x20(%r14)
movq %rax, 0x30(%r14)
movups %xmm0, 0x40(%r14)
movq %rax, 0x38(%r14)
movl $0x0, 0x50(%r14)
movl $0x20, 0x28(%r14)
testb $0x1, %r13b
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rax
jne 0x3e34f
movq %rdx, -0x38(%rbp)
movq %r12, %r13
leaq 0x88(%r14), %rdx
movl $0x0, 0x98(%r14)
movl 0xa0(%r14), %eax
testb $0x4, %al
je 0x3e0ff
movq $0x0, 0x90(%r14)
movl $0x0, 0x9c(%r14)
andl $-0x5, %eax
movl %eax, 0xa0(%r14)
movq 0x118(%r14), %rdi
movq (%rdi), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
leaq -0xbe05(%rip), %rsi     # 0x32312
callq *0xa0(%rax)
testl %eax, %eax
movq %rbx, %rdi
movq -0x38(%rbp), %rdx
jne 0x3e251
movl 0x98(%r14), %r8d
movl $0xffffffe8, %eax       # imm = 0xFFFFFFE8
cmpl $0x16, %r8d
jb 0x3e251
movq 0x90(%r14), %r12
movzwl (%r12), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
cmpl $0x611e, %ecx           # imm = 0x611E
jne 0x3e251
leaq 0x2(%r12), %rdi
leaq 0xa8(%r14), %rsi
movl %r8d, -0x40(%rbp)
callq 0x376e7
leaq 0xa(%r12), %rdi
leaq 0xb8(%r14), %rsi
callq 0x376e7
movzbl 0x15(%r12), %eax
movzbl 0x14(%r12), %edx
movl %edx, %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x13(%r12), %r10d
movl %r10d, %edi
shll $0x10, %edi
movzbl 0x12(%r12), %r8d
movl %r8d, %r9d
shll $0x18, %r9d
orl %edi, %r9d
movq %rbx, %rdi
movl $0x0, 0x100(%r14)
shrl %r8d
addl $0x7bc, %r8d            # imm = 0x7BC
movl %r8d, 0xf4(%r14)
movl -0x40(%rbp), %esi
shrl $0x15, %r9d
andl $0xf, %r9d
movl %r9d, 0xf0(%r14)
andl $0x1f, %r10d
movl %r10d, 0xec(%r14)
shrl $0x3, %edx
movl %edx, 0xe8(%r14)
movq -0x38(%rbp), %rdx
shrl $0x5, %ecx
andl $0x3f, %ecx
movl %ecx, 0xe4(%r14)
addl %eax, %eax
andl $0x3e, %eax
movl %eax, 0xe0(%r14)
addq $0x16, %r12
movl 0x90(%r14), %eax
movl %r12d, %ecx
subl %eax, %ecx
movl %ecx, 0x60(%r14)
xorl %eax, %eax
cmpl $0x16, %esi
je 0x3e251
leaq 0x20(%r14), %rdx
addl $-0x16, %esi
movq %r12, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3eebc
xorl %eax, %eax
movq -0x38(%rbp), %rdx
movq %rbx, %rdi
testl %eax, %eax
je 0x3e37c
movq %rax, %r12
movq %r15, %rbx
leaq 0x37dad(%rip), %rsi     # 0x76013
movq %r15, %rdi
xorl %eax, %eax
callq 0x3ac51
jmp 0x3e295
movq %r12, %r13
leaq 0x37752(%rip), %rsi     # 0x759ce
movq %r15, %rbx
movq %r15, %rdi
callq 0x31af5
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, -0x30(%rbp)
movq -0x48(%rbp), %r15
movq 0xd8(%rbx), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0xb0(%rax), %rax
testq %rax, %rax
je 0x3e2bc
movq %r15, %rdi
callq *%rax
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2eb38
movq -0x30(%rbp), %rsi
testq %rsi, %rsi
je 0x3e2d8
movq %r13, %rdi
callq 0x3503f
testq %r14, %r14
je 0x3e31d
movq 0xc8(%r14), %rsi
testq %rsi, %rsi
je 0x3e2f1
movq %r13, %rdi
callq 0x3503f
leaq 0x88(%r14), %rdi
callq 0x31d8f
leaq 0x68(%r14), %rdi
callq 0x31d8f
movq %r14, %rdi
addq $0x20, %rdi
callq 0x309f2
movq %r13, %rdi
movq %r14, %rsi
callq 0x2eb38
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %r13
leaq 0x37695(%rip), %rsi     # 0x759ce
movq %r15, %rbx
movq %r15, %rdi
callq 0x31af5
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x3e295
movq %r12, %r13
cmpq $0x0, 0x38(%rax)
je 0x3e394
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3e465
movq %rbx, %rdi
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
testl %eax, %eax
movq %r15, %rbx
jne 0x3e295
movq %r14, %rsi
callq 0x3e645
xorl %eax, %eax
movq -0x58(%rbp), %rcx
testq %rcx, %rcx
je 0x3e320
movq %r14, (%rcx)
jmp 0x3e320
leaq 0x37c2a(%rip), %rsi     # 0x75fc5
movq %r15, %rbx
movq %r15, %rdi
xorl %eax, %eax
callq 0x3ac51
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
jmp 0x3e295