// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: pager_read_db_header
static int pager_read_db_header(Pager *pPager)
{
	unsigned char zRaw[UNQLITE_MIN_PAGE_SIZE]; /* Minimum page size */
	sxi64 n = 0;              /* Size of db file in bytes */
	int rc;
	/* Get the file size first */
	rc = unqliteOsFileSize(pPager->pfd,&n);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	pPager->dbByteSize = n;
	if( n > 0 ){
		unqlite_kv_methods *pMethods;
		SyString *pKv;
		pgno nPage;
		if( n < UNQLITE_MIN_PAGE_SIZE ){
			/* A valid unqlite database must be at least 512 bytes long */
			unqliteGenError(pPager->pDb,"Malformed database image");
			return UNQLITE_CORRUPT;
		}
		/* Read the database header */
		rc = unqliteOsRead(pPager->pfd,zRaw,sizeof(zRaw),0);
		if( rc != UNQLITE_OK ){
			unqliteGenError(pPager->pDb,"IO error while reading database header");
			return rc;
		}
		/* Extract the header */
		rc = pager_extract_header(pPager,zRaw,sizeof(zRaw));
		if( rc != UNQLITE_OK ){
			unqliteGenError(pPager->pDb,rc == UNQLITE_NOMEM ? "Unqlite is running out of memory" : "Malformed database image");
			return rc;
		}
		/* Update pager state  */
		nPage = (pgno)(n / pPager->iPageSize);
		if( nPage==0 && n>0 ){
			nPage = 1;
		}
		pPager->dbSize = nPage;
		/* Laod the target Key/Value storage engine */
		pKv = &pPager->sKv;
		pMethods = unqliteFindKVStore(pKv->zString,pKv->nByte);
		if( pMethods == 0 ){
			unqliteGenErrorFormat(pPager->pDb,"No such Key/Value storage engine '%z'",pKv);
			return UNQLITE_NOTIMPLEMENTED;
		}
		/* Install the new KV storage engine */
		rc = unqlitePagerRegisterKvEngine(pPager,pMethods);
		if( rc != UNQLITE_OK ){
			return rc;
		}
	}else{
		/* Set a default page and sector size */
		pPager->iSectorSize = GetSectorSize(pPager->pfd);
		pPager->iPageSize = unqliteGetPageSize();
		SyStringInitFromBuf(&pPager->sKv,pPager->pEngine->pIo->pMethods->zName,SyStrlen(pPager->pEngine->pIo->pMethods->zName));
		pPager->dbSize = 0;
	}
	/* Allocate a temporary page size */
	pPager->zTmpPage = (unsigned char *)SyMemBackendAlloc(pPager->pAllocator,(sxu32)pPager->iPageSize);
	if( pPager->zTmpPage == 0 ){
		unqliteGenOutofMem(pPager->pDb);
		return UNQLITE_NOMEM;
	}
	SyZero(pPager->zTmpPage,(sxu32)pPager->iPageSize);
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x10(%rbp)
movq $0x0, -0x218(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
leaq -0x218(%rbp), %rsi
callq 0x698b0
movl %eax, -0x21c(%rbp)
cmpl $0x0, -0x21c(%rbp)
je 0x690fb
movl -0x21c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x693ab
movq -0x218(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
cmpq $0x0, -0x218(%rbp)
jle 0x692cc
cmpq $0x200, -0x218(%rbp) # imm = 0x200
jge 0x69145
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x59003(%rip), %rsi     # 0xc2137
callq 0x58950
movl $0xffffffe8, -0x4(%rbp) # imm = 0xFFFFFFE8
jmp 0x693ab
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
leaq -0x210(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
xorl %eax, %eax
movl %eax, %ecx
callq 0x69cd0
movl %eax, -0x21c(%rbp)
cmpl $0x0, -0x21c(%rbp)
je 0x69193
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x58fd0(%rip), %rsi     # 0xc2150
callq 0x58950
movl -0x21c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x693ab
movq -0x10(%rbp), %rdi
leaq -0x210(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x69ed0
movl %eax, -0x21c(%rbp)
cmpl $0x0, -0x21c(%rbp)
je 0x691ed
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x21c(%rbp), %ecx
leaq 0x58f6b(%rip), %rsi     # 0xc2137
leaq 0x58fa4(%rip), %rax     # 0xc2177
cmpl $-0x1, %ecx
cmoveq %rax, %rsi
callq 0x58950
movl -0x21c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x693ab
movq -0x218(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x208(%rcx), %rcx
cqto
idivq %rcx
movq %rax, -0x238(%rbp)
cmpq $0x0, -0x238(%rbp)
jne 0x6922a
cmpq $0x0, -0x218(%rbp)
jle 0x6922a
movq $0x1, -0x238(%rbp)
movq -0x238(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
addq $0x1e0, %rax            # imm = 0x1E0
movq %rax, -0x230(%rbp)
movq -0x230(%rbp), %rax
movq (%rax), %rdi
movq -0x230(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x67320
movq %rax, -0x228(%rbp)
cmpq $0x0, -0x228(%rbp)
jne 0x6929d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x230(%rbp), %rdx
leaq 0x58f0e(%rip), %rsi     # 0xc2198
movb $0x0, %al
callq 0x68100
movl $0xffffffef, -0x4(%rbp) # imm = 0xFFFFFFEF
jmp 0x693ab
movq -0x10(%rbp), %rdi
movq -0x228(%rbp), %rsi
callq 0x67660
movl %eax, -0x21c(%rbp)
cmpl $0x0, -0x21c(%rbp)
je 0x692ca
movl -0x21c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x693ab
jmp 0x69342
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x6a0e0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20c(%rax)
callq 0x680d0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x208(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x1e0(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
callq 0x55d10
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1e8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x208(%rax), %esi
callq 0x5bf70
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x210(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x210(%rax)
jne 0x6938a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x67f90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x693ab
movq -0x10(%rbp), %rax
movq 0x210(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x208(%rax), %esi
callq 0x54280
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nopw (%rax,%rax)