// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/bam_index.c
// Function: bam_iter_query
bam_iter_t bam_iter_query(const bam_index_t *idx, int tid, int beg, int end)
{
	uint16_t *bins;
	int i, n_bins, n_off;
	pair64_t *off;
	khint_t k;
	khash_t(i) *index;
	uint64_t min_off;
	bam_iter_t iter = 0;

	if (beg < 0) beg = 0;
	if (end < beg) return 0;
	// initialize iter
	iter = calloc(1, sizeof(struct __bam_iter_t));
	iter->tid = tid, iter->beg = beg, iter->end = end; iter->i = -1;
	//
	bins = (uint16_t*)calloc(BAM_MAX_BIN, 2);
	n_bins = reg2bins(beg, end, bins);
	index = idx->index[tid];
	if (idx->index2[tid].n > 0) {
		min_off = (beg>>BAM_LIDX_SHIFT >= idx->index2[tid].n)? idx->index2[tid].offset[idx->index2[tid].n-1]
			: idx->index2[tid].offset[beg>>BAM_LIDX_SHIFT];
		if (min_off == 0) { // improvement for index files built by tabix prior to 0.1.4
			int n = beg>>BAM_LIDX_SHIFT;
			if (n > idx->index2[tid].n) n = idx->index2[tid].n;
			for (i = n - 1; i >= 0; --i)
				if (idx->index2[tid].offset[i] != 0) break;
			if (i >= 0) min_off = idx->index2[tid].offset[i];
		}
	} else min_off = 0; // tabix 0.1.2 may produce such index files
	for (i = n_off = 0; i < n_bins; ++i) {
		if ((k = kh_get(i, index, bins[i])) != kh_end(index))
			n_off += kh_value(index, k).n;
	}
	if (n_off == 0) {
		free(bins); return iter;
	}
	off = (pair64_t*)calloc(n_off, 16);
	for (i = n_off = 0; i < n_bins; ++i) {
		if ((k = kh_get(i, index, bins[i])) != kh_end(index)) {
			int j;
			bam_binlist_t *p = &kh_value(index, k);
			for (j = 0; j < p->n; ++j)
				if (p->list[j].v > min_off) off[n_off++] = p->list[j];
		}
	}
	free(bins);
	if (n_off == 0) {
		free(off); return iter;
	}
	{
		bam1_t *b = (bam1_t*)calloc(1, sizeof(bam1_t));
		int l;
		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
#if defined(BAM_TRUE_OFFSET) || defined(BAM_VIRTUAL_OFFSET16)
			for (i = 1, l = 0; i < n_off; ++i) {
#ifdef BAM_TRUE_OFFSET
				if (off[l].v + BAM_MIN_CHUNK_GAP > off[i].u) off[l].v = off[i].v;
#else
				if (off[l].v>>16 == off[i].u>>16) off[l].v = off[i].v;
#endif
				else off[++l] = off[i];
			}
			n_off = l + 1;
#endif
		}
		bam_destroy1(b);
	}
	iter->n_off = n_off; iter->off = off;
	return iter;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %r14d
