// File: /workspace/github_cpp_makefile_star2_O3/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), %eax
movl %eax, 0x4(%rsp)
movl (%rdi), %edi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x2340
cmpq $0xef53, (%r14)         # imm = 0xEF53
jne 0xc906
leaq 0x90(%rsp), %r14
movq %r14, %rdi
callq 0x2160
movb 0x82(%r14), %al
cmpb $0x32, %al
jg 0xc8b8
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 0xc8f0
xorl 0x112(%rsp), %eax
movl $0x2e34332e, %ecx       # imm = 0x2E34332E
xorl 0x115(%rsp), %ecx
orl %eax, %ecx
jne 0xc902
leaq 0x119(%rsp), %rdi
callq 0x2350
cmpl $0xe, %eax
jle 0xc902
jmp 0xc906
cmpb $0x33, %al
jne 0xc906
leaq 0x114(%rsp), %rdi
callq 0x2350
cmpl $0x5, %eax
jg 0xc906
cmpl $0x2, %eax
je 0xcb44
cmpl $0x5, %eax
jne 0xc902
leaq 0x116(%rsp), %rdi
callq 0x2350
cmpl $0x3, %eax
jle 0xc902
jmp 0xc906
leaq 0x119(%rsp), %rdi
callq 0x2350
cmpl $0x3b, %eax
jg 0xc906
orb $0x8, 0xf(%rbx)
movb $0x1, %bpl
xorl %r13d, %r13d
leaq 0xa0(%rsp), %r14
leaq 0x90(%rsp), %r15
leaq 0x8(%rsp), %r12
movl (%rbx), %edi
movslq %r13d, %rcx
movl $0x98, %edx
movq %r15, %rsi
callq 0x2360
cmpl $0x98, %eax
jne 0xc9ab
testb $0x8, 0x9a(%rsp)
movl $0xffff87b7, %ecx       # imm = 0xFFFF87B7
je 0xc9d6
cmpl $0xbeefc0de, 0xa0(%rsp) # imm = 0xBEEFC0DE
jne 0xc9d6
cmpl $0x1, 0xa4(%rsp)
jne 0xc9ca
testl %r13d, %r13d
je 0xc97b
movq 0x120(%rsp), %rax
cmpq 0x88(%rsp), %rax
jbe 0xc98b
movl $0x88, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x2240
movl 0x20(%rsp), %eax
addl %eax, %r13d
testb $0x1, %bpl
movl $0x0, %ebp
jne 0xc921
movl %eax, 0x10(%rbx)
movq 0x70(%rbx), %rcx
movb $0x1, %dl
jmp 0xca67
orl %eax, %r13d
je 0xc9d1
movl $0xffff87b7, %ecx       # imm = 0xFFFF87B7
testl %eax, %eax
jns 0xc9d6
callq 0x2050
movl (%rax), %ecx
testl %ecx, %ecx
jne 0xc9d6
movl 0x20(%rsp), %eax
jmp 0xc99d
movl $0xffff87b6, %ecx       # imm = 0xFFFF87B6
jmp 0xc9d6
movl $0x2, %ecx
cmpl $0x2, %ecx
jne 0xcb19
movl 0x14(%rbx), %ecx
movl $0x8000, %eax           # imm = 0x8000
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl %eax, 0x10(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x80(%rsp)
movups %xmm0, 0x70(%rsp)
movups %xmm0, 0x60(%rsp)
movups %xmm0, 0x50(%rsp)
movups %xmm0, 0x40(%rsp)
movups %xmm0, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movabsq $0x1beefc0de, %rcx      # imm = 0x1BEEFC0DE
movq %rcx, 0x8(%rsp)
movq 0x70(%rbx), %rcx
movl %eax, 0x20(%rsp)
movq $0x1, 0x80(%rsp)
movl 0xc(%rbx), %edx
orl $0x8, %edx
movw %dx, 0x24(%rsp)
movq $-0x1, %rdx
movq %rdx, 0x48(%rsp)
movq %rdx, 0x78(%rsp)
movq $0x100000, 0x18(%rsp)   # imm = 0x100000
xorl %edx, %edx
testq %rcx, %rcx
jne 0xca75
movq 0x18(%rsp), %rcx
movq %rcx, 0x70(%rbx)
movq 0x80(%rsp), %rsi
incq %rsi
movl %eax, %eax
imulq %rsi, %rax
cmpq %rax, %rcx
jae 0xca92
movq %rax, 0x70(%rbx)
movq %rax, %rcx
movq %rcx, 0x18(%rsp)
testb %dl, %dl
je 0xcaa9
testb $0x1, 0x4(%rsp)
je 0xcac1
movq 0x10(%rsp), %rsi
jmp 0xcac3
testb $0x1, 0x4(%rsp)
jne 0xcb06
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0xd9e7
testl %eax, %eax
jne 0xcb17
xorl %esi, %esi
movq %rbx, %rdi
callq 0xc13c
testl %eax, %eax
jne 0xcb17
movl 0x10(%rbx), %esi
leal -0x10(%rsi), %eax
movl %eax, %ecx
shrl $0x3, %ecx
decl %ecx
movl %ecx, 0xd8(%rbx)
shrl $0x2, %eax
leal -0x2(,%rax,2), %eax
movl %eax, 0xdc(%rbx)
movq 0x70(%rbx), %rax
xorl %ecx, %ecx
xorl %edx, %edx
divq %rsi
movq %rax, 0x80(%rbx)
jmp 0xcb19
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0xc13c
testl %eax, %eax
je 0xcb2d
movl %eax, %ecx
movl %ecx, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rbx), %rax
leaq 0x8(%rsp), %rsi
movq %rax, 0x8(%rsi)
movq %rbx, %rdi
callq 0xd9e7
jmp 0xcacb
leaq 0x116(%rsp), %rdi
callq 0x2350
cmpl $0x1d, %eax
jle 0xc902
jmp 0xc906