// File: /workspace/github_cpp_makefile_star2_O1/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 0x9a0d
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %rbx
movl $0xffff87c2, %eax       # imm = 0xFFFF87C2
testb $0x13, 0x7c(%rdi)
jne 0x9b52
movl %edx, %ebp
movq %rsi, %r15
testq %rsi, %rsi
je 0x9b22
movq %rcx, 0x20(%rsp)
movq 0x50(%rbx), %rax
cmpq $0x0, 0x40(%rax)
jne 0x9a5b
movq %rbx, %rdi
movl $0x1, %esi
callq 0x9d2e
movq %r15, %rdi
callq 0x2110
movq %rax, %rdx
movq %rbx, 0x28(%rsp)
movl 0x78(%rbx), %esi
xorl %r8d, %r8d
cmpq $0x3, %rsi
jb 0x9ae6
movq 0x28(%rsp), %rax
movq 0x50(%rax), %r12
addq $0x68, %r12
xorl %r8d, %r8d
movl $0x2, %r13d
movq %rsi, 0x10(%rsp)
movq -0x8(%r12), %rax
testq %rax, %rax
je 0x9ad3
cmpq %rax, %rdx
jne 0x9ada
movq (%r12), %rsi
movq %r15, %rdi
movl %r8d, %ebx
movl %ebp, %r14d
movq %r15, %rbp
movq %rdx, %r15
callq 0x2070
movq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %rbp, %r15
movl %r14d, %ebp
movl %ebx, %r8d
testl %eax, %eax
jne 0x9ada
jmp 0x9c0e
testl %r8d, %r8d
cmovel %r13d, %r8d
incq %r13
addq $0x30, %r12
cmpq %r13, %rsi
jne 0x9a92
testl %r8d, %r8d
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %rbx
jne 0x9b03
movq 0x20(%rbx), %rcx
movl $0xffff87b9, %eax       # imm = 0xFFFF87B9
cmpl 0x24(%rcx), %esi
jae 0x9b52
movq 0x58(%rbx), %rax
testb $0xc, 0x34(%rax)
je 0x9b64
cmpl $0x40000, %ebp          # imm = 0x40000
movl $0xffff87b2, %ecx       # imm = 0xFFFF87B2
movl $0xffff87c0, %eax       # imm = 0xFFFF87C0
cmovbl %ecx, %eax
jmp 0x9b52
movl $0x1, (%rcx)
andl $0x7e, %ebp
je 0x9b43
movq 0x58(%rbx), %rax
movzwl 0x34(%rax), %ecx
orl %ecx, %ebp
cmpl %ecx, %ebp
je 0x9b43
movw %bp, 0x34(%rax)
orb $0x4, 0x7c(%rbx)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x9d2e
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r8d, 0x8(%rsp)
leaq 0x1c(%rsp), %rax
movl $0x0, (%rax)
leaq 0x40(%rsp), %r12
movq %rdx, 0x10(%rsp)
movq %rdx, (%r12)
movq %r15, 0x8(%r12)
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x4177
leaq 0x30(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
movl $0xf, %ecx
leaq 0x1c(%rsp), %r8
callq 0x5cd7
movb $0x1c, %dil
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
je 0x9c1b
movl %eax, %esi
testl %eax, %eax
movl 0x8(%rsp), %edx
movq %r14, %r12
jne 0x9c7e
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
xorl %esi, %esi
cmpw $0x2, %cx
jne 0x9b52
jmp 0x9c7e
movq 0x20(%rsp), %rax
movl %r13d, (%rax)
jmp 0x9b50
movl $0xffff87b2, %esi       # imm = 0xFFFF87B2
cmpl $0x40000, %ebp          # imm = 0x40000
movl 0x8(%rsp), %edx
movq %r14, %r12
jb 0x9c7e
leaq 0x30(%rsp), %rdx
movq $0x30, (%rdx)
leaq 0x50(%rsp), %rax
movq %rax, 0x8(%rdx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rax)
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movq $-0x1, 0x28(%rax)
movw %bp, 0x4(%rax)
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movl $0x2, %ecx
callq 0x427c
movl 0x8(%rsp), %edx
movl %eax, %esi
movb $0x1d, %dil
testl %esi, %esi
je 0x9c89
movl %esi, %eax
jmp 0x9b52
movl %edi, %r14d
movl %esi, 0xc(%rsp)
movl %edx, %ebp
testl %edx, %edx
jne 0x9c99
movl 0x78(%rbx), %ebp
movq %r15, %rdi
callq 0x23c0
movq 0x50(%rbx), %rcx
movl %ebp, %edx
movq %rdx, %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 %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, (%r12)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x9d2e
cmpl $0x0, 0x8(%rsp)
jne 0x9d25
incl 0x78(%rbx)
movl 0xc(%rsp), %eax
jmp 0x9b52