// File: /workspace/github_cpp_makefile_star2_O2/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_txn_renew0
static int
mdb_txn_renew0(MDB_txn *txn)
{
	MDB_env *env = txn->mt_env;
	MDB_txninfo *ti = env->me_txns;
	MDB_meta *meta;
	unsigned int i, nr, flags = txn->mt_flags;
	uint16_t x;
	int rc, new_notls = 0;

	if ((flags &= MDB_TXN_RDONLY) != 0) {
		if (!ti) {
			meta = mdb_env_pick_meta(env);
			txn->mt_txnid = meta->mm_txnid;
			txn->mt_u.reader = NULL;
		} else {
			MDB_reader *r = (env->me_flags & MDB_NOTLS) ? txn->mt_u.reader :
				pthread_getspecific(env->me_txkey);
			if (r) {
				if (r->mr_pid != env->me_pid || r->mr_txnid != (txnid_t)-1)
					return MDB_BAD_RSLOT;
			} else {
				MDB_PID_T pid = env->me_pid;
				MDB_THR_T tid = pthread_self();
				mdb_mutexref_t rmutex = env->me_rmutex;

				if (!env->me_live_reader) {
					rc = mdb_reader_pid(env, Pidset, pid);
					if (rc)
						return rc;
					env->me_live_reader = 1;
				}

				if (LOCK_MUTEX(rc, env, rmutex))
					return rc;
				nr = ti->mti_numreaders;
				for (i=0; i<nr; i++)
					if (ti->mti_readers[i].mr_pid == 0)
						break;
				if (i == env->me_maxreaders) {
					UNLOCK_MUTEX(rmutex);
					return MDB_READERS_FULL;
				}
				r = &ti->mti_readers[i];
				/* Claim the reader slot, carefully since other code
				 * uses the reader table un-mutexed: First reset the
				 * slot, next publish it in mti_numreaders.  After
				 * that, it is safe for mdb_env_close() to touch it.
				 * When it will be closed, we can finally claim it.
				 */
				r->mr_pid = 0;
				r->mr_txnid = (txnid_t)-1;
				r->mr_tid = tid;
				if (i == nr)
					ti->mti_numreaders = ++nr;
				env->me_close_readers = nr;
				r->mr_pid = pid;
				UNLOCK_MUTEX(rmutex);

				new_notls = (env->me_flags & MDB_NOTLS);
				if (!new_notls && (rc=pthread_setspecific(env->me_txkey, r))) {
					r->mr_pid = 0;
					return rc;
				}
			}
			do /* LY: Retry on a race, ITS#7970. */
				r->mr_txnid = ti->mti_txnid;
			while(r->mr_txnid != ti->mti_txnid);
			txn->mt_txnid = r->mr_txnid;
			txn->mt_u.reader = r;
			meta = env->me_metas[txn->mt_txnid & 1];
		}

	} else {
		/* Not yet touching txn == env->me_txn0, it may be active */
		if (ti) {
			if (LOCK_MUTEX(rc, env, env->me_wmutex))
				return rc;
			txn->mt_txnid = ti->mti_txnid;
			meta = env->me_metas[txn->mt_txnid & 1];
		} else {
			meta = mdb_env_pick_meta(env);
			txn->mt_txnid = meta->mm_txnid;
		}
		txn->mt_txnid++;
#if MDB_DEBUG
		if (txn->mt_txnid == mdb_debug_start)
			mdb_debug = 1;
#endif
		txn->mt_child = NULL;
		txn->mt_loose_pgs = NULL;
		txn->mt_loose_count = 0;
		txn->mt_dirty_room = MDB_IDL_UM_MAX;
		txn->mt_u.dirty_list = env->me_dirty_list;
		txn->mt_u.dirty_list[0].mid = 0;
		txn->mt_free_pgs = env->me_free_pgs;
		txn->mt_free_pgs[0] = 0;
		txn->mt_spill_pgs = NULL;
		env->me_txn = txn;
		memcpy(txn->mt_dbiseqs, env->me_dbiseqs, env->me_maxdbs * sizeof(unsigned int));
	}

	/* Copy the DB info and flags */
	memcpy(txn->mt_dbs, meta->mm_dbs, CORE_DBS * sizeof(MDB_db));

	/* Moved to here to avoid a data race in read TXNs */
	txn->mt_next_pgno = meta->mm_last_pg+1;

	txn->mt_flags = flags;

	/* Setup db info */
	txn->mt_numdbs = env->me_numdbs;
	for (i=CORE_DBS; i<txn->mt_numdbs; i++) {
		x = env->me_dbflags[i];
		txn->mt_dbs[i].md_flags = x & PERSISTENT_FLAGS;
		txn->mt_dbflags[i] = (x & MDB_VALID) ? DB_VALID|DB_USRVALID|DB_STALE : 0;
	}
	txn->mt_dbflags[MAIN_DBI] = DB_VALID|DB_USRVALID;
	txn->mt_dbflags[FREE_DBI] = DB_VALID;

	if (env->me_flags & MDB_FATAL_ERROR) {
		DPUTS("environment had fatal error, must shutdown!");
		rc = MDB_PANIC;
	} else if (env->me_maxpg < txn->mt_next_pgno) {
		rc = MDB_MAP_RESIZED;
	} else {
		return MDB_SUCCESS;
	}
	mdb_txn_end(txn, new_notls /*0 or MDB_END_SLOT*/ | MDB_END_FAIL_BEGIN);
	return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x20(%rdi), %r14
movq 0x40(%r14), %rbp
movl $0x20000, %r13d         # imm = 0x20000
andl 0x7c(%rdi), %r13d
jne 0x2ea0
testq %rbp, %rbp
je 0x2ec8
leaq 0x40(%rbp), %rdi
callq 0x2420
testl %eax, %eax
je 0x2e90
movq 0x40(%r14), %rsi
addq $0x40, %rsi
movq %r14, %rdi
movl %eax, %edx
callq 0xcec6
testl %eax, %eax
jne 0x3036
movq 0x30(%rbp), %rax
movl %eax, %ecx
andl $0x1, %ecx
movq 0x48(%r14,%rcx,8), %r15
jmp 0x2eef
testq %rbp, %rbp
je 0x2f4f
testb $0x20, 0xe(%r14)
jne 0x3045
movl 0xa0(%r14), %edi
callq 0x20b0
movq %rax, %r12
jmp 0x3049
movq 0x48(%r14), %rax
movq 0x80(%rax), %rax
movq 0x50(%r14), %rcx
xorl %edx, %edx
cmpq 0x80(%rcx), %rax
setb %dl
movq 0x48(%r14,%rdx,8), %r15
movq 0x80(%r15), %rax
incq %rax
movq %rax, 0x18(%rbx)
andq $0x0, 0x8(%rbx)
andq $0x0, 0x30(%rbx)
andl $0x0, 0x38(%rbx)
movl $0x1ffff, 0x80(%rbx)    # imm = 0x1FFFF
movq 0xd0(%r14), %rax
movq %rax, 0x48(%rbx)
andq $0x0, (%rax)
movq 0xc8(%r14), %rax
movq %rax, 0x28(%rbx)
andq $0x0, (%rax)
andq $0x0, 0x40(%rbx)
movq %rbx, 0x60(%r14)
movq 0x60(%rbx), %rdi
movq 0x98(%r14), %rsi
movl 0x24(%r14), %edx
shlq $0x2, %rdx
callq 0x2240
jmp 0x2f7f
movq 0x48(%r14), %rax
movq 0x80(%rax), %rax
movq 0x50(%r14), %rcx
xorl %edx, %edx
cmpq 0x80(%rcx), %rax
setb %dl
movq 0x48(%r14,%rdx,8), %r15
movq 0x80(%r15), %rax
movq %rax, 0x18(%rbx)
andq $0x0, 0x48(%rbx)
pushq $0x5
popq %rdx
movq 0x58(%rbx), %rdi
leaq 0x18(%r15), %rsi
pushq $0xc
popq %rcx
rep  movsq (%rsi), %es:(%rdi)
movq 0x78(%r15), %rax
incq %rax
movq %rax, 0x10(%rbx)
movl %r13d, 0x7c(%rbx)
movl 0x20(%r14), %r8d
movl %r8d, 0x78(%rbx)
pushq $0x2
popq %rax
pushq $0x32
popq %rcx
xorl %esi, %esi
pushq $0x1a
popq %rdi
movl %r8d, %r8d
cmpq %r8, %rax
jae 0x2ff9
movq 0x90(%r14), %r8
movzwl (%r8,%rax,2), %r8d
movl %r8d, %r9d
andl $0x7fff, %r9d           # imm = 0x7FFF
movq 0x58(%rbx), %r10
movw %r9w, (%r10,%rcx,2)
testw %r8w, %r8w
movl %edi, %r8d
cmovnsl %esi, %r8d
movq 0x70(%rbx), %r9
movb %r8b, (%r9,%rax)
incq %rax
movl 0x78(%rbx), %r8d
addq $0x18, %rcx
jmp 0x2fb2
movq 0x70(%rbx), %rax
movb $0x18, 0x1(%rax)
movq 0x70(%rbx), %rax
movb $0x8, (%rax)
cmpl $0x0, 0xc(%r14)
js 0x3025
movq 0x80(%r14), %rcx
xorl %eax, %eax
movl $0xffff87bf, %ebp       # imm = 0xFFFF87BF
cmpq 0x10(%rbx), %rcx
jb 0x302a
jmp 0x3036
movl $0xffff87b5, %ebp       # imm = 0xFFFF87B5
movq %rbx, %rdi
movl %edx, %esi
callq 0x351c
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rbx), %r12
testq %r12, %r12
je 0x306f
movl 0x8(%r12), %ecx
movl $0xffff87c1, %eax       # imm = 0xFFFF87C1
cmpl 0x28(%r14), %ecx
jne 0x3036
movq (%r12), %rcx
cmpq $-0x1, %rcx
jne 0x3036
xorl %edx, %edx
jmp 0x3179
movl 0x28(%r14), %r12d
callq 0x2300
movq %rax, (%rsp)
movq 0x40(%r14), %r15
cmpl $0x0, 0xe0(%r14)
je 0x30cc
addq $0x8, %r15
movq %r15, %rdi
callq 0x2420
testl %eax, %eax
je 0x30ab
movq %r14, %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0xcec6
testl %eax, %eax
jne 0x3036
movl 0x38(%rbp), %eax
leaq 0x80(%rbp), %rcx
xorl %edx, %edx
cmpl %edx, %eax
je 0x30ef
movl %edx, %esi
shlq $0x6, %rsi
cmpl $0x0, 0x8(%rcx,%rsi)
je 0x30f1
incl %edx
jmp 0x30b7
pushq $0x6
popq %rsi
movq %r14, %rdi
movl %r12d, %edx
callq 0x94eb
testl %eax, %eax
jne 0x3036
movl $0x1, 0xe0(%r14)
jmp 0x308a
movl %eax, %edx
cmpl 0x18(%r14), %edx
jne 0x3109
movq %r15, %rdi
callq 0x2260
movl $0xffff87ba, %eax       # imm = 0xFFFF87BA
jmp 0x3036
movl %r12d, %edi
movl %edx, %esi
shlq $0x6, %rsi
leaq (%rcx,%rsi), %r12
movl $0x0, 0x8(%rcx,%rsi)
movq $-0x1, (%rcx,%rsi)
movq (%rsp), %r8
movq %r8, 0x10(%rcx,%rsi)
cmpl %eax, %edx
jne 0x3138
incl %eax
movl %eax, 0x38(%rbp)
movl %eax, 0x1c(%r14)
movl %edi, 0x8(%r12)
movq %r15, %rdi
callq 0x2260
movl $0x200000, %edx         # imm = 0x200000
andl 0xc(%r14), %edx
jne 0x3179
movl 0xa0(%r14), %edi
movq %r12, %rsi
callq 0x2430
testl %eax, %eax
je 0x3068
movl $0x0, 0x8(%r12)
jmp 0x3036
movq 0x30(%rbp), %rax
movq %rax, (%r12)
movq (%r12), %rax
cmpq 0x30(%rbp), %rax
jne 0x3179
movq (%r12), %rax
movq %rax, 0x18(%rbx)
movq %r12, 0x48(%rbx)
andl $0x1, %eax
movq 0x48(%r14,%rax,8), %r15
orl $0x5, %edx
jmp 0x2f82