// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: lhSplit
static int lhSplit(lhpage *pTarget,int *pRetry)
{
	lhash_kv_engine *pEngine = pTarget->pHash;
	lhash_bmap_rec *pRec;
	lhpage *pOld,*pNew;
	unqlite_page *pRaw;
	int rc;
	/* Get the real page number of the bucket to split */
	pRec = lhMapFindBucket(pEngine,pEngine->split_bucket);
	if( pRec == 0 ){
		/* Can't happen */
		return UNQLITE_CORRUPT;
	}
	/* Load the page to be split */
	rc = lhLoadPage(pEngine,pRec->iReal,0,&pOld,0);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Request a new page */
	rc = lhAcquirePage(pEngine,&pRaw);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Initialize the page */
	pNew = lhNewPage(pEngine,pRaw,0);
	if( pNew == 0 ){
		return UNQLITE_NOMEM;
	}
	/* Mark as an empty page */
	rc = lhSetEmptyPage(pNew);
	if( rc != UNQLITE_OK ){
		goto fail;
	}
	/* Install and write the logical map record */
	rc = lhMapWriteRecord(pEngine,
		pEngine->split_bucket + pEngine->max_split_bucket,
		pRaw->iPage
		);
	if( rc != UNQLITE_OK ){
		goto fail;
	}
	if( pTarget->pRaw->iPage == pOld->pRaw->iPage ){
		*pRetry = 1;
	}
	/* Perform the split */
	rc = lhPageSplit(pOld,pNew,pEngine->split_bucket,pEngine->nmax_split_nucket - 1);
	if( rc != UNQLITE_OK ){
		goto fail;
	}
	/* Update the database header */
	pEngine->split_bucket++;
	/* Acquire a writer lock on the first page */
	rc = pEngine->pIo->xWrite(pEngine->pHeader);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	if( pEngine->split_bucket >= pEngine->max_split_bucket ){
		/* Increment the generation number */
		pEngine->split_bucket = 0;
		pEngine->max_split_bucket = pEngine->nmax_split_nucket;
		pEngine->nmax_split_nucket <<= 1;
		if( !pEngine->nmax_split_nucket ){
			/* If this happen to your installation, please tell us <chm@symisc.net> */
			pEngine->pIo->xErr(pEngine->pIo->pHandle,"Database page (64-bit integer) limit reached");
			return UNQLITE_LIMIT;
		}
		/* Reflect in the page header */
		SyBigEndianPack64(&pEngine->pHeader->zData[4/*Magic*/+4/*Hash*/+8/*Free list*/],pEngine->split_bucket);
		SyBigEndianPack64(&pEngine->pHeader->zData[4/*Magic*/+4/*Hash*/+8/*Free list*/+8/*Split bucket*/],pEngine->max_split_bucket);
	}else{
		/* Modify only the split bucket */
		SyBigEndianPack64(&pEngine->pHeader->zData[4/*Magic*/+4/*Hash*/+8/*Free list*/],pEngine->split_bucket);
	}
	/* All done */
	return UNQLITE_OK;
fail:
	pEngine->pIo->xPageUnref(pNew->pRaw);
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x120(%rax), %rsi
callq 0x62520
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x658e6
movl $0xffffffe8, -0x4(%rbp) # imm = 0xFFFFFFE8
jmp 0x65b63
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq -0x30(%rbp), %rcx
xorl %r8d, %r8d
callq 0x62dc0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x65916
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x65b63
movq -0x20(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x625b0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x65937
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x65b63
movq -0x20(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x626e0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x6595f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x65b63
movq -0x38(%rbp), %rdi
callq 0x627c0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x65976
jmp 0x65b48
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x120(%rax), %rsi
movq -0x20(%rbp), %rax
addq 0x128(%rax), %rsi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x62ae0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x659ab
jmp 0x65b48
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpq 0x10(%rcx), %rax
jne 0x659cf
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x120(%rax), %rdx
movq -0x20(%rbp), %rax
movq 0x130(%rax), %rcx
subq $0x1, %rcx
callq 0x65b70
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x65a04
jmp 0x65b48
movq -0x20(%rbp), %rax
movq 0x120(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x120(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0xd0(%rcx), %rdi
callq *%rax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x65a46
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x65b63
movq -0x20(%rbp), %rax
movq 0x120(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x128(%rcx), %rax
jb 0x65b1d
movq -0x20(%rbp), %rax
movq $0x0, 0x120(%rax)
movq -0x20(%rbp), %rax
movq 0x130(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x128(%rax)
movq -0x20(%rbp), %rax
movq 0x130(%rax), %rcx
shlq %rcx
movq %rcx, 0x130(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x130(%rax)
jne 0x65ad7
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x80(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rdi
leaq 0x5c2ff(%rip), %rsi     # 0xc1dc8
callq *%rax
movl $0xfffffff9, -0x4(%rbp) # imm = 0xFFFFFFF9
jmp 0x65b63
movq -0x20(%rbp), %rax
movq 0xd0(%rax), %rax
movq (%rax), %rdi
addq $0x10, %rdi
movq -0x20(%rbp), %rax
movq 0x120(%rax), %rsi
callq 0x61c60
movq -0x20(%rbp), %rax
movq 0xd0(%rax), %rax
movq (%rax), %rdi
addq $0x18, %rdi
movq -0x20(%rbp), %rax
movq 0x128(%rax), %rsi
callq 0x61c60
jmp 0x65b3f
movq -0x20(%rbp), %rax
movq 0xd0(%rax), %rax
movq (%rax), %rdi
addq $0x10, %rdi
movq -0x20(%rbp), %rax
movq 0x120(%rax), %rsi
callq 0x61c60
movl $0x0, -0x4(%rbp)
jmp 0x65b63
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)