// File: /workspace/github_cpp_makefile_star2_O2/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 0xafe9
movq %rsi, %rbx
cmpw $0x1, 0x40(%rsi)
jbe 0xb009
movzwl 0x42(%r14), %eax
movq 0x48(%r14,%rax,8), %r12
movzwl 0x42(%rbx), %eax
movq 0x48(%rbx,%rax,8), %r15
movq %rbx, %rdi
callq 0x9908
testl %eax, %eax
je 0xacb7
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x20(%rsp)
movzwl 0xc(%r15), %ebp
addl $0x1fff0, %ebp          # imm = 0x1FFF0
shrl %ebp
testb $0x20, 0xa(%r12)
jne 0xade1
xorl %r15d, %r15d
movq %rbx, 0x28(%rsp)
movzwl %r15w, %eax
movzwl 0xc(%r12), %ecx
addl $-0x10, %ecx
shrl %ecx
cmpl %eax, %ecx
jbe 0xae40
movzwl 0x10(%r12,%rax,2), %r13d
addq %r12, %r13
testw %r15w, %r15w
jne 0xad0b
testb $0x1, 0xa(%r12)
jne 0xad6b
movzwl 0x6(%r13), %eax
movq %rax, 0x8(%rsp)
leaq 0x8(%r13), %rcx
movq %rcx, 0x10(%rsp)
leal (%r15,%rbp), %ecx
movl (%r13), %edx
movq %rdx, 0x30(%rsp)
addq %r13, %rax
addq $0x8, %rax
movq %rax, 0x38(%rsp)
movzwl 0x4(%r13), %r9d
movq %r9, %r8
shlq $0x20, %r8
orq %rdx, %r8
movzwl %cx, %esi
movq %rbx, %rdi
leaq 0x8(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x8110
testl %eax, %eax
jne 0xaca5
incl %r15d
jmp 0xacdd
movq %r14, %rdi
movq %rbp, %rbx
leaq 0x40(%rsp), %rbp
movq %rbp, %rsi
callq 0xac08
movq %rbp, %rdi
callq 0xb029
testl %eax, %eax
jne 0xaca5
movzwl 0x82(%rsp), %eax
movq 0x88(%rsp,%rax,8), %rax
testb $0x20, 0xa(%rax)
jne 0xadba
movzwl 0x10(%rax), %ecx
addq %rcx, %rax
addq $0x8, %rax
movzwl -0x2(%rax), %ecx
movq %rcx, 0x8(%rsp)
jmp 0xadca
movq 0x68(%rsp), %rcx
movl (%rcx), %ecx
movq %rcx, 0x8(%rsp)
addq $0x10, %rax
movq %rbx, %rbp
movq %rax, 0x10(%rsp)
movzwl 0x6(%r13), %eax
movq 0x28(%rsp), %rbx
jmp 0xad1e
movq 0x28(%r14), %rax
movl (%rax), %eax
leaq 0x8(%rsp), %r15
movq %rax, (%r15)
leaq 0x10(%r12), %rax
movq %rax, 0x8(%r15)
xorl %r13d, %r13d
movzwl %r13w, %eax
movzwl 0xc(%r12), %ecx
addl $-0x10, %ecx
shrl %ecx
cmpl %eax, %ecx
jbe 0xae40
leal (%rbp,%r13), %eax
movzwl %ax, %esi
movq %rbx, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x8110
testl %eax, %eax
jne 0xaca5
movq 0x8(%rsp), %rax
addq %rax, 0x10(%rsp)
incl %r13d
jmp 0xadfb
decw 0x42(%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0x6e79
movzwl 0x42(%r14), %ecx
cmpw $0x0, 0x148(%r14,%rcx,2)
je 0xaf02
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 0xaee4
cmpl $0x0, 0x20(%r14)
je 0xaee4
cmpq $0x0, (%r15)
je 0xaf31
movq 0x48(%r15), %rax
cmpq $0x0, (%rax)
je 0xaee4
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0xb54e
movq 0x18(%rsp), %rcx
movl %eax, %eax
cmpq %rax, (%rcx)
jb 0xaee4
shlq $0x4, %rax
cmpq %r12, (%rcx,%rax)
jne 0xaee4
addq %rax, %rcx
cmpq %r13, 0x8(%rcx)
je 0xaf2c
andb $-0x4, 0x44(%r14)
orb $0x2, 0x7c(%r15)
movl $0xffff87b4, %eax       # imm = 0xFFFF87B4
jmp 0xaca5
addq $0x28, %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0xb24b
testl %eax, %eax
jne 0xaca5
movzwl 0xa(%r13), %eax
jmp 0xaf4b
leaq 0x8(%rsp), %rsi
andq $0x0, (%rsi)
movq %r14, %rdi
callq 0x699a
movzwl 0x42(%r14), %ecx
testl %eax, %eax
je 0xae64
incl %ecx
movw %cx, 0x42(%r14)
jmp 0xaca5
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)
andl $0x2, %eax
movq 0x28(%r14), %rcx
decq 0x8(%rcx,%rax,4)
movl 0x20(%r14), %eax
movq 0x18(%r14), %rcx
shlq $0x3, %rax
addq 0x68(%rcx), %rax
movq 0x20(%rsp), %rsi
movq (%rax), %rax
testq %rax, %rax
je 0xafad
testb $0x4, 0x44(%r14)
movq %rax, %rcx
je 0xaf82
movq 0x10(%rax), %rcx
cmpq %r14, %rcx
je 0xaf6c
movzwl 0x40(%rcx), %edx
cmpw 0x40(%r14), %dx
jb 0xaf6c
movzwl 0x42(%r14), %edx
cmpq %r13, 0x48(%rcx,%rdx,8)
jne 0xaf6c
movq %rsi, 0x48(%rcx,%rdx,8)
addw %bp, 0x148(%rcx,%rdx,2)
jmp 0xaf6c
movzwl 0x40(%rbx), %r14d
movq 0x28(%rbx), %rax
movzwl 0x6(%rax), %ebp
movq %rbx, %rdi
callq 0x9f86
movq %rbx, %rdi
callq 0xa19a
movq 0x28(%rbx), %rcx
cmpw %bp, 0x6(%rcx)
sbbw $0x0, %r14w
movw %r14w, 0x40(%rbx)
leal -0x1(%r14), %ecx
movw %cx, 0x42(%rbx)
jmp 0xaca5
movq 0x18(%r14), %rax
movq 0x20(%rax), %rdi
leaq 0x3c4a(%rip), %rsi      # 0xec42
leaq 0x3c55(%rip), %rdx      # 0xec54
movl $0x1e3f, %ecx           # imm = 0x1E3F
callq 0xb672
movq 0x18(%r14), %rax
movq 0x20(%rax), %rdi
leaq 0x3c4b(%rip), %rsi      # 0xec63
leaq 0x3c35(%rip), %rdx      # 0xec54
movl $0x1e40, %ecx           # imm = 0x1E40
callq 0xb672