// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rsi, 0x10(%rsp)
movq %rdi, %rbp
movq (%r8), %rax
movq (%rdx), %r14
cmpq %r14, %rax
jge 0xfbac
xorq %rax, %r14
movq %r14, (%r8)
xorq (%rdx), %r14
movq %r14, (%rdx)
xorq (%r8), %r14
movq %r14, (%r8)
movq %r14, %rax
movq (%rdx), %r14
movq %rdx, 0x8(%rsp)
movq %r8, 0x18(%rsp)
cmpq %rbx, %r14
jg 0xfd4b
cmpq %rbx, %rax
jle 0xfd4b
movq (%rbp), %r15
leaq (%r15,%r15), %r13
cmpq %rbx, %r15
movq %rbx, %rax
notq %rax
leaq (%rax,%r15,2), %rsi
cmovgq %rbx, %rsi
movq %rbp, %rdi
callq 0xf933
movq %r12, (%rsp)
movl %eax, (%r12)
movq 0x10(%rbp), %rcx
cltq
imulq $0x28, %rax, %rax
movq (%rcx,%rax), %rdx
movslq 0x8(%rcx,%rax), %rax
addq %rdx, %rax
movq %r13, %r12
subq %rax, %r12
cmpq %rbx, %r15
cmovgq %rdx, %r12
subq %rdx, %r13
cmpq %rbx, %r15
cmovgq %rax, %r13
cmpq %r12, %r14
cmovleq %r12, %r14
movq 0x8(%rsp), %r15
movq %r14, (%r15)
movq 0x18(%rsp), %r14
movq (%r14), %rcx
cmpq %r13, %rcx
cmovgeq %r13, %rcx
movq %rcx, (%r14)
movq (%rbp), %rdi
movq (%r15), %rdx
leaq 0x20(%rsp), %rbp
movq 0x10(%rsp), %rsi
movq %rbp, %r8
callq 0xfab6
movq (%r15), %rcx
movq (%rbp), %r15
testq %rax, %rax
je 0xfc90
movq %rax, %r11
movq (%r14), %r9
movq %r9, %rax
subq %rcx, %rax
cmpq %r15, %rax
jne 0xfcfa
cmpq %r15, %rax
jne 0xfcdb
movq %r11, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1c339(%rip), %rax     # 0x2bfd0
movq (%rax), %rdi
movq (%r14), %r9
movq (%rsp), %rax
movl (%rax), %eax
subq $0x30, %rsp
movq %r13, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movq %r15, (%rsp)
andq $0x0, 0x8(%rsp)
leaq 0x13768(%rip), %rsi     # 0x2342e
leaq 0x137b9(%rip), %rdx     # 0x23486
movq %rbx, %r8
xorl %eax, %eax
callq 0x3500
addq $0x30, %rsp
leaq 0x137b2(%rip), %rdi     # 0x23494
leaq 0x13597(%rip), %rsi     # 0x23280
leaq 0x136de(%rip), %rcx     # 0x233ce
movl $0x200, %edx            # imm = 0x200
callq 0x31a0
movq 0x1c2cf(%rip), %rax     # 0x2bfd0
movq (%rax), %rdi
movq (%rsp), %rax
movl (%rax), %r10d
subq $0x8, %rsp
leaq 0x13718(%rip), %rsi     # 0x2342e
leaq 0x13769(%rip), %rdx     # 0x23486
movq %rbx, %r8
xorl %eax, %eax
pushq %r13
pushq %r12
pushq %r10
pushq %r11
pushq %r15
movq %r11, %rbx
callq 0x3500
movq %rbx, %r11
addq $0x30, %rsp
movq (%r14), %rax
movq 0x8(%rsp), %rcx
subq (%rcx), %rax
jmp 0xfc79
leaq 0x13662(%rip), %rdi     # 0x233b4
leaq 0x13527(%rip), %rsi     # 0x23280
leaq 0x1366e(%rip), %rcx     # 0x233ce
movl $0x1f0, %edx            # imm = 0x1F0
callq 0x31a0