// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_reader_check0
static int ESECT
mdb_reader_check0(MDB_env *env, int rlocked, int *dead)
{
	mdb_mutexref_t rmutex = rlocked ? NULL : env->me_rmutex;
	unsigned int i, j, rdrs;
	MDB_reader *mr;
	MDB_PID_T *pids, pid;
	int rc = MDB_SUCCESS, count = 0;

	rdrs = env->me_txns->mti_numreaders;
	pids = malloc((rdrs+1) * sizeof(MDB_PID_T));
	if (!pids)
		return ENOMEM;
	pids[0] = 0;
	mr = env->me_txns->mti_readers;
	for (i=0; i<rdrs; i++) {
		pid = mr[i].mr_pid;
		if (pid && pid != env->me_pid) {
			if (mdb_pid_insert(pids, pid) == 0) {
				if (!mdb_reader_pid(env, Pidcheck, pid)) {
					/* Stale reader found */
					j = i;
					if (rmutex) {
						if ((rc = LOCK_MUTEX0(rmutex)) != 0) {
							if ((rc = mdb_mutex_failed(env, rmutex, rc)))
								break;
							rdrs = 0; /* the above checked all readers */
						} else {
							/* Recheck, a new process may have reused pid */
							if (mdb_reader_pid(env, Pidcheck, pid))
								j = rdrs;
						}
					}
					for (; j<rdrs; j++)
							if (mr[j].mr_pid == pid) {
								DPRINTF(("clear stale reader pid %u txn %"Z"d",
									(unsigned) pid, mr[j].mr_txnid));
								mr[j].mr_pid = 0;
								count++;
							}
					if (rmutex)
						UNLOCK_MUTEX(rmutex);
				}
			}
		}
	}
	free(pids);
	if (dead)
		*dead = count;
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x15701
xorl %eax, %eax
movq %rax, -0x60(%rbp)
jmp 0x15711
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
addq $0x8, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movl 0x38(%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x2260
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x1575f
movl $0xc, -0x4(%rbp)
jmp 0x158df
movq -0x48(%rbp), %rax
movl $0x0, (%rax)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
addq $0x80, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x158c0
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x158b0
movl -0x4c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x28(%rcx), %eax
je 0x158b0
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x16ac0
cmpl $0x0, %eax
jne 0x158ae
movq -0x10(%rbp), %rdi
movl -0x4c(%rbp), %edx
movl $0x5, %esi
callq 0xebd0
cmpl $0x0, %eax
jne 0x158ac
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x1584f
movq -0x28(%rbp), %rdi
callq 0x2400
movl %eax, -0x50(%rbp)
cmpl $0x0, %eax
je 0x1582f
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x50(%rbp), %edx
callq 0x158f0
movl %eax, -0x50(%rbp)
cmpl $0x0, %eax
je 0x15826
jmp 0x158c0
movl $0x0, -0x34(%rbp)
jmp 0x1584d
movq -0x10(%rbp), %rdi
movl -0x4c(%rbp), %edx
movl $0x5, %esi
callq 0xebd0
cmpl $0x0, %eax
je 0x1584b
movl -0x34(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x1584d
jmp 0x1584f
jmp 0x15851
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x1589a
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x4c(%rbp), %eax
jne 0x1588d
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1588f
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x15851
cmpq $0x0, -0x28(%rbp)
je 0x158aa
movq -0x28(%rbp), %rdi
callq 0x2250
jmp 0x158ac
jmp 0x158ae
jmp 0x158b0
jmp 0x158b2
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x15782
movq -0x48(%rbp), %rdi
callq 0x2030
cmpq $0x0, -0x20(%rbp)
je 0x158d9
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)