// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/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 $0x28, %rsp
movl %edx, %r15d
xorl %ebx, %ebx
testl %edx, %edx
cmovlel %ebx, %r15d
cmpl %ecx, %r15d
jle 0x106f8
xorl %eax, %eax
jmp 0x10bc6
movl %ecx, %r13d
movl %esi, %ebp
movq %rdi, %r12
movl $0x1, %edi
movl $0x30, %esi
callq 0x32e0
movl %ebp, 0x4(%rax)
movl %r15d, 0x8(%rax)
movl %r13d, 0xc(%rax)
movq %rax, 0x10(%rsp)
movl $0xffffffff, 0x14(%rax) # imm = 0xFFFFFFFF
movl $0x924a, %edi           # imm = 0x924A
movl $0x2, %esi
callq 0x32e0
movq %rax, %r14
cmpl %r13d, %r15d
jae 0x10832
movl $0x20000000, %eax       # imm = 0x20000000
cmpl %eax, %r13d
cmovbl %r13d, %eax
decl %eax
movw $0x0, (%r14)
movl %r15d, %ecx
shrl $0x1a, %ecx
movl %eax, %edx
shrl $0x1a, %edx
movl $0x1, %ebx
subl %ecx, %edx
jb 0x10781
addl $0x2, %edx
movl $0x1, %ebx
leal (%rcx,%rbx), %esi
movw %si, (%r14,%rbx,2)
incq %rbx
cmpq %rbx, %rdx
jne 0x10771
movl %r15d, %ecx
shrl $0x17, %ecx
movl %eax, %edx
shrl $0x17, %edx
cmpl %edx, %ecx
ja 0x107ab
addl $0x9, %edx
movl %ebx, %esi
leaq (%r14,%rsi,2), %rsi
addl $0x8, %ecx
incl %ecx
movw %cx, (%rsi)
addq $0x2, %rsi
incl %ebx
cmpl %edx, %ecx
jb 0x1079c
movl %r15d, %ecx
shrl $0x14, %ecx
movl %eax, %edx
shrl $0x14, %edx
cmpl %edx, %ecx
ja 0x107d5
addl $0x49, %edx
movl %ebx, %esi
leaq (%r14,%rsi,2), %rsi
addl $0x48, %ecx
incl %ecx
movw %cx, (%rsi)
addq $0x2, %rsi
incl %ebx
cmpl %edx, %ecx
jb 0x107c6
movl %r15d, %ecx
shrl $0x11, %ecx
movl %eax, %edx
shrl $0x11, %edx
cmpl %edx, %ecx
ja 0x10805
addl $0x249, %edx            # imm = 0x249
movl %ebx, %esi
leaq (%r14,%rsi,2), %rsi
addl $0x248, %ecx            # imm = 0x248
incl %ecx
movw %cx, (%rsi)
addq $0x2, %rsi
incl %ebx
cmpl %edx, %ecx
jb 0x107f6
movl %r15d, %ecx
shrl $0xe, %ecx
shrl $0xe, %eax
cmpl %eax, %ecx
ja 0x10832
addl $0x1249, %eax           # imm = 0x1249
movl %ebx, %edx
leaq (%r14,%rdx,2), %rdx
addl $0x1248, %ecx           # imm = 0x1248
incl %ecx
movw %cx, (%rdx)
addq $0x2, %rdx
incl %ebx
cmpl %eax, %ecx
jb 0x10823
movslq %ebp, %rax
movq 0x10(%r12), %rcx
movq 0x18(%r12), %rsi
movq %rax, %rdi
shlq $0x4, %rdi
movslq (%rsi,%rdi), %rdx
movq $0x0, 0x8(%rsp)
testq %rdx, %rdx
jle 0x108a0
leaq (%rsi,%rdi), %r9
shrl $0xe, %r15d
cmpl %edx, %r15d
movq 0x8(%r9), %r9
leaq -0x8(%r9,%rdx,8), %r10
leaq (%r9,%r15,8), %r9
cmovgeq %r10, %r9
movq (%r9), %r12
testq %r12, %r12
jne 0x108a3
cmpl %edx, %r15d
cmovll %r15d, %edx
addq %rdi, %rsi
addq $0x8, %rsi
testl %edx, %edx
jle 0x108a0
movq (%rsi), %rdi
movq -0x8(%rdi,%rdx,8), %r12
decq %rdx
testq %r12, %r12
je 0x1088a
jmp 0x108a3
xorl %r12d, %r12d
movq (%rcx,%rax,8), %r13
movl %ebx, %eax
movq %rax, 0x20(%rsp)
testl %ebx, %ebx
movq %r14, (%rsp)
jle 0x1097b
movl (%r13), %r9d
leal -0x1(%r9), %eax
movl %eax, 0x1c(%rsp)
xorl %r11d, %r11d
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
movl $0x0, %eax
testl %r9d, %r9d
je 0x10950
movzwl (%r14,%r15,2), %esi
movl %esi, %eax
xorl %edx, %edx
divl %r9d
movl %edx, %edi
movl %esi, %eax
xorl %edx, %edx
divl 0x1c(%rsp)
incl %edx
movq 0x10(%r13), %r14
movl %edi, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movl (%r14,%rcx,4), %ebp
leal (%rax,%rax), %ecx
andl $0x1e, %ecx
movl %ebp, %r10d
shrl %cl, %r10d
testb $0x2, %r10b
jne 0x10942
testb $0x1, %r10b
jne 0x1092b
movq 0x18(%r13), %r10
movl %eax, %r8d
cmpl %esi, (%r10,%r8,4)
je 0x10942
addl %edx, %eax
cmpl %r9d, %eax
movl %r9d, %ecx
cmovbl %r11d, %ecx
subl %ecx, %eax
cmpl %edi, %eax
jne 0x108fd
movl %r9d, %eax
jmp 0x1094c
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %r9d, %eax
movq (%rsp), %r14
cmpl %r9d, %eax
je 0x1096d
movq 0x20(%r13), %rcx
movl %eax, %eax
shlq $0x4, %rax
movq 0x8(%rsp), %rdx
addl 0x4(%rcx,%rax), %edx
movq %rdx, 0x8(%rsp)
incq %r15
cmpq 0x20(%rsp), %r15
jne 0x108d5
movq 0x8(%rsp), %rax
testl %eax, %eax
je 0x10a8e
movslq %eax, %rdi
movl $0x10, %esi
callq 0x32e0
movq %rax, %r15
testl %ebx, %ebx
jle 0x10a96
xorl %r9d, %r9d
xorl %ebx, %ebx
movl (%r13), %r10d
movl $0x0, %eax
testl %r10d, %r10d
je 0x10a2d
movzwl (%r14,%r9,2), %esi
movl %esi, %eax
xorl %edx, %edx
divl %r10d
movl %edx, %edi
leal -0x1(%r10), %ecx
movl %esi, %eax
xorl %edx, %edx
divl %ecx
incl %edx
movq 0x10(%r13), %r11
movl %edi, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movl (%r11,%rcx,4), %r14d
leal (%rax,%rax), %ecx
andl $0x1e, %ecx
movl %r14d, %ebp
shrl %cl, %ebp
testb $0x2, %bpl
jne 0x10a1e
testb $0x1, %bpl
jne 0x10a01
movq 0x18(%r13), %rbp
movl %eax, %r8d
cmpl %esi, (%rbp,%r8,4)
je 0x10a1e
addl %edx, %eax
cmpl %r10d, %eax
movl %r10d, %ecx
movl $0x0, %r8d
cmovbl %r8d, %ecx
subl %ecx, %eax
cmpl %edi, %eax
jne 0x109d3
movl %r10d, %eax
jmp 0x10a29
shrl %cl, %r14d
testb $0x3, %r14b
cmovnel %r10d, %eax
movq (%rsp), %r14
cmpl %r10d, %eax
je 0x10a7e
movq 0x20(%r13), %rcx
movl %eax, %eax
shlq $0x4, %rax
cmpl $0x0, 0x4(%rcx,%rax)
je 0x10a7e
addq %rax, %rcx
movl $0x8, %eax
xorl %edx, %edx
movq 0x8(%rcx), %rsi
cmpq %r12, (%rsi,%rax)
jbe 0x10a6f
addq %rax, %rsi
addq $-0x8, %rsi
movslq %ebx, %rdi
incl %ebx
shlq $0x4, %rdi
movups (%rsi), %xmm0
movups %xmm0, (%r15,%rdi)
incq %rdx
movl 0x4(%rcx), %esi
addq $0x10, %rax
cmpq %rsi, %rdx
jb 0x10a4d
incq %r9
cmpq 0x20(%rsp), %r9
jne 0x109a5
jmp 0x10a98
movq %r14, %rdi
jmp 0x10b93
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x3060
testl %ebx, %ebx
je 0x10b90
movl $0x1, %edi
movl $0x38, %esi
callq 0x32e0
movq %rax, %r14
movslq %ebx, %rdi
movq %r15, %rsi
callq 0xed1d
xorl %ecx, %ecx
cmpl $0x2, %ebx
jl 0x10b0e
movl %ebx, %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 0x10b05
leaq (%rdx,%rsi), %r8
addq $-0x8, %r8
incl %ecx
movups (%r8), %xmm0
movups %xmm0, 0x10(%rdi,%r15)
addq $0x10, %rsi
cmpq %rsi, %rax
jne 0x10ade
movl %ecx, %eax
testl %ecx, %ecx
jle 0x10b37
movq %rax, %rdx
shlq $0x4, %rdx
xorl %esi, %esi
movq 0x10(%r15,%rsi), %rdi
cmpq %rdi, 0x8(%r15,%rsi)
jb 0x10b2e
movq %rdi, 0x8(%r15,%rsi)
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x10b1d
testl %ecx, %ecx
jle 0x10b9f
shlq $0x4, %rax
addq $0x10, %rax
xorl %ebx, %ebx
movl $0x10, %ecx
movslq %ebx, %rdx
shlq $0x4, %rdx
movq (%r15,%rcx), %rsi
xorq 0x8(%r15,%rdx), %rsi
cmpq $0xffff, %rsi           # imm = 0xFFFF
ja 0x10b74
addq %r15, %rdx
addq $0x8, %rdx
movq 0x8(%r15,%rcx), %rsi
movq %rsi, (%rdx)
jmp 0x10b83
leaq (%r15,%rcx), %rsi
incl %ebx
movups (%rsi), %xmm0
movups %xmm0, 0x10(%rdx,%r15)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x10b4a
incl %ebx
jmp 0x10ba4
movq %r15, %rdi
callq 0x3060
movq 0x10(%rsp), %rax
jmp 0x10bc6
movl $0x1, %ebx
testq %r14, %r14
je 0x10bba
movq 0x30(%r14), %rdi
callq 0x3060
movq %r14, %rdi
callq 0x3060
movq 0x10(%rsp), %rax
movl %ebx, 0x10(%rax)
movq %r15, 0x28(%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq