// File: /workspace/github_cpp_makefile_star2_O1/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_page_merge
static int
mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
{
	MDB_page	*psrc, *pdst;
	MDB_node	*srcnode;
	MDB_val		 key, data;
	unsigned	 nkeys;
	int			 rc;
	indx_t		 i, j;

	psrc = csrc->mc_pg[csrc->mc_top];
	pdst = cdst->mc_pg[cdst->mc_top];

	DPRINTF(("merging page %"Z"u into %"Z"u", psrc->mp_pgno, pdst->mp_pgno));

	mdb_cassert(csrc, csrc->mc_snum > 1);	/* can't merge root page */
	mdb_cassert(csrc, cdst->mc_snum > 1);

	/* Mark dst as dirty. */
	if ((rc = mdb_page_touch(cdst)))
		return rc;

	/* Move all nodes from src to dst.
	 */
	j = nkeys = NUMKEYS(pdst);
	if (IS_LEAF2(psrc)) {
		key.mv_size = csrc->mc_db->md_pad;
		key.mv_data = METADATA(psrc);
		for (i = 0; i < NUMKEYS(psrc); i++, j++) {
			rc = mdb_node_add(cdst, j, &key, NULL, 0, 0);
			if (rc != MDB_SUCCESS)
				return rc;
			key.mv_data = (char *)key.mv_data + key.mv_size;
		}
	} else {
		for (i = 0; i < NUMKEYS(psrc); i++, j++) {
			srcnode = NODEPTR(psrc, i);
			if (i == 0 && IS_BRANCH(psrc)) {
				MDB_cursor mn;
				MDB_node *s2;
				mdb_cursor_copy(csrc, &mn);
				/* must find the lowest key below src */
				rc = mdb_page_search_lowest(&mn);
				if (rc)
					return rc;
				if (IS_LEAF2(mn.mc_pg[mn.mc_top])) {
					key.mv_size = mn.mc_db->md_pad;
					key.mv_data = LEAF2KEY(mn.mc_pg[mn.mc_top], 0, key.mv_size);
				} else {
					s2 = NODEPTR(mn.mc_pg[mn.mc_top], 0);
					key.mv_size = NODEKSZ(s2);
					key.mv_data = NODEKEY(s2);
				}
			} else {
				key.mv_size = srcnode->mn_ksize;
				key.mv_data = NODEKEY(srcnode);
			}

			data.mv_size = NODEDSZ(srcnode);
			data.mv_data = NODEDATA(srcnode);
			rc = mdb_node_add(cdst, j, &key, &data, NODEPGNO(srcnode), srcnode->mn_flags);
			if (rc != MDB_SUCCESS)
				return rc;
		}
	}

	DPRINTF(("dst page %"Z"u now has %u keys (%.1f%% filled)",
	    pdst->mp_pgno, NUMKEYS(pdst),
		(float)PAGEFILL(cdst->mc_txn->mt_env, pdst) / 10));

	/* Unlink the src page from parent and add to free list.
	 */
	csrc->mc_top--;
	mdb_node_del(csrc, 0);
	if (csrc->mc_ki[csrc->mc_top] == 0) {
		key.mv_size = 0;
		rc = mdb_update_key(csrc, &key);
		if (rc) {
			csrc->mc_top++;
			return rc;
		}
	}
	csrc->mc_top++;

	psrc = csrc->mc_pg[csrc->mc_top];
	/* If not operating on FreeDB, allow this page to be reused
	 * in this txn. Otherwise just add to free list.
	 */
	rc = mdb_page_loose(csrc, psrc);
	if (rc)
		return rc;
	if (IS_LEAF(psrc))
		csrc->mc_db->md_leaf_pages--;
	else
		csrc->mc_db->md_branch_pages--;
	{
		/* Adjust other cursors pointing to mp */
		MDB_cursor *m2, *m3;
		MDB_dbi dbi = csrc->mc_dbi;

		for (m2 = csrc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
			if (csrc->mc_flags & C_SUB)
				m3 = &m2->mc_xcursor->mx_cursor;
			else
				m3 = m2;
			if (m3 == csrc) continue;
			if (m3->mc_snum < csrc->mc_snum) continue;
			if (m3->mc_pg[csrc->mc_top] == psrc) {
				m3->mc_pg[csrc->mc_top] = pdst;
				m3->mc_ki[csrc->mc_top] += nkeys;
			}
		}
	}
	{
		unsigned int snum = cdst->mc_snum;
		uint16_t depth = cdst->mc_db->md_depth;
		mdb_cursor_pop(cdst);
		rc = mdb_rebalance(cdst);
		/* Did the tree shrink? */
		if (depth > cdst->mc_db->md_depth)
			snum--;
		cdst->mc_snum = snum;
		cdst->mc_top = snum-1;
	}
	return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdi, %r14
cmpw $0x1, 0x40(%rdi)
jbe 0xbd30
movq %rsi, %rbx
cmpw $0x1, 0x40(%rsi)
jbe 0xbd50
movzwl 0x42(%r14), %eax
movq 0x48(%r14,%rax,8), %r12
movzwl 0x42(%rbx), %eax
movq 0x48(%rbx,%rax,8), %r15
movq %rbx, %rdi
callq 0xa4f1
testl %eax, %eax
je 0xb979
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x28(%rsp)
movzwl 0xc(%r15), %eax
addl $0x1fff0, %eax          # imm = 0x1FFF0
shrl %eax
movq %rax, 0x20(%rsp)
testb $0x20, 0xa(%r12)
jne 0xbabe
movzwl 0xc(%r12), %eax
andl $-0x2, %eax
cmpw $0x10, %ax
je 0xbb33
xorl %r15d, %r15d
leaq 0x40(%rsp), %rbp
movzwl %r15w, %eax
movzwl 0x10(%r12,%rax,2), %r13d
addq %r12, %r13
testw %r15w, %r15w
jne 0xb9d1
testb $0x1, 0xa(%r12)
jne 0xba4f
movzwl 0x6(%r13), %eax
movq %rax, 0x8(%rsp)
leaq 0x8(%r13), %rax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
addl %r15d, %eax
movl (%r13), %ecx
movq %rcx, 0x30(%rsp)
movzwl 0x6(%r13), %edx
leaq 0x8(%r13,%rdx), %rdx
movq %rdx, 0x38(%rsp)
movzwl 0x4(%r13), %r9d
movq %r9, %r8
shlq $0x20, %r8
orq %rcx, %r8
movzwl %ax, %esi
movq %rbx, %rdi
leaq 0x8(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x8b01
testl %eax, %eax
jne 0xb967
incl %r15d
movzwl %r15w, %eax
movzwl 0xc(%r12), %ecx
addl $-0x10, %ecx
shrl %ecx
cmpl %eax, %ecx
ja 0xb9b6
jmp 0xbb33
movq %r14, %rdi
movq %rbp, %rsi
callq 0xb8b7
movq %rbp, %rdi
callq 0xbd70
movl %eax, %ecx
testl %eax, %eax
jne 0xbaad
movzwl 0x82(%rsp), %eax
movq 0x88(%rsp,%rax,8), %rax
testb $0x20, 0xa(%rax)
jne 0xba94
movzwl 0x10(%rax), %edx
addq %rdx, %rax
addq $0x8, %rax
movzwl -0x2(%rax), %edx
movq %rdx, 0x8(%rsp)
jmp 0xbaa4
movq 0x68(%rsp), %rdx
movl (%rdx), %edx
movq %rdx, 0x8(%rsp)
addq $0x10, %rax
movq %rax, 0x10(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x18(%rsp)
testl %ecx, %ecx
je 0xb9e4
jmp 0xb967
movq 0x28(%r14), %rax
movl (%rax), %eax
movq %rax, 0x8(%rsp)
leaq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movzwl 0xc(%r12), %eax
andl $-0x2, %eax
cmpw $0x10, %ax
je 0xbb33
movw $0x1, %r13w
leaq 0x8(%rsp), %r15
movq 0x20(%rsp), %rax
movl %eax, %ebp
movzwl %bp, %esi
movq %rbx, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8b01
testl %eax, %eax
jne 0xb967
movq 0x8(%rsp), %rax
addq %rax, 0x10(%rsp)
incl %ebp
movzwl %r13w, %eax
movzwl 0xc(%r12), %ecx
addl $-0x10, %ecx
shrl %ecx
incl %r13d
cmpl %eax, %ecx
ja 0xbaf3
decw 0x42(%r14)
xorl %r13d, %r13d
movq %r14, %rdi
xorl %esi, %esi
callq 0x776d
movzwl 0x42(%r14), %eax
cmpw $0x0, 0x148(%r14,%rax,2)
je 0xbbea
movzwl 0x42(%r14), %eax
incl %eax
movw %ax, 0x42(%r14)
movzwl %ax, %eax
movq 0x48(%r14,%rax,8), %r12
movq (%r12), %rbp
movq 0x18(%r14), %r15
testb $0x10, 0xa(%r12)
je 0xbc23
cmpl $0x0, 0x20(%r14)
je 0xbc23
cmpq $0x0, (%r15)
je 0xbc10
movq 0x48(%r15), %rax
movb $0x1, %cl
xorl %r13d, %r13d
cmpq $0x0, (%rax)
je 0xbc1a
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0xc310
movq 0x18(%rsp), %rcx
movl %eax, %eax
cmpq %rax, (%rcx)
jb 0xbc18
shlq $0x4, %rax
cmpq %rbp, (%rcx,%rax)
jne 0xbc18
addq %rax, %rcx
cmpq %r12, 0x8(%rcx)
movb $0x1, %cl
je 0xbd25
andb $-0x4, 0x44(%r14)
orb $0x2, 0x7c(%r15)
xorl %r13d, %r13d
xorl %ecx, %ecx
jmp 0xbc1a
leaq 0x8(%rsp), %rsi
movq $0x0, (%rsi)
movq %r14, %rdi
callq 0x71ec
testl %eax, %eax
je 0xbb5a
incw 0x42(%r14)
jmp 0xb967
movl $0x1, %r13d
jmp 0xbc23
movb $0x1, %cl
movl $0xffff87b4, %eax       # imm = 0xFFFF87B4
testb %cl, %cl
je 0xbc60
testl %r13d, %r13d
je 0xbc43
movq 0x30(%r15), %rax
movq %rax, 0x30(%r12)
movq %r12, 0x30(%r15)
incl 0x38(%r15)
orb $0x40, 0xb(%r12)
xorl %eax, %eax
jmp 0xbc60
addq $0x28, %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0xbfd5
movl %eax, %ecx
movl $0xffff87b4, %eax       # imm = 0xFFFF87B4
testl %ecx, %ecx
cmovnel %ecx, %eax
je 0xbc3f
testl %eax, %eax
jne 0xb967
movzwl 0xa(%r12), %eax
andl $0x2, %eax
movq 0x28(%r14), %rcx
decq 0x8(%rcx,%rax,4)
movl 0x20(%r14), %eax
movq 0x18(%r14), %rcx
movq 0x68(%rcx), %rcx
movq (%rcx,%rax,8), %rax
testq %rax, %rax
movq 0x28(%rsp), %rdi
je 0xbcdb
movl 0x44(%r14), %ecx
movq 0x20(%rsp), %r8
movq %rax, %rdx
testb $0x4, %cl
je 0xbca9
movq 0x10(%rax), %rdx
cmpq %r14, %rdx
je 0xbcd3
movzwl 0x40(%rdx), %esi
cmpw 0x40(%r14), %si
jb 0xbcd3
movzwl 0x42(%r14), %esi
cmpq %r12, 0x48(%rdx,%rsi,8)
jne 0xbcd3
movq %rdi, 0x48(%rdx,%rsi,8)
addw %r8w, 0x148(%rdx,%rsi,2)
movq (%rax), %rax
testq %rax, %rax
jne 0xbc9d
movzwl 0x40(%rbx), %r14d
movq 0x28(%rbx), %rax
movzwl 0x6(%rax), %ebp
testw %r14w, %r14w
je 0xbcfe
movl %r14d, %eax
decw %ax
movw %ax, 0x40(%rbx)
je 0xbcfe
decw 0x42(%rbx)
movq %rbx, %rdi
callq 0xae39
movq 0x28(%rbx), %rcx
cmpw %bp, 0x6(%rcx)
sbbw $0x0, %r14w
movw %r14w, 0x40(%rbx)
leal -0x1(%r14), %ecx
movw %cx, 0x42(%rbx)
jmp 0xb967
movl $0x1, %r13d
jmp 0xbc1a
movq 0x18(%r14), %rax
movq 0x20(%rax), %rdi
leaq 0x3ee3(%rip), %rsi      # 0xfc22
leaq 0x3eee(%rip), %rdx      # 0xfc34
movl $0x1e3f, %ecx           # imm = 0x1E3F
callq 0xc43e
movq 0x18(%r14), %rax
movq 0x20(%rax), %rdi
leaq 0x3ee4(%rip), %rsi      # 0xfc43
leaq 0x3ece(%rip), %rdx      # 0xfc34
movl $0x1e40, %ecx           # imm = 0x1E40
callq 0xc43e