// File: BenLangmead[P]bowtie/./range_source.h
// Function: CostAwareRangeSourceDriver<EbwtRangeSource>::foundFirstRange(Range*)
bool foundFirstRange(Range* r) {
		assert(r != NULL);
		assert(checkRange(r));
		this->foundRange = true;
		lastRange_ = r;
		if(strandFix_) {
			// We found a range but there may be an equally good range
			// on the other strand; let's try to get it.
			const size_t sz = active_.size();
			for(size_t i = 1; i < sz; i++) {
				// Same mate, different orientation?
				if(rss_[i]->mate1() == r->mate1 && rss_[i]->fw() != r->fw) {
					// Yes; see if it has the same cost
					TRangeSrcDrPtr p = active_[i];
					uint16_t minCost = max(this->minCost, p->minCost);
					if(minCost > r->cost) break;
					// Yes, it has the same cost
					assert_eq(minCost, r->cost); // can't be better
					// Advance it until it's done, we've found a range,
					// or its cost increases
					if(this->verbose_) cout << " Looking for opposite range to avoid strand bias:" << endl;
					while(!p->done && !p->foundRange) {
						p->advance(ADV_COST_CHANGES);
						assert_geq(p->minCost, minCost);
						if(p->minCost > minCost) break;
					}
					if(p->foundRange) {
						// Found one!  Now we have to choose which one
						// to give out first; we choose randomly using
						// the size of the ranges as weights.
						delayedRange_ = &p->range();
						assert(checkRange(delayedRange_));
						size_t tot = (delayedRange_->bot - delayedRange_->top) +
						             (lastRange_->bot    - lastRange_->top);
						uint32_t rq = rand_.nextU32() % tot;
						// We picked this range, not the first one
						if(rq < (delayedRange_->bot - delayedRange_->top)) {
							Range *tmp = lastRange_;
							lastRange_ = delayedRange_;
							delayedRange_ = tmp;
						}
						p->foundRange = false;
					}
					// Return true iff we need to force a re-sort
					return true;
				}
			}
			// OK, now we have a choice of two equally good ranges from
			// each strand.
		}
		return false;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movb $0x1, 0x8(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x70(%rax)
testb $0x1, 0x51(%rax)
je 0x4b0d0
movq -0x58(%rbp), %rdi
addq $0x30, %rdi
callq 0x4a490
movq %rax, -0x20(%rbp)
movq $0x1, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x4b0ce
movq -0x58(%rbp), %rdi
addq $0x10, %rdi
movq -0x28(%rbp), %rsi
callq 0x4a4b0
movq (%rax), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
andb $0x1, %al
movzbl %al, %eax
movq -0x18(%rbp), %rcx
movb 0x15(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x4b0bb
movq -0x58(%rbp), %rdi
addq $0x10, %rdi
movq -0x28(%rbp), %rsi
callq 0x4a4b0
movq (%rax), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
andb $0x1, %al
movzbl %al, %eax
movq -0x18(%rbp), %rcx
movb 0x14(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x4b0bb
movq -0x58(%rbp), %rdi
addq $0x30, %rdi
movq -0x28(%rbp), %rsi
callq 0x4a4b0
movq -0x58(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x30(%rbp)
addq $0xa, %rdi
movq -0x30(%rbp), %rsi
addq $0xa, %rsi
callq 0x499e0
movw (%rax), %ax
movw %ax, -0x32(%rbp)
movzwl -0x32(%rbp), %eax
movq -0x18(%rbp), %rcx
movzwl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jle 0x4afa6
jmp 0x4b0ce
movq -0x58(%rbp), %rax
testb $0x1, 0x88(%rax)
je 0x4afd5
movq 0x5cffe(%rip), %rdi     # 0xa7fb8
leaq 0x41de5(%rip), %rsi     # 0x8cda6
callq 0xa5d0
movq %rax, %rdi
movq 0x5cf98(%rip), %rsi     # 0xa7f68
callq 0xa640
jmp 0x4afd7
movq -0x30(%rbp), %rcx
xorl %eax, %eax
testb $0x1, 0x9(%rcx)
movb %al, -0x59(%rbp)
jne 0x4aff2
movq -0x30(%rbp), %rax
movb 0x8(%rax), %al
xorb $-0x1, %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x4affb
jmp 0x4b01e
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
movl $0x2, %esi
callq *0x20(%rax)
movq -0x30(%rbp), %rax
movzwl 0xa(%rax), %eax
movzwl -0x32(%rbp), %ecx
cmpl %ecx, %eax
jle 0x4b01c
jmp 0x4b01e
jmp 0x4afd7
movq -0x30(%rbp), %rax
testb $0x1, 0x8(%rax)
je 0x4b0b5
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
movq -0x58(%rbp), %rdi
movq %rax, 0x78(%rdi)
movq 0x78(%rdi), %rax
movl 0x4(%rax), %eax
movq 0x78(%rdi), %rcx
subl (%rcx), %eax
movq 0x70(%rdi), %rcx
movl 0x4(%rcx), %ecx
movq 0x70(%rdi), %rdx
subl (%rdx), %ecx
addl %ecx, %eax
movl %eax, %eax
movq %rax, -0x40(%rbp)
addq $0x58, %rdi
callq 0x47b20
movl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x40(%rbp)
movq %rdx, %rax
movq -0x58(%rbp), %rdx
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq 0x78(%rdx), %rcx
movl 0x4(%rcx), %ecx
movq 0x78(%rdx), %rdx
subl (%rdx), %ecx
cmpl %ecx, %eax
jae 0x4b0ad
movq -0x58(%rbp), %rax
movq 0x70(%rax), %rcx
movq %rcx, -0x50(%rbp)
movq 0x78(%rax), %rcx
movq %rcx, 0x70(%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x78(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0x8(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x4b0d4
jmp 0x4b0bd
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x4aee7
jmp 0x4b0d0
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nop