// File: /workspace/github_c_makefile_star2_O2/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), %eax
pushq $-0x49
popq %r9
cmpq %rsi, %rax
jb 0x31185
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r8
movq %rdi, %r14
cmpl $0xa, 0x38(%rdi)
movq (%rdi), %rdi
jae 0x30ee2
movl 0x110(%rdi), %ecx
pushq $0x2
popq %r10
movl %ecx, %eax
cltd
idivl %r10d
cmpl %esi, %eax
jg 0x30ee8
jmp 0x31177
movl 0x110(%rdi), %ecx
movq %rdi, -0x30(%rbp)
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movzwl 0x12(%r14), %r13d
movslq %ecx, %rcx
xorl %edx, %edx
movzwl %r13w, %r15d
cmpq %rcx, %r15
jge 0x31177
addq %rax, %r15
movzwl (%r15), %r13d
rolw $0x8, %r13w
movzwl 0x2(%r15), %ebx
rolw $0x8, %bx
cmpw %si, %bx
jae 0x3103c
movq %r15, %rdx
testw %r13w, %r13w
jne 0x30efd
movq %rsi, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movq -0x30(%rbp), %r12
movq (%r12), %rax
movq (%rax), %rdi
callq *0x68(%rax)
movq %rax, %r15
movq 0x18(%r14), %rsi
movq 0x28(%r14), %rbx
leaq 0x4(%rax), %rdi
callq 0x2f66d
movq %r15, %rdi
addq $0xc, %r15
movslq 0x110(%r12), %rax
addq %rdi, %rax
movq %rax, -0x48(%rbp)
andw $0x0, 0x10(%r14)
testq %rbx, %rbx
je 0x3104c
movq 0x20(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rcx
movq 0x8(%r14), %rdx
cmpq 0x10(%rdx), %rcx
jne 0x31033
cmpq $0x0, 0x18(%rbx)
je 0x30fa6
xorl %r12d, %r12d
jmp 0x30fb5
movq (%rax), %rax
movzwl 0x28(%rbx), %ecx
leaq (%rax,%rcx), %r12
addq $0x1a, %r12
movzwl 0x10(%r14), %eax
movw %ax, 0x10(%rbx)
movl %r15d, %eax
movq %rdi, %r13
subl %edi, %eax
movw %ax, 0x28(%rbx)
movw %ax, 0x10(%r14)
movl (%rbx), %eax
bswapl %eax
movl %eax, (%r15)
movl 0x4(%rbx), %eax
bswapl %eax
movl %eax, 0x4(%r15)
leaq 0x8(%r15), %rdi
movq 0x8(%rbx), %rsi
callq 0x2f66d
movzwl 0x10(%rbx), %eax
rolw $0x8, %ax
movw %ax, 0x10(%r15)
leaq 0x12(%r15), %rdi
movq 0x18(%rbx), %rsi
callq 0x2f66d
addq $0x1a, %r15
testq %r12, %r12
je 0x3102a
movl 0x4(%rbx), %edx
addl 0x8(%rbx), %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2d3b0
movl 0x4(%rbx), %eax
addq 0x8(%rbx), %r15
addq %rax, %r15
cmpq -0x48(%rbp), %r15
movq %r13, %rdi
jae 0x3104c
movq 0x60(%rbx), %rbx
jmp 0x30f77
movq %rdx, %r12
movq %rsi, -0x38(%rbp)
movq %r8, -0x40(%rbp)
jmp 0x310e3
movq -0x48(%rbp), %rax
subq %r15, %rax
movw %ax, 0x5c(%r14)
testw $0xfffc, %ax            # imm = 0xFFFC
je 0x3107d
movl %r15d, %eax
subl %edi, %eax
movw %ax, 0x12(%r14)
andw $0x0, (%r15)
movzwl 0x5c(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r15)
jmp 0x31089
andw $0x0, 0x5c(%r14)
andw $0x0, 0x12(%r14)
movzwl 0x10(%r14), %eax
rolw $0x8, %ax
movw %ax, (%rdi)
movzwl 0x12(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rdi)
movq 0x8(%r14), %rax
movq (%rax), %rsi
movq -0x30(%rbp), %rax
movl 0x110(%rax), %edx
callq 0x2d3b0
movzwl 0x5c(%r14), %ebx
cmpl %ebx, -0x38(%rbp)
pushq $-0x49
popq %r9
ja 0x31177
movq (%r14), %rax
movq %rax, -0x30(%rbp)
movq 0x8(%r14), %rdi
movzwl 0x12(%r14), %r15d
addq (%rdi), %r15
xorl %r13d, %r13d
xorl %r12d, %r12d
movq -0x30(%rbp), %rax
movq (%rax), %rax
callq *0x28(%rax)
movl %eax, %r9d
testl %eax, %eax
movq -0x38(%rbp), %rsi
jne 0x31177
movq 0x8(%r14), %rax
movq (%rax), %rax
subq %rax, %r15
movq -0x40(%rbp), %rcx
movw %r15w, (%rcx)
movzwl %bx, %ecx
subl %esi, %ecx
setae %dl
cmpl $0x4, %ecx
setge %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x3114e
movzwl %r15w, %edx
movl %esi, %ecx
addq %rdx, %rcx
movq %r13, %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 %r12, %rcx
jmp 0x31154
movq %r12, %rcx
movq %r13, %rdx
testq %rcx, %rcx
je 0x3115e
movb %dl, 0x1(%rcx)
jmp 0x3116d
movw %dx, 0x12(%r14)
movb %dl, 0x3(%rax)
addq $0x2, %rax
movq %rax, %rcx
movb %dh, (%rcx)
subw %si, 0x5c(%r14)
xorl %r9d, %r9d
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movl %r9d, %eax
retq