// File: /workspace/github_c_makefile_star2_O2/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 %rcx, -0x40(%rbp)
movq %rdx, -0x38(%rbp)
movq %rsi, %r12
movq (%rdi), %rax
movq %rax, -0x50(%rbp)
movq 0x40(%rdi), %r13
movq 0x48(%r13), %rbx
addq $0x1a, %r12
xorl %r15d, %r15d
leaq -0x2a(%rbp), %r14
cmpl 0x58(%r13), %r15d
jge 0x31889
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x30e98
testl %eax, %eax
je 0x3186b
movq %rbx, %rdi
pushq $0x1a
popq %rsi
movq %r14, %rdx
callq 0x30e98
testl %eax, %eax
je 0x3186b
movq 0x50(%rbx), %rbx
incl %r15d
jmp 0x31838
movq -0x38(%rbp), %rcx
testq %rcx, %rcx
je 0x3187b
movzwl -0x2a(%rbp), %eax
movw %ax, (%rcx)
movq -0x40(%rbp), %rax
movq %rbx, (%rax)
xorl %ebx, %ebx
jmp 0x3192b
leaq -0x58(%rbp), %rsi
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x3064e
movl %eax, %ebx
testl %eax, %eax
jne 0x3192b
movq 0x48(%r13), %r14
testq %r14, %r14
cmoveq %r13, %r14
movq -0x58(%rbp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x306e9
testq %rax, %rax
je 0x31928
movq %rax, %r13
movq %rbx, -0x48(%rbp)
movq %rax, %rdi
callq 0x30756
testl %eax, %eax
jne 0x3191a
cmpq $0x0, -0x38(%rbp)
je 0x3190c
leaq -0x2a(%rbp), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x30e98
testl %eax, %eax
je 0x31901
pushq $0x1a
popq %rsi
leaq -0x2a(%rbp), %rdx
movq %r13, %rdi
callq 0x30e98
movzwl -0x2a(%rbp), %eax
movq -0x38(%rbp), %rcx
movw %ax, (%rcx)
movq (%r15), %rax
movq 0x8(%r14), %rdi
callq *0x28(%rax)
testl %eax, %eax
je 0x3193c
movl %eax, %ebx
movq (%r15), %rax
movq 0x8(%r13), %rdi
callq *0x50(%rax)
jmp 0x3192b
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rax
movq (%rax), %rdi
addq $0x4, %rdi
movq -0x48(%rbp), %rbx
movq 0x10(%rbx), %rsi
callq 0x2f66d
movq 0x10(%rbx), %rax
movq %rax, 0x18(%r14)
movq -0x40(%rbp), %rax
movq %r13, (%rax)
jmp 0x31882