// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, %r14
cmpw $0x1, 0x40(%rdi)
jbe 0xb84a
movq %rsi, %rbx
cmpw $0x1, 0x40(%rsi)
jbe 0xb86a
movzwl 0x42(%r14), %eax
movq 0x48(%r14,%rax,8), %r12
movzwl 0x42(%rbx), %eax
movq 0x48(%rbx,%rax,8), %r15
movq %rbx, %rdi
callq 0xa0ec
testl %eax, %eax
je 0xb4f3
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x18(%rsp)
movzwl 0xc(%r15), %eax
addl $0x1fff0, %eax          # imm = 0x1FFF0
shrl %eax
movq %rax, 0x10(%rsp)
testb $0x20, 0xa(%r12)
jne 0xb61f
movzwl 0xc(%r12), %eax
andl $-0x2, %eax
cmpw $0x10, %ax
je 0xb690
xorl %r15d, %r15d
leaq 0x30(%rsp), %rbp
movzwl %r15w, %eax
movzwl 0x10(%r12,%rax,2), %r13d
addq %r12, %r13
testw %r15w, %r15w
jne 0xb54b
testb $0x1, 0xa(%r12)
jne 0xb5c1
movzwl 0x6(%r13), %eax
movq %rax, (%rsp)
leaq 0x8(%r13), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rsp), %rcx
addl %r15d, %ecx
movl (%r13), %edx
movq %rdx, 0x20(%rsp)
leaq 0x8(%r13,%rax), %rax
movq %rax, 0x28(%rsp)
movzwl 0x4(%r13), %r9d
movq %r9, %r8
shlq $0x20, %r8
orq %rdx, %r8
movzwl %cx, %esi
movq %rbx, %rdi
movq %rsp, %rdx
leaq 0x20(%rsp), %rcx
callq 0x87d4
testl %eax, %eax
jne 0xb4e1
incl %r15d
movzwl %r15w, %eax
movzwl 0xc(%r12), %ecx
addl $-0x10, %ecx
shrl %ecx
cmpl %eax, %ecx
ja 0xb530
jmp 0xb690
movq %r14, %rdi
movq %rbp, %rsi
callq 0xb444
movq %rbp, %rdi
callq 0xb88a
testl %eax, %eax
jne 0xb4e1
movzwl 0x72(%rsp), %eax
movq 0x78(%rsp,%rax,8), %rax
testb $0x20, 0xa(%rax)
jne 0xb601
movzwl 0x10(%rax), %ecx
addq %rcx, %rax
addq $0x8, %rax
movzwl -0x2(%rax), %ecx
movq %rcx, (%rsp)
jmp 0xb610
movq 0x58(%rsp), %rcx
movl (%rcx), %ecx
movq %rcx, (%rsp)
addq $0x10, %rax
movq %rax, 0x8(%rsp)
movzwl 0x6(%r13), %eax
jmp 0xb55d
movq 0x28(%r14), %rax
movl (%rax), %eax
movq %rax, (%rsp)
leaq 0x10(%r12), %rax
movq %rax, 0x8(%rsp)
movzwl 0xc(%r12), %eax
andl $-0x2, %eax
cmpw $0x10, %ax
je 0xb690
movw $0x1, %r13w
movq %rsp, %r15
movq 0x10(%rsp), %rax
movl %eax, %ebp
movzwl %bp, %esi
movq %rbx, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x87d4
testl %eax, %eax
jne 0xb4e1
movq (%rsp), %rax
addq %rax, 0x8(%rsp)
incl %ebp
movzwl %r13w, %eax
movzwl 0xc(%r12), %ecx
addl $-0x10, %ecx
shrl %ecx
incl %r13d
cmpl %eax, %ecx
ja 0xb651
decw 0x42(%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0x7385
movzwl 0x42(%r14), %ecx
cmpw $0x0, 0x148(%r14,%rcx,2)
je 0xb749
incl %ecx
movw %cx, 0x42(%r14)
movzwl %cx, %eax
movq 0x48(%r14,%rax,8), %r13
movq (%r13), %r12
movq 0x18(%r14), %r15
movzwl 0xa(%r13), %eax
testb $0x10, %al
je 0xb72b
cmpl $0x0, 0x20(%r14)
je 0xb72b
cmpq $0x0, (%r15)
je 0xb779
movq 0x48(%r15), %rbp
cmpq $0x0, (%rbp)
je 0xb72b
movq %rbp, %rdi
movq %r12, %rsi
callq 0xbea3
movl %eax, %eax
cmpq %rax, (%rbp)
jb 0xb72b
shlq $0x4, %rax
cmpq %r12, (%rbp,%rax)
jne 0xb72b
addq %rax, %rbp
cmpq %r13, 0x8(%rbp)
je 0xb774
andb $-0x4, 0x44(%r14)
orb $0x2, 0x7c(%r15)
movl $0xffff87b4, %eax       # imm = 0xFFFF87B4
jmp 0xb4e1
addq $0x28, %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0xbad1
testl %eax, %eax
jne 0xb4e1
movzwl 0xa(%r13), %eax
jmp 0xb793
movq %rsp, %rsi
movq $0x0, (%rsi)
movq %r14, %rdi
callq 0x6e4d
movzwl 0x42(%r14), %ecx
testl %eax, %eax
je 0xb6b4
incl %ecx
movw %cx, 0x42(%r14)
jmp 0xb4e1
movzwl 0xa(%r13), %eax
movq 0x30(%r15), %rcx
movq %rcx, 0x30(%r13)
movq %r13, 0x30(%r15)
incl 0x38(%r15)
orl $0x4000, %eax           # imm = 0x4000
movw %ax, 0xa(%r13)
movq 0x18(%rsp), %rdi
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
je 0xb800
movl 0x44(%r14), %ecx
movq 0x10(%rsp), %r8
movq %rax, %rdx
testb $0x4, %cl
je 0xb7ce
movq 0x10(%rax), %rdx
cmpq %r14, %rdx
je 0xb7f8
movzwl 0x40(%rdx), %esi
cmpw 0x40(%r14), %si
jb 0xb7f8
movzwl 0x42(%r14), %esi
cmpq %r13, 0x48(%rdx,%rsi,8)
jne 0xb7f8
movq %rdi, 0x48(%rdx,%rsi,8)
addw %r8w, 0x148(%rdx,%rsi,2)
movq (%rax), %rax
testq %rax, %rax
jne 0xb7c2
movzwl 0x40(%rbx), %r14d
movq 0x28(%rbx), %rax
movzwl 0x6(%rax), %ebp
testw %r14w, %r14w
je 0xb823
movl %r14d, %eax
decw %ax
movw %ax, 0x40(%rbx)
je 0xb823
decw 0x42(%rbx)
movq %rbx, %rdi
callq 0xa9b7
movq 0x28(%rbx), %rcx
cmpw %bp, 0x6(%rcx)
sbbw $0x0, %r14w
movw %r14w, 0x40(%rbx)
leal -0x1(%r14), %ecx
movw %cx, 0x42(%rbx)
jmp 0xb4e1
movq 0x18(%r14), %rax
movq 0x20(%rax), %rdi
leaq 0x4409(%rip), %rsi      # 0xfc62
leaq 0x4414(%rip), %rdx      # 0xfc74
movl $0x1e3f, %ecx           # imm = 0x1E3F
callq 0xbfbd
movq 0x18(%r14), %rax
movq 0x20(%rax), %rdi
leaq 0x440a(%rip), %rsi      # 0xfc83
leaq 0x43f4(%rip), %rdx      # 0xfc74
movl $0x1e40, %ecx           # imm = 0x1E40
callq 0xbfbd