// File: /workspace/github_c_makefile_star2_O3/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, %r12d
xorl %ebp, %ebp
testl %edx, %edx
cmovlel %ebp, %r12d
cmpl %ecx, %r12d
jle 0x10aa9
xorl %eax, %eax
jmp 0x11263
movl %ecx, %r13d
movl %esi, %r15d
movq %rdi, %r14
movl $0x1, %edi
movl $0x30, %esi
callq 0x32e0
movl %r15d, 0x4(%rax)
movl %r12d, 0x8(%rax)
movl %r13d, 0xc(%rax)
movq %rax, 0x18(%rsp)
movl $0xffffffff, 0x14(%rax) # imm = 0xFFFFFFFF
movl $0x924a, %edi           # imm = 0x924A
movl $0x2, %esi
callq 0x32e0
movq %rax, %rbx
cmpl %r13d, %r12d
jae 0x10ef2
movl $0x20000000, %eax       # imm = 0x20000000
cmpl %eax, %r13d
cmovbl %r13d, %eax
decl %eax
movd %eax, %xmm1
movd %r12d, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
psrld $0x1a, %xmm0
movd %xmm0, %edx
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %ecx
movl $0x1, %ebp
subl %edx, %ecx
jb 0x10e41
addl $0x2, %ecx
leaq 0x6(%rcx), %rdx
andq $-0x8, %rdx
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movdqa 0xb67a(%rip), %xmm1     # 0x1c1c0
paddd %xmm0, %xmm1
paddd 0xb67e(%rip), %xmm0     # 0x1c1d0
leaq -0x2(%rcx), %rsi
movq %rsi, %xmm2
pshufd $0x44, %xmm2, %xmm2     # xmm2 = xmm2[0,1,0,1]
xorl %esi, %esi
movdqa 0xb686(%rip), %xmm4     # 0x1c1f0
movdqa 0x962e(%rip), %xmm5     # 0x1a1a0
movdqa 0x9636(%rip), %xmm6     # 0x1a1b0
movdqa 0x963e(%rip), %xmm7     # 0x1a1c0
pxor %xmm7, %xmm2
pcmpeqd %xmm8, %xmm8
movdqa 0xb66c(%rip), %xmm9     # 0x1c200
movq %rsi, %xmm10
pshufd $0x44, %xmm10, %xmm10   # xmm10 = xmm10[0,1,0,1]
movdqa %xmm10, %xmm11
por %xmm6, %xmm11
pxor %xmm7, %xmm11
movdqa %xmm11, %xmm14
pcmpgtd %xmm2, %xmm14
pshufd $0xa0, %xmm14, %xmm13   # xmm13 = xmm14[0,0,2,2]
pshuflw $0xe8, %xmm13, %xmm15   # xmm15 = xmm13[0,2,2,3,4,5,6,7]
pcmpeqd %xmm2, %xmm11
pshufd $0xf5, %xmm11, %xmm12   # xmm12 = xmm11[1,1,3,3]
pshuflw $0xe8, %xmm12, %xmm11   # xmm11 = xmm12[0,2,2,3,4,5,6,7]
pand %xmm15, %xmm11
pshufd $0xf5, %xmm14, %xmm14   # xmm14 = xmm14[1,1,3,3]
pshuflw $0xe8, %xmm14, %xmm15   # xmm15 = xmm14[0,2,2,3,4,5,6,7]
por %xmm11, %xmm15
pxor %xmm8, %xmm15
packssdw %xmm15, %xmm15
movdqa %xmm0, %xmm11
psubd %xmm8, %xmm11
movd %xmm15, %edi
testb $0x1, %dil
je 0x10c14
movd %xmm11, %edi
movw %di, 0x2(%rbx,%rsi,2)
pand %xmm13, %xmm12
por %xmm14, %xmm12
packssdw %xmm12, %xmm12
pxor %xmm8, %xmm12
packssdw %xmm12, %xmm12
movd %xmm12, %edi
shrl $0x10, %edi
testb $0x1, %dil
je 0x10c46
pextrw $0x2, %xmm11, %edi
movw %di, 0x4(%rbx,%rsi,2)
movdqa %xmm10, %xmm13
por %xmm5, %xmm13
pxor %xmm7, %xmm13
movdqa %xmm13, %xmm14
pcmpgtd %xmm2, %xmm14
pshufd $0xa0, %xmm14, %xmm12   # xmm12 = xmm14[0,0,2,2]
pcmpeqd %xmm2, %xmm13
pshufd $0xf5, %xmm13, %xmm13   # xmm13 = xmm13[1,1,3,3]
movdqa %xmm13, %xmm15
pand %xmm12, %xmm15
pshufd $0xf5, %xmm14, %xmm14   # xmm14 = xmm14[1,1,3,3]
por %xmm14, %xmm15
packssdw %xmm15, %xmm15
pxor %xmm8, %xmm15
packssdw %xmm15, %xmm15
pextrw $0x2, %xmm15, %edi
testb $0x1, %dil
je 0x10cab
pextrw $0x4, %xmm11, %edi
movw %di, 0x6(%rbx,%rsi,2)
pshufhw $0x84, %xmm12, %xmm12   # xmm12 = xmm12[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm13, %xmm13   # xmm13 = xmm13[0,1,2,3,4,5,4,6]
pand %xmm12, %xmm13
pshufhw $0x84, %xmm14, %xmm12   # xmm12 = xmm14[0,1,2,3,4,5,4,6]
por %xmm13, %xmm12
pxor %xmm8, %xmm12
packssdw %xmm12, %xmm12
pextrw $0x3, %xmm12, %edi
testb $0x1, %dil
je 0x10ce8
pextrw $0x6, %xmm11, %edi
movw %di, 0x8(%rbx,%rsi,2)
movdqa %xmm10, %xmm12
por %xmm4, %xmm12
movdqa %xmm1, %xmm11
psubd %xmm8, %xmm11
pxor %xmm7, %xmm12
movdqa %xmm12, %xmm14
pcmpgtd %xmm2, %xmm14
pshufd $0xa0, %xmm14, %xmm13   # xmm13 = xmm14[0,0,2,2]
pshuflw $0xe8, %xmm13, %xmm15   # xmm15 = xmm13[0,2,2,3,4,5,6,7]
pcmpeqd %xmm2, %xmm12
pshufd $0xf5, %xmm12, %xmm12   # xmm12 = xmm12[1,1,3,3]
pshuflw $0xe8, %xmm12, %xmm3    # xmm3 = xmm12[0,2,2,3,4,5,6,7]
pand %xmm15, %xmm3
pshufd $0xf5, %xmm14, %xmm14   # xmm14 = xmm14[1,1,3,3]
pshuflw $0xe8, %xmm14, %xmm15   # xmm15 = xmm14[0,2,2,3,4,5,6,7]
por %xmm3, %xmm15
pxor %xmm8, %xmm15
packssdw %xmm15, %xmm3
pextrw $0x4, %xmm3, %edi
testb $0x1, %dil
je 0x10d5d
movd %xmm11, %edi
movw %di, 0xa(%rbx,%rsi,2)
pand %xmm13, %xmm12
por %xmm14, %xmm12
packssdw %xmm12, %xmm12
pxor %xmm8, %xmm12
packssdw %xmm12, %xmm3
pextrw $0x5, %xmm3, %edi
testb $0x1, %dil
je 0x10d8c
pextrw $0x2, %xmm11, %edi
movw %di, 0xc(%rbx,%rsi,2)
por 0xb44b(%rip), %xmm10    # 0x1c1e0
pxor %xmm7, %xmm10
movdqa %xmm10, %xmm3
pcmpgtd %xmm2, %xmm3
pshufd $0xa0, %xmm3, %xmm12    # xmm12 = xmm3[0,0,2,2]
pcmpeqd %xmm2, %xmm10
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
movdqa %xmm10, %xmm14
pand %xmm12, %xmm14
pshufd $0xf5, %xmm3, %xmm13    # xmm13 = xmm3[1,1,3,3]
por %xmm13, %xmm14
packssdw %xmm14, %xmm3
pxor %xmm8, %xmm3
packssdw %xmm3, %xmm3
pextrw $0x6, %xmm3, %edi
testb $0x1, %dil
je 0x10ded
pextrw $0x4, %xmm11, %edi
movw %di, 0xe(%rbx,%rsi,2)
pshufhw $0x84, %xmm12, %xmm3    # xmm3 = xmm12[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm10, %xmm10   # xmm10 = xmm10[0,1,2,3,4,5,4,6]
pand %xmm3, %xmm10
pshufhw $0x84, %xmm13, %xmm3    # xmm3 = xmm13[0,1,2,3,4,5,4,6]
por %xmm10, %xmm3
pxor %xmm8, %xmm3
packssdw %xmm3, %xmm3
pextrw $0x7, %xmm3, %edi
testb $0x1, %dil
je 0x10e28
pextrw $0x6, %xmm11, %edi
movw %di, 0x10(%rbx,%rsi,2)
addq $0x8, %rsi
paddd %xmm9, %xmm0
paddd %xmm9, %xmm1
cmpq %rsi, %rdx
jne 0x10b94
movl %ecx, %ebp
movl %r12d, %ecx
shrl $0x17, %ecx
movl %eax, %edx
shrl $0x17, %edx
cmpl %edx, %ecx
ja 0x10e6b
addl $0x9, %edx
movl %ebp, %esi
leaq (%rbx,%rsi,2), %rsi
addl $0x8, %ecx
incl %ecx
movw %cx, (%rsi)
addq $0x2, %rsi
incl %ebp
cmpl %edx, %ecx
jb 0x10e5c
movl %r12d, %ecx
shrl $0x14, %ecx
movl %eax, %edx
shrl $0x14, %edx
cmpl %edx, %ecx
ja 0x10e95
addl $0x49, %edx
movl %ebp, %esi
leaq (%rbx,%rsi,2), %rsi
addl $0x48, %ecx
incl %ecx
movw %cx, (%rsi)
addq $0x2, %rsi
incl %ebp
cmpl %edx, %ecx
jb 0x10e86
movl %r12d, %ecx
shrl $0x11, %ecx
movl %eax, %edx
shrl $0x11, %edx
cmpl %edx, %ecx
ja 0x10ec5
addl $0x249, %edx            # imm = 0x249
movl %ebp, %esi
leaq (%rbx,%rsi,2), %rsi
addl $0x248, %ecx            # imm = 0x248
incl %ecx
movw %cx, (%rsi)
addq $0x2, %rsi
incl %ebp
cmpl %edx, %ecx
jb 0x10eb6
movl %r12d, %ecx
shrl $0xe, %ecx
shrl $0xe, %eax
cmpl %eax, %ecx
ja 0x10ef2
addl $0x1249, %eax           # imm = 0x1249
movl %ebp, %edx
leaq (%rbx,%rdx,2), %rdx
addl $0x1248, %ecx           # imm = 0x1248
incl %ecx
movw %cx, (%rdx)
addq $0x2, %rdx
incl %ebp
cmpl %eax, %ecx
jb 0x10ee3
movslq %r15d, %rax
movq 0x18(%r14), %rdx
movq %rax, %rsi
shlq $0x4, %rsi
movslq (%rdx,%rsi), %rcx
testq %rcx, %rcx
jle 0x10f47
addq %rsi, %rdx
shrl $0xe, %r12d
cmpl %ecx, %r12d
movq 0x8(%rdx), %rdx
leaq -0x8(%rdx,%rcx,8), %rsi
leaq (%rdx,%r12,8), %rdi
cmovaeq %rsi, %rdi
movq (%rdi), %r13
testq %r13, %r13
jne 0x10f4a
cmpl %ecx, %r12d
cmovbl %r12d, %ecx
testq %rcx, %rcx
jle 0x10f47
movq -0x8(%rdx,%rcx,8), %r13
decq %rcx
testq %r13, %r13
je 0x10f33
jmp 0x10f4a
xorl %r13d, %r13d
testl %ebp, %ebp
jle 0x11249
movq 0x10(%r14), %rcx
movq (%rcx,%rax,8), %r12
movl (%r12), %r8d
leal -0x1(%r8), %eax
movl %eax, 0x4(%rsp)
movl %ebp, %eax
movq %rax, 0x20(%rsp)
xorl %r11d, %r11d
xorl %r15d, %r15d
movl $0x0, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movl $0x0, %eax
testl %r8d, %r8d
je 0x10ffe
movzwl (%rbx,%r15,2), %esi
movl %esi, %eax
xorl %edx, %edx
divl %r8d
movl %edx, %edi
movl %esi, %eax
xorl %edx, %edx
divl 0x4(%rsp)
incl %edx
movq 0x10(%r12), %r14
movl %edi, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movl (%r14,%rcx,4), %r9d
leal (%rax,%rax), %ecx
andl $0x1e, %ecx
movl %r9d, %ebx
shrl %cl, %ebx
testb $0x2, %bl
jne 0x10fee
testb $0x1, %bl
jne 0x10fd5
movq 0x18(%r12), %rbx
movl %eax, %r10d
cmpl %esi, (%rbx,%r10,4)
je 0x10fee
addl %edx, %eax
cmpl %r8d, %eax
movl %r8d, %ecx
cmovbl %r11d, %ecx
subl %ecx, %eax
cmpl %edi, %eax
jne 0x10fa9
movq 0x10(%rsp), %rbx
jmp 0x1101a
shrl %cl, %r9d
testb $0x3, %r9b
cmovnel %r8d, %eax
movq 0x10(%rsp), %rbx
cmpl %r8d, %eax
je 0x1101a
movq 0x20(%r12), %rcx
movl %eax, %eax
shlq $0x4, %rax
movl 0x8(%rsp), %edx
addl 0x4(%rcx,%rax), %edx
movl %edx, 0x8(%rsp)
incq %r15
cmpq 0x20(%rsp), %r15
jne 0x10f80
movl 0x8(%rsp), %eax
testl %eax, %eax
je 0x11249
movslq %eax, %rdi
movl $0x10, %esi
callq 0x32e0
movq %rax, %r14
testl %ebp, %ebp
jle 0x1124e
movl (%r12), %r8d
leal -0x1(%r8), %eax
movl %eax, 0x4(%rsp)
xorl %r10d, %r10d
xorl %r11d, %r11d
xorl %ebp, %ebp
movq %r14, 0x8(%rsp)
movl $0x0, %eax
testl %r8d, %r8d
je 0x110ed
movzwl (%rbx,%r11,2), %esi
movl %esi, %eax
xorl %edx, %edx
divl %r8d
movl %edx, %edi
movl %esi, %eax
xorl %edx, %edx
divl 0x4(%rsp)
incl %edx
movq 0x10(%r12), %r14
movl %edi, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movl (%r14,%rcx,4), %r9d
leal (%rax,%rax), %ecx
andl $0x1e, %ecx
movl %r9d, %ebx
shrl %cl, %ebx
testb $0x2, %bl
jne 0x110d8
testb $0x1, %bl
jne 0x110ba
movq 0x18(%r12), %rbx
movl %eax, %r15d
cmpl %esi, (%rbx,%r15,4)
je 0x110d8
addl %edx, %eax
cmpl %r8d, %eax
movl %r8d, %ecx
cmovbl %r10d, %ecx
subl %ecx, %eax
cmpl %edi, %eax
jne 0x1108e
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
jmp 0x1113b
shrl %cl, %r9d
testb $0x3, %r9b
cmovnel %r8d, %eax
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
cmpl %r8d, %eax
je 0x1113b
movq 0x20(%r12), %rcx
movl %eax, %edx
shlq $0x4, %rdx
movl 0x4(%rcx,%rdx), %eax
testq %rax, %rax
je 0x1113b
addq %rdx, %rcx
movq 0x8(%rcx), %rcx
addq $0x8, %rcx
xorl %edx, %edx
cmpq %r13, (%rcx)
jbe 0x1112f
leaq -0x8(%rcx), %rsi
movslq %ebp, %rdi
incl %ebp
shlq $0x4, %rdi
movdqu (%rsi), %xmm0
movdqu %xmm0, (%r14,%rdi)
incq %rdx
addq $0x10, %rcx
cmpq %rax, %rdx
jb 0x11113
incq %r11
cmpq 0x20(%rsp), %r11
jne 0x11065
movq %rbx, %rdi
callq 0x3060
testl %ebp, %ebp
je 0x11256
movslq %ebp, %rdi
movq %r14, %rsi
callq 0xf05c
movl $0x1, %eax
cmpl $0x2, %ebp
jl 0x11238
movl %ebp, %ecx
leaq 0x18(%r14), %rsi
shlq $0x4, %rcx
addq $-0x10, %rcx
xorl %edi, %edi
xorl %edx, %edx
movslq %edx, %r8
shlq $0x4, %r8
movq 0x8(%r14,%r8), %r9
cmpq 0x18(%r14,%rdi), %r9
jae 0x111ad
leaq (%rsi,%rdi), %r9
addq $-0x8, %r9
incl %edx
movdqu (%r9), %xmm0
movdqu %xmm0, 0x10(%r8,%r14)
addq $0x10, %rdi
cmpq %rdi, %rcx
jne 0x11184
testl %edx, %edx
jle 0x11238
movl %edx, %ecx
movq %rcx, %rsi
shlq $0x4, %rsi
xorl %edi, %edi
movq 0x10(%r14,%rdi), %r8
cmpq %r8, 0x8(%r14,%rdi)
jb 0x111d6
movq %r8, 0x8(%r14,%rdi)
addq $0x10, %rdi
cmpq %rdi, %rsi
jne 0x111c5
testl %edx, %edx
jle 0x11238
shlq $0x4, %rcx
addq $0x10, %rcx
xorl %eax, %eax
movl $0x10, %edx
movslq %eax, %rsi
shlq $0x4, %rsi
movq (%r14,%rdx), %rdi
xorq 0x8(%r14,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0x1121c
addq %r14, %rsi
addq $0x8, %rsi
movq 0x8(%r14,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x1122d
leaq (%r14,%rdx), %rdi
incl %eax
movdqu (%rdi), %xmm0
movdqu %xmm0, 0x10(%rsi,%r14)
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x111f2
incl %eax
movq 0x18(%rsp), %rcx
movl %eax, 0x10(%rcx)
movq %rcx, %rax
movq %r14, 0x28(%rcx)
jmp 0x11263
movq %rbx, %rdi
jmp 0x11259
movq %rbx, %rdi
callq 0x3060
movq %r14, %rdi
callq 0x3060
movq 0x18(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq