// File: /workspace/github_cpp_makefile_star2_O2/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_dbi_open
int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
{
	MDB_val key, data;
	MDB_dbi i;
	MDB_cursor mc;
	MDB_db dummy;
	int rc, dbflag, exact;
	unsigned int unused = 0, seq;
	size_t len;

	if (flags & ~VALID_FLAGS)
		return EINVAL;
	if (txn->mt_flags & MDB_TXN_BLOCKED)
		return MDB_BAD_TXN;

	/* main DB? */
	if (!name) {
		*dbi = MAIN_DBI;
		if (flags & PERSISTENT_FLAGS) {
			uint16_t f2 = flags & PERSISTENT_FLAGS;
			/* make sure flag changes get committed */
			if ((txn->mt_dbs[MAIN_DBI].md_flags | f2) != txn->mt_dbs[MAIN_DBI].md_flags) {
				txn->mt_dbs[MAIN_DBI].md_flags |= f2;
				txn->mt_flags |= MDB_TXN_DIRTY;
			}
		}
		mdb_default_cmp(txn, MAIN_DBI);
		return MDB_SUCCESS;
	}

	if (txn->mt_dbxs[MAIN_DBI].md_cmp == NULL) {
		mdb_default_cmp(txn, MAIN_DBI);
	}

	/* Is the DB already open? */
	len = strlen(name);
	for (i=CORE_DBS; i<txn->mt_numdbs; i++) {
		if (!txn->mt_dbxs[i].md_name.mv_size) {
			/* Remember this free slot */
			if (!unused) unused = i;
			continue;
		}
		if (len == txn->mt_dbxs[i].md_name.mv_size &&
			!strncmp(name, txn->mt_dbxs[i].md_name.mv_data, len)) {
			*dbi = i;
			return MDB_SUCCESS;
		}
	}

	/* If no free slot and max hit, fail */
	if (!unused && txn->mt_numdbs >= txn->mt_env->me_maxdbs)
		return MDB_DBS_FULL;

	/* Cannot mix named databases with some mainDB flags */
	if (txn->mt_dbs[MAIN_DBI].md_flags & (MDB_DUPSORT|MDB_INTEGERKEY))
		return (flags & MDB_CREATE) ? MDB_INCOMPATIBLE : MDB_NOTFOUND;

	/* Find the DB info */
	dbflag = DB_NEW|DB_VALID|DB_USRVALID;
	exact = 0;
	key.mv_size = len;
	key.mv_data = (void *)name;
	mdb_cursor_init(&mc, txn, MAIN_DBI, NULL);
	rc = mdb_cursor_set(&mc, &key, &data, MDB_SET, &exact);
	if (rc == MDB_SUCCESS) {
		/* make sure this is actually a DB */
		MDB_node *node = NODEPTR(mc.mc_pg[mc.mc_top], mc.mc_ki[mc.mc_top]);
		if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) != F_SUBDATA)
			return MDB_INCOMPATIBLE;
	} else if (rc == MDB_NOTFOUND && (flags & MDB_CREATE)) {
		/* Create if requested */
		data.mv_size = sizeof(MDB_db);
		data.mv_data = &dummy;
		memset(&dummy, 0, sizeof(dummy));
		dummy.md_root = P_INVALID;
		dummy.md_flags = flags & PERSISTENT_FLAGS;
		rc = mdb_cursor_put(&mc, &key, &data, F_SUBDATA);
		dbflag |= DB_DIRTY;
	}

	/* OK, got info, add to table */
	if (rc == MDB_SUCCESS) {
		unsigned int slot = unused ? unused : txn->mt_numdbs;
		txn->mt_dbxs[slot].md_name.mv_data = strdup(name);
		txn->mt_dbxs[slot].md_name.mv_size = len;
		txn->mt_dbxs[slot].md_rel = NULL;
		txn->mt_dbflags[slot] = dbflag;
		/* txn-> and env-> are the same in read txns, use
		 * tmp variable to avoid undefined assignment
		 */
		seq = ++txn->mt_env->me_dbiseqs[slot];
		txn->mt_dbiseqs[slot] = seq;

		memcpy(&txn->mt_dbs[slot], data.mv_data, sizeof(MDB_db));
		*dbi = slot;
		mdb_default_cmp(txn, slot);
		if (!unused) {
			txn->mt_numdbs++;
		}
	}

	return rc;
}

// Assembly:
testl $0xfffbff81, %edx       # imm = 0xFFFBFF81
je 0x8f23
pushq $0x16
popq %rax
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %rbx
movl %edx, 0x8(%rsp)
movl $0xffff87c2, %eax       # imm = 0xFFFF87C2
testb $0x13, 0x7c(%rdi)
jne 0x90f2
movq %rsi, %r14
testq %rsi, %rsi
je 0x9018
movq %rcx, 0x10(%rsp)
movq 0x50(%rbx), %rax
cmpq $0x0, 0x40(%rax)
jne 0x8f71
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x91ff
movq %r14, 0x18(%rsp)
movq %r14, %rdi
callq 0x2110
movq %rax, %rdx
movl 0x78(%rbx), %r14d
xorl %ebp, %ebp
pushq $0x2
popq %r15
pushq $0x68
popq %r12
cmpq %r14, %r15
jae 0x8fd3
movq 0x50(%rbx), %rax
movq -0x8(%rax,%r12), %rcx
testq %rcx, %rcx
je 0x8fc4
cmpq %rcx, %rdx
jne 0x8fca
movq (%rax,%r12), %rsi
movq 0x18(%rsp), %rdi
movq %rdx, %r13
callq 0x2070
movq %r13, %rdx
testl %eax, %eax
jne 0x8fca
jmp 0x90e8
testl %ebp, %ebp
cmovel %r15d, %ebp
incq %r15
addq $0x30, %r12
jmp 0x8f8f
testl %ebp, %ebp
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
jne 0x8ff4
movq 0x20(%rbx), %rcx
movl $0xffff87b9, %eax       # imm = 0xFFFF87B9
cmpl 0x24(%rcx), %r14d
jae 0x90f2
movq 0x58(%rbx), %rax
testb $0xc, 0x34(%rax)
je 0x904d
cmpl $0x40000, 0x8(%rsp)     # imm = 0x40000
movl $0xffff87b2, %ecx       # imm = 0xFFFF87B2
movl $0xffff87c0, %eax       # imm = 0xFFFF87C0
cmovbl %ecx, %eax
jmp 0x90f2
movl $0x1, (%rcx)
movl 0x8(%rsp), %edx
andl $0x7e, %edx
je 0x903d
movq 0x58(%rbx), %rax
movzwl 0x34(%rax), %ecx
orl %ecx, %edx
cmpl %ecx, %edx
je 0x903d
movw %dx, 0x34(%rax)
orb $0x4, 0x7c(%rbx)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x91ff
jmp 0x90f0
movl %ebp, 0xc(%rsp)
leaq 0x2c(%rsp), %r13
andl $0x0, (%r13)
leaq 0x40(%rsp), %rbp
movq %rdx, 0x20(%rsp)
movq %rdx, (%rbp)
movq %r12, 0x8(%rbp)
leaq 0x80(%rsp), %r14
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x4451
leaq 0x30(%rsp), %rdx
pushq $0xf
popq %rcx
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %r8
callq 0x5619
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
je 0x9104
testl %eax, %eax
movl 0xc(%rsp), %r13d
jne 0x9163
movzwl 0xc2(%rsp), %eax
movq 0xc8(%rsp,%rax,8), %rcx
movzwl 0x1c8(%rsp,%rax,2), %eax
movzwl 0x10(%rcx,%rax,2), %eax
movzwl 0x4(%rcx,%rax), %ecx
andl $0x6, %ecx
movl $0xffff87c0, %eax       # imm = 0xFFFF87C0
movb $0x1c, %r14b
cmpw $0x2, %cx
je 0x916a
jmp 0x90f2
movq 0x10(%rsp), %rax
movl %r15d, (%rax)
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffff87b2, %eax       # imm = 0xFFFF87B2
cmpl $0x40000, 0x8(%rsp)     # imm = 0x40000
jb 0x90f2
leaq 0x30(%rsp), %rdx
movq $0x30, (%rdx)
leaq 0x50(%rsp), %rax
movq %rax, 0x8(%rdx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
andq $0x0, 0x20(%rax)
orq $-0x1, 0x28(%rax)
movl 0x8(%rsp), %ecx
movw %cx, 0x4(%rax)
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rsi
pushq $0x2
popq %rcx
callq 0x453f
movl 0xc(%rsp), %r13d
movb $0x1d, %r14b
jmp 0x9166
movb $0x1c, %r14b
testl %eax, %eax
jne 0x90f2
movl %r13d, %ebp
testl %r13d, %r13d
jne 0x9175
movl 0x78(%rbx), %ebp
movq %r12, %rdi
callq 0x23d0
movq 0x50(%rbx), %rcx
movl %ebp, %edx
imulq $0x30, %rdx, %rsi
movq %rax, 0x8(%rcx,%rsi)
movq 0x20(%rsp), %rax
movq %rax, (%rcx,%rsi)
andq $0x0, 0x20(%rcx,%rsi)
movq 0x70(%rbx), %rax
movb %r14b, (%rax,%rdx)
movq 0x20(%rbx), %rax
movq 0x98(%rax), %rax
movl (%rax,%rdx,4), %ecx
incl %ecx
movl %ecx, (%rax,%rdx,4)
movq 0x60(%rbx), %rax
movl %ecx, (%rax,%rdx,4)
movq 0x58(%rbx), %rax
movq 0x38(%rsp), %rcx
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rax,%rsi)
movups %xmm1, 0x10(%rax,%rsi)
movups %xmm0, (%rax,%rsi)
movl %ebp, (%r15)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x91ff
xorl %eax, %eax
testl %r13d, %r13d
jne 0x90f2
incl 0x78(%rbx)
jmp 0x90f2