// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x7c(%rax), %eax
andl $0x20013, %eax          # imm = 0x20013
cmpl $0x0, %eax
je 0xcbbd
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x7c(%rax), %edx
andl $0x20000, %edx          # imm = 0x20000
movl $0xffff87c2, %eax       # imm = 0xFFFF87C2
movl $0xd, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0xcfe8
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xcbd8
movl $0x16, -0x4(%rbp)
jmp 0xcfe8
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movzwl 0x148(%rax,%rcx,2), %eax
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rdx
movzwl 0x42(%rdx), %edx
movq 0x48(%rcx,%rdx,8), %rcx
movzwl 0xc(%rcx), %ecx
subl $0x10, %ecx
shrl %ecx
cmpl %ecx, %eax
jb 0xcc16
movl $0xffff87b2, -0x4(%rbp) # imm = 0xFFFF87B2
jmp 0xcfe8
movl -0x14(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
jne 0xcc46
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x96e0
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
je 0xcc46
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xcfe8
movq -0x10(%rbp), %rdi
callq 0x9be0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0xcc63
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xcfe8
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x20, %eax
cmpl $0x20, %eax
jne 0xcc8d
jmp 0xcfc3
movq -0x28(%rbp), %rax
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x10(%rbp), %rsi
movzwl 0x42(%rsi), %esi
movzwl 0x148(%rdx,%rsi,2), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x4, %eax
cmpl $0x4, %eax
jne 0xcf41
movl -0x14(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xcd02
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x1a8(%rax), %rdx
subq $0x1, %rdx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x20(%rax)
jmp 0xcf10
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x2, %eax
cmpl $0x2, %eax
je 0xcd33
movq -0x20(%rbp), %rcx
addq $0x8, %rcx
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %eax
cltq
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x8000, %esi           # imm = 0x8000
callq 0xcb70
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0xcd59
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xcfe8
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x1a8(%rax)
je 0xcf0e
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xcdbb
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq -0x20(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
addq $0x188, %rsi            # imm = 0x188
movl $0x30, %edx
callq 0x2230
jmp 0xcee2
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movzwl 0x148(%rax,%rcx,2), %esi
callq 0xd000
movq -0x28(%rbp), %rax
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x10(%rbp), %rsi
movzwl 0x42(%rsi), %esi
movzwl 0x148(%rdx,%rsi,2), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rcx
addq $0x8, %rcx
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %eax
cltq
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0xcee0
movq -0x40(%rbp), %rax
cmpq -0x10(%rbp), %rax
je 0xce68
movq -0x40(%rbp), %rax
movzwl 0x40(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x40(%rcx), %ecx
cmpl %ecx, %eax
jge 0xce6a
jmp 0xced0
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
cmpq -0x28(%rbp), %rax
jne 0xcece
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movzwl 0x148(%rax,%rcx,2), %eax
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rdx
movzwl 0x42(%rdx), %edx
movzwl 0x148(%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jne 0xcece
movq -0x20(%rbp), %rcx
addq $0x8, %rcx
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %eax
cltq
addq %rax, %rcx
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x48(%rax)
jmp 0xced0
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0xce3f
jmp 0xcee2
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %ecx
orl $0x8, %ecx
movl %ecx, 0x44(%rax)
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xcfe8
jmp 0xcf10
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xcf3f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
xorl %esi, %esi
callq 0xd200
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0xcf3d
jmp 0xcfd1
jmp 0xcf3f
jmp 0xcf5f
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
xorl -0x14(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xcf5d
movl $0xffff87c0, -0x2c(%rbp) # imm = 0xFFFF87C0
jmp 0xcfd1
jmp 0xcf5f
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x1, %eax
cmpl $0x1, %eax
jne 0xcfc1
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
movslq %ecx, %rcx
movq 0x8(%rax,%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x50(%rbp), %rsi
leaq -0x48(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xa740
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
jne 0xcfbd
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xabe0
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
je 0xcfbf
jmp 0xcfd1
jmp 0xcfc1
jmp 0xcfc3
movq -0x10(%rbp), %rdi
callq 0xd630
movl %eax, -0x4(%rbp)
jmp 0xcfe8
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x7c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x7c(%rax)
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)