// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/bntseq.c
// Function: bns_fetch_seq
uint8_t *bns_fetch_seq(const bntseq_t *bns, const uint8_t *pac, int64_t *beg, int64_t mid, int64_t *end, int *rid)
{
	int64_t far_beg, far_end, len;
	int is_rev;
	uint8_t *seq;

	if (*end < *beg) *end ^= *beg, *beg ^= *end, *end ^= *beg; // if end is smaller, swap
	assert(*beg <= mid && mid < *end);
	*rid = bns_pos2rid(bns, bns_depos(bns, mid, &is_rev));
	far_beg = bns->anns[*rid].offset;
	far_end = far_beg + bns->anns[*rid].len;
	if (is_rev) { // flip to the reverse strand
		int64_t tmp = far_beg;
		far_beg = (bns->l_pac<<1) - far_end;
		far_end = (bns->l_pac<<1) - tmp;
	}
	*beg = *beg > far_beg? *beg : far_beg;
	*end = *end < far_end? *end : far_end;
	seq = bns_get_seq(bns->l_pac, pac, *beg, *end, &len);
	if (seq == 0 || *end - *beg != len) {
		fprintf(stderr, "[E::%s] begin=%ld, mid=%ld, end=%ld, len=%ld, seq=%p, rid=%d, far_beg=%ld, far_end=%ld\n",
				__func__, (long)*beg, (long)mid, (long)*end, (long)len, seq, *rid, (long)far_beg, (long)far_end);
	}
	assert(seq && *end - *beg == len); // assertion failure should never happen
	return seq;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r12
movq %rcx, %r15
movq %rsi, 0x8(%rsp)
movq %rdi, %rbp
movq (%r8), %rax
movq (%rdx), %rbx
cmpq %rbx, %rax
jge 0x1172a
xorq %rax, %rbx
movq %rbx, (%r8)
xorq (%rdx), %rbx
movq %rbx, (%rdx)
xorq (%r8), %rbx
movq %rbx, (%r8)
movq %rbx, %rax
movq (%rdx), %rbx
movq %rdx, 0x10(%rsp)
movq %r8, 0x18(%rsp)
cmpq %r15, %rbx
jg 0x118b1
cmpq %r15, %rax
jle 0x118b1
movq (%rbp), %r14
leaq (%r14,%r14), %r13
cmpq %r15, %r14
movq %r15, %rax
notq %rax
leaq (%rax,%r14,2), %rsi
cmovgq %r15, %rsi
movq %rbp, %rdi
callq 0x11494
movq %r12, (%rsp)
movl %eax, (%r12)
movq 0x10(%rbp), %rcx
cltq
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rdx
movslq 0x8(%rcx,%rax,8), %rax
addq %rdx, %rax
movq %r13, %r12
subq %rax, %r12
cmpq %r15, %r14
cmovgq %rdx, %r12
subq %rdx, %r13
cmpq %r15, %r14
cmovgq %rax, %r13
cmpq %r12, %rbx
cmovleq %r12, %rbx
movq 0x10(%rsp), %r14
movq %rbx, (%r14)
movq 0x18(%rsp), %rbx
movq (%rbx), %rcx
cmpq %r13, %rcx
cmovgeq %r13, %rcx
movq %rcx, (%rbx)
movq (%rbp), %rdi
movq (%r14), %rdx
leaq 0x20(%rsp), %rbp
movq 0x8(%rsp), %rsi
movq %rbp, %r8
callq 0x1161f
movq (%r14), %rcx
movq (%rbp), %rbp
testq %rax, %rax
je 0x1180a
movq %rax, %r11
movq (%rbx), %r9
movq %r9, %rax
subq %rcx, %rax
cmpq %rbp, %rax
jne 0x11866
cmpq %rbp, %rax
jne 0x11847
movq %r11, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1e7bf(%rip), %rax     # 0x2ffd0
movq (%rax), %rdi
movq (%rbx), %r9
movq (%rsp), %rax
movl (%rax), %r10d
subq $0x8, %rsp
leaq 0x16c6d(%rip), %rsi     # 0x28496
leaq 0x16cbe(%rip), %rdx     # 0x284ee
movq %r15, %r8
xorl %eax, %eax
pushq %r13
pushq %r12
pushq %r10
pushq $0x0
pushq %rbp
callq 0x3500
addq $0x30, %rsp
leaq 0x16cae(%rip), %rdi     # 0x284fc
leaq 0x16a93(%rip), %rsi     # 0x282e8
leaq 0x16bda(%rip), %rcx     # 0x28436
movl $0x200, %edx            # imm = 0x200
callq 0x31a0
movq 0x1e763(%rip), %rax     # 0x2ffd0
movq (%rax), %rdi
movq (%rsp), %rax
movl (%rax), %r10d
subq $0x8, %rsp
leaq 0x16c14(%rip), %rsi     # 0x28496
leaq 0x16c65(%rip), %rdx     # 0x284ee
movq %r15, %r8
xorl %eax, %eax
pushq %r13
pushq %r12
pushq %r10
pushq %r11
pushq %rbp
movq %r11, %r15
callq 0x3500
movq %r15, %r11
addq $0x30, %rsp
movq (%rbx), %rax
subq (%r14), %rax
jmp 0x117f3
leaq 0x16b64(%rip), %rdi     # 0x2841c
leaq 0x16a29(%rip), %rsi     # 0x282e8
leaq 0x16b70(%rip), %rcx     # 0x28436
movl $0x1f0, %edx            # imm = 0x1F0
callq 0x31a0