// File: /workspace/github_c_makefile_star2_O1/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 0x3921b
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 $0x9, 0x38(%rdi)
ja 0x38f5b
movq (%r14), %rcx
movl 0x110(%rcx), %edx
movl %edx, %ecx
shrl $0x1f, %ecx
addl %edx, %ecx
sarl %ecx
cmpl %esi, %ecx
jle 0x3920d
movq (%r14), %rdx
movq 0x8(%r14), %rcx
movq (%rcx), %r9
movzwl 0x12(%r14), %r8d
movq %rdx, -0x40(%rbp)
movslq 0x110(%rdx), %rcx
movzwl %si, %edx
xorl %edi, %edi
movzwl %r8w, %r15d
cmpq %rcx, %r15
jge 0x3920d
addq %r9, %r15
movzwl (%r15), %r8d
rolw $0x8, %r8w
movzwl 0x2(%r15), %ebx
rolw $0x8, %bx
cmpw %si, %bx
jae 0x390b4
movq %r15, %rdi
testw %r8w, %r8w
jne 0x38f7a
movq %rsi, -0x48(%rbp)
movl %edx, -0x2c(%rbp)
movq %r13, -0x38(%rbp)
movq -0x40(%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 0x390c8
movq 0x20(%r13), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rcx
movq 0x8(%r14), %rdx
cmpq 0x10(%rdx), %rcx
jne 0x390ab
cmpq $0x0, 0x18(%r13)
je 0x3901b
xorl %edi, %edi
jmp 0x3902b
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 0x390a6
movl 0x4(%r13), %edx
addl 0x8(%r13), %edx
movq %r15, %rsi
movq %r8, %rbx
callq 0x34fc5
movq %rbx, %r8
movl 0x4(%r13), %eax
addq 0x8(%r13), %r15
addq %rax, %r15
cmpq %r12, %r15
jae 0x390c8
movq 0x60(%r13), %r13
jmp 0x38fed
movq %rsi, -0x48(%rbp)
movq %r8, -0x38(%rbp)
movq %rdi, -0x40(%rbp)
movl %edx, -0x2c(%rbp)
jmp 0x3916d
subq %r15, %r12
movw %r12w, 0x5c(%r14)
testl $0xfffc, %r12d          # imm = 0xFFFC
je 0x390fa
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 0x39106
xorl %eax, %eax
movw %ax, 0x5c(%r14)
movw %ax, 0x12(%r14)
movq -0x38(%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 -0x40(%rbp), %rax
movl 0x110(%rax), %edx
movq %r8, %rdi
callq 0x34fc5
movzwl 0x5c(%r14), %ebx
cmpl %ebx, -0x2c(%rbp)
movl $0xffffffb7, %eax       # imm = 0xFFFFFFB7
ja 0x3920d
movq 0x8(%r14), %rax
movzwl 0x12(%r14), %r15d
addq (%rax), %r15
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq (%r14), %rax
movq 0x8(%r14), %rdi
movq (%rax), %rax
callq *0x28(%rax)
testl %eax, %eax
jne 0x3920d
movq 0x8(%r14), %rax
movq (%rax), %rax
subq %rax, %r15
movw %r15w, (%r13)
movzwl %bx, %ecx
movl -0x2c(%rbp), %esi
subl %esi, %ecx
setae %dl
cmpl $0x4, %ecx
setge %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x391db
movzwl %r15w, %edx
movl %esi, %ecx
addq %rdx, %rcx
movq -0x38(%rbp), %rdx
movb %dl, 0x1(%rax,%rcx)
movb %dh, (%rax,%rcx)
leaq (%rax,%rcx), %rdx
movq -0x48(%rbp), %rsi
subl %esi, %ebx
movb %bl, 0x3(%rax,%rcx)
movb %bh, 0x2(%rax,%rcx)
movq 0x8(%r14), %rax
subl (%rax), %edx
movq -0x40(%rbp), %rax
jmp 0x391e7
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rdx
movq -0x48(%rbp), %rsi
testq %rax, %rax
je 0x391f1
movb %dl, 0x1(%rax)
jmp 0x39204
movw %dx, 0x12(%r14)
movq 0x8(%r14), %rax
movq (%rax), %rax
movb %dl, 0x3(%rax)
addq $0x2, %rax
movb %dh, (%rax)
subw %si, 0x5c(%r14)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq