// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: CollectionStore
static int CollectionStore(
	unqlite_col *pCol, /* Target collection */
	jx9_value *pValue  /* JSON value to be stored */
	)
{
	SyBlob *pWorker = &pCol->sWorker;
	unqlite_kv_methods *pMethods;
	unqlite_kv_engine *pEngine;
	sxu32 nKeyLen;
	int rc;	
	/* Point to the underlying KV store */
	pEngine = unqlitePagerGetKvEngine(pCol->pVm->pDb);
	pMethods = pEngine->pIo->pMethods;
	if( pCol->nTotRec >= SXI64_HIGH ){
		/* Collection limit reached. No more records */
		unqliteGenErrorFormat(pCol->pVm->pDb,
				"Collection '%z': Records limit reached",
				&pCol->sName
			);
		return UNQLITE_LIMIT;
	}
	if( pMethods->xReplace == 0 ){
		unqliteGenErrorFormat(pCol->pVm->pDb,
				"Cannot store record into collection '%z' due to a read-only Key/Value storage engine",
				&pCol->sName
			);
		return UNQLITE_READ_ONLY;
	}
	/* Reset the working buffer */
	SyBlobReset(pWorker);
	if( jx9_value_is_json_object(pValue) ){
		jx9_value sId;
		/* If the given type is a JSON object, then add the special __id field */
		jx9MemObjInitFromInt(pCol->pVm->pJx9Vm,&sId,pCol->nLastid);
		jx9_array_add_strkey_elem(pValue,"__id",&sId);
		jx9MemObjRelease(&sId);
	}
	/* Prepare the unique ID for this record */
	SyBlobFormat(pWorker,"%z_%qd",&pCol->sName,pCol->nLastid);
	nKeyLen = SyBlobLength(pWorker);
	if( nKeyLen < 1 ){
		unqliteGenOutofMem(pCol->pVm->pDb);
		return UNQLITE_NOMEM;
	}
	/* Turn to FastJson */
	rc = FastJsonEncode(pValue,pWorker,0);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Finally perform the insertion */
	rc = pMethods->xReplace(
		pEngine,
		SyBlobData(pWorker),nKeyLen,
		SyBlobDataAt(pWorker,nKeyLen),SyBlobLength(pWorker)-nKeyLen
		);
	if( rc == UNQLITE_OK ){
		/* Save the value in the cache */
		CollectionCacheInstallRecord(pCol,pCol->nLastid,pValue);
		/* Increment the unique __id */
		pCol->nLastid++;
		pCol->nTotRec++;
		/* Reflect the change */
		rc = CollectionSetHeader(0,pCol,pCol->nLastid,pCol->nTotRec,0);
	}
	if( rc != UNQLITE_OK ){
		unqliteGenErrorFormat(pCol->pVm->pDb,
				"IO error while storing record into collection '%z'",
				&pCol->sName
			);
		return rc;
	}
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x68, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x58930
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rcx, 0xb8(%rax)
jl 0x79794
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0x49381(%rip), %rsi     # 0xc2b02
movb $0x0, %al
callq 0x68100
movl $0xfffffff9, -0x4(%rbp) # imm = 0xFFFFFFF9
jmp 0x79977
movq -0x28(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x797cb
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0x49371(%rip), %rsi     # 0xc2b29
movb $0x0, %al
callq 0x68100
movl $0xffffffb5, -0x4(%rbp) # imm = 0xFFFFFFB5
jmp 0x79977
movq -0x20(%rbp), %rdi
callq 0x65e00
movq -0x18(%rbp), %rdi
callq 0x57660
cmpl $0x0, %eax
je 0x79821
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0xe0(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rdx
leaq -0x78(%rbp), %rsi
callq 0x79990
movq -0x18(%rbp), %rdi
leaq 0x4936f(%rip), %rsi     # 0xc2b7e
leaq -0x78(%rbp), %rdx
callq 0x579b0
leaq -0x78(%rbp), %rdi
callq 0x56ab0
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rcx
leaq 0x48d6a(%rip), %rsi     # 0xc25a9
movb $0x0, %al
callq 0x6e820
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x34(%rbp)
cmpl $0x1, -0x34(%rbp)
jae 0x79871
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x67f90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x79977
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x6e2e0
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x79894
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x79977
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rcx
movq 0x8(%rcx), %rsi
movl -0x34(%rbp), %edx
movq -0x20(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl -0x34(%rbp), %r8d
addq %r8, %rcx
movq -0x20(%rbp), %r8
movl 0x10(%r8), %r8d
subl -0x34(%rbp), %r8d
movl %r8d, %r8d
callq *%rax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x79942
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rsi
movq -0x18(%rbp), %rdx
callq 0x724f0
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xa8(%rax)
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xb8(%rax)
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rcx
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdi
callq 0x6dae0
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x79970
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
leaq 0x49222(%rip), %rsi     # 0xc2b83
movb $0x0, %al
callq 0x68100
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x79977
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)