// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movl %esi, %r12d
movq %rdi, %r15
testl %esi, %esi
js 0x78ab
xorl %r13d, %r13d
testl %edx, %edx
cmovlel %r13d, %edx
cmpl %ecx, %edx
jg 0x7edf
movq 0x28(%r15), %rax
movl %r12d, %esi
movq (%rax,%rsi,8), %r13
testq %r13, %r13
je 0x7ec3
movl $0x1, %edi
movl $0x28, %esi
movl %edx, %ebx
movl %ecx, %ebp
callq 0x21f0
movl %r12d, 0x4(%rax)
movl %ebx, 0x8(%rax)
movl %ebp, 0x8(%rsp)
movl %ebp, 0xc(%rax)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rax, 0x18(%rsp)
movl %edx, 0x14(%rax)
movl 0x4(%r15), %eax
movl 0x8(%r15), %ecx
movq %rcx, 0x30(%rsp)
leal (%rcx,%rcx,2), %ecx
shll %cl, %edx
notl %edx
movl $0x92492493, %esi       # imm = 0x92492493
imulq %rdx, %rsi
movl %ebx, %r12d
movl %ebx, %r8d
movl %eax, %ecx
shrl %cl, %r8d
shrq $0x22, %rsi
addl %esi, %r8d
movl (%r13), %edx
leal -0x1(%rdx), %esi
testl %edx, %edx
je 0x7810
movl %esi, %r9d
andl %r8d, %r9d
movl %r8d, %ecx
shrl $0x3, %ecx
leal (,%r8,8), %r10d
xorl %ecx, %r10d
orl $0x1, %r10d
andl %esi, %r10d
movq 0x10(%r13), %r11
movl %r9d, %edi
movl %edi, %ecx
shrl $0x4, %ecx
movl (%r11,%rcx,4), %ebx
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
movl %ebx, %ebp
shrl %cl, %ebp
testb $0x2, %bpl
jne 0x7806
testb $0x1, %bpl
jne 0x77f8
movq 0x18(%r13), %r14
movl %edi, %r15d
cmpl %r8d, (%r14,%r15,4)
je 0x7806
addl %r10d, %edi
andl %esi, %edi
cmpl %r9d, %edi
jne 0x77cc
movl %edx, %edi
jmp 0x7812
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %edx, %edi
jmp 0x7812
xorl %edi, %edi
cmpl %edx, %edi
jne 0x7835
leal -0x1(%r8), %ecx
movl %ecx, %r9d
andl $-0x8, %r9d
incl %r9d
movl %ecx, %r10d
sarl $0x3, %r10d
cmpl %r9d, %r8d
cmovgl %ecx, %r10d
movl %r10d, %r8d
cmpl %edx, %edi
jne 0x7842
testl %r8d, %r8d
jne 0x77a3
testl %r8d, %r8d
cmovel %r8d, %edi
testl %edx, %edx
je 0x7a04
testl %r8d, %r8d
jne 0x7a04
movl %esi, %r8d
andl $0x1, %r8d
movq 0x10(%r13), %r9
xorl %edi, %edi
movl %edi, %ecx
shrl $0x4, %ecx
movl (%r9,%rcx,4), %r10d
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
movl %r10d, %r11d
shrl %cl, %r11d
testb $0x2, %r11b
jne 0x79fa
testb $0x1, %r11b
jne 0x789d
movq 0x18(%r13), %r11
movl %edi, %ebx
cmpl $0x0, (%r11,%rbx,4)
je 0x79fa
addl %r8d, %edi
andl %esi, %edi
jne 0x7867
movl %edx, %edi
jmp 0x7a04
cmpl $-0x2, %r12d
je 0x7957
cmpl $-0x3, %r12d
jne 0x79f3
xorl %r13d, %r13d
cmpl $0x0, 0x14(%r15)
jle 0x7edf
movq 0x28(%r15), %rax
movq (%rax), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x7e64
movl 0xc(%r15), %esi
incl %esi
leal -0x1(%rdx), %edi
movl %edi, %r8d
andl %esi, %r8d
movl %esi, %ecx
shrl $0x3, %ecx
leal (,%rsi,8), %r9d
xorl %ecx, %r9d
orl $0x1, %r9d
andl %edi, %r9d
movq 0x10(%rax), %r10
movl %r8d, %r13d
movl %r13d, %ecx
shrl $0x4, %ecx
movl (%r10,%rcx,4), %r11d
leal (,%r13,2), %ecx
andl $0x1e, %ecx
movl %r11d, %ebx
shrl %cl, %ebx
testb $0x2, %bl
jne 0x7e59
testb $0x1, %bl
jne 0x7944
movq 0x18(%rax), %rbx
movl %r13d, %r14d
cmpl %esi, (%rbx,%r14,4)
je 0x7e59
addl %r9d, %r13d
andl %edi, %r13d
cmpl %r8d, %r13d
jne 0x790b
movl %edx, %r13d
jmp 0x7e64
movslq 0x14(%r15), %rax
xorl %r13d, %r13d
testq %rax, %rax
jle 0x7edf
movq 0x28(%r15), %rcx
movq -0x8(%rcx,%rax,8), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x7e86
movl 0xc(%r15), %esi
incl %esi
leal -0x1(%rdx), %edi
movl %edi, %r8d
andl %esi, %r8d
movl %esi, %ecx
shrl $0x3, %ecx
leal (,%rsi,8), %r9d
xorl %ecx, %r9d
orl $0x1, %r9d
andl %edi, %r9d
movq 0x10(%rax), %r10
movl %r8d, %r13d
movl %r13d, %ecx
shrl $0x4, %ecx
movl (%r10,%rcx,4), %r11d
leal (,%r13,2), %ecx
andl $0x1e, %ecx
movl %r11d, %ebx
shrl %cl, %ebx
testb $0x2, %bl
jne 0x7e7b
testb $0x1, %bl
jne 0x79e0
movq 0x18(%rax), %rbx
movl %r13d, %r14d
cmpl %esi, (%rbx,%r14,4)
je 0x7e7b
addl %r9d, %r13d
andl %edi, %r13d
cmpl %r8d, %r13d
jne 0x79a7
movl %edx, %r13d
jmp 0x7e86
xorl %ebx, %ebx
jmp 0x7ea2
shrl %cl, %r10d
testb $0x3, %r10b
cmovnel %edx, %edi
cmpl %edx, %edi
jne 0x7a0c
xorl %ecx, %ecx
jmp 0x7a1b
movq 0x20(%r13), %rcx
movl %edi, %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
movl 0x8(%rsp), %edx
movq %rcx, 0x20(%rsp)
cmpl %edx, %r12d
movl $0x0, %edi
jge 0x7b74
movslq %edx, %rdx
movq 0x30(%rsp), %rsi
addl %esi, %eax
leal (%rax,%rsi,2), %r15d
movl $0x1, %r8d
movl %r15d, %ecx
shlq %cl, %r8
cmpq %rdx, %r8
cmovgeq %rdx, %r8
testl %esi, %esi
js 0x7b72
movl %r12d, %r9d
decq %r8
xorl %edi, %edi
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
xorl %r10d, %r10d
xorl %r11d, %r11d
movq %r8, 0x40(%rsp)
movq %r9, 0x38(%rsp)
movq %r9, %rbx
movl %r15d, %ecx
shrq %cl, %rbx
leal (%r11,%rbx), %edx
movq %r8, %r12
sarq %cl, %r12
leal (%r11,%r12), %ebp
movl %ebp, %eax
subl %edx, %eax
leal (%rax,%rdi), %ecx
incl %ecx
cmpl %r14d, %ecx
movq %rdi, 0x10(%rsp)
jle 0x7b13
addl %edi, %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, %r14d
sarl $0x10, %r14d
orl %eax, %r14d
movslq %r14d, %rax
incl %r14d
leaq 0x4(,%rax,4), %rsi
movq 0x8(%rsp), %rdi
movq %r10, 0x50(%rsp)
movq %r11, 0x48(%rsp)
movl %edx, 0x2c(%rsp)
callq 0x22e0
movl 0x2c(%rsp), %edx
movq 0x48(%rsp), %r11
movq 0x50(%rsp), %r10
movq 0x38(%rsp), %r9
movq 0x40(%rsp), %r8
movq %rax, 0x8(%rsp)
cmpl %edx, %ebp
jge 0x7b1e
movq 0x10(%rsp), %rdi
jmp 0x7b49
movq 0x10(%rsp), %rdi
movslq %edi, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,4), %rax
subl %ebx, %r12d
incl %r12d
movl %edx, %ecx
xorl %edx, %edx
leal (%rcx,%rdx), %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
cmpl %edx, %r12d
jne 0x7b39
addl %edx, %edi
addl $-0x3, %r15d
leal (%r10,%r10,2), %ecx
movl $0x1, %eax
shll %cl, %eax
addl %r11d, %eax
leal 0x1(%r10), %ecx
movl %eax, %r11d
cmpl 0x30(%rsp), %r10d
movl %ecx, %r10d
jne 0x7a7f
jmp 0x7b7d
xorl %edi, %edi
movq $0x0, 0x8(%rsp)
movl %edi, %eax
movq %rax, 0x10(%rsp)
movq %rdi, %rbp
testl %edi, %edi
jle 0x7c42
movl (%r13), %edx
leal -0x1(%rdx), %esi
xorl %edi, %edi
xorl %eax, %eax
movl $0x0, %r8d
testl %edx, %edx
je 0x7c1d
movq 0x8(%rsp), %rcx
movl (%rcx,%rdi,4), %r9d
movl %esi, %r10d
andl %r9d, %r10d
movl %r9d, %ecx
shrl $0x3, %ecx
leal (,%r9,8), %r11d
xorl %ecx, %r11d
orl $0x1, %r11d
andl %esi, %r11d
movq 0x10(%r13), %rbx
movl %r10d, %r8d
movl %r8d, %ecx
shrl $0x4, %ecx
movl (%rbx,%rcx,4), %r14d
leal (%r8,%r8), %ecx
andl $0x1e, %ecx
movl %r14d, %r15d
shrl %cl, %r15d
testb $0x2, %r15b
jne 0x7c12
testb $0x1, %r15b
jne 0x7c02
movq 0x18(%r13), %r15
movl %r8d, %r12d
cmpl %r9d, (%r15,%r12,4)
je 0x7c12
addl %r11d, %r8d
andl %esi, %r8d
cmpl %r10d, %r8d
jne 0x7bd2
movl %edx, %r8d
jmp 0x7c1d
shrl %cl, %r14d
testb $0x3, %r14b
cmovnel %edx, %r8d
cmpl %edx, %r8d
je 0x7c32
movq 0x20(%r13), %rcx
movl %r8d, %r8d
leaq (%r8,%r8,2), %r8
addl 0x4(%rcx,%r8,8), %eax
incq %rdi
cmpq 0x10(%rsp), %rdi
jne 0x7b9a
jmp 0x7c44
xorl %eax, %eax
testl %eax, %eax
je 0x7e4f
movslq %eax, %rdi
movl $0x10, %esi
callq 0x21f0
movq %rax, %r15
testl %ebp, %ebp
jle 0x7d57
xorl %eax, %eax
xorl %r14d, %r14d
movq 0x20(%rsp), %r9
movl (%r13), %edx
movl $0x0, %esi
testl %edx, %edx
je 0x7cf4
movq 0x8(%rsp), %rcx
movl (%rcx,%rax,4), %edi
leal -0x1(%rdx), %r8d
movl %r8d, %r9d
andl %edi, %r9d
movl %edi, %ecx
shrl $0x3, %ecx
leal (,%rdi,8), %r10d
xorl %ecx, %r10d
orl $0x1, %r10d
andl %r8d, %r10d
movq 0x10(%r13), %r11
movl %r9d, %esi
movl %esi, %ecx
shrl $0x4, %ecx
movl (%r11,%rcx,4), %ebx
leal (%rsi,%rsi), %ecx
andl $0x1e, %ecx
movl %ebx, %r12d
shrl %cl, %r12d
testb $0x2, %r12b
jne 0x7ce7
testb $0x1, %r12b
jne 0x7cd8
movq 0x18(%r13), %r12
movl %esi, %ebp
cmpl %edi, (%r12,%rbp,4)
je 0x7ce7
addl %r10d, %esi
andl %r8d, %esi
cmpl %r9d, %esi
jne 0x7cab
movl %edx, %esi
jmp 0x7cef
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %edx, %esi
movq 0x20(%rsp), %r9
cmpl %edx, %esi
je 0x7d47
movq 0x20(%r13), %rcx
movl %esi, %edx
leaq (%rdx,%rdx,2), %rdx
cmpl $0x0, 0x4(%rcx,%rdx,8)
jle 0x7d47
leaq (%rcx,%rdx,8), %rcx
movl $0x8, %edx
xorl %esi, %esi
movq 0x10(%rcx), %rdi
cmpq %r9, (%rdi,%rdx)
jbe 0x7d37
addq %rdx, %rdi
addq $-0x8, %rdi
movslq %r14d, %r8
incl %r14d
shlq $0x4, %r8
movups (%rdi), %xmm0
movups %xmm0, (%r15,%r8)
incq %rsi
movslq 0x4(%rcx), %rdi
addq $0x10, %rdx
cmpq %rdi, %rsi
jl 0x7d14
incq %rax
cmpq 0x10(%rsp), %rax
jne 0x7c6e
jmp 0x7d5a
xorl %r14d, %r14d
testl %r14d, %r14d
je 0x7e47
movslq %r14d, %rdi
movq %r15, %rsi
callq 0x5a8b
xorl %ecx, %ecx
cmpl $0x2, %r14d
movq 0x18(%rsp), %r13
jl 0x7dbe
movl %r14d, %eax
leaq 0x18(%r15), %rdx
shlq $0x4, %rax
addq $-0x10, %rax
xorl %esi, %esi
xorl %ecx, %ecx
movslq %ecx, %rdi
shlq $0x4, %rdi
movq 0x8(%r15,%rdi), %r8
cmpq 0x18(%r15,%rsi), %r8
jae 0x7db5
leaq (%rdx,%rsi), %r8
addq $-0x8, %r8
incl %ecx
movups (%r8), %xmm0
movups %xmm0, 0x10(%rdi,%r15)
addq $0x10, %rsi
cmpq %rsi, %rax
jne 0x7d8e
movl %ecx, %eax
testl %ecx, %ecx
jle 0x7de7
movq %rax, %rdx
shlq $0x4, %rdx
xorl %esi, %esi
movq 0x10(%r15,%rsi), %rdi
cmpq %rdi, 0x8(%r15,%rsi)
jb 0x7dde
movq %rdi, 0x8(%r15,%rsi)
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x7dcd
testl %ecx, %ecx
jle 0x7ec8
shlq $0x4, %rax
addq $0x10, %rax
xorl %ecx, %ecx
movl $0x10, %edx
movslq %ecx, %rsi
shlq $0x4, %rsi
movq (%r15,%rdx), %rdi
xorq 0x8(%r15,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0x7e28
addq %r15, %rsi
addq $0x8, %rsi
movq 0x8(%r15,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x7e37
leaq (%r15,%rdx), %rdi
incl %ecx
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r15)
addq $0x10, %rdx
cmpq %rdx, %rax
jne 0x7dfe
incl %ecx
jmp 0x7ecd
movq %r15, %rdi
callq 0x2050
movq 0x18(%rsp), %r13
jmp 0x7edf
shrl %cl, %r11d
testb $0x3, %r11b
cmovnel %edx, %r13d
cmpl %edx, %r13d
je 0x7ec3
movq 0x20(%rax), %rax
movl %r13d, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rax
jmp 0x7e9f
shrl %cl, %r11d
testb $0x3, %r11b
cmovnel %edx, %r13d
cmpl %edx, %r13d
je 0x7ec3
movq 0x20(%rax), %rax
movl %r13d, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rax
addq $0x8, %rax
movq (%rax), %rbx
cmpq $-0x1, %rbx
je 0x7ec3
movl $0x1, %edi
movl $0x28, %esi
callq 0x21f0
movq %rax, %r13
orb $0x1, (%rax)
movq %rbx, 0x18(%rax)
jmp 0x7edf
xorl %r13d, %r13d
jmp 0x7edf
movl $0x1, %ecx
movl %ecx, 0x10(%r13)
movq %r15, 0x20(%r13)
movq 0x8(%rsp), %rdi
callq 0x2050
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq