// File: /workspace/github_cpp_makefile_star2_O3/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, %r12d         # imm = 0x20000
andl 0x7c(%rdi), %r12d
jne 0x3148
testq %rbp, %rbp
je 0x3170
leaq 0x40(%rbp), %rdi
callq 0x2410
testl %eax, %eax
je 0x3138
movq 0x40(%r14), %rsi
addq $0x40, %rsi
movq %r14, %rdi
movl %eax, %edx
callq 0xd942
testl %eax, %eax
jne 0x32e3
movq 0x30(%rbp), %rax
movl %eax, %ecx
andl $0x1, %ecx
movq 0x48(%r14,%rcx,8), %r15
jmp 0x3197
testq %rbp, %rbp
je 0x31f7
testb $0x20, 0xe(%r14)
jne 0x32f2
movl 0xa0(%r14), %edi
callq 0x20b0
movq %rax, %r13
jmp 0x32f6
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)
xorl %eax, %eax
movq %rax, 0x8(%rbx)
movq %rax, 0x30(%rbx)
movl $0x0, 0x38(%rbx)
movl $0x1ffff, 0x80(%rbx)    # imm = 0x1FFFF
movq 0xd0(%r14), %rcx
movq %rcx, 0x48(%rbx)
movq %rax, (%rcx)
movq 0xc8(%r14), %rcx
movq %rcx, 0x28(%rbx)
movq %rax, (%rcx)
movq %rax, 0x40(%rbx)
movq %rbx, 0x60(%r14)
movq 0x60(%rbx), %rdi
movq 0x98(%r14), %rsi
movl 0x24(%r14), %edx
shlq $0x2, %rdx
callq 0x2240
jmp 0x322a
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)
movq $0x0, 0x48(%rbx)
movl $0x5, %edx
movq 0x58(%rbx), %rdi
leaq 0x18(%r15), %rsi
movl $0xc, %ecx
rep  movsq (%rsi), %es:(%rdi)
movq 0x78(%r15), %rax
incq %rax
movq %rax, 0x10(%rbx)
movl %r12d, 0x7c(%rbx)
movl 0x20(%r14), %eax
movl %eax, 0x78(%rbx)
cmpl $0x3, %eax
jb 0x32a6
movl $0x2, %eax
movl $0x32, %ecx
xorl %esi, %esi
movq 0x90(%r14), %rdi
movzwl (%rdi,%rax,2), %edi
movl %edi, %r8d
andl $0x7fff, %r8d           # imm = 0x7FFF
movq 0x58(%rbx), %r9
movw %r8w, (%r9,%rcx,2)
testw %di, %di
movl $0x1a, %edi
cmovnsl %esi, %edi
movq 0x70(%rbx), %r8
movb %dil, (%r8,%rax)
incq %rax
movl 0x78(%rbx), %edi
addq $0x18, %rcx
cmpq %rdi, %rax
jb 0x3266
movq 0x70(%rbx), %rax
movb $0x18, 0x1(%rax)
movq 0x70(%rbx), %rax
movb $0x8, (%rax)
cmpl $0x0, 0xc(%r14)
js 0x32d2
movq 0x80(%r14), %rcx
xorl %eax, %eax
movl $0xffff87bf, %ebp       # imm = 0xFFFF87BF
cmpq 0x10(%rbx), %rcx
jb 0x32d7
jmp 0x32e3
movl $0xffff87b5, %ebp       # imm = 0xFFFF87B5
movq %rbx, %rdi
movl %edx, %esi
callq 0x380d
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rbx), %r13
testq %r13, %r13
je 0x331b
movl 0x8(%r13), %ecx
movl $0xffff87c1, %eax       # imm = 0xFFFF87C1
cmpl 0x28(%r14), %ecx
jne 0x32e3
movq (%r13), %rcx
cmpq $-0x1, %rcx
jne 0x32e3
xorl %edx, %edx
jmp 0x342e
movl 0x28(%r14), %r13d
callq 0x22f0
movq %rax, (%rsp)
movq 0x40(%r14), %r15
cmpl $0x0, 0xe0(%r14)
je 0x337a
addq $0x8, %r15
movq %r15, %rdi
callq 0x2410
testl %eax, %eax
je 0x3357
movq %r14, %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0xd942
testl %eax, %eax
jne 0x32e3
movl 0x38(%rbp), %eax
testl %eax, %eax
je 0x339f
leaq 0x88(%rbp), %rdx
xorl %ecx, %ecx
cmpl $0x0, (%rdx)
je 0x33a1
incl %ecx
addq $0x40, %rdx
cmpl %ecx, %eax
jne 0x3367
movl %eax, %ecx
jmp 0x33a1
movq %r14, %rdi
movl $0x6, %esi
movl %r13d, %edx
callq 0x9c89
testl %eax, %eax
jne 0x32e3
movl $0x1, 0xe0(%r14)
jmp 0x3336
xorl %ecx, %ecx
cmpl 0x18(%r14), %ecx
jne 0x33b9
movq %r15, %rdi
callq 0x2260
movl $0xffff87ba, %eax       # imm = 0xFFFF87BA
jmp 0x32e3
movl %r13d, %edi
leaq 0x80(%rbp), %rdx
movl %ecx, %esi
shlq $0x6, %rsi
leaq (%rdx,%rsi), %r13
movl $0x0, 0x8(%rdx,%rsi)
movq $-0x1, (%rdx,%rsi)
movq (%rsp), %r8
movq %r8, 0x10(%rdx,%rsi)
cmpl %eax, %ecx
jne 0x33ef
incl %eax
movl %eax, 0x38(%rbp)
movl %eax, 0x1c(%r14)
movl %edi, 0x8(%r13)
movq %r15, %rdi
callq 0x2260
movl $0x200000, %edx         # imm = 0x200000
andl 0xc(%r14), %edx
jne 0x342e
movl 0xa0(%r14), %edi
movq %r13, %rsi
callq 0x2420
testl %eax, %eax
je 0x3314
movl $0x0, 0x8(%r13)
jmp 0x32e3
movq 0x30(%rbp), %rax
movq %rax, (%r13)
movq (%r13), %rax
cmpq 0x30(%rbp), %rax
jne 0x342e
movq (%r13), %rax
movq %rax, 0x18(%rbx)
movq %r13, 0x48(%rbx)
andl $0x1, %eax
movq 0x48(%r14,%rax,8), %r15
orl $0x5, %edx
jmp 0x322f