// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: lhAllocateSpace
static int lhAllocateSpace(lhpage *pPage,sxu64 nAmount,sxu16 *pOfft)
{
	const unsigned char *zEnd,*zPtr;
	sxu16 iNext,iBlksz,nByte;
	unsigned char *zPrev;
	int rc;
	if( (sxu64)pPage->nFree < nAmount ){
		/* Don't bother looking for a free chunk */
		return UNQLITE_FULL;
	}
	if( pPage->nCell < 10 && ((int)nAmount >= (pPage->pHash->iPageSize / 2)) ){
		/* Big chunk need an overflow page for its data */
		return UNQLITE_FULL;
	}
	zPtr = &pPage->pRaw->zData[pPage->sHdr.iFree];
	zEnd = &pPage->pRaw->zData[pPage->pHash->iPageSize];
	nByte = (sxu16)nAmount;
	zPrev = 0;
	iBlksz = 0; /* cc warning */
	/* Perform the lookup */
	for(;;){
		if( zPtr >= zEnd ){
			return UNQLITE_FULL;
		}
		/* Offset of the next free block */
		SyBigEndianUnpack16(zPtr,&iNext);
		/* Block size */
		SyBigEndianUnpack16(&zPtr[2],&iBlksz);
		if( iBlksz >= nByte ){
			/* Got one */
			break;
		}
		zPrev = (unsigned char *)zPtr;
		if( iNext == 0 ){
			/* No more free blocks, defragment the page */
			rc = lhPageDefragment(pPage);
			if( rc == UNQLITE_OK && pPage->nFree >= nByte) {
				/* Free blocks are merged together */
				iNext = 0;
				zPtr = &pPage->pRaw->zData[pPage->sHdr.iFree];
				iBlksz = pPage->nFree;
				zPrev = 0;
				break;
			}else{
				return UNQLITE_FULL;
			}
		}
		/* Point to the next free block */
		zPtr = &pPage->pRaw->zData[iNext];
	}
	/* Acquire writer lock on this page */
	rc = pPage->pHash->pIo->xWrite(pPage->pRaw);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Save block offset */
	*pOfft = (sxu16)(zPtr - pPage->pRaw->zData);
	/* Fix pointers */
	if( iBlksz >= nByte && (iBlksz - nByte) > 3 ){
		unsigned char *zBlock = &pPage->pRaw->zData[(*pOfft) + nByte];
		/* Create a new block */
		zPtr = zBlock;
		SyBigEndianPack16(zBlock,iNext); /* Offset of the next block */
		SyBigEndianPack16(&zBlock[2],iBlksz-nByte); /* Block size*/
		/* Offset of the new block */
		iNext = (sxu16)(zPtr - pPage->pRaw->zData);
	}
	/* Fix offsets */
	if( zPrev ){
		SyBigEndianPack16(zPrev,iNext);
	}else{
		/* First block */
		pPage->sHdr.iFree = iNext;
		/* Reflect on the page header */
		SyBigEndianPack16(&pPage->pRaw->zData[2/* Offset of the first cell1*/],iNext);
	}
	/* All done */
	pPage->nFree -= nByte;
	return UNQLITE_OK;
}

// Assembly:
movzwl 0x5c(%rdi), %ecx
movl $0xffffffb7, %eax       # imm = 0xFFFFFFB7
cmpq %rsi, %rcx
jb 0x38980
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r13
movq %rdi, %r14
cmpl $0xa, 0x38(%rdi)
movq (%rdi), %rdi
jae 0x386d1
movl 0x110(%rdi), %ecx
movl %ecx, %edx
shrl $0x1f, %edx
addl %ecx, %edx
sarl %edx
cmpl %esi, %edx
jg 0x386d7
jmp 0x38972
movl 0x110(%rdi), %ecx
movq %rdi, -0x38(%rbp)
movq 0x8(%r14), %rdi
movq (%rdi), %rdx
movzwl 0x12(%r14), %r8d
movslq %ecx, %rcx
xorl %r9d, %r9d
movzwl %r8w, %r15d
cmpq %rcx, %r15
jge 0x38972
addq %rdx, %r15
movzwl (%r15), %r8d
rolw $0x8, %r8w
movzwl 0x2(%r15), %ebx
rolw $0x8, %bx
cmpw %si, %bx
jae 0x38824
movq %r15, %r9
testw %r8w, %r8w
jne 0x386ed
movq %rsi, -0x48(%rbp)
movq %r13, -0x30(%rbp)
movq -0x38(%rbp), %rbx
movq (%rbx), %rax
movq (%rax), %rdi
callq *0x68(%rax)
movq %rax, %r8
movq 0x18(%r14), %rax
movq 0x28(%r14), %r13
bswapq %rax
movq %rax, 0x4(%r8)
leaq 0xc(%r8), %r15
movslq 0x110(%rbx), %r12
addq %r8, %r12
movw $0x0, 0x10(%r14)
testq %r13, %r13
je 0x38835
movq 0x20(%r13), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rcx
movq 0x8(%r14), %rdx
cmpq 0x10(%rdx), %rcx
jne 0x3881b
cmpq $0x0, 0x18(%r13)
je 0x3878b
xorl %edi, %edi
jmp 0x3879b
movq (%rax), %rax
movzwl 0x28(%r13), %ecx
leaq (%rax,%rcx), %rdi
addq $0x1a, %rdi
movzwl 0x10(%r14), %eax
movw %ax, 0x10(%r13)
movl %r15d, %eax
subl %r8d, %eax
movw %ax, 0x28(%r13)
movw %ax, 0x10(%r14)
movl (%r13), %eax
bswapl %eax
movl %eax, (%r15)
movl 0x4(%r13), %eax
bswapl %eax
movl %eax, 0x4(%r15)
movq 0x8(%r13), %rax
bswapq %rax
movq %rax, 0x8(%r15)
movzwl 0x10(%r13), %eax
rolw $0x8, %ax
movw %ax, 0x10(%r15)
movq 0x18(%r13), %rax
bswapq %rax
movq %rax, 0x12(%r15)
addq $0x1a, %r15
testq %rdi, %rdi
je 0x38816
movl 0x4(%r13), %edx
addl 0x8(%r13), %edx
movq %r15, %rsi
movq %r8, %rbx
callq 0x34893
movq %rbx, %r8
movl 0x4(%r13), %eax
addq 0x8(%r13), %r15
addq %rax, %r15
cmpq %r12, %r15
jae 0x38835
movq 0x60(%r13), %r13
jmp 0x3875d
movq %rsi, -0x48(%rbp)
movq %r8, -0x40(%rbp)
movq %r9, -0x30(%rbp)
jmp 0x388e1
subq %r15, %r12
movw %r12w, 0x5c(%r14)
testl $0xfffc, %r12d          # imm = 0xFFFC
je 0x38867
movl %r15d, %eax
subl %r8d, %eax
movw %ax, 0x12(%r14)
movw $0x0, (%r15)
movzwl 0x5c(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r15)
jmp 0x38873
xorl %eax, %eax
movw %ax, 0x5c(%r14)
movw %ax, 0x12(%r14)
movq -0x30(%rbp), %r13
movzwl 0x10(%r14), %eax
rolw $0x8, %ax
movw %ax, (%r8)
movzwl 0x12(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r8)
movq 0x8(%r14), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rax
movl 0x110(%rax), %edx
movq %r8, %rdi
callq 0x34893
movzwl 0x5c(%r14), %ebx
cmpl %ebx, -0x48(%rbp)
movl $0xffffffb7, %eax       # imm = 0xFFFFFFB7
ja 0x38972
movq (%r14), %rax
movq %rax, -0x38(%rbp)
movq 0x8(%r14), %rdi
movzwl 0x12(%r14), %r15d
addq (%rdi), %r15
movq $0x0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
callq *0x28(%rax)
testl %eax, %eax
movq -0x48(%rbp), %rsi
jne 0x38972
movq 0x8(%r14), %rax
movq (%rax), %rax
subq %rax, %r15
movw %r15w, (%r13)
movzwl %bx, %ecx
subl %esi, %ecx
setae %dl
cmpl $0x4, %ecx
setge %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x38948
movzwl %r15w, %edx
movl %esi, %ecx
addq %rdx, %rcx
movq -0x40(%rbp), %rdx
movb %dl, 0x1(%rax,%rcx)
movb %dh, (%rax,%rcx)
leaq (%rax,%rcx), %rdx
subl %esi, %ebx
movb %bl, 0x3(%rax,%rcx)
movb %bh, 0x2(%rax,%rcx)
movq 0x8(%r14), %rax
movq (%rax), %rax
subl %eax, %edx
movq -0x30(%rbp), %rcx
jmp 0x38950
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rdx
testq %rcx, %rcx
je 0x3895a
movb %dl, 0x1(%rcx)
jmp 0x38969
movw %dx, 0x12(%r14)
movb %dl, 0x3(%rax)
addq $0x2, %rax
movq %rax, %rcx
movb %dh, (%rcx)
subw %si, 0x5c(%r14)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq