// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x18(%rbp), %rax
addq $0x88, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x44(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x6db2b
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x58930
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jae 0x6dc37
movq -0x18(%rbp), %rax
addq $0xe0, %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rdi
movl $0x611e, %esi           # imm = 0x611E
callq 0x6e180
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x6db78
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6dd49
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6e1c0
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x6db99
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6dd49
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6e1c0
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x6dbba
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6dd49
callq 0x5b3f0
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x6dbe2
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq *%rax
jmp 0x6dbf0
movq -0x50(%rbp), %rdi
movl $0x38, %esi
callq 0x54280
movq -0x50(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x6e200
movq -0x38(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x6e270
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x6dc1d
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6dd49
movq -0x38(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x60(%rax)
movl $0x1, -0x44(%rbp)
jmp 0x6dcd5
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x20(%rbp)
jl 0x6dc62
movq -0x68(%rbp), %rdi
addq $0x2, %rdi
movq -0x20(%rbp), %rsi
callq 0x61c60
movl $0x1, -0x44(%rbp)
cmpq $0x0, -0x28(%rbp)
jl 0x6dc81
movq -0x68(%rbp), %rdi
addq $0xa, %rdi
movq -0x28(%rbp), %rsi
callq 0x61c60
movl $0x1, -0x44(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x6dcd3
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x60(%rax), %esi
callq 0x6e2b0
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %edx, %edx
callq 0x6e2e0
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x6dcbb
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6dd49
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
callq 0x6e750
movl $0x1, -0x44(%rbp)
jmp 0x6dcd5
cmpl $0x0, -0x44(%rbp)
je 0x6dd42
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rcx
movq (%rcx), %rsi
movq -0x70(%rbp), %rcx
movl 0x8(%rcx), %edx
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x38(%rbp), %r8
movl 0x10(%r8), %r8d
callq *%rax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x6dd40
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq -0x70(%rbp), %rdx
leaq 0x547da(%rip), %rsi     # 0xc250b
movb $0x0, %al
callq 0x68100
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6dd49
jmp 0x6dd42
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)