// File: /workspace/github_c_makefile_star2_O2/DReichLab[P]adna/hts.c
// Function: hts_itr_query
hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, int beg, int end)
{
	int i, n_off, l, bin;
	hts_pair64_t *off;
	khint_t k;
	bidx_t *bidx;
	uint64_t min_off;
	hts_itr_t *iter = 0;
	cand_bins_t bins = {0,0,0};

	if (tid < 0) {
		uint64_t off0 = (uint64_t)-1;
		khint_t k;
		if (tid == HTS_IDX_START) {
			if (idx->n > 0) {
				bidx = idx->bidx[0];
				k = kh_get(bin, bidx, idx->n_bins + 1);
				if (k == kh_end(bidx)) return 0;
				off0 = kh_val(bidx, k).list[0].u;
			} else return 0;
		} else if (tid == HTS_IDX_NOCOOR) {
			if (idx->n > 0) {
				bidx = idx->bidx[idx->n - 1];
				k = kh_get(bin, bidx, idx->n_bins + 1);
				if (k == kh_end(bidx)) return 0;
				off0 = kh_val(bidx, k).list[0].v;
			} else return 0;
		} else off0 = 0;
		if (off0 != (uint64_t)-1) {
			iter = (hts_itr_t*)calloc(1, sizeof(hts_itr_t));
			iter->read_rest = 1;
			iter->curr_off = off0;
			return iter;
		} else return 0;
	}
	if (beg < 0) beg = 0;
	if (end < beg) return 0;
	if ((bidx = idx->bidx[tid]) == 0) return 0;

	iter = (hts_itr_t*)calloc(1, sizeof(hts_itr_t));
	iter->tid = tid, iter->beg = beg, iter->end = end; iter->i = -1;

	// compute min_off
	bin = hts_bin_first(idx->n_lvls) + (beg>>idx->min_shift);
	do {
		int first;
		k = kh_get(bin, bidx, bin);
		if (k != kh_end(bidx)) break;
		first = (hts_bin_parent(bin)<<3) + 1;
		if (bin > first) --bin;
		else bin = hts_bin_parent(bin);
	} while (bin);
	if (bin == 0) k = kh_get(bin, bidx, bin);
	min_off = k != kh_end(bidx)? kh_val(bidx, k).loff : 0;
	// retrieve bins
	reg2bins(beg, end, iter, idx->min_shift, idx->n_lvls, &bins);
	for (i = n_off = 0; i < bins.n; ++i)
		if ((k = kh_get(bin, bidx, bins.a[i])) != kh_end(bidx))
			n_off += kh_value(bidx, k).n;
	if (n_off == 0) return iter;
	off = (hts_pair64_t*)calloc(n_off, 16);
	for (i = n_off = 0; i < bins.n; ++i) {
		if ((k = kh_get(bin, bidx, bins.a[i])) != kh_end(bidx)) {
			int j;
			hts_bin_t *p = &kh_value(bidx, k);
			for (j = 0; j < p->n; ++j)
				if (p->list[j].v > min_off) off[n_off++] = p->list[j];
		}
	}
	if (n_off == 0) {
		free(off); return iter;
	}
	ks_introsort(_off, n_off, off);
	// resolve completely contained adjacent blocks
	for (i = 1, l = 0; i < n_off; ++i)
		if (off[l].v < off[i].v) off[++l] = off[i];
	n_off = l + 1;
	// resolve overlaps between adjacent blocks; this may happen due to the merge in indexing
	for (i = 1; i < n_off; ++i)
		if (off[i-1].v >= off[i].u) off[i-1].v = off[i].u;
	// merge adjacent blocks
	for (i = 1, l = 0; i < n_off; ++i) {
		if (off[l].v>>16 == off[i].u>>16) off[l].v = off[i].v;
		else off[++l] = off[i];
	}
	n_off = l + 1;
	iter->n_off = n_off; iter->off = off;
	free(bins.a);
	return iter;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %ebx
movq %rdi, %r13
testl %esi, %esi
js 0x6cac
movl %ecx, %r12d
movl %edx, %ebp
xorl %r14d, %r14d
testl %edx, %edx
cmovlel %r14d, %ebp
cmpl %ecx, %ebp
jg 0x7051
movq 0x28(%r13), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6d27
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x21f0
movq %rax, %r15
movl %ebx, 0x4(%rax)
movl %ebp, 0x8(%rax)
movl %r12d, 0xc(%rax)
orl $-0x1, 0x14(%rax)
movl 0x4(%r13), %edx
movl 0x8(%r13), %eax
movq %rax, (%rsp)
leal (%rax,%rax,2), %ecx
pushq $-0x1
popq %rax
shll %cl, %eax
notl %eax
pushq $0x7
popq %rsi
movl %ebp, %r13d
movq %rdx, 0x18(%rsp)
movl %edx, %ecx
shrl %cl, %r13d
xorl %edx, %edx
divl %esi
addl %eax, %r13d
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x7063
movl (%rbx), %ebx
cmpl %ebx, %eax
jne 0x6d4e
leal -0x1(%r13), %ecx
movl %ecx, %edx
andl $-0x8, %edx
incl %edx
movl %ecx, %esi
sarl $0x3, %esi
cmpl %edx, %r13d
cmovgl %ecx, %esi
movl %esi, %r13d
movl %eax, %ebx
testl %esi, %esi
jne 0x6c6e
jmp 0x6d53
cmpl $-0x2, %ebx
je 0x6ce7
cmpl $-0x3, %ebx
jne 0x6d2f
cmpl $0x0, 0x14(%r13)
jle 0x6d27
movq 0x28(%r13), %rax
movq (%rax), %rbx
movl 0xc(%r13), %esi
incl %esi
movq %rbx, %rdi
callq 0x7063
cmpl (%rbx), %eax
je 0x6d27
movq 0x20(%rbx), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x10(%rcx,%rax), %rax
jmp 0x6d1e
movslq 0x14(%r13), %rax
testq %rax, %rax
jle 0x6d27
movq 0x28(%r13), %rcx
movq -0x8(%rcx,%rax,8), %rbx
movl 0xc(%r13), %esi
incl %esi
movq %rbx, %rdi
callq 0x7063
cmpl (%rbx), %eax
je 0x6d27
movq 0x20(%rbx), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x10(%rcx,%rax), %rax
addq $0x8, %rax
movq (%rax), %rbx
cmpq $-0x1, %rbx
jne 0x6d31
xorl %r14d, %r14d
jmp 0x7051
xorl %ebx, %ebx
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x21f0
movq %rax, %r14
movl $0x1, (%rax)
movq %rbx, 0x18(%rax)
jmp 0x7051
testl %r13d, %r13d
jne 0x6d5f
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x7063
xorl %r14d, %r14d
cmpl %ebx, %eax
jne 0x6d6a
xorl %ebx, %ebx
jmp 0x6d7e
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x8(%rcx,%rax), %rbx
cmpl %r12d, %ebp
movq %r15, 0x10(%rsp)
jge 0x6e8c
movq (%rsp), %rax
movq 0x18(%rsp), %rcx
addl %eax, %ecx
leal (%rcx,%rax,2), %edx
pushq $0x1
popq %rsi
movl %edx, %ecx
shlq %cl, %rsi
movslq %r12d, %rax
cmpq %rax, %rsi
cmovgeq %rax, %rsi
movl %ebp, %r15d
decq %rsi
xorl %r14d, %r14d
pushq $0x1
popq %r9
xorl %r12d, %r12d
xorl %edi, %edi
xorl %r10d, %r10d
xorl %r11d, %r11d
movq %rsi, 0x28(%rsp)
cmpl (%rsp), %r11d
jg 0x6e8e
movq %r15, %r13
movl %edx, %ecx
shrq %cl, %r13
addl %r10d, %r13d
movq %rsi, %rbp
sarq %cl, %rbp
addl %r10d, %ebp
movl %ebp, %eax
subl %r13d, %eax
leal (%rax,%r14), %ecx
incl %ecx
cmpl %r12d, %ecx
jle 0x6e5a
addl %r14d, %eax
movl %eax, %ecx
sarl %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x8, %eax
orl %ecx, %eax
movl %eax, %r12d
sarl $0x10, %r12d
orl %eax, %r12d
movslq %r12d, %rax
incl %r12d
leaq 0x4(,%rax,4), %rsi
movl %edx, 0x24(%rsp)
movl %r10d, 0x18(%rsp)
movq %r11, 0x30(%rsp)
callq 0x22f0
movq 0x30(%rsp), %r11
movl 0x18(%rsp), %r10d
pushq $0x1
popq %r9
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %edx
movq %rax, %rdi
movslq %r14d, %rax
leaq (%rdi,%rax,4), %rax
cmpl %ebp, %r13d
jg 0x6e75
movl %r13d, (%rax)
incl %r13d
incl %r14d
addq $0x4, %rax
jmp 0x6e61
leal (%r11,%r11,2), %ecx
movl %r9d, %eax
shll %cl, %eax
addl $-0x3, %edx
addl %eax, %r10d
incl %r11d
jmp 0x6dc9
xorl %edi, %edi
xorl %r15d, %r15d
testl %r14d, %r14d
cmovlel %r15d, %r14d
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
movq %rdi, (%rsp)
cmpq %r15, %r14
je 0x6ed2
movl (%rdi,%r15,4), %esi
movq %r12, %rdi
callq 0x7063
cmpl (%r12), %eax
je 0x6ec9
movq 0x20(%r12), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
addl 0x4(%rcx,%rax), %ebp
incq %r15
movq (%rsp), %rdi
jmp 0x6ea3
testl %ebp, %ebp
je 0x704c
movslq %ebp, %rdi
pushq $0x10
popq %rsi
callq 0x21f0
movq (%rsp), %rcx
movq %rax, %r13
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
cmpq %r14, %r15
je 0x6f60
movl (%rcx,%r15,4), %esi
movq %r12, %rdi
callq 0x7063
cmpl (%r12), %eax
jne 0x6f16
incq %r15
movq (%rsp), %rcx
jmp 0x6ef6
movq 0x20(%r12), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rdx
leaq (%rcx,%rdx), %rax
movslq 0x4(%rcx,%rdx), %rcx
pushq $0x8
popq %rdx
xorl %esi, %esi
cmpq %rcx, %rsi
jge 0x6f0d
movq 0x10(%rax), %rdi
cmpq %rbx, (%rdi,%rdx)
jbe 0x6f57
addq %rdx, %rdi
addq $-0x8, %rdi
movslq %ebp, %r8
incl %ebp
shlq $0x4, %r8
movups (%rdi), %xmm0
movups %xmm0, (%r13,%r8)
incq %rsi
addq $0x10, %rdx
jmp 0x6f2f
testl %ebp, %ebp
je 0x7044
movslq %ebp, %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x5506
leaq 0x18(%r13), %rcx
xorl %eax, %eax
pushq $0x1
popq %rdi
cmpq %r15, %rdi
jge 0x6fad
movslq %eax, %rdx
shlq $0x4, %rdx
movq 0x8(%r13,%rdx), %rsi
cmpq (%rcx), %rsi
jae 0x6fa4
leaq -0x8(%rcx), %rsi
incl %eax
movups (%rsi), %xmm0
movups %xmm0, 0x10(%rdx,%r13)
incq %rdi
addq $0x10, %rcx
jmp 0x6f7f
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movq %rax, %rdx
shlq $0x4, %rdx
movq 0x10(%rsp), %r14
cmpq %rcx, %rdx
je 0x6fdc
movq 0x10(%r13,%rcx), %rsi
cmpq %rsi, 0x8(%r13,%rcx)
jb 0x6fd6
movq %rsi, 0x8(%r13,%rcx)
addq $0x10, %rcx
jmp 0x6fc0
movl %eax, %eax
shlq $0x4, %rax
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %rax
je 0x702f
movslq %ecx, %rsi
shlq $0x4, %rsi
movq 0x10(%r13,%rdx), %rdi
xorq 0x8(%r13,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0x7016
addq %r13, %rsi
addq $0x8, %rsi
movq 0x18(%r13,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x7029
leaq (%rdx,%r13), %rdi
addq $0x10, %rdi
incl %ecx
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r13)
addq $0x10, %rdx
jmp 0x6fe6
incl %ecx
movl %ecx, 0x10(%r14)
movq %r13, 0x20(%r14)
movq (%rsp), %rdi
callq 0x2050
jmp 0x7051
movq %r13, %rdi
callq 0x2050
movq 0x10(%rsp), %r14
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq