// File: /workspace/github_cpp_makefile_star2_O3/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_cursor_del
int
mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
{
	MDB_node	*leaf;
	MDB_page	*mp;
	int rc;

	if (mc->mc_txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_BLOCKED))
		return (mc->mc_txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;

	if (!(mc->mc_flags & C_INITIALIZED))
		return EINVAL;

	if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mc->mc_pg[mc->mc_top]))
		return MDB_NOTFOUND;

	if (!(flags & MDB_NOSPILL) && (rc = mdb_page_spill(mc, NULL, NULL)))
		return rc;

	rc = mdb_cursor_touch(mc);
	if (rc)
		return rc;

	mp = mc->mc_pg[mc->mc_top];
	if (IS_LEAF2(mp))
		goto del_key;
	leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);

	if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
		if (flags & MDB_NODUPDATA) {
			/* mdb_cursor_del0() will subtract the final entry */
			mc->mc_db->md_entries -= mc->mc_xcursor->mx_db.md_entries - 1;
		} else {
			if (!F_ISSET(leaf->mn_flags, F_SUBDATA)) {
				mc->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
			}
			rc = mdb_cursor_del(&mc->mc_xcursor->mx_cursor, MDB_NOSPILL);
			if (rc)
				return rc;
			/* If sub-DB still has entries, we're done */
			if (mc->mc_xcursor->mx_db.md_entries) {
				if (leaf->mn_flags & F_SUBDATA) {
					/* update subDB info */
					void *db = NODEDATA(leaf);
					memcpy(db, &mc->mc_xcursor->mx_db, sizeof(MDB_db));
				} else {
					MDB_cursor *m2;
					/* shrink fake page */
					mdb_node_shrink(mp, mc->mc_ki[mc->mc_top]);
					leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
					mc->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
					/* fix other sub-DB cursors pointed at this fake page */
					for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
						if (m2 == mc || m2->mc_snum < mc->mc_snum) continue;
						if (m2->mc_pg[mc->mc_top] == mp &&
							m2->mc_ki[mc->mc_top] == mc->mc_ki[mc->mc_top])
							m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
					}
				}
				mc->mc_db->md_entries--;
				mc->mc_flags |= C_DEL;
				return rc;
			}
			/* otherwise fall thru and delete the sub-DB */
		}

		if (leaf->mn_flags & F_SUBDATA) {
			/* add all the child DB's pages to the free list */
			rc = mdb_drop0(&mc->mc_xcursor->mx_cursor, 0);
			if (rc)
				goto fail;
		}
	}
	/* LMDB passes F_SUBDATA in 'flags' to delete a DB record */
	else if ((leaf->mn_flags ^ flags) & F_SUBDATA) {
		rc = MDB_INCOMPATIBLE;
		goto fail;
	}

	/* add overflow pages to free list */
	if (F_ISSET(leaf->mn_flags, F_BIGDATA)) {
		MDB_page *omp;
		pgno_t pg;

		memcpy(&pg, NODEDATA(leaf), sizeof(pg));
		if ((rc = mdb_page_get(mc->mc_txn, pg, &omp, NULL)) ||
			(rc = mdb_ovpage_free(mc, omp)))
			goto fail;
	}

del_key:
	return mdb_cursor_del0(mc);

fail:
	mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
	return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq 0x18(%rdi), %rax
movl 0x7c(%rax), %eax
testl $0x20013, %eax          # imm = 0x20013
je 0x8b16
btl $0x11, %eax
movl $0xffff87c2, %ecx       # imm = 0xFFFF87C2
movl $0xd, %eax
cmovael %ecx, %eax
jmp 0x8b6a
movq %rdi, %rbx
movl $0x16, %eax
testb $0x1, 0x44(%rdi)
je 0x8b6a
movzwl 0x42(%rbx), %eax
movzwl 0x148(%rbx,%rax,2), %ecx
movq 0x48(%rbx,%rax,8), %rax
movzwl 0xc(%rax), %edx
addl $-0x10, %edx
shrl %edx
movl $0xffff87b2, %eax       # imm = 0xFFFF87B2
cmpl %ecx, %edx
jbe 0x8b6a
movl %esi, %ebp
testw %bp, %bp
js 0x8b5e
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6b25
testl %eax, %eax
jne 0x8b6a
movq %rbx, %rdi
callq 0x6d99
testl %eax, %eax
je 0x8b75
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x42(%rbx), %eax
movq 0x48(%rbx,%rax,8), %r14
testb $0x20, 0xa(%r14)
jne 0x8ccd
movzwl 0x148(%rbx,%rax,2), %eax
movzwl 0x10(%r14,%rax,2), %eax
leaq (%r14,%rax), %r15
movzwl 0x4(%r14,%rax), %ecx
movzwl %cx, %edx
testb $0x4, %dl
jne 0x8bbe
xorl %ebp, %edx
movl $0xffff87c0, %eax       # imm = 0xFFFF87C0
testb $0x2, %dl
je 0x8cc8
jmp 0x8d0b
testb $0x20, %bpl
jne 0x8c99
testb $0x2, %dl
jne 0x8be1
movzwl 0x6(%r15), %eax
addq %r15, %rax
addq $0x8, %rax
movq 0x10(%rbx), %rcx
movq %rax, 0x48(%rcx)
movq 0x10(%rbx), %rdi
movl $0x8000, %esi           # imm = 0x8000
callq 0x8aee
testl %eax, %eax
jne 0x8b6a
movq 0x10(%rbx), %rdi
cmpq $0x0, 0x1a8(%rdi)
movzwl 0x4(%r15), %ecx
je 0x8cb3
testb $0x2, %cl
jne 0x8d18
movzwl 0x42(%rbx), %eax
movzwl 0x148(%rbx,%rax,2), %esi
movq %r14, %rdi
callq 0x8d54
movzwl 0x42(%rbx), %eax
movzwl 0x148(%rbx,%rax,2), %ecx
movzwl 0x10(%r14,%rcx,2), %edx
leaq (%r14,%rdx), %rsi
movzwl 0x6(%r14,%rdx), %edx
leaq 0x8(%rdx,%rsi), %rdx
movq 0x10(%rbx), %rsi
movq %rdx, 0x48(%rsi)
movq 0x18(%rbx), %rsi
movq 0x68(%rsi), %rsi
movl 0x20(%rbx), %edi
movq (%rsi,%rdi,8), %rsi
testq %rsi, %rsi
je 0x8d41
cmpq %rbx, %rsi
je 0x8c94
movzwl 0x40(%rsi), %edi
cmpw 0x40(%rbx), %di
jb 0x8c94
cmpq %r14, 0x48(%rsi,%rax,8)
jne 0x8c94
cmpw %cx, 0x148(%rsi,%rax,2)
jne 0x8c94
movq 0x10(%rsi), %rdi
movq %rdx, 0x48(%rdi)
movq (%rsi), %rsi
jmp 0x8c63
movq 0x10(%rbx), %rdi
movq 0x28(%rbx), %rax
movq 0x20(%rax), %rdx
subq 0x1a8(%rdi), %rdx
incq %rdx
movq %rdx, 0x20(%rax)
testb $0x2, %cl
je 0x8cc8
xorl %esi, %esi
callq 0x8e46
testl %eax, %eax
jne 0x8d0b
movzwl 0x4(%r15), %ecx
testb $0x1, %cl
jne 0x8cdf
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x90e3
movzwl 0x6(%r15), %eax
movq 0x8(%r15,%rax), %rsi
movq 0x18(%rbx), %rdi
movq %rsp, %rdx
xorl %ecx, %ecx
callq 0x74c4
testl %eax, %eax
jne 0x8d0b
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x7758
testl %eax, %eax
je 0x8ccd
movq 0x18(%rbx), %rcx
orl $0x2, 0x7c(%rcx)
jmp 0x8b6a
addq $0x188, %rdi            # imm = 0x188
movzwl 0x6(%r15), %eax
movups (%rdi), %xmm0
movups 0x10(%rdi), %xmm1
movups 0x20(%rdi), %xmm2
movups %xmm2, 0x28(%r15,%rax)
movups %xmm1, 0x18(%r15,%rax)
movups %xmm0, 0x8(%r15,%rax)
movq 0x28(%rbx), %rax
decq 0x20(%rax)
orb $0x8, 0x44(%rbx)
xorl %eax, %eax
jmp 0x8b6a