// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
movq (%r8), %rcx
movq (%rdx), %rax
cmpq %rax, %rcx
jge 0x11437
xorq %rcx, %rax
movq %rax, (%r8)
xorq (%rdx), %rax
movq %rax, (%rdx)
xorq %rax, (%r8)
movq %rdx, 0x20(%rsp)
movq (%rdx), %r14
cmpq %r15, %r14
jg 0x1156a
cmpq %r15, (%r8)
jle 0x1156a
movq (%rdi), %rbx
leaq (%rbx,%rbx), %r13
cmpq %r15, %rbx
movq %r15, %rax
notq %rax
leaq (%rax,%rbx,2), %rsi
cmovgq %r15, %rsi
movq %rdi, %rbp
movq %r8, 0x8(%rsp)
callq 0x111bc
movq %r12, 0x10(%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, %rbx
cmovgq %rdx, %r12
subq %rdx, %r13
cmpq %r15, %rbx
cmovgq %rax, %r13
cmpq %r12, %r14
cmovleq %r12, %r14
movq 0x20(%rsp), %rbx
movq %r14, (%rbx)
movq 0x8(%rsp), %rax
movq (%rax), %rcx
cmpq %r13, %rcx
cmovgeq %r13, %rcx
movq %rcx, (%rax)
movq (%rbp), %rdi
movq (%rbx), %rdx
movq %rsp, %r8
movq 0x18(%rsp), %rsi
callq 0x11344
movq %rax, %r14
testq %rax, %rax
movq 0x8(%rsp), %rbp
je 0x11523
movq (%rbp), %rax
subq (%rbx), %rax
cmpq (%rsp), %rax
jne 0x11523
testq %r14, %r14
je 0x11589
movq (%rbp), %rax
subq (%rbx), %rax
cmpq (%rsp), %rax
jne 0x11589
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1eaa6(%rip), %rax     # 0x2ffd0
movq (%rax), %rdi
movq (%rbx), %rcx
movq (%rbp), %r9
movq 0x10(%rsp), %rax
movl (%rax), %r10d
subq $0x8, %rsp
leaq 0x16eb7(%rip), %rsi     # 0x283fe
leaq 0x16f08(%rip), %rdx     # 0x28456
movq %r15, %r8
xorl %eax, %eax
pushq %r13
pushq %r12
pushq %r10
pushq %r14
pushq 0x28(%rsp)
callq 0x3500
addq $0x30, %rsp
jmp 0x114fb
leaq 0x16e13(%rip), %rdi     # 0x28384
leaq 0x16cd8(%rip), %rsi     # 0x28250
leaq 0x16e1f(%rip), %rcx     # 0x2839e
movl $0x1f0, %edx            # imm = 0x1F0
callq 0x31a0
leaq 0x16ed4(%rip), %rdi     # 0x28464
leaq 0x16cb9(%rip), %rsi     # 0x28250
leaq 0x16e00(%rip), %rcx     # 0x2839e
movl $0x200, %edx            # imm = 0x200
callq 0x31a0