// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_cursor_del0
static int
mdb_cursor_del0(MDB_cursor *mc)
{
	int rc;
	MDB_page *mp;
	indx_t ki;
	unsigned int nkeys;

	ki = mc->mc_ki[mc->mc_top];
	mdb_node_del(mc, mc->mc_db->md_pad);
	mc->mc_db->md_entries--;
	rc = mdb_rebalance(mc);

	if (rc == MDB_SUCCESS) {
		MDB_cursor *m2, *m3;
		MDB_dbi dbi = mc->mc_dbi;

		/* DB is totally empty now, just bail out.
		 * Other cursors adjustments were already done
		 * by mdb_rebalance and aren't needed here.
		 */
		if (!mc->mc_snum)
			return rc;

		mp = mc->mc_pg[mc->mc_top];
		nkeys = NUMKEYS(mp);

		/* if mc points past last node in page, find next sibling */
		if (mc->mc_ki[mc->mc_top] >= nkeys) {
			rc = mdb_cursor_sibling(mc, 1);
			if (rc == MDB_NOTFOUND) {
				mc->mc_flags |= C_EOF;
				rc = MDB_SUCCESS;
			}
		}

		/* Adjust other cursors pointing to mp */
		for (m2 = mc->mc_txn->mt_cursors[dbi]; !rc && m2; m2=m2->mc_next) {
			m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2;
			if (! (m2->mc_flags & m3->mc_flags & C_INITIALIZED))
				continue;
			if (m3 == mc || m3->mc_snum < mc->mc_snum)
				continue;
			if (m3->mc_pg[mc->mc_top] == mp) {
				if (m3->mc_ki[mc->mc_top] >= ki) {
					m3->mc_flags |= C_DEL;
					if (m3->mc_ki[mc->mc_top] > ki)
						m3->mc_ki[mc->mc_top]--;
					else if (mc->mc_db->md_flags & MDB_DUPSORT)
						m3->mc_xcursor->mx_cursor.mc_flags |= C_EOF;
				}
				if (m3->mc_ki[mc->mc_top] >= nkeys) {
					rc = mdb_cursor_sibling(m3, 1);
					if (rc == MDB_NOTFOUND) {
						m3->mc_flags |= C_EOF;
						rc = MDB_SUCCESS;
					}
				}
			}
		}
		mc->mc_flags |= C_DEL;
	}

	if (rc)
		mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movw 0x148(%rax,%rcx,2), %ax
movw %ax, -0x22(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl (%rax), %esi
callq 0xa4b0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rdi
callq 0x10590
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0xd8d8
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
cmpw $0x0, 0x40(%rax)
jne 0xd6b1
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xd8f5
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movzwl 0x148(%rax,%rcx,2), %eax
cmpl -0x28(%rbp), %eax
jb 0xd71f
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xfe30
movl %eax, -0x14(%rbp)
cmpl $0xffff87b2, -0x14(%rbp) # imm = 0xFFFF87B2
jne 0xd71d
movq -0x10(%rbp), %rax
movl 0x44(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x44(%rax)
movl $0x0, -0x14(%rbp)
jmp 0xd71f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x14(%rbp)
movb %al, -0x3d(%rbp)
jne 0xd74c
cmpq $0x0, -0x30(%rbp)
setne %al
movb %al, -0x3d(%rbp)
movb -0x3d(%rbp), %al
testb $0x1, %al
jne 0xd758
jmp 0xd8cb
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xd775
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0xd77d
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movl 0x44(%rax), %eax
movq -0x38(%rbp), %rcx
andl 0x44(%rcx), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xd7a0
jmp 0xd8bb
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
je 0xd7be
movq -0x38(%rbp), %rax
movzwl 0x40(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x40(%rcx), %ecx
cmpl %ecx, %eax
jge 0xd7c3
jmp 0xd8bb
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
cmpq -0x20(%rbp), %rax
jne 0xd8b9
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movzwl 0x148(%rax,%rcx,2), %eax
movzwl -0x22(%rbp), %ecx
cmpl %ecx, %eax
jl 0xd86e
movq -0x38(%rbp), %rax
movl 0x44(%rax), %ecx
orl $0x8, %ecx
movl %ecx, 0x44(%rax)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movzwl 0x148(%rax,%rcx,2), %eax
movzwl -0x22(%rbp), %ecx
cmpl %ecx, %eax
jle 0xd845
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movw 0x148(%rax,%rcx,2), %dx
addw $-0x1, %dx
movw %dx, 0x148(%rax,%rcx,2)
jmp 0xd86c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0x4(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xd86a
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x44(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x44(%rax)
jmp 0xd86c
jmp 0xd86e
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movzwl 0x148(%rax,%rcx,2), %eax
cmpl -0x28(%rbp), %eax
jb 0xd8b7
movq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0xfe30
movl %eax, -0x14(%rbp)
cmpl $0xffff87b2, -0x14(%rbp) # imm = 0xFFFF87B2
jne 0xd8b5
movq -0x38(%rbp), %rax
movl 0x44(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x44(%rax)
movl $0x0, -0x14(%rbp)
jmp 0xd8b7
jmp 0xd8b9
jmp 0xd8bb
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0xd736
movq -0x10(%rbp), %rax
movl 0x44(%rax), %ecx
orl $0x8, %ecx
movl %ecx, 0x44(%rax)
cmpl $0x0, -0x14(%rbp)
je 0xd8ef
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x7c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x7c(%rax)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop