// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
movq %r8, %r12
movq %rsi, %rbx
movq %rdi, %r14
testq %rdi, %rdi
jne 0x3dacb
movq (%rbx), %rax
movq (%rax), %rax
movq 0xd8(%rax), %rax
movq 0x10(%rax), %r14
leaq 0x88(%rbx), %r15
movq (%r14), %rax
movq 0x8(%rax), %r9
cmpl $0x0, 0x98(%rbx)
je 0x3db16
movq 0x90(%rbx), %rax
testq %rdx, %rdx
js 0x3daf8
movq %rdx, %rsi
bswapq %rsi
movq %rsi, 0x2(%rax)
testq %rcx, %rcx
js 0x3dba5
bswapq %rcx
movq %rcx, 0xa(%rax)
testq %r12, %r12
jne 0x3dbaa
jmp 0x3dbea
movq %r9, %r12
leaq -0x30(%rbp), %rsi
movw $0x1e61, (%rsi)         # imm = 0x1E61
movq %r15, %rdi
movl $0x2, %edx
callq 0x359ad
testl %eax, %eax
jne 0x3db9d
leaq -0x30(%rbp), %rsi
movq $0x0, (%rsi)
movq %r15, %rdi
movl $0x8, %edx
callq 0x359ad
testl %eax, %eax
jne 0x3db9d
leaq -0x30(%rbp), %rsi
movq $0x0, (%rsi)
movq %r15, %rdi
movl $0x8, %edx
callq 0x359ad
testl %eax, %eax
jne 0x3db9d
leaq 0xe0(%rbx), %r13
movq %r13, %rsi
callq 0x33c4e
leaq -0x3c(%rbp), %rsi
movq %r13, %rdi
movq %rsi, %r13
callq 0x3ddc7
movl (%r13), %esi
movq %r15, %rdi
callq 0x3ddff
testl %eax, %eax
je 0x3dc3f
movl %eax, %r13d
jmp 0x3dc2d
testq %r12, %r12
je 0x3dbe5
movq %r9, -0x38(%rbp)
movl 0x60(%rbx), %eax
cmpl %eax, 0x98(%rbx)
jbe 0x3dbbf
movl %eax, 0x98(%rbx)
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3de22
movl %eax, %r13d
testl %eax, %eax
jne 0x3dc2d
leaq 0x20(%rbx), %rsi
movq %r12, %rdi
callq 0x3e19f
movq -0x38(%rbp), %r9
jmp 0x3dbea
testq %rdx, %rdx
js 0x3dc2a
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
movq 0x90(%rbx), %rcx
movl 0x98(%rbx), %r8d
movq %r14, %rdi
callq *0x38(%r9)
testl %eax, %eax
je 0x3dc2a
movl %eax, %r13d
movq (%rbx), %rax
addq $0x8, %rbx
movq (%rax), %rdi
leaq 0x3641c(%rip), %rsi     # 0x7403a
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3a376
jmp 0x3dc2d
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x98(%rbx), %eax
movl %eax, 0x60(%rbx)
movq %r12, %r9
jmp 0x3dbea