// File: BenLangmead[P]bowtie/./ebwt.h
// Function: Ebwt::joinedToTextOff(unsigned long, unsigned long, unsigned long&, unsigned long&, unsigned long&) const
void Ebwt::joinedToTextOff(TIndexOffU qlen, TIndexOffU off,
				 TIndexOffU& tidx,
				 TIndexOffU& textoff,
				 TIndexOffU& tlen) const
{
	TIndexOffU top = 0;
	TIndexOffU bot = _nFrag; // 1 greater than largest addressable element
	TIndexOffU elt = OFF_MASK;
	// Begin binary search
	while(true) {
		ASSERT_ONLY(TIndexOffU oldelt = elt);
		elt = top + ((bot - top) >> 1);
		assert_neq(oldelt, elt); // must have made progress
		TIndexOffU lower = _rstarts[elt*3];
		TIndexOffU upper;
		if(elt == _nFrag-1) {
			upper = _eh._len;
		} else {
			upper = _rstarts[((elt+1)*3)];
		}
		assert_gt(upper, lower);
		TIndexOffU fraglen = upper - lower;
		if(lower <= off) {
			if(upper > off) { // not last element, but it's within
				// off is in this range; check if it falls off
				if(off + qlen > upper) {
					// it falls off; signal no-go and return
					tidx = OFF_MASK;
					assert_lt(elt, _nFrag-1);
					return;
				}
				tidx = _rstarts[(elt*3)+1];
				assert_lt(tidx, this->_nPat);
				assert_leq(fraglen, this->_plen[tidx]);
				// it doesn't fall off; now calculate textoff.
				// Initially it's the number of characters that precede
				// the alignment in the fragment
				uint32_t fragoff = off - _rstarts[(elt*3)];
				if(!this->_fw) {
					fragoff = fraglen - fragoff - 1;
					fragoff -= (qlen-1);
				}
				// Add the alignment's offset into the fragment
				// ('fragoff') to the fragment's offset within the text
				textoff = fragoff + _rstarts[(elt*3)+2];
				assert_lt(textoff, this->_plen[tidx]);
				break; // done with binary search
			} else {
				// 'off' belongs somewhere in the region between elt
				// and bot
				top = elt;
			}
		} else {
			// 'off' belongs somewhere in the region between top and
			// elt
			bot = elt;
		}
		// continue with binary search
	}
	tlen = this->_plen[tidx];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x38(%rbp)
movq 0x88(%rax), %rax
movq %rax, -0x40(%rbp)
movq $-0x1, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
subq -0x38(%rbp), %rcx
shrq %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jne 0x88b9
movq 0x41827(%rip), %rdi     # 0x49fc0
leaq 0x36b4a(%rip), %rsi     # 0x3f2ea
callq 0x5360
movq %rax, %rdi
movq -0x50(%rbp), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x36974(%rip), %rsi     # 0x3f12f
callq 0x5360
movq %rax, %rdi
leaq 0x10016(%rip), %rsi     # 0x187e0
callq 0x5120
movq %rax, %rdi
movq -0x50(%rbp), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x1002b(%rip), %rsi     # 0x18810
callq 0x5120
movq %rax, %rdi
leaq 0x36940(%rip), %rsi     # 0x3f134
callq 0x5360
movq %rax, %rdi
movq -0x48(%rbp), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x36920(%rip), %rsi     # 0x3f12f
callq 0x5360
movq %rax, %rdi
leaq 0xffc2(%rip), %rsi      # 0x187e0
callq 0x5120
movq %rax, %rdi
movq -0x48(%rbp), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0xffd7(%rip), %rsi      # 0x18810
callq 0x5120
movq %rax, %rdi
leaq 0x38d70(%rip), %rsi     # 0x415b8
callq 0x5360
movq %rax, %rdi
movq 0x41729(%rip), %rsi     # 0x49f80
callq 0x53c0
movq 0x4175d(%rip), %rdi     # 0x49fc0
leaq 0x36853(%rip), %rsi     # 0x3f0bd
callq 0x5360
movq %rax, %rdi
leaq 0x37f11(%rip), %rsi     # 0x4078a
callq 0x5360
movq %rax, %rdi
movl $0xa15, %esi            # imm = 0xA15
callq 0x5750
movq %rax, %rdi
movq 0x416eb(%rip), %rsi     # 0x49f80
callq 0x53c0
leaq 0x37dc9(%rip), %rdi     # 0x4066a
leaq 0x36815(%rip), %rsi     # 0x3f0bd
movl $0xa15, %edx            # imm = 0xA15
leaq 0x36a51(%rip), %rcx     # 0x3f305
callq 0x51e0
movq -0x78(%rbp), %rcx
movq 0x98(%rcx), %rax
imulq $0x3, -0x48(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq 0x88(%rcx), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x88f6
movq -0x78(%rbp), %rax
movq 0x108(%rax), %rax
movq %rax, -0x60(%rbp)
jmp 0x8915
movq -0x78(%rbp), %rax
movq 0x98(%rax), %rax
movq -0x48(%rbp), %rcx
addq $0x1, %rcx
imulq $0x3, %rcx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
ja 0x89d8
movq 0x41696(%rip), %rdi     # 0x49fc0
leaq 0x367c7(%rip), %rsi     # 0x3f0f8
callq 0x5360
movq %rax, %rdi
movq -0x60(%rbp), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x367c2(%rip), %rsi     # 0x3f10e
callq 0x5360
movq %rax, %rdi
movq -0x58(%rbp), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x38c51(%rip), %rsi     # 0x415b8
callq 0x5360
movq %rax, %rdi
movq 0x4160a(%rip), %rsi     # 0x49f80
callq 0x53c0
movq 0x4163e(%rip), %rdi     # 0x49fc0
leaq 0x36734(%rip), %rsi     # 0x3f0bd
callq 0x5360
movq %rax, %rdi
leaq 0x37df2(%rip), %rsi     # 0x4078a
callq 0x5360
movq %rax, %rdi
movl $0xa1d, %esi            # imm = 0xA1D
callq 0x5750
movq %rax, %rdi
movq 0x415cc(%rip), %rsi     # 0x49f80
callq 0x53c0
leaq 0x37caa(%rip), %rdi     # 0x4066a
leaq 0x366f6(%rip), %rsi     # 0x3f0bd
movl $0xa1d, %edx            # imm = 0xA1D
leaq 0x36932(%rip), %rcx     # 0x3f305
callq 0x51e0
movq -0x60(%rbp), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x18(%rbp), %rax
ja 0x8e2d
movq -0x60(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x8e23
movq -0x18(%rbp), %rax
addq -0x10(%rbp), %rax
cmpq -0x60(%rbp), %rax
jbe 0x8afe
movq -0x78(%rbp), %rcx
movq -0x20(%rbp), %rax
movq $-0x1, (%rax)
movq -0x48(%rbp), %rax
movq 0x88(%rcx), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jb 0x8af9
movq 0x41580(%rip), %rdi     # 0x49fc0
leaq 0x36800(%rip), %rsi     # 0x3f247
callq 0x5360
movq %rax, %rdi
movq -0x48(%rbp), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x367fb(%rip), %rsi     # 0x3f25d
callq 0x5360
movq %rax, %rdi
movq -0x78(%rbp), %rax
movq 0x88(%rax), %rsi
subq $0x1, %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x38b30(%rip), %rsi     # 0x415b8
callq 0x5360
movq %rax, %rdi
movq 0x414e9(%rip), %rsi     # 0x49f80
callq 0x53c0
movq 0x4151d(%rip), %rdi     # 0x49fc0
leaq 0x36613(%rip), %rsi     # 0x3f0bd
callq 0x5360
movq %rax, %rdi
leaq 0x37cd1(%rip), %rsi     # 0x4078a
callq 0x5360
movq %rax, %rdi
movl $0xa25, %esi            # imm = 0xA25
callq 0x5750
movq %rax, %rdi
movq 0x414ab(%rip), %rsi     # 0x49f80
callq 0x53c0
leaq 0x37b89(%rip), %rdi     # 0x4066a
leaq 0x365d5(%rip), %rsi     # 0x3f0bd
movl $0xa25, %edx            # imm = 0xA25
leaq 0x36811(%rip), %rcx     # 0x3f305
callq 0x51e0
jmp 0x8e57
movq -0x78(%rbp), %rcx
movq 0x98(%rcx), %rax
imulq $0x3, -0x48(%rbp), %rdx
movq 0x8(%rax,%rdx,8), %rdx
movq -0x20(%rbp), %rax
movq %rdx, (%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
cmpq 0x80(%rcx), %rax
jb 0x8bed
movq 0x4148b(%rip), %rdi     # 0x49fc0
leaq 0x3670b(%rip), %rsi     # 0x3f247
callq 0x5360
movq %rax, %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x36703(%rip), %rsi     # 0x3f25d
callq 0x5360
movq %rax, %rdi
movq -0x78(%rbp), %rax
movq 0x80(%rax), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x38a3c(%rip), %rsi     # 0x415b8
callq 0x5360
movq %rax, %rdi
movq 0x413f5(%rip), %rsi     # 0x49f80
callq 0x53c0
movq 0x41429(%rip), %rdi     # 0x49fc0
leaq 0x3651f(%rip), %rsi     # 0x3f0bd
callq 0x5360
movq %rax, %rdi
leaq 0x37bdd(%rip), %rsi     # 0x4078a
callq 0x5360
movq %rax, %rdi
movl $0xa29, %esi            # imm = 0xA29
callq 0x5750
movq %rax, %rdi
movq 0x413b7(%rip), %rsi     # 0x49f80
callq 0x53c0
leaq 0x37a95(%rip), %rdi     # 0x4066a
leaq 0x364e1(%rip), %rsi     # 0x3f0bd
movl $0xa29, %edx            # imm = 0xA29
leaq 0x3671d(%rip), %rcx     # 0x3f305
callq 0x51e0
movq -0x78(%rbp), %rcx
movq -0x68(%rbp), %rax
movq 0x90(%rcx), %rcx
movq -0x20(%rbp), %rdx
movq (%rdx), %rdx
cmpq (%rcx,%rdx,8), %rax
jbe 0x8cd4
movq 0x413ac(%rip), %rdi     # 0x49fc0
leaq 0x36588(%rip), %rsi     # 0x3f1a3
callq 0x5360
movq %rax, %rdi
movq -0x68(%rbp), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x36584(%rip), %rsi     # 0x3f1ba
callq 0x5360
movq %rax, %rdi
movq -0x78(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x38955(%rip), %rsi     # 0x415b8
callq 0x5360
movq %rax, %rdi
movq 0x4130e(%rip), %rsi     # 0x49f80
callq 0x53c0
movq 0x41342(%rip), %rdi     # 0x49fc0
leaq 0x36438(%rip), %rsi     # 0x3f0bd
callq 0x5360
movq %rax, %rdi
leaq 0x37af6(%rip), %rsi     # 0x4078a
callq 0x5360
movq %rax, %rdi
movl $0xa2a, %esi            # imm = 0xA2A
callq 0x5750
movq %rax, %rdi
movq 0x412d0(%rip), %rsi     # 0x49f80
callq 0x53c0
leaq 0x379ae(%rip), %rdi     # 0x4066a
leaq 0x363fa(%rip), %rsi     # 0x3f0bd
movl $0xa2a, %edx            # imm = 0xA2A
leaq 0x36636(%rip), %rcx     # 0x3f305
callq 0x51e0
movq -0x78(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x98(%rax), %rdx
imulq $0x3, -0x48(%rbp), %rsi
subq (%rdx,%rsi,8), %rcx
movl %ecx, -0x6c(%rbp)
testb $0x1, 0x10(%rax)
jne 0x8d17
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, -0x6c(%rbp)
movq -0x10(%rbp), %rcx
subq $0x1, %rcx
movl -0x6c(%rbp), %eax
subq %rcx, %rax
movl %eax, -0x6c(%rbp)
movq -0x78(%rbp), %rcx
movl -0x6c(%rbp), %eax
movl %eax, %edx
movq 0x98(%rcx), %rax
imulq $0x3, -0x48(%rbp), %rsi
addq 0x10(%rax,%rsi,8), %rdx
movq -0x28(%rbp), %rax
movq %rdx, (%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x90(%rcx), %rcx
movq -0x20(%rbp), %rdx
movq (%rdx), %rdx
cmpq (%rcx,%rdx,8), %rax
jb 0x8e21
movq 0x41262(%rip), %rdi     # 0x49fc0
leaq 0x364e2(%rip), %rsi     # 0x3f247
callq 0x5360
movq %rax, %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x364da(%rip), %rsi     # 0x3f25d
callq 0x5360
movq %rax, %rdi
movq -0x78(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x50c0
movq %rax, %rdi
leaq 0x38808(%rip), %rsi     # 0x415b8
callq 0x5360
movq %rax, %rdi
movq 0x411c1(%rip), %rsi     # 0x49f80
callq 0x53c0
movq 0x411f5(%rip), %rdi     # 0x49fc0
leaq 0x362eb(%rip), %rsi     # 0x3f0bd
callq 0x5360
movq %rax, %rdi
leaq 0x379a9(%rip), %rsi     # 0x4078a
callq 0x5360
movq %rax, %rdi
movl $0xa36, %esi            # imm = 0xA36
callq 0x5750
movq %rax, %rdi
movq 0x41183(%rip), %rsi     # 0x49f80
callq 0x53c0
leaq 0x37861(%rip), %rdi     # 0x4066a
leaq 0x362ad(%rip), %rsi     # 0x3f0bd
movl $0xa36, %edx            # imm = 0xA36
leaq 0x364e9(%rip), %rcx     # 0x3f305
callq 0x51e0
jmp 0x8e3a
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x8e35
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x8766
movq -0x78(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
addq $0x80, %rsp
popq %rbp
retq