// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x58930
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x59720
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x6d62c
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6d94a
movl -0x20(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x6d68f
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %ecx
movl %ecx, %edx
movl $0x1, %ecx
callq *%rax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x6d68d
movq -0x48(%rbp), %rdi
movl -0x1c(%rbp), %edx
movq -0x18(%rbp), %rcx
leaq 0x54df7(%rip), %rsi     # 0xc2465
movb $0x0, %al
callq 0x68100
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x598f0
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6d94a
jmp 0x6d68f
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movl $0x140, %esi            # imm = 0x140
callq 0x541f0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x6d6c1
movq -0x48(%rbp), %rdi
callq 0x67f90
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
jmp 0x6d8b7
movq -0x50(%rbp), %rdi
movl $0x140, %esi            # imm = 0x140
callq 0x54280
movq -0x50(%rbp), %rdi
addq $0x68, %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x58bb0
movq -0x50(%rbp), %rdi
addq $0x88, %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x58bb0
movq -0x10(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x118(%rax)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x6da40
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x6d74a
movq -0x48(%rbp), %rdi
callq 0x67f90
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
jmp 0x6d8b7
movq -0x50(%rbp), %rax
movl $0x40, 0xdc(%rax)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movq -0x50(%rbp), %rax
movl 0xdc(%rax), %eax
shlq $0x3, %rax
movl %eax, %esi
callq 0x5bf70
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0xc8(%rax)
movq -0x50(%rbp), %rax
cmpq $0x0, 0xc8(%rax)
jne 0x6d7a6
movq -0x48(%rbp), %rdi
callq 0x67f90
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
jmp 0x6d8b7
movq -0x50(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x50(%rbp), %rax
movl 0xdc(%rax), %eax
shlq $0x3, %rax
movl %eax, %esi
callq 0x54280
movq -0x60(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x1c(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0xe0(%rax), %rdi
movq -0x50(%rbp), %rsi
addq $0x20, %rsi
callq 0x6da90
movl -0x20(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x6d85c
movq -0x30(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x6d830
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rdx
addq $0x8, %rdx
leaq 0x54c81(%rip), %rsi     # 0xc249e
movb $0x0, %al
callq 0x68100
movl $0xfffffff6, -0x54(%rbp) # imm = 0xFFFFFFF6
jmp 0x6d8b7
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x6dae0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x6d85a
movl $0xfffffff6, -0x54(%rbp) # imm = 0xFFFFFFF6
jmp 0x6d8b7
jmp 0x6d88c
movq -0x50(%rbp), %rdi
callq 0x6dd60
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x6d88a
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rdx
addq $0x8, %rdx
leaq 0x54c6b(%rip), %rsi     # 0xc24ec
movb $0x0, %al
callq 0x68100
jmp 0x6d8b7
jmp 0x6d88c
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x6dee0
cmpq $0x0, -0x28(%rbp)
je 0x6d8ab
movq -0x50(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6d94a
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x598f0
cmpq $0x0, -0x60(%rbp)
je 0x6d8dc
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movq -0x60(%rbp), %rsi
callq 0x5d6b0
cmpq $0x0, -0x50(%rbp)
je 0x6d944
movq -0x50(%rbp), %rax
cmpq $0x0, 0xc8(%rax)
je 0x6d909
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movq -0x50(%rbp), %rax
movq 0xc8(%rax), %rsi
callq 0x5d6b0
movq -0x50(%rbp), %rdi
addq $0x88, %rdi
callq 0x58c00
movq -0x50(%rbp), %rdi
addq $0x68, %rdi
callq 0x58c00
movq -0x50(%rbp), %rdi
addq $0x20, %rdi
callq 0x56ab0
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movq -0x50(%rbp), %rsi
callq 0x54490
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)