// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: lhFindSlavePage
static int lhFindSlavePage(lhpage *pPage,sxu64 nAmount,sxu16 *pOfft,lhpage **ppSlave)
{
	lhash_kv_engine *pEngine = pPage->pHash;
	lhpage *pMaster = pPage->pMaster;
	lhpage *pSlave = pMaster->pSlave;
	unqlite_page *pRaw;
	lhpage *pNew;
	sxu16 iOfft;
	sxi32 i;
	int rc;
	/* Look for an already attached slave page */
	for( i = 0 ; i < pMaster->iSlave ; ++i ){
		/* Find a free chunk big enough */
		rc = lhAllocateSpace(pSlave,L_HASH_CELL_SZ+nAmount,&iOfft);
		if( rc != UNQLITE_OK ){
			/* A space for cell header only */
			rc = lhAllocateSpace(pSlave,L_HASH_CELL_SZ,&iOfft);
		}
		if( rc == UNQLITE_OK ){
			/* All done */
			if( pOfft ){
				*pOfft = iOfft;
			}
			*ppSlave = pSlave;
			return UNQLITE_OK;
		}
		/* Point to the next slave page */
		pSlave = pSlave->pNextSlave;
	}
	/* Acquire a new slave page */
	rc = lhAcquirePage(pEngine,&pRaw);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Last slave page */
	pSlave = pMaster->pSlave;
	if( pSlave == 0 ){
		/* First slave page */
		pSlave = pMaster;
	}
	/* Initialize the page */
	pNew = lhNewPage(pEngine,pRaw,pMaster);
	if( pNew == 0 ){
		return UNQLITE_NOMEM;
	}
	/* Mark as an empty page */
	rc = lhSetEmptyPage(pNew);
	if( rc != UNQLITE_OK ){
		goto fail;
	}
	if( pOfft ){
		/* Look for a free block */
		if( UNQLITE_OK != lhAllocateSpace(pNew,L_HASH_CELL_SZ+nAmount,&iOfft) ){
			/* Cell header only */
			lhAllocateSpace(pNew,L_HASH_CELL_SZ,&iOfft); /* Never fail */
		}	
		*pOfft = iOfft;
	}
	/* Link this page to the previous slave page */
	rc = pEngine->pIo->xWrite(pSlave->pRaw);
	if( rc != UNQLITE_OK ){
		goto fail;
	}
	/* Reflect in the page header */
	SyBigEndianPack64(&pSlave->pRaw->zData[2/*Cell offset*/+2/*Free block offset*/],pRaw->iPage);
	pSlave->sHdr.iSlave = pRaw->iPage;
	/* All done */
	*ppSlave = pNew;
	return UNQLITE_OK;
fail:
	pEngine->pIo->xPageUnref(pNew->pRaw); /* pNew will be released in this call */
	return rc;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rsi, %r13
movq %rdi, -0x50(%rbp)
cmpl $0x0, 0x58(%rsi)
jle 0x39038
movq 0x48(%r13), %rbx
movq -0x48(%rbp), %rax
leaq 0x1a(%rax), %r15
xorl %r12d, %r12d
leaq -0x2a(%rbp), %r14
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x38687
testl %eax, %eax
je 0x390dc
movl $0x1a, %esi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x38687
testl %eax, %eax
je 0x390dc
movq 0x50(%rbx), %rbx
incl %r12d
cmpl 0x58(%r13), %r12d
jl 0x38ffd
leaq -0x58(%rbp), %rsi
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x37ddc
movl %eax, %ebx
testl %eax, %eax
jne 0x390f5
movq 0x48(%r13), %r14
testq %r14, %r14
cmoveq %r13, %r14
movq -0x58(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x37e75
testq %rax, %rax
je 0x39106
movq %rax, %r13
movq %rax, %rdi
callq 0x37eed
testl %eax, %eax
jne 0x390ce
movq -0x40(%rbp), %rbx
testq %rbx, %rbx
je 0x390c0
movq -0x48(%rbp), %rsi
addq $0x1a, %rsi
leaq -0x2a(%rbp), %rdx
movq %r13, %rdi
callq 0x38687
testl %eax, %eax
je 0x390b9
leaq -0x2a(%rbp), %rdx
movl $0x1a, %esi
movq %r13, %rdi
callq 0x38687
movzwl -0x2a(%rbp), %eax
movw %ax, (%rbx)
movq (%r15), %rax
movq 0x8(%r14), %rdi
callq *0x28(%rax)
testl %eax, %eax
je 0x3910d
movl %eax, %ebx
movq (%r15), %rax
movq 0x8(%r13), %rdi
callq *0x50(%rax)
jmp 0x390f5
movq -0x40(%rbp), %rcx
testq %rcx, %rcx
je 0x390ec
movzwl -0x2a(%rbp), %eax
movw %ax, (%rcx)
movq -0x38(%rbp), %rax
movq %rbx, (%rax)
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x390f5
movq 0x8(%r14), %rax
movq (%rax), %rax
movq 0x10(%r12), %rcx
bswapq %rcx
movq %rcx, 0x4(%rax)
movq 0x10(%r12), %rax
movq %rax, 0x18(%r14)
movq -0x38(%rbp), %rax
movq %r13, (%rax)
jmp 0x390f3