// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_drop
int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
{
	MDB_cursor *mc, *m2;
	int rc;

	if ((unsigned)del > 1 || !TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
		return EINVAL;

	if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY))
		return EACCES;

	if (TXN_DBI_CHANGED(txn, dbi))
		return MDB_BAD_DBI;

	rc = mdb_cursor_open(txn, dbi, &mc);
	if (rc)
		return rc;

	rc = mdb_drop0(mc, mc->mc_db->md_flags & MDB_DUPSORT);
	/* Invalidate the dropped DB's cursors */
	for (m2 = txn->mt_cursors[dbi]; m2; m2 = m2->mc_next)
		m2->mc_flags &= ~(C_INITIALIZED|C_EOF);
	if (rc)
		goto leave;

	/* Can't delete the main DB */
	if (del && dbi >= CORE_DBS) {
		rc = mdb_del0(txn, MAIN_DBI, &mc->mc_dbx->md_name, NULL, F_SUBDATA);
		if (!rc) {
			txn->mt_dbflags[dbi] = DB_STALE;
			mdb_dbi_close(txn->mt_env, dbi);
		} else {
			txn->mt_flags |= MDB_TXN_ERROR;
		}
	} else {
		/* reset the DB record, mark it dirty */
		txn->mt_dbflags[dbi] |= DB_DIRTY;
		txn->mt_dbs[dbi].md_depth = 0;
		txn->mt_dbs[dbi].md_branch_pages = 0;
		txn->mt_dbs[dbi].md_leaf_pages = 0;
		txn->mt_dbs[dbi].md_overflow_pages = 0;
		txn->mt_dbs[dbi].md_entries = 0;
		txn->mt_dbs[dbi].md_root = P_INVALID;

		txn->mt_flags |= MDB_TXN_DIRTY;
	}
leave:
	mdb_cursor_close(mc);
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
cmpl $0x1, -0x18(%rbp)
ja 0xe7f2
cmpq $0x0, -0x10(%rbp)
je 0xe7f2
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x78(%rcx), %eax
jae 0xe7f2
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movl -0x14(%rbp), %ecx
movzbl (%rax,%rcx), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0xe7fe
movl $0x16, -0x4(%rbp)
jmp 0xea01
movq -0x10(%rbp), %rax
movl 0x7c(%rax), %eax
andl $0x20000, %eax          # imm = 0x20000
cmpl $0x20000, %eax          # imm = 0x20000
jne 0xe81d
movl $0xd, -0x4(%rbp)
jmp 0xea01
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x14(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq 0x98(%rcx), %rcx
movl -0x14(%rbp), %edx
cmpl (%rcx,%rdx,4), %eax
je 0xe84e
movl $0xffff87c4, -0x4(%rbp) # imm = 0xFFFF87C4
jmp 0xea01
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
leaq -0x20(%rbp), %rdx
callq 0xd900
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0xe872
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xea01
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movzwl 0x4(%rax), %esi
andl $0x4, %esi
callq 0xd200
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xe8c1
movq -0x28(%rbp), %rax
movl 0x44(%rax), %ecx
andl $-0x4, %ecx
movl %ecx, 0x44(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0xe8a0
cmpl $0x0, -0x2c(%rbp)
je 0xe8cc
jmp 0xe9f2
cmpl $0x0, -0x18(%rbp)
je 0xe934
cmpl $0x2, -0x14(%rbp)
jb 0xe934
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdx
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %ecx
movl $0x2, %r8d
callq 0xde50
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0xe922
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movl -0x14(%rbp), %ecx
movb $0x2, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0xe680
jmp 0xe92f
movq -0x10(%rbp), %rax
movl 0x7c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x7c(%rax)
jmp 0xe9f0
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movl -0x14(%rbp), %ecx
movzbl (%rax,%rcx), %edx
orl $0x1, %edx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movw $0x0, 0x6(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq $-0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movl 0x7c(%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0x7c(%rax)
jmp 0xe9f2
movq -0x20(%rbp), %rdi
callq 0xdc60
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)