// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x7c(%rax), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x34(%rbp), %eax
andl $0x20000, %eax          # imm = 0x20000
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
je 0x37c1
cmpq $0x0, -0x20(%rbp)
jne 0x3541
movq -0x18(%rbp), %rdi
callq 0x7850
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq 0x80(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x48(%rax)
jmp 0x37bc
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x200000, %eax         # imm = 0x200000
cmpl $0x0, %eax
je 0x3560
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x3573
movq -0x18(%rbp), %rax
movl 0xa0(%rax), %edi
callq 0x20a0
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x35b0
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jne 0x359f
movq -0x48(%rbp), %rax
movq (%rax), %rax
cmpq $-0x1, %rax
je 0x35ab
movl $0xffff87c1, -0x4(%rbp) # imm = 0xFFFF87C1
jmp 0x3a4b
jmp 0x3763
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x4c(%rbp)
callq 0x22e0
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
addq $0x8, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0xe0(%rax)
jne 0x3613
movq -0x18(%rbp), %rdi
movl -0x4c(%rbp), %edx
movl $0x6, %esi
callq 0xebd0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x3605
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3a4b
movq -0x18(%rbp), %rax
movl $0x1, 0xe0(%rax)
movq -0x60(%rbp), %rdi
callq 0x2400
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
je 0x3647
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl -0x3c(%rbp), %edx
callq 0x158f0
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
je 0x3647
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3a4b
movq -0x20(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jae 0x368b
movq -0x20(%rbp), %rax
addq $0x80, %rax
movl -0x2c(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl $0x0, %eax
jne 0x367e
jmp 0x368b
jmp 0x3680
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3658
movl -0x2c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x36ac
movq -0x60(%rbp), %rdi
callq 0x2250
movl $0xffff87ba, -0x4(%rbp) # imm = 0xFFFF87BA
jmp 0x3a4b
movq -0x20(%rbp), %rax
addq $0x80, %rax
movl -0x2c(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x48(%rbp), %rax
movq $-0x1, (%rax)
movq -0x58(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jne 0x36fe
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x30(%rbp)
movq -0x20(%rbp), %rax
movl %ecx, 0x38(%rax)
movl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl -0x4c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x60(%rbp), %rdi
callq 0x2250
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x200000, %eax         # imm = 0x200000
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0x3761
movq -0x18(%rbp), %rax
movl 0xa0(%rax), %edi
movq -0x48(%rbp), %rsi
callq 0x2410
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
je 0x3761
movq -0x48(%rbp), %rax
movl $0x0, 0x8(%rax)
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3a4b
jmp 0x3763
jmp 0x3765
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x30(%rcx), %rcx
cmpq %rcx, %rax
jne 0x3765
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
andq $0x1, %rcx
movq 0x48(%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x3919
cmpq $0x0, -0x20(%rbp)
je 0x3837
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
addq $0x40, %rdi
callq 0x2400
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
je 0x380c
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rsi
addq $0x40, %rsi
movl -0x3c(%rbp), %edx
callq 0x158f0
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
je 0x380c
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3a4b
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
andq $0x1, %rcx
movq 0x48(%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x3857
movq -0x18(%rbp), %rdi
callq 0x7850
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq 0x80(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movl $0x1ffff, 0x80(%rax)    # imm = 0x1FFFF
movq -0x18(%rbp), %rax
movq 0xd0(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
movq 0xc8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x98(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, %edx
shlq $0x2, %rdx
callq 0x2230
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x18, %rsi
movl $0x60, %edx
callq 0x2230
movq -0x28(%rbp), %rax
movq 0x78(%rax), %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x7c(%rax)
movq -0x18(%rbp), %rax
movl 0x20(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x78(%rax)
movl $0x2, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x78(%rcx), %eax
jae 0x39dc
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rax
movl -0x2c(%rbp), %ecx
movw (%rax,%rcx,2), %ax
movw %ax, -0x36(%rbp)
movzwl -0x36(%rbp), %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movw %ax, %cx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x2c(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x4(%rax)
movzwl -0x36(%rbp), %edx
andl $0x8000, %edx           # imm = 0x8000
xorl %eax, %eax
movl $0x1a, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movl -0x2c(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3966
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movb $0x18, 0x1(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movb $0x8, (%rax)
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x3a0d
movl $0xffff87b5, -0x3c(%rbp) # imm = 0xFFFF87B5
jmp 0x3a36
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x3a2b
movl $0xffff87bf, -0x3c(%rbp) # imm = 0xFFFF87BF
jmp 0x3a34
movl $0x0, -0x4(%rbp)
jmp 0x3a4b
jmp 0x3a36
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
orl $0x5, %esi
callq 0x4030
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)