// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: CollectionSetHeader
static int CollectionSetHeader(
	unqlite_kv_engine *pEngine, /* Underlying KV storage engine */
	unqlite_col *pCol,          /* Target collection */
	jx9_int64 iRec,             /* Last record ID */
	jx9_int64 iTotal,           /* Total number of records in this collection */
	jx9_value *pSchema          /* Collection schema */
	)
{
	SyBlob *pHeader = &pCol->sHeader;
	unqlite_kv_methods *pMethods;
	int iWrite = 0;
	int rc;
	if( pEngine == 0 ){
		/* Default storage engine */
		pEngine = unqlitePagerGetKvEngine(pCol->pVm->pDb);
	}
	pMethods = pEngine->pIo->pMethods;
	if( SyBlobLength(pHeader) < 1 ){
		Sytm *pCreate = &pCol->sCreation; /* Creation time */
		unqlite_vfs *pVfs;
		sxu32 iDos;
		/* Magic number */
		rc = SyBlobAppendBig16(pHeader,UNQLITE_COLLECTION_MAGIC);
		if( rc != UNQLITE_OK ){
			return rc;
		}
		/* Initial record ID */
		rc = SyBlobAppendBig64(pHeader,0);
		if( rc != UNQLITE_OK ){
			return rc;
		}
		/* Total records in the collection */
		rc = SyBlobAppendBig64(pHeader,0);
		if( rc != UNQLITE_OK ){
			return rc;
		}
		pVfs = (unqlite_vfs *)unqliteExportBuiltinVfs();
		/* Creation time of the collection */
		if( pVfs->xCurrentTime ){
			/* Get the creation time */
			pVfs->xCurrentTime(pVfs,pCreate);
		}else{
			/* Zero the structure */
			SyZero(pCreate,sizeof(Sytm));
		}
		/* Convert to DOS time */
		SyTimeFormatToDos(pCreate,&iDos);
		rc = SyBlobAppendBig32(pHeader,iDos);
		if( rc != UNQLITE_OK ){
			return rc;
		}
		/* Offset to start writing collection schema */
		pCol->nSchemaOfft = SyBlobLength(pHeader);
		iWrite = 1;
	}else{
		unsigned char *zBinary = (unsigned char *)SyBlobData(pHeader);
		/* Header update */
		if( iRec >= 0 ){
			/* Update record ID */
			SyBigEndianPack64(&zBinary[2/* Magic number*/],(sxu64)iRec);
			iWrite = 1;
		}
		if( iTotal >= 0 ){
			/* Total records */
			SyBigEndianPack64(&zBinary[2/* Magic number*/+8/* Record ID*/],(sxu64)iTotal);
			iWrite = 1;
		}
		if( pSchema ){
			/* Collection Schema */
			SyBlobTruncate(pHeader,pCol->nSchemaOfft);
			/* Encode the schema to FastJson */
			rc = FastJsonEncode(pSchema,pHeader,0);
			if( rc != UNQLITE_OK ){
				return rc;
			}
			/* Copy the collection schema */
			jx9MemObjStore(pSchema,&pCol->sSchema);
			iWrite = 1;
		}
	}
	if( iWrite ){
		SyString *pId = &pCol->sName;
		/* Reflect the disk and/or in-memory image */
		rc = pMethods->xReplace(pEngine,
			(const void *)pId->zString,pId->nByte,
			SyBlobData(pHeader),SyBlobLength(pHeader)
			);
		if( rc != UNQLITE_OK ){
			unqliteGenErrorFormat(pCol->pVm->pDb,
				"Cannot save collection '%z' header in the underlying storage engine",
				pId
				);
			return rc;
		}
	}
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
testq %rdi, %rdi
jne 0x357dc
movq (%rbx), %rax
movq (%rax), %rax
movq 0xd8(%rax), %rax
movq 0x10(%rax), %r14
leaq 0x88(%rbx), %rcx
movq (%r14), %rax
movq 0x8(%rax), %r12
cmpl $0x0, 0x98(%rbx)
je 0x35852
movq %rcx, -0x40(%rbp)
movq %r14, -0x38(%rbp)
movq %r12, -0x30(%rbp)
movq 0x90(%rbx), %r12
testq %r15, %r15
js 0x3581e
leaq 0x2(%r12), %rdi
movq %r15, %rsi
movq %r8, %r14
callq 0x2f66d
movq %r14, %r8
testq %r13, %r13
js 0x358c7
addq $0xa, %r12
movq %r12, %rdi
movq %r13, %rsi
movq %r8, %r14
callq 0x2f66d
movq %r14, %r8
testq %r14, %r14
movq -0x30(%rbp), %r12
movq -0x38(%rbp), %r14
jne 0x358d4
jmp 0x35910
movq %rcx, %r15
movq %rcx, %rdi
movl $0x611e, %esi           # imm = 0x611E
callq 0x35975
testl %eax, %eax
jne 0x358bf
movq %r15, %rdi
callq 0x35999
testl %eax, %eax
jne 0x358bf
movq %r15, %rdi
callq 0x35999
testl %eax, %eax
jne 0x358bf
movq %r12, -0x30(%rbp)
leaq 0xe0(%rbx), %r13
movq %r13, %rsi
callq 0x2c8a0
leaq -0x44(%rbp), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x359b7
movl (%r12), %eax
bswapl %eax
leaq -0x48(%rbp), %rsi
movl %eax, (%rsi)
pushq $0x4
popq %rdx
movq %r15, %rdi
callq 0x2e250
testl %eax, %eax
je 0x35966
movl %eax, %r13d
jmp 0x35954
testq %r8, %r8
movq -0x30(%rbp), %r12
movq -0x38(%rbp), %r14
je 0x3590b
movl 0x60(%rbx), %eax
cmpl %eax, 0x98(%rbx)
jbe 0x358e5
movl %eax, 0x98(%rbx)
movq %r8, %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
movq %r8, %r15
callq 0x359ef
movl %eax, %r13d
testl %eax, %eax
jne 0x35954
leaq 0x20(%rbx), %rsi
movq %r15, %rdi
callq 0x35ceb
jmp 0x35910
testq %r15, %r15
js 0x35951
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
movq 0x90(%rbx), %rcx
movl 0x98(%rbx), %r8d
movq %r14, %rdi
callq *0x38(%r12)
testl %eax, %eax
je 0x35951
movl %eax, %r13d
movq (%rbx), %rax
addq $0x8, %rbx
movq (%rax), %rdi
leaq 0x2e6d5(%rip), %rsi     # 0x6401a
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32836
jmp 0x35954
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x98(%rbx), %eax
movl %eax, 0x60(%rbx)
movq -0x30(%rbp), %r12
jmp 0x35910