// File: /workspace/github_cpp_makefile_star2_O3/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:
movl $0x16, %eax
testl $0xfffbff81, %edx       # imm = 0xFFFBFF81
je 0x9655
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, %rbx
movl $0xffff87c2, %eax       # imm = 0xFFFF87C2
testb $0x13, 0x7c(%rdi)
jne 0x9795
movq %rcx, %r12
movl %edx, %ebp
movq %rsi, %r15
testq %rsi, %rsi
je 0x9762
movq 0x50(%rbx), %rdi
cmpq $0x0, 0x40(%rdi)
jne 0x96a2
movq 0x58(%rbx), %rsi
movl $0x1, %edx
callq 0x9961
movq %r15, %rdi
callq 0x2110
movq %rax, %r14
movl 0x78(%rbx), %esi
cmpq $0x3, %rsi
jb 0x9730
movl %ebp, 0x10(%rsp)
movq %r12, (%rsp)
movq 0x50(%rbx), %r12
addq $0x68, %r12
xorl %ecx, %ecx
movl $0x2, %ebp
movq %rsi, 0x8(%rsp)
movq -0x8(%r12), %rax
testq %rax, %rax
je 0x970e
cmpq %rax, %r14
jne 0x9713
movq (%r12), %rsi
movq %r15, %rdi
movq %r15, %r13
movq %rbx, %r15
movl %ecx, %ebx
movq %r14, %rdx
callq 0x2070
movq 0x8(%rsp), %rsi
movl %ebx, %ecx
movq %r15, %rbx
movq %r13, %r15
testl %eax, %eax
jne 0x9713
jmp 0x9844
testl %ecx, %ecx
cmovel %ebp, %ecx
incq %rbp
addq $0x30, %r12
cmpq %rbp, %rsi
jne 0x96d2
testl %ecx, %ecx
movq (%rsp), %r12
movl 0x10(%rsp), %ebp
je 0x9730
xorl %r13d, %r13d
jmp 0x9743
movq 0x20(%rbx), %rcx
movl $0xffff87b9, %eax       # imm = 0xFFFF87B9
cmpl 0x24(%rcx), %esi
jae 0x9795
xorl %ecx, %ecx
movb $0x1, %r13b
movq 0x58(%rbx), %rax
testb $0xc, 0x34(%rax)
je 0x97a7
cmpl $0x40000, %ebp          # imm = 0x40000
movl $0xffff87b2, %ecx       # imm = 0xFFFF87B2
movl $0xffff87c0, %eax       # imm = 0xFFFF87C0
cmovbl %ecx, %eax
jmp 0x9795
movl $0x1, (%r12)
movq 0x58(%rbx), %rsi
andl $0x7e, %ebp
je 0x9785
movzwl 0x34(%rsi), %eax
orl %eax, %ebp
cmpl %eax, %ebp
je 0x9785
movw %bp, 0x34(%rsi)
orb $0x4, 0x7c(%rbx)
movq 0x50(%rbx), %rdi
movl $0x1, %edx
callq 0x9961
xorl %eax, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r13d, 0x8(%rsp)
movl %ecx, (%rsp)
leaq 0x1c(%rsp), %rax
movl $0x0, (%rax)
leaq 0x30(%rsp), %r13
movq %r14, 0x10(%rsp)
movq %r14, (%r13)
movq %r15, 0x8(%r13)
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x40be
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
movl $0xf, %ecx
leaq 0x1c(%rsp), %r8
callq 0x5ab9
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
je 0x984f
testl %eax, %eax
jne 0x98ac
movzwl 0xb2(%rsp), %eax
movq 0xb8(%rsp,%rax,8), %rcx
movzwl 0x1b8(%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, %bpl
cmpw $0x2, %cx
je 0x98b7
jmp 0x9795
movq (%rsp), %rax
movl %ebp, (%rax)
jmp 0x9793
movl $0xffff87b2, %eax       # imm = 0xFFFF87B2
cmpl $0x40000, %ebp          # imm = 0x40000
jb 0x9795
leaq 0x20(%rsp), %rdx
movq $0x30, (%rdx)
leaq 0x40(%rsp), %rax
movq %rax, 0x8(%rdx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movq $0x0, 0x20(%rax)
movq $-0x1, 0x28(%rax)
movw %bp, 0x4(%rax)
leaq 0x70(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl $0x2, %ecx
callq 0x41b9
movb $0x1d, %bpl
jmp 0x98af
movb $0x1c, %bpl
testl %eax, %eax
jne 0x9795
cmpb $0x0, 0x8(%rsp)
je 0x98c4
movl 0x78(%rbx), %eax
movl %eax, (%rsp)
movq %r15, %rdi
callq 0x23c0
movq 0x50(%rbx), %rcx
movl (%rsp), %edx
movl %edx, %edi
movq %rdi, %rsi
shlq $0x4, %rsi
leaq (%rsi,%rsi,2), %rsi
movq %rax, 0x8(%rcx,%rsi)
movq 0x10(%rsp), %rax
movq %rax, (%rcx,%rsi)
movq $0x0, 0x20(%rcx,%rsi)
movq 0x70(%rbx), %rax
movb %bpl, (%rax,%rdi)
movq 0x20(%rbx), %rax
movq 0x98(%rax), %rax
movl (%rax,%rdi,4), %ecx
incl %ecx
movl %ecx, (%rax,%rdi,4)
movq 0x60(%rbx), %rax
movl %ecx, (%rax,%rdi,4)
movq 0x58(%rbx), %rax
movq 0x28(%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 %edx, (%r12)
movq 0x50(%rbx), %rdi
movq 0x58(%rbx), %rsi
callq 0x9961
xorl %eax, %eax
cmpb $0x0, 0x8(%rsp)
je 0x9795
incl 0x78(%rbx)
jmp 0x9795