// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jge 0x6502a
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x1a, %rsi
leaq -0x52(%rbp), %rdx
callq 0x63d40
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x64fe1
movq -0x40(%rbp), %rdi
movl $0x1a, %esi
leaq -0x52(%rbp), %rdx
callq 0x63d40
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x65010
cmpq $0x0, -0x20(%rbp)
je 0x64ff9
movw -0x52(%rbp), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x65161
movq -0x40(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x64fa2
movq -0x30(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x625b0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x6504b
movl -0x5c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x65161
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x65066
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x626e0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x6508e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x65161
movq -0x50(%rbp), %rdi
callq 0x627c0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x650a5
jmp 0x65146
cmpq $0x0, -0x20(%rbp)
je 0x650e6
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x1a, %rsi
leaq -0x52(%rbp), %rdx
callq 0x63d40
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x650db
movq -0x50(%rbp), %rdi
movl $0x1a, %esi
leaq -0x52(%rbp), %rdx
callq 0x63d40
movw -0x52(%rbp), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x65106
jmp 0x65146
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
addq $0x4, %rdi
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x61c60
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x65161
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movl -0x5c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)