// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: unqlitePagerOpen
UNQLITE_PRIVATE int unqlitePagerOpen(
  unqlite_vfs *pVfs,       /* The virtual file system to use */
  unqlite *pDb,            /* Database handle */
  const char *zFilename,   /* Name of the database file to open */
  unsigned int iFlags      /* flags controlling this file */
  )
{
	unqlite_kv_methods *pMethods = 0;
	int is_mem,rd_only,no_jrnl;
	Pager *pPager;
	sxu32 nByte;
	sxu32 nLen;
	int rc;

	/* Select the appropriate KV storage subsytem  */
	if( (iFlags & UNQLITE_OPEN_IN_MEMORY) || unqliteInMemory(zFilename) ){
		/* An in-memory database, record that  */
		pMethods = unqliteFindKVStore("mem",sizeof("mem") - 1); /* Always available */
		iFlags |= UNQLITE_OPEN_IN_MEMORY;
	}else{
		/* Install the default key value storage subsystem [i.e. Linear Hash] */
		pMethods = unqliteFindKVStore("hash",sizeof("hash")-1);
		if( pMethods == 0 ){
			/* Use the b+tree storage backend if the linear hash storage is not available */
			pMethods = unqliteFindKVStore("btree",sizeof("btree")-1);
		}
	}
	if( pMethods == 0 ){
		/* Can't happen */
		unqliteGenError(pDb,"Cannot install a default Key/Value storage engine");
		return UNQLITE_NOTIMPLEMENTED;
	}
	is_mem = (iFlags & UNQLITE_OPEN_IN_MEMORY) != 0;
	rd_only = (iFlags & UNQLITE_OPEN_READONLY) != 0;
	no_jrnl = (iFlags & UNQLITE_OPEN_OMIT_JOURNALING) != 0;
	rc = UNQLITE_OK;
	if( is_mem ){
		/* Omit journaling for in-memory database */
		no_jrnl = 1;
	}
	/* Total number of bytes to allocate */
	nByte = sizeof(Pager);
	nLen = 0;
	if( !is_mem ){
		nLen = SyStrlen(zFilename);
		nByte += pVfs->mxPathname + nLen + sizeof(char) /* null termniator */;
	}
	/* Allocate */
	pPager = (Pager *)SyMemBackendAlloc(&pDb->sMem,nByte);
	if( pPager == 0 ){
		return UNQLITE_NOMEM;
	}
	/* Zero the structure */
	SyZero(pPager,nByte);
	/* Fill-in the structure */
	pPager->pAllocator = &pDb->sMem;
	pPager->pDb = pDb;
	pDb->sDB.pPager = pPager;
	/* Allocate page table */
	pPager->nSize = 128; /* Must be a power of two */
	nByte = pPager->nSize * sizeof(Page *);
	pPager->apHash = (Page **)SyMemBackendAlloc(pPager->pAllocator,nByte);
	if( pPager->apHash == 0 ){
		rc = UNQLITE_NOMEM;
		goto fail;
	}
	SyZero(pPager->apHash,nByte);
	pPager->is_mem = is_mem;
	pPager->no_jrnl = no_jrnl;
	pPager->is_rdonly = rd_only;
	pPager->iOpenFlags = iFlags;
	pPager->pVfs = pVfs;
	SyRandomnessInit(&pPager->sPrng,0,0);
	SyRandomness(&pPager->sPrng,(void *)&pPager->cksumInit,sizeof(sxu32));
	/* Unlimited cache size */
	pPager->nCacheMax = SXU32_HIGH;
	/* Copy filename and journal name */
	if( !is_mem ){
		pPager->zFilename = (char *)&pPager[1];
		rc = UNQLITE_OK;
		if( pVfs->xFullPathname ){
			rc = pVfs->xFullPathname(pVfs,zFilename,pVfs->mxPathname + nLen,pPager->zFilename);
		}
		if( rc != UNQLITE_OK ){
			/* Simple filename copy */
			SyMemcpy(zFilename,pPager->zFilename,nLen);
			pPager->zFilename[nLen] = 0;
			rc = UNQLITE_OK;
		}else{
			nLen = SyStrlen(pPager->zFilename);
		}
		pPager->zJournal = (char *) SyMemBackendAlloc(pPager->pAllocator,nLen + sizeof(UNQLITE_JOURNAL_FILE_SUFFIX) + sizeof(char));
		if( pPager->zJournal == 0 ){
			rc = UNQLITE_NOMEM;
			goto fail;
		}
		/* Copy filename */
		SyMemcpy(pPager->zFilename,pPager->zJournal,nLen);
		/* Copy journal suffix */
		SyMemcpy(UNQLITE_JOURNAL_FILE_SUFFIX,&pPager->zJournal[nLen],sizeof(UNQLITE_JOURNAL_FILE_SUFFIX)-1);
		/* Append the nul terminator to the journal path */
		pPager->zJournal[nLen + ( sizeof(UNQLITE_JOURNAL_FILE_SUFFIX) - 1)] = 0;
	}
	/* Finally, register the selected KV engine */
	rc = unqlitePagerRegisterKvEngine(pPager,pMethods);
	if( rc != UNQLITE_OK ){
		goto fail;
	}
	/* Set the pager state */
	if( pPager->is_mem ){
		pPager->iState = PAGER_WRITER_FINISHED;
		pPager->iLock = EXCLUSIVE_LOCK;
	}else{
		pPager->iState = PAGER_OPEN;
		pPager->iLock = NO_LOCK;
	}
	/* All done, ready for processing */
	return UNQLITE_OK;
fail:
	SyMemBackendFree(&pDb->sMem,pPager);
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movl -0x24(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x66c9a
movq -0x20(%rbp), %rdi
callq 0x67280
cmpl $0x0, %eax
je 0x66cbc
leaq 0x5b097(%rip), %rdi     # 0xc1d38
movl $0x3, %esi
callq 0x67320
movq %rax, -0x30(%rbp)
movl -0x24(%rbp), %eax
orl $0x80, %eax
movl %eax, -0x24(%rbp)
jmp 0x66cef
leaq 0x5b0bd(%rip), %rdi     # 0xc1d80
movl $0x4, %esi
callq 0x67320
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x66ced
leaq 0x5b12c(%rip), %rdi     # 0xc1e0b
movl $0x5, %esi
callq 0x67320
movq %rax, -0x30(%rbp)
jmp 0x66cef
cmpq $0x0, -0x30(%rbp)
jne 0x66d12
movq -0x18(%rbp), %rdi
leaq 0x5b110(%rip), %rsi     # 0xc1e11
callq 0x58950
movl $0xffffffef, -0x4(%rbp) # imm = 0xFFFFFFEF
jmp 0x67071
movl -0x24(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x34(%rbp)
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x38(%rbp)
movl -0x24(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x54(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x66d64
movl $0x1, -0x3c(%rbp)
movl $0x260, -0x4c(%rbp)     # imm = 0x260
movl $0x0, -0x50(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x66d9f
movq -0x20(%rbp), %rdi
callq 0x55d10
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
addl -0x50(%rbp), %eax
movl %eax, %eax
movl %eax, %ecx
addq $0x1, %rcx
movl -0x4c(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x5bf70
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x66dc2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x67071
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x54280
movq -0x18(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xd8(%rax)
movq -0x48(%rbp), %rax
movl $0x80, 0x250(%rax)
movq -0x48(%rbp), %rax
movl 0x250(%rax), %eax
shlq $0x3, %rax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rdi
movl -0x4c(%rbp), %esi
callq 0x5bf70
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x248(%rax)
movq -0x48(%rbp), %rax
cmpq $0x0, 0x248(%rax)
jne 0x66e4a
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
jmp 0x6705e
movq -0x48(%rbp), %rax
movq 0x248(%rax), %rdi
movl -0x4c(%rbp), %esi
callq 0x54280
movl -0x34(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x1fc(%rax)
movl -0x3c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x204(%rax)
movl -0x38(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x200(%rax)
movl -0x24(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x16c(%rax)
movq -0x10(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x48(%rbp), %rdi
addq $0x64, %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x673c0
movq -0x48(%rbp), %rdi
addq $0x64, %rdi
movq -0x48(%rbp), %rsi
addq $0x168, %rsi            # imm = 0x168
movl $0x4, %edx
callq 0x67550
movq -0x48(%rbp), %rax
movl $0xffffffff, 0x258(%rax) # imm = 0xFFFFFFFF
cmpl $0x0, -0x34(%rbp)
jne 0x66ff6
movq -0x48(%rbp), %rcx
addq $0x260, %rcx            # imm = 0x260
movq -0x48(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, -0x54(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x66f32
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %edx
addl -0x50(%rbp), %edx
movq -0x48(%rbp), %rcx
movq 0x18(%rcx), %rcx
callq *%rax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x66f64
movq -0x20(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rsi
movl -0x50(%rbp), %edx
callq 0x5d570
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x54(%rbp)
jmp 0x66f74
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x55d10
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rdi
movl -0x50(%rbp), %eax
addq $0x11, %rax
addq $0x1, %rax
movl %eax, %esi
callq 0x5bf70
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x48(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x66faf
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
jmp 0x6705e
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rsi
movl -0x50(%rbp), %edx
callq 0x5d570
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rsi
movl -0x50(%rbp), %eax
addq %rax, %rsi
leaq 0x5ae67(%rip), %rdi     # 0xc1e43
movl $0x10, %edx
callq 0x5d570
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x50(%rbp), %ecx
movb $0x0, 0x10(%rax,%rcx)
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x67660
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x6700e
jmp 0x6705e
movq -0x48(%rbp), %rax
cmpl $0x0, 0x1fc(%rax)
je 0x67039
movq -0x48(%rbp), %rax
movl $0x5, 0x1f0(%rax)
movq -0x48(%rbp), %rax
movl $0x4, 0x1f4(%rax)
jmp 0x67055
movq -0x48(%rbp), %rax
movl $0x0, 0x1f0(%rax)
movq -0x48(%rbp), %rax
movl $0x0, 0x1f4(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x67071
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x5d6b0
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)