// File: /workspace/github_c_makefile_star2_O1/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 %r8, %r13
movq %rsi, %rbx
movq %rdi, %r14
testq %rdi, %rdi
jne 0x3e495
movq (%rbx), %rax
movq (%rax), %rax
movq 0xd8(%rax), %rax
movq 0x10(%rax), %r14
leaq 0x88(%rbx), %r12
movq (%r14), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
cmpl $0x0, 0x98(%rbx)
je 0x3e4d1
movq 0x90(%rbx), %rax
testq %rdx, %rdx
js 0x3e565
bswapq %rdx
movq %rdx, 0x2(%rax)
movl $0x1, %edx
jmp 0x3e567
leaq -0x30(%rbp), %rsi
movw $0x1e61, (%rsi)         # imm = 0x1E61
movq %r12, %rdi
movl $0x2, %edx
callq 0x360ae
testl %eax, %eax
jne 0x3e555
leaq -0x30(%rbp), %rsi
movq $0x0, (%rsi)
movq %r12, %rdi
movl $0x8, %edx
callq 0x360ae
testl %eax, %eax
jne 0x3e555
leaq -0x30(%rbp), %rsi
movq $0x0, (%rsi)
movq %r12, %rdi
movl $0x8, %edx
callq 0x360ae
testl %eax, %eax
jne 0x3e555
leaq 0xe0(%rbx), %r15
movq %r15, %rsi
callq 0x342c9
leaq -0x30(%rbp), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x3e7c7
movl (%r13), %esi
movq %r12, %rdi
callq 0x3e7ff
testl %eax, %eax
je 0x3e630
movl %eax, %r15d
xorl %edx, %edx
xorl %eax, %eax
testb %al, %al
jne 0x3e5ce
jmp 0x3e61e
xorl %edx, %edx
movq %r14, -0x40(%rbp)
testq %rcx, %rcx
js 0x3e57c
bswapq %rcx
movq %rcx, 0xa(%rax)
movl $0x1, %edx
movb $0x1, %r14b
testq %r13, %r13
je 0x3e5c5
movl %edx, -0x34(%rbp)
movl 0x60(%rbx), %eax
cmpl %eax, 0x98(%rbx)
jbe 0x3e598
movl %eax, 0x98(%rbx)
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x3e822
testl %eax, %eax
je 0x3e5b4
movl %eax, %r15d
xorl %r14d, %r14d
movl -0x34(%rbp), %edx
jmp 0x3e5c5
leaq 0x20(%rbx), %rsi
movq %r13, %rdi
callq 0x3ebc8
movl $0x1, %edx
testb %r14b, %r14b
movq -0x40(%rbp), %r14
je 0x3e61e
testl %edx, %edx
je 0x3e61b
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
movq 0x90(%rbx), %rcx
movl 0x98(%rbx), %r8d
movq %r14, %rdi
movq -0x48(%rbp), %rax
callq *0x38(%rax)
movl %eax, %r14d
testl %eax, %eax
je 0x3e616
movq (%rbx), %rax
addq $0x8, %rbx
movq (%rax), %rdi
leaq 0x37a29(%rip), %rsi     # 0x76032
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3ac51
movl %r14d, %r15d
testl %r14d, %r14d
jne 0x3e61e
xorl %r15d, %r15d
movl %r15d, %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)
movb $0x1, %al
movl $0x1, %edx
jmp 0x3e55c