// File: /workspace/github_cpp_makefile_star2_O2/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_env_open
int ESECT
mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
{
	int		oflags, rc, len, excl = -1;
	char *lpath, *dpath;

	if (env->me_fd!=INVALID_HANDLE_VALUE || (flags & ~(CHANGEABLE|CHANGELESS)))
		return EINVAL;

	len = strlen(path);
	if (flags & MDB_NOSUBDIR) {
		rc = len + sizeof(LOCKSUFF) + len + 1;
	} else {
		rc = len + sizeof(LOCKNAME) + len + sizeof(DATANAME);
	}
	lpath = malloc(rc);
	if (!lpath)
		return ENOMEM;
	if (flags & MDB_NOSUBDIR) {
		dpath = lpath + len + sizeof(LOCKSUFF);
		sprintf(lpath, "%s" LOCKSUFF, path);
		strcpy(dpath, path);
	} else {
		dpath = lpath + len + sizeof(LOCKNAME);
		sprintf(lpath, "%s" LOCKNAME, path);
		sprintf(dpath, "%s" DATANAME, path);
	}

	rc = MDB_SUCCESS;
	flags |= env->me_flags;
	if (flags & MDB_RDONLY) {
		/* silently ignore WRITEMAP when we're only getting read access */
		flags &= ~MDB_WRITEMAP;
	} else {
		if (!((env->me_free_pgs = mdb_midl_alloc(MDB_IDL_UM_MAX)) &&
			  (env->me_dirty_list = calloc(MDB_IDL_UM_SIZE, sizeof(MDB_ID2)))))
			rc = ENOMEM;
	}
	env->me_flags = flags |= MDB_ENV_ACTIVE;
	if (rc)
		goto leave;

	env->me_path = strdup(path);
	env->me_dbxs = calloc(env->me_maxdbs, sizeof(MDB_dbx));
	env->me_dbflags = calloc(env->me_maxdbs, sizeof(uint16_t));
	env->me_dbiseqs = calloc(env->me_maxdbs, sizeof(unsigned int));
	if (!(env->me_dbxs && env->me_path && env->me_dbflags && env->me_dbiseqs)) {
		rc = ENOMEM;
		goto leave;
	}
	env->me_dbxs[FREE_DBI].md_cmp = mdb_cmp_long; /* aligned MDB_INTEGERKEY */

	/* For RDONLY, get lockfile after we know datafile exists */
	if (!(flags & (MDB_RDONLY|MDB_NOLOCK))) {
		rc = mdb_env_setup_locks(env, lpath, mode, &excl);
		if (rc)
			goto leave;
	}

#ifdef _WIN32
	if (F_ISSET(flags, MDB_RDONLY)) {
		oflags = GENERIC_READ;
		len = OPEN_EXISTING;
	} else {
		oflags = GENERIC_READ|GENERIC_WRITE;
		len = OPEN_ALWAYS;
	}
	mode = FILE_ATTRIBUTE_NORMAL;
	env->me_fd = CreateFileA(dpath, oflags, FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, len, mode, NULL);
#else
	if (F_ISSET(flags, MDB_RDONLY))
		oflags = O_RDONLY;
	else
		oflags = O_RDWR | O_CREAT;

	env->me_fd = open(dpath, oflags, mode);
#endif
	if (env->me_fd == INVALID_HANDLE_VALUE) {
		rc = ErrCode();
		goto leave;
	}

	if ((flags & (MDB_RDONLY|MDB_NOLOCK)) == MDB_RDONLY) {
		rc = mdb_env_setup_locks(env, lpath, mode, &excl);
		if (rc)
			goto leave;
	}

	if ((rc = mdb_env_open2(env)) == MDB_SUCCESS) {
		if (flags & (MDB_RDONLY|MDB_WRITEMAP)) {
			env->me_mfd = env->me_fd;
		} else {
			/* Synchronous fd for meta writes. Needed even with
			 * MDB_NOSYNC/MDB_NOMETASYNC, in case these get reset.
			 */
#ifdef _WIN32
			len = OPEN_EXISTING;
			env->me_mfd = CreateFileA(dpath, oflags,
				FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, len,
				mode | FILE_FLAG_WRITE_THROUGH, NULL);
#else
			oflags &= ~O_CREAT;
			env->me_mfd = open(dpath, oflags | MDB_DSYNC, mode);
#endif
			if (env->me_mfd == INVALID_HANDLE_VALUE) {
				rc = ErrCode();
				goto leave;
			}
		}
		DPRINTF(("opened dbenv %p", (void *) env));
		if (excl > 0) {
			rc = mdb_env_share_locks(env, &excl);
			if (rc)
				goto leave;
		}
		if (!(flags & MDB_RDONLY)) {
			MDB_txn *txn;
			int tsize = sizeof(MDB_txn), size = tsize + env->me_maxdbs *
				(sizeof(MDB_db)+sizeof(MDB_cursor *)+sizeof(unsigned int)+1);
			if ((env->me_pbuf = calloc(1, env->me_psize)) &&
				(txn = calloc(1, size)))
			{
				txn->mt_dbs = (MDB_db *)((char *)txn + tsize);
				txn->mt_cursors = (MDB_cursor **)(txn->mt_dbs + env->me_maxdbs);
				txn->mt_dbiseqs = (unsigned int *)(txn->mt_cursors + env->me_maxdbs);
				txn->mt_dbflags = (unsigned char *)(txn->mt_dbiseqs + env->me_maxdbs);
				txn->mt_env = env;
				txn->mt_dbxs = env->me_dbxs;
				txn->mt_flags = MDB_TXN_FINISHED;
				env->me_txn0 = txn;
			} else {
				rc = ENOMEM;
			}
		}
	}

leave:
	if (rc) {
		mdb_env_close0(env, excl);
	}
	free(lpath);
	return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2b8, %rsp            # imm = 0x2B8
orl $-0x1, 0xc(%rsp)
pushq $0x16
popq %r12
testl $0xfe00bffe, %edx       # imm = 0xFE00BFFE
jne 0xbb9f
movq %rdi, %rbx
cmpl $-0x1, (%rdi)
jne 0xbb9f
movl %ecx, %r14d
movl %edx, %ebp
movq %rsi, %r13
movq %rsi, %rdi
callq 0x2110
movq %rax, %r12
btl $0xe, %ebp
leal 0x14(%rax,%rax), %eax
leal 0x7(%r12,%r12), %ecx
cmovael %eax, %ecx
movslq %ecx, %rdi
callq 0x2270
testq %rax, %rax
je 0xb964
movslq %r12d, %r15
addq %rax, %r15
btl $0xe, %ebp
movq %rax, 0x18(%rsp)
jb 0xb96d
addq $0xa, %r15
leaq 0x2ae0(%rip), %rsi      # 0xe421
movq %rax, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2380
leaq 0x2ad8(%rip), %rsi      # 0xe42d
movq %r15, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2380
jmp 0xb990
pushq $0xc
popq %r12
jmp 0xbb9f
addq $0x6, %r15
leaq 0x2aa1(%rip), %rsi      # 0xe419
movq %rax, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2380
movq %r15, %rdi
movq %r13, %rsi
callq 0x2080
orl 0xc(%rbx), %ebp
btl $0x11, %ebp
jb 0xb9dc
movl $0x1ffff, %edi          # imm = 0x1FFFF
callq 0xb17a
movq %rax, 0xc8(%rbx)
testq %rax, %rax
je 0xbb7d
pushq $0x10
popq %rsi
movl $0x20000, %edi          # imm = 0x20000
callq 0x2200
movq %rax, 0xd0(%rbx)
testq %rax, %rax
je 0xbb7d
movq %r15, 0x10(%rsp)
movl %r14d, 0x4(%rsp)
jmp 0xb9ec
movq %r15, 0x10(%rsp)
movl %r14d, 0x4(%rsp)
andl $0xfff7ffff, %ebp       # imm = 0xFFF7FFFF
movl %ebp, %eax
orl $0x20000000, %eax       # imm = 0x20000000
movl %eax, 0xc(%rbx)
movq %r13, %rdi
callq 0x23d0
movq %rax, %r14
movq %rax, 0x30(%rbx)
movl 0x24(%rbx), %r12d
pushq $0x30
popq %rsi
movq %r12, %rdi
callq 0x2200
movq %rax, %r13
movq %rax, 0x88(%rbx)
pushq $0x2
popq %rsi
movq %r12, %rdi
callq 0x2200
movq %rax, %r15
movq %rax, 0x90(%rbx)
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0x2200
movq %rax, 0x98(%rbx)
testq %r13, %r13
sete %cl
testq %r14, %r14
sete %dl
orb %cl, %dl
pushq $0xc
popq %r14
jne 0xbb8a
testq %r15, %r15
sete %cl
testq %rax, %rax
sete %al
orb %cl, %al
jne 0xbb8a
leaq -0x8cd7(%rip), %rax     # 0x2da1
movq %rax, 0x10(%r13)
movl %ebp, %r15d
andl $0x420000, %r15d        # imm = 0x420000
je 0xbbb4
xorl %eax, %eax
btl $0x11, %ebp
pushq $0x42
popq %r12
cmovbl %eax, %r12d
movq 0x10(%rsp), %rdi
movl %r12d, %esi
movl 0x4(%rsp), %edx
xorl %eax, %eax
callq 0x2320
movl %eax, (%rbx)
cmpl $-0x1, %eax
je 0xbfde
cmpl $0x20000, %r15d         # imm = 0x20000
jne 0xbae1
leaq 0xc(%rsp), %rcx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movl 0x4(%rsp), %edx
callq 0xbffa
testl %eax, %eax
jne 0xbbd2
movl (%rbx), %eax
movl 0xc(%rbx), %ecx
movl %ecx, 0x8(%rsp)
leaq 0x240(%rsp), %r15
movl %eax, %edi
movq %r15, %rsi
callq 0x2350
cmpq $0xef53, (%r15)         # imm = 0xEF53
jne 0xbc25
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
callq 0x2160
movb 0x82(%r15), %al
cmpb $0x32, %al
jg 0xbbd7
leaq 0x132(%rsp), %r15
leaq 0x2f18(%rip), %rsi      # 0xea4d
pushq $0x7
popq %rdx
movq %r15, %rdi
callq 0x2280
testl %eax, %eax
je 0xbc0f
leaq 0x2f06(%rip), %rsi      # 0xea55
pushq $0x7
popq %rdx
movq %r15, %rdi
callq 0x2280
testl %eax, %eax
jne 0xbc21
leaq 0x139(%rsp), %rdi
callq 0x2360
cmpl $0xe, %eax
jle 0xbc21
jmp 0xbc25
orl $0x20000000, %ebp       # imm = 0x20000000
movl %ebp, 0xc(%rbx)
pushq $0xc
popq %r14
movq %rbx, %rdi
callq 0xc25b
movl %r14d, %r12d
movq 0x18(%rsp), %rdi
callq 0x2030
movl %r12d, %eax
addq $0x2b8, %rsp            # imm = 0x2B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc(%rsp), %rcx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movl 0x4(%rsp), %edx
callq 0xbffa
testl %eax, %eax
je 0xba8c
movl %eax, %r14d
jmp 0xbb8a
cmpb $0x33, %al
jne 0xbc25
leaq 0x134(%rsp), %rdi
callq 0x2360
cmpl $0x5, %eax
jg 0xbc25
cmpl $0x2, %eax
je 0xbd82
cmpl $0x5, %eax
jne 0xbc21
leaq 0x136(%rsp), %rdi
callq 0x2360
cmpl $0x3, %eax
jle 0xbc21
jmp 0xbc25
leaq 0x139(%rsp), %rdi
callq 0x2360
cmpl $0x3b, %eax
jg 0xbc25
orb $0x8, 0xf(%rbx)
movl %r12d, 0x24(%rsp)
xorl %r15d, %r15d
pushq $0x2
popq %r12
movl $0xffff87b7, %r13d      # imm = 0xFFFF87B7
subl $0x1, %r12d
jb 0xbda1
movl (%rbx), %edi
movslq %r15d, %rcx
movl $0x98, %edx
leaq 0xb0(%rsp), %rsi
callq 0x2370
cmpl $0x98, %eax
jne 0xbcb7
testb $0x8, 0xba(%rsp)
je 0xbce5
cmpl $0xbeefc0de, 0xc0(%rsp) # imm = 0xBEEFC0DE
jne 0xbce5
cmpl $0x1, 0xc4(%rsp)
jne 0xbcc2
testl %r15d, %r15d
je 0xbc97
movq 0x140(%rsp), %rax
cmpq 0xa8(%rsp), %rax
jbe 0xbcae
movl $0x88, %edx
leaq 0x28(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x2240
movl 0x40(%rsp), %eax
addl %eax, %r15d
jmp 0xbc37
orl %eax, %r15d
jne 0xbcca
pushq $0x2
popq %r13
jmp 0xbce5
movl $0xffff87b6, %r13d      # imm = 0xFFFF87B6
jmp 0xbce5
movl $0xffff87b7, %r13d      # imm = 0xFFFF87B7
testl %eax, %eax
jns 0xbce5
callq 0x2050
movl (%rax), %r13d
testl %r13d, %r13d
je 0xbd9d
cmpl $0x2, %r13d
movq 0x10(%rsp), %r12
jne 0xbfe6
movl 0x14(%rbx), %eax
movl $0x8000, %ecx           # imm = 0x8000
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x10(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0xa0(%rsp)
movups %xmm0, 0x90(%rsp)
movups %xmm0, 0x80(%rsp)
movups %xmm0, 0x70(%rsp)
movups %xmm0, 0x60(%rsp)
movups %xmm0, 0x50(%rsp)
movups %xmm0, 0x40(%rsp)
movups %xmm0, 0x30(%rsp)
movabsq $0x1beefc0de, %rax      # imm = 0x1BEEFC0DE
movq %rax, 0x28(%rsp)
movq 0x70(%rbx), %rax
movl %ecx, 0x40(%rsp)
movq $0x1, 0xa0(%rsp)
movl 0xc(%rbx), %ecx
orl $0x8, %ecx
movw %cx, 0x44(%rsp)
orq $-0x1, 0x68(%rsp)
orq $-0x1, 0x98(%rsp)
movq $0x100000, 0x38(%rsp)   # imm = 0x100000
xorl %ecx, %ecx
jmp 0xbdaf
leaq 0x136(%rsp), %rdi
callq 0x2360
cmpl $0x1d, %eax
jle 0xbc21
jmp 0xbc25
movl 0x40(%rsp), %eax
movq 0x10(%rsp), %r12
movl %eax, 0x10(%rbx)
movq 0x70(%rbx), %rax
movb $0x1, %cl
testq %rax, %rax
jne 0xbdbd
movq 0x38(%rsp), %rax
movq %rax, 0x70(%rbx)
movq 0xa0(%rsp), %rsi
incq %rsi
movl 0x40(%rsp), %edx
imulq %rsi, %rdx
cmpq %rdx, %rax
jae 0xbddc
movq %rdx, 0x70(%rbx)
movq %rdx, %rax
movq %rax, 0x38(%rsp)
testb %cl, %cl
je 0xbdf3
testb $0x1, 0x8(%rsp)
je 0xbe12
movq 0x30(%rsp), %rsi
jmp 0xbe14
testb $0x1, 0x8(%rsp)
jne 0xbe19
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0xcf68
testl %eax, %eax
jne 0xbe2d
movb $0x1, %r15b
xorl %esi, %esi
jmp 0xbe21
xorl %esi, %esi
movb $0x1, %r15b
jmp 0xbe21
movq 0x30(%rsp), %rsi
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0xb7e4
testl %eax, %eax
je 0xbe35
movl %eax, %r13d
jmp 0xbfe6
testb %r15b, %r15b
movl 0x4(%rsp), %r15d
jne 0xbe60
testb $0x1, 0x8(%rsp)
je 0xbe4f
movq 0x38(%rbx), %rax
movq %rax, 0x30(%rsp)
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0xcf68
testl %eax, %eax
jne 0xbe2d
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 %edx, %edx
divq %rcx
movq %rax, 0x80(%rbx)
testl $0xa0000, %ebp          # imm = 0xA0000
je 0xbea2
movl (%rbx), %eax
movl %eax, 0x8(%rbx)
jmp 0xbec8
movl 0x24(%rsp), %esi
andl $0x2, %esi
orl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2320
movl %eax, 0x8(%rbx)
cmpl $-0x1, %eax
je 0xbfde
cmpl $0x0, 0xc(%rsp)
jle 0xbf4c
movq 0x48(%rbx), %rax
movq 0x80(%rax), %rax
movq 0x50(%rbx), %rcx
xorl %edx, %edx
cmpq 0x80(%rcx), %rax
setb %dl
movq 0x48(%rbx,%rdx,8), %rax
movq 0x80(%rax), %rax
movq 0x40(%rbx), %rcx
movq %rax, 0x30(%rcx)
xorps %xmm0, %xmm0
leaq 0xb0(%rsp), %r15
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movq $0x1, 0x10(%r15)
pushq $0x6
popq %r12
movl 0x4(%rbx), %edi
xorl %r13d, %r13d
movl %r12d, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x20c0
testl %eax, %eax
je 0xbf43
callq 0x2050
movl (%rax), %r13d
cmpl $0x4, %r13d
je 0xbf1e
testl %r13d, %r13d
jne 0xbfe6
xorl %r12d, %r12d
btl $0x11, %ebp
jb 0xbb95
movl 0x10(%rbx), %esi
movl 0x24(%rbx), %r15d
pushq $0x1
popq %rdi
callq 0x2200
movq %rax, 0x58(%rbx)
testq %rax, %rax
je 0xbb8a
imull $0x3d, %r15d, %eax
addl $0x88, %eax
movslq %eax, %rsi
pushq $0x1
popq %rdi
callq 0x2200
testq %rax, %rax
je 0xbb8a
leaq 0x88(%rax), %rcx
movq %rcx, 0x58(%rax)
imulq $0x30, %r15, %rcx
addq %rax, %rcx
addq $0x88, %rcx
movq %rcx, 0x68(%rax)
leaq (%rcx,%r15,8), %rcx
movq %rcx, 0x60(%rax)
leaq (%rcx,%r15,4), %rcx
movq %rcx, 0x70(%rax)
movq %rbx, 0x20(%rax)
movq 0x88(%rbx), %rcx
movq %rcx, 0x50(%rax)
movl $0x1, 0x7c(%rax)
movq %rax, 0x68(%rbx)
jmp 0xbb95
callq 0x2050
movl (%rax), %r13d
movl %r13d, %r14d
testl %r13d, %r13d
jne 0xbb8a
xorl %r12d, %r12d
jmp 0xbb95