// File: /workspace/github_cpp_makefile_star2_O1/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_env_open2
static int ESECT
mdb_env_open2(MDB_env *env)
{
	unsigned int flags = env->me_flags;
	int i, newenv = 0, rc;
	MDB_meta meta;

#ifdef _WIN32
	/* See if we should use QueryLimited */
	rc = GetVersion();
	if ((rc & 0xff) > 5)
		env->me_pidquery = MDB_PROCESS_QUERY_LIMITED_INFORMATION;
	else
		env->me_pidquery = PROCESS_QUERY_INFORMATION;
#endif /* _WIN32 */

#ifdef BROKEN_FDATASYNC
	/* ext3/ext4 fdatasync is broken on some older Linux kernels.
	 * https://lkml.org/lkml/2012/9/3/83
	 * Kernels after 3.6-rc6 are known good.
	 * https://lkml.org/lkml/2012/9/10/556
	 * See if the DB is on ext3/ext4, then check for new enough kernel
	 * Kernels 2.6.32.60, 2.6.34.15, 3.2.30, and 3.5.4 are also known
	 * to be patched.
	 */
	{
		struct statfs st;
		fstatfs(env->me_fd, &st);
		while (st.f_type == 0xEF53) {
			struct utsname uts;
			int i;
			uname(&uts);
			if (uts.release[0] < '3') {
				if (!strncmp(uts.release, "2.6.32.", 7)) {
					i = atoi(uts.release+7);
					if (i >= 60)
						break;	/* 2.6.32.60 and newer is OK */
				} else if (!strncmp(uts.release, "2.6.34.", 7)) {
					i = atoi(uts.release+7);
					if (i >= 15)
						break;	/* 2.6.34.15 and newer is OK */
				}
			} else if (uts.release[0] == '3') {
				i = atoi(uts.release+2);
				if (i > 5)
					break;	/* 3.6 and newer is OK */
				if (i == 5) {
					i = atoi(uts.release+4);
					if (i >= 4)
						break;	/* 3.5.4 and newer is OK */
				} else if (i == 2) {
					i = atoi(uts.release+4);
					if (i >= 30)
						break;	/* 3.2.30 and newer is OK */
				}
			} else {	/* 4.x and newer is OK */
				break;
			}
			env->me_flags |= MDB_FSYNCONLY;
			break;
		}
	}
#endif

	if ((i = mdb_env_read_header(env, &meta)) != 0) {
		if (i != ENOENT)
			return i;
		DPUTS("new mdbenv");
		newenv = 1;
		env->me_psize = env->me_os_psize;
		if (env->me_psize > MAX_PAGESIZE)
			env->me_psize = MAX_PAGESIZE;
		memset(&meta, 0, sizeof(meta));
		mdb_env_init_meta0(env, &meta);
		meta.mm_mapsize = DEFAULT_MAPSIZE;
	} else {
		env->me_psize = meta.mm_psize;
	}

	/* Was a mapsize configured? */
	if (!env->me_mapsize) {
		env->me_mapsize = meta.mm_mapsize;
	}
	{
		/* Make sure mapsize >= committed data size.  Even when using
		 * mm_mapsize, which could be broken in old files (ITS#7789).
		 */
		size_t minsize = (meta.mm_last_pg + 1) * meta.mm_psize;
		if (env->me_mapsize < minsize)
			env->me_mapsize = minsize;
	}
	meta.mm_mapsize = env->me_mapsize;

	if (newenv && !(flags & MDB_FIXEDMAP)) {
		/* mdb_env_map() may grow the datafile.  Write the metapages
		 * first, so the file will be valid if initialization fails.
		 * Except with FIXEDMAP, since we do not yet know mm_address.
		 * We could fill in mm_address later, but then a different
		 * program might end up doing that - one with a memory layout
		 * and map address which does not suit the main program.
		 */
		rc = mdb_env_init_meta(env, &meta);
		if (rc)
			return rc;
		newenv = 0;
	}

	rc = mdb_env_map(env, (flags & MDB_FIXEDMAP) ? meta.mm_address : NULL);
	if (rc)
		return rc;

	if (newenv) {
		if (flags & MDB_FIXEDMAP)
			meta.mm_address = env->me_map;
		i = mdb_env_init_meta(env, &meta);
		if (i != MDB_SUCCESS) {
			return i;
		}
	}

	env->me_maxfree_1pg = (env->me_psize - PAGEHDRSZ) / sizeof(pgno_t) - 1;
	env->me_nodemax = (((env->me_psize - PAGEHDRSZ) / MDB_MINKEYS) & -2)
		- sizeof(indx_t);
#if !(MDB_MAXKEYSIZE)
	env->me_maxkey = env->me_nodemax - (NODESIZE + sizeof(MDB_db));
#endif
	env->me_maxpg = env->me_mapsize / env->me_psize;

#if MDB_DEBUG
	{
		MDB_meta *meta = mdb_env_pick_meta(env);
		MDB_db *db = &meta->mm_dbs[MAIN_DBI];

		DPRINTF(("opened database version %u, pagesize %u",
			meta->mm_version, env->me_psize));
		DPRINTF(("using meta page %d",    (int) (meta->mm_txnid & 1)));
		DPRINTF(("depth: %u",             db->md_depth));
		DPRINTF(("entries: %"Z"u",        db->md_entries));
		DPRINTF(("branch pages: %"Z"u",   db->md_branch_pages));
		DPRINTF(("leaf pages: %"Z"u",     db->md_leaf_pages));
		DPRINTF(("overflow pages: %"Z"u", db->md_overflow_pages));
		DPRINTF(("root: %"Z"u",           db->md_root));
	}
#endif

	return MDB_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rdi, %rbx
movl 0xc(%rdi), %ebp
movl (%rdi), %edi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x2340
cmpq $0xef53, (%r14)         # imm = 0xEF53
jne 0xcd9b
leaq 0x90(%rsp), %r14
movq %r14, %rdi
callq 0x2160
movb 0x82(%r14), %al
cmpb $0x32, %al
jg 0xcd4d
movl $0x2e362e32, %eax       # imm = 0x2E362E32
movl 0x112(%rsp), %ecx
xorl %eax, %ecx
movl $0x2e32332e, %edx       # imm = 0x2E32332E
xorl 0x115(%rsp), %edx
orl %ecx, %edx
je 0xcd85
xorl 0x112(%rsp), %eax
movl $0x2e34332e, %ecx       # imm = 0x2E34332E
xorl 0x115(%rsp), %ecx
orl %eax, %ecx
jne 0xcd97
leaq 0x119(%rsp), %rdi
callq 0x2350
cmpl $0xe, %eax
jle 0xcd97
jmp 0xcd9b
cmpb $0x33, %al
jne 0xcd9b
leaq 0x114(%rsp), %rdi
callq 0x2350
cmpl $0x5, %eax
jg 0xcd9b
cmpl $0x2, %eax
je 0xcfc0
cmpl $0x5, %eax
jne 0xcd97
leaq 0x116(%rsp), %rdi
callq 0x2350
cmpl $0x3, %eax
jle 0xcd97
jmp 0xcd9b
leaq 0x119(%rsp), %rdi
callq 0x2350
cmpl $0x3b, %eax
jg 0xcd9b
orb $0x8, 0xf(%rbx)
movl %ebp, 0x4(%rsp)
xorl %ebp, %ebp
movl $0x1, %r15d
leaq 0x90(%rsp), %r12
leaq 0x8(%rsp), %r13
movl (%rbx), %edi
movslq %ebp, %rcx
movl $0x98, %edx
movq %r12, %rsi
callq 0x2360
cmpl $0x98, %eax
jne 0xce2d
testb $0x8, 0x9a(%rsp)
movl $0xffff87b7, %r14d      # imm = 0xFFFF87B7
je 0xce53
cmpl $0xbeefc0de, 0xa0(%rsp) # imm = 0xBEEFC0DE
jne 0xce53
cmpl $0x1, 0xa4(%rsp)
jne 0xce45
testl %ebp, %ebp
je 0xce0a
movq 0x120(%rsp), %rax
cmpq 0x88(%rsp), %rax
jbe 0xce1f
movl $0x88, %edx
movq %r13, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x2240
addl 0x20(%rsp), %ebp
decl %r15d
je 0xcdb4
xorl %r14d, %r14d
jmp 0xce53
orl %eax, %ebp
je 0xce4d
movl $0xffff87b7, %r14d      # imm = 0xFFFF87B7
testl %eax, %eax
jns 0xce53
callq 0x2050
movl (%rax), %r14d
jmp 0xce53
movl $0xffff87b6, %r14d      # imm = 0xFFFF87B6
jmp 0xce53
movl $0x2, %r14d
testl %r14d, %r14d
je 0xcecb
cmpl $0x2, %r14d
jne 0xcfab
movl 0x14(%rbx), %eax
movl $0x8000, %ebp           # imm = 0x8000
cmpl %ebp, %eax
cmovbl %eax, %ebp
movl %ebp, 0x10(%rbx)
leaq 0x8(%rsp), %r15
movl $0x88, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x2190
movabsq $0x1beefc0de, %rax      # imm = 0x1BEEFC0DE
movq %rax, (%r15)
movq 0x70(%rbx), %rax
movq %rax, 0x10(%r15)
movl %ebp, 0x18(%r15)
movq $0x1, 0x78(%r15)
movl 0xc(%rbx), %eax
orl $0x8, %eax
movw %ax, 0x1c(%r15)
movq $-0x1, %rax
movq %rax, 0x40(%r15)
movq %rax, 0x70(%r15)
movq $0x100000, 0x10(%r15)   # imm = 0x100000
jmp 0xced2
movl 0x20(%rsp), %eax
movl %eax, 0x10(%rbx)
cmpq $0x0, 0x70(%rbx)
movl 0x4(%rsp), %r15d
jne 0xcee7
movq 0x18(%rsp), %rax
movq %rax, 0x70(%rbx)
movq 0x80(%rsp), %rcx
incq %rcx
movl 0x20(%rsp), %eax
imulq %rcx, %rax
cmpq %rax, 0x70(%rbx)
jae 0xcf04
movq %rax, 0x70(%rbx)
movq 0x70(%rbx), %rax
movq %rax, 0x18(%rsp)
movb $0x1, %bpl
testl %r14d, %r14d
je 0xcf30
testb $0x1, %r15b
jne 0xcf2e
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0xde54
testl %eax, %eax
jne 0xcf4b
jmp 0xcf30
xorl %ebp, %ebp
testb $0x1, %r15b
je 0xcf3d
movq 0x10(%rsp), %rsi
jmp 0xcf3f
xorl %esi, %esi
movq %rbx, %rdi
callq 0xc5c6
testl %eax, %eax
je 0xcf50
movl %eax, %r14d
jmp 0xcfab
testb %bpl, %bpl
jne 0xcf75
testb $0x1, %r15b
je 0xcf64
movq 0x38(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0xde54
testl %eax, %eax
jne 0xcf4b
movl 0x10(%rbx), %ecx
leal -0x10(%rcx), %eax
movl %eax, %edx
shrl $0x3, %edx
decl %edx
movl %edx, 0xd8(%rbx)
shrl $0x2, %eax
leal -0x2(,%rax,2), %eax
movl %eax, 0xdc(%rbx)
movq 0x70(%rbx), %rax
xorl %r14d, %r14d
xorl %edx, %edx
divq %rcx
movq %rax, 0x80(%rbx)
movl %r14d, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x116(%rsp), %rdi
callq 0x2350
cmpl $0x1d, %eax
jle 0xcd97
jmp 0xcd9b