xorl %r15d, %r15d
testl %edx, %edx
cmovlel %r15d, %r14d
cmpl %ecx, %r14d
jg 0x29a3db
movl %ecx, %r13d
movl %esi, %r12d
movq %rdi, %rbx
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x31f40
movq %rax, %r15
movl %r12d, 0x4(%rax)
movl %r14d, 0x8(%rax)
movl %r13d, 0xc(%rax)
orl $-0x1, 0x14(%rax)
pushq $0x2
popq %rsi
movl $0x924a, %edi           # imm = 0x924A
callq 0x31f40
cmpl %r13d, %r14d
jae 0x29a104
movl $0x20000000, %r8d       # imm = 0x20000000
cmpl %r8d, %r13d
cmovbl %r13d, %r8d
decl %r8d
movl %r8d, %edx
shrl $0x1a, %edx
leaq 0x2(%rax), %rcx
movl %r14d, %esi
shrl $0x1a, %esi
pushq $0x1
popq %rbp
leal (%rsi,%rbp), %edi
decl %edi
cmpl %edx, %edi
ja 0x29a171
leaq 0x1(%rbp), %rdi
addl %esi, %ebp
movw %bp, (%rcx)
addq $0x2, %rcx
movq %rdi, %rbp
jmp 0x29a0e9
xorl %ebp, %ebp
movslq %r12d, %r9
movq 0x10(%rbx), %rcx
movq 0x18(%rbx), %rsi
movq %r9, %rdi
shlq $0x4, %rdi
movslq (%rsi,%rdi), %rdx
xorl %r12d, %r12d
testq %rdx, %rdx
jle 0x29a20b
addq %rdi, %rsi
shrl $0xe, %r14d
cmpl %edx, %r14d
movq 0x8(%rsi), %rsi
leaq -0x8(%rsi,%rdx,8), %rdi
leaq (%rsi,%r14,8), %r8
cmovaeq %rdi, %r8
movq (%r8), %rbx
testq %rbx, %rbx
jne 0x29a20d
cmpl %edx, %r14d
cmovbl %r14d, %edx
testq %rdx, %rdx
jle 0x29a20b
movq -0x8(%rsi,%rdx,8), %rbx
decq %rdx
testq %rbx, %rbx
je 0x29a156
jmp 0x29a20d
movl %r14d, %edx
shrl $0x17, %edx
addl $0x9, %edx
movl %r8d, %esi
shrl $0x17, %esi
addl $0x9, %esi
cmpl %esi, %edx
ja 0x29a195
incq %rbp
movw %dx, (%rcx)
incl %edx
addq $0x2, %rcx
jmp 0x29a183
movl %r14d, %edx
shrl $0x14, %edx
addl $0x49, %edx
movl %r8d, %esi
shrl $0x14, %esi
addl $0x49, %esi
cmpl %esi, %edx
ja 0x29a1b9
incq %rbp
movw %dx, (%rcx)
incl %edx
addq $0x2, %rcx
jmp 0x29a1a7
movl %r14d, %edx
shrl $0x11, %edx
movl $0x249, %edi            # imm = 0x249
addl %edi, %edx
movl %r8d, %esi
shrl $0x11, %esi
addl %edi, %esi
cmpl %esi, %edx
ja 0x29a1e0
incq %rbp
movw %dx, (%rcx)
incl %edx
addq $0x2, %rcx
jmp 0x29a1ce
movl %r14d, %edx
shrl $0xe, %edx
movl $0x1249, %esi           # imm = 0x1249
addl %esi, %edx
shrl $0xe, %r8d
addl %esi, %r8d
cmpl %r8d, %edx
ja 0x29a106
incq %rbp
movw %dx, (%rcx)
incl %edx
addq $0x2, %rcx
jmp 0x29a1f4
xorl %ebx, %ebx
movq (%rcx,%r9,8), %r13
xorl %r14d, %r14d
movq %rax, 0x8(%rsp)
cmpq %r14, %rbp
je 0x29a24a
movzwl (%rax,%r14,2), %esi
movq %r13, %rdi
callq 0x299fe7
cmpl (%r13), %eax
je 0x29a240
movq 0x20(%r13), %rcx
movl %eax, %eax
shlq $0x4, %rax
addl 0x4(%rcx,%rax), %r12d
incq %r14
movq 0x8(%rsp), %rax
jmp 0x29a219
testl %r12d, %r12d
je 0x29a3c4
movq %r15, 0x10(%rsp)
movslq %r12d, %rdi
pushq $0x10
popq %rsi
callq 0x31f40
movq 0x8(%rsp), %rdi
movq %rax, %r12
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq %rbp, %r15
je 0x29a2e2
movzwl (%rdi,%r15,2), %esi
movq %r13, %rdi
callq 0x299fe7
cmpl (%r13), %eax
jne 0x29a293
movq 0x8(%rsp), %rdi
incq %r15
jmp 0x29a271
movq 0x20(%r13), %rcx
movl %eax, %edx
shlq $0x4, %rdx
leaq (%rcx,%rdx), %rax
movl 0x4(%rcx,%rdx), %ecx
pushq $0x8
popq %rdx
xorl %esi, %esi
movq 0x8(%rsp), %rdi
cmpq %rcx, %rsi
jae 0x29a28e
movq 0x8(%rax), %r8
cmpq %rbx, (%r8,%rdx)
jbe 0x29a2d9
leaq (%r8,%rdx), %r9
addq $-0x8, %r9
movslq %r14d, %r8
incl %r14d
shlq $0x4, %r8
movups (%r9), %xmm0
movups %xmm0, (%r12,%r8)
incq %rsi
addq $0x10, %rdx
jmp 0x29a2af
callq 0x33058
testl %r14d, %r14d
je 0x29a3ce
movslq %r14d, %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x298933
leaq 0x18(%r12), %rcx
xorl %eax, %eax
pushq $0x1
popq %rdi
cmpq %r15, %rdi
jge 0x29a336
movslq %eax, %rdx
shlq $0x4, %rdx
movq 0x8(%r12,%rdx), %rsi
cmpq (%rcx), %rsi
jae 0x29a32d
leaq -0x8(%rcx), %rsi
incl %eax
movups (%rsi), %xmm0
movups %xmm0, 0x10(%rdx,%r12)
incq %rdi
addq $0x10, %rcx
jmp 0x29a308
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movq %rax, %rdx
shlq $0x4, %rdx
movq 0x10(%rsp), %r15
cmpq %rcx, %rdx
je 0x29a365
movq 0x10(%r12,%rcx), %rsi
cmpq %rsi, 0x8(%r12,%rcx)
jb 0x29a35f
movq %rsi, 0x8(%r12,%rcx)
addq $0x10, %rcx
jmp 0x29a349
movl %eax, %eax
shlq $0x4, %rax
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %rax
je 0x29a3b8
movslq %ecx, %rsi
shlq $0x4, %rsi
movq 0x10(%r12,%rdx), %rdi
xorq 0x8(%r12,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0x29a39f
addq %r12, %rsi
addq $0x8, %rsi
movq 0x18(%r12,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x29a3b2
leaq (%r12,%rdx), %rdi
addq $0x10, %rdi
incl %ecx
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r12)
addq $0x10, %rdx
jmp 0x29a36f
incl %ecx
movl %ecx, 0x10(%r15)
movq %r12, 0x28(%r15)
jmp 0x29a3db
movq %rax, %rdi
callq 0x33058
jmp 0x29a3db
movq %r12, %rdi
callq 0x33058
movq 0x10(%rsp), %r15
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq