// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x5c(%rax), %eax
cmpq -0x18(%rbp), %rax
jae 0x63d6e
movl $0xffffffb7, -0x4(%rbp) # imm = 0xFFFFFFB7
jmp 0x63feb
movq -0x10(%rbp), %rax
cmpl $0xa, 0x38(%rax)
jae 0x63da9
movq -0x18(%rbp), %rax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl 0x110(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x54(%rbp), %eax
cmpl %ecx, %eax
jl 0x63da9
movl $0xffffffb7, -0x4(%rbp) # imm = 0xFFFFFFB7
jmp 0x63feb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movslq 0x110(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movw %ax, -0x36(%rbp)
movq $0x0, -0x40(%rbp)
movw $0x0, -0x34(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x63e0f
movl $0xffffffb7, -0x4(%rbp) # imm = 0xFFFFFFB7
jmp 0x63feb
movq -0x30(%rbp), %rdi
leaq -0x32(%rbp), %rsi
callq 0x64c70
movq -0x30(%rbp), %rdi
addq $0x2, %rdi
leaq -0x34(%rbp), %rsi
callq 0x64c70
movzwl -0x34(%rbp), %eax
movzwl -0x36(%rbp), %ecx
cmpl %ecx, %eax
jl 0x63e3e
jmp 0x63ece
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movzwl -0x32(%rbp), %eax
cmpl $0x0, %eax
jne 0x63eb3
movq -0x10(%rbp), %rdi
callq 0x64ca0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0x63ea7
movq -0x10(%rbp), %rax
movzwl 0x5c(%rax), %eax
movzwl -0x36(%rbp), %ecx
cmpl %ecx, %eax
jl 0x63ea7
movw $0x0, -0x32(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movw 0x5c(%rax), %ax
movw %ax, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
jmp 0x63ece
movl $0xffffffb7, -0x4(%rbp) # imm = 0xFFFFFFB7
jmp 0x63feb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movzwl -0x32(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0x63df9
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x63efa
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x63feb
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movzwl -0x34(%rbp), %eax
movzwl -0x36(%rbp), %ecx
cmpl %ecx, %eax
jl 0x63f98
movzwl -0x34(%rbp), %eax
movzwl -0x36(%rbp), %ecx
subl %ecx, %eax
cmpl $0x3, %eax
jle 0x63f98
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movzwl (%rcx), %ecx
movzwl -0x36(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %rdi
movzwl -0x32(%rbp), %esi
callq 0x63d00
movq -0x50(%rbp), %rdi
addq $0x2, %rdi
movzwl -0x34(%rbp), %eax
movzwl -0x36(%rbp), %ecx
subl %ecx, %eax
movzwl %ax, %esi
callq 0x63d00
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
movw %ax, -0x32(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x63fae
movq -0x40(%rbp), %rdi
movzwl -0x32(%rbp), %esi
callq 0x63d00
jmp 0x63fd2
movw -0x32(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x12(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
addq $0x2, %rdi
movzwl -0x32(%rbp), %esi
callq 0x63d00
movzwl -0x36(%rbp), %edx
movq -0x10(%rbp), %rax
movzwl 0x5c(%rax), %ecx
subl %edx, %ecx
movw %cx, 0x5c(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)