// File: /workspace/github_c_makefile_star2_O3/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, -0x78(%rbp)
movl %ecx, %r15d
movl %edx, -0x2c(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdi, %rbx
movq (%rdi), %r13
movq 0xd8(%r13), %rax
movq 0x10(%rax), %r14
movq (%r14), %rax
movq 0x8(%rax), %r12
leaq -0x50(%rbp), %rsi
movq %r13, %rdi
callq 0x321b9
testl %eax, %eax
jne 0x3d9be
movq %r12, -0x60(%rbp)
movq %r14, -0x70(%rbp)
movq %rbx, -0x68(%rbp)
testb $0x1, %r15b
jne 0x3d5ab
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movl -0x2c(%rbp), %edx
movl $0x1, %ecx
movq -0x60(%rbp), %rax
callq *0x50(%rax)
testl %eax, %eax
je 0x3d5ab
movl %eax, %r15d
leaq 0x36a30(%rip), %rsi     # 0x73f94
movq %r13, %rdi
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rcx
xorl %eax, %eax
callq 0x3a376
movq 0xd8(%r13), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0xb0(%rax), %rax
testq %rax, %rax
je 0x3d598
movq %rbx, %rdi
callq *%rax
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2e659
movl %r15d, %eax
jmp 0x3d9be
movl %r15d, -0x54(%rbp)
movq -0x68(%rbp), %r12
leaq 0x8(%r12), %rdi
movq %rdi, -0x48(%rbp)
movl $0x140, %esi            # imm = 0x140
callq 0x2e481
movq %rax, %rbx
testq %rax, %rax
movq %r13, %r14
je 0x3d874
movq $-0x4, %rax
movl $0x0, 0x4(%rbx,%rax)
addq $0x4, %rax
cmpq $0x13c, %rax            # imm = 0x13C
jb 0x3d5dc
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%rbx)
movq -0x48(%rbp), %r13
movq %r13, 0x68(%rbx)
xorl %eax, %eax
movl %eax, 0x80(%rbx)
movups %xmm0, 0x90(%rbx)
movq %r13, 0x88(%rbx)
movl %eax, 0xa0(%rbx)
movq %r12, (%rbx)
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq %rax, 0x118(%rbx)
movq %r13, %rdi
movq -0x38(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x3da5b
testq %rax, %rax
je 0x3d89e
movq %rax, -0x38(%rbp)
movl $0x40, 0xdc(%rbx)
movq %r13, %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x33d86
movq %rax, 0xc8(%rbx)
testq %rax, %rax
je 0x3d8bd
movl 0xdc(%rbx), %ecx
shll $0x3, %ecx
testl %ecx, %ecx
movq -0x40(%rbp), %r15
je 0x3d6b0
addq %rax, %rcx
movb $0x0, (%rax)
incq %rax
cmpq %rcx, %rax
jae 0x3d6b0
movb $0x0, (%rax)
incq %rax
cmpq %rcx, %rax
jae 0x3d6b0
movb $0x0, (%rax)
incq %rax
cmpq %rcx, %rax
jae 0x3d6b0
movb $0x0, (%rax)
incq %rax
cmpq %rcx, %rax
jb 0x3d684
leaq 0x8(%rbx), %rdx
movq -0x38(%rbp), %rax
movq %rax, 0x8(%rbx)
movl -0x2c(%rbp), %eax
movl %eax, 0x10(%rbx)
movq 0xe0(%r12), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rbx)
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x50(%rbx)
movq %rax, 0x30(%rbx)
movups %xmm0, 0x40(%rbx)
movq %rax, 0x38(%rbx)
movl $0x20, 0x28(%rbx)
testb $0x1, -0x54(%rbp)
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rax
jne 0x3d8d4
movq %rdx, %r15
leaq 0x88(%rbx), %rdx
movl $0x0, 0x98(%rbx)
movl 0xa0(%rbx), %eax
testb $0x4, %al
je 0x3d73a
movq $0x0, 0x90(%rbx)
movl $0x0, 0x9c(%rbx)
andl $-0x5, %eax
movl %eax, 0xa0(%rbx)
movq 0x118(%rbx), %rdi
movq (%rdi), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
leaq -0xba2b(%rip), %rsi     # 0x31d27
callq *0xa0(%rax)
testl %eax, %eax
movq %r15, %rdx
jne 0x3d91b
movl 0x98(%rbx), %r15d
movl $0xffffffe8, %eax       # imm = 0xFFFFFFE8
cmpl $0x16, %r15d
jb 0x3d91b
leaq 0x90(%rbx), %rcx
movq (%rcx), %r13
movzwl (%r13), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
cmpl $0x611e, %ecx           # imm = 0x611E
jne 0x3d91b
leaq 0x2(%r13), %rdi
leaq 0xa8(%rbx), %rsi
callq 0x36f8d
leaq 0xa(%r13), %rdi
leaq 0xb8(%rbx), %rsi
callq 0x36f8d
movzbl 0x15(%r13), %eax
movzbl 0x14(%r13), %edx
movl %edx, %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x13(%r13), %esi
movl %esi, %edi
shll $0x10, %edi
movzbl 0x12(%r13), %r8d
movl %r8d, %r9d
shll $0x18, %r9d
orl %edi, %r9d
movl $0x0, 0x100(%rbx)
shrl %r8d
addl $0x7bc, %r8d            # imm = 0x7BC
movl %r8d, 0xf4(%rbx)
shrl $0x15, %r9d
andl $0xf, %r9d
movl %r9d, 0xf0(%rbx)
andl $0x1f, %esi
movl %esi, 0xec(%rbx)
shrl $0x3, %edx
movl %edx, 0xe8(%rbx)
shrl $0x5, %ecx
andl $0x3f, %ecx
movl %ecx, 0xe4(%rbx)
addl %eax, %eax
andl $0x3e, %eax
movl %eax, 0xe0(%rbx)
addq $0x16, %r13
movl 0x90(%rbx), %eax
movl %r13d, %ecx
subl %eax, %ecx
movl %ecx, 0x60(%rbx)
cmpl $0x16, %r15d
je 0x3d8f8
movq %rbx, %rdx
addq $0x20, %rdx
addl $-0x16, %r15d
movq %r13, %rdi
movl %r15d, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3e491
jmp 0x3d8f8
leaq 0x3615b(%rip), %rsi     # 0x739d6
movq %r14, %rdi
callq 0x31537
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, -0x38(%rbp)
movq -0x50(%rbp), %r15
movq -0x48(%rbp), %r13
jmp 0x3d937
leaq 0x36131(%rip), %rsi     # 0x739d6
movq %r14, %rdi
callq 0x31537
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, -0x38(%rbp)
jmp 0x3d933
leaq 0x36112(%rip), %rsi     # 0x739d6
movq %r14, %rdi
callq 0x31537
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x3d933
cmpq $0x0, 0x38(%rax)
je 0x3d9cd
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3da9b
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
testl %eax, %eax
jne 0x3d937
movq -0x68(%rbp), %rdi
movq %rbx, %rsi
callq 0x3dc4d
xorl %eax, %eax
movq -0x78(%rbp), %rcx
testq %rcx, %rcx
je 0x3d9be
movq %rbx, (%rcx)
jmp 0x3d9be
movl %eax, %r12d
leaq 0x366f6(%rip), %rsi     # 0x7401b
movq %r14, %rdi
xorl %eax, %eax
callq 0x3a376
movq -0x48(%rbp), %r13
movq -0x40(%rbp), %r15
movq 0xd8(%r14), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0xb0(%rax), %rax
testq %rax, %rax
je 0x3d95a
movq %r15, %rdi
callq *%rax
movq %r14, %rdi
movq %r15, %rsi
callq 0x2e659
movq -0x38(%rbp), %rsi
testq %rsi, %rsi
je 0x3d976
movq %r13, %rdi
callq 0x3490d
testq %rbx, %rbx
je 0x3d9bb
movq 0xc8(%rbx), %rsi
testq %rsi, %rsi
je 0x3d98f
movq %r13, %rdi
callq 0x3490d
leaq 0x88(%rbx), %rdi
callq 0x317be
leaq 0x68(%rbx), %rdi
callq 0x317be
movq %rbx, %rdi
addq $0x20, %rdi
callq 0x30465
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2e659
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x365f9(%rip), %rsi     # 0x73fcd
movq %r14, %rdi
xorl %eax, %eax
callq 0x3a376
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
jmp 0x3d937