// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_index.c
// Function: get_chunk_coordinates
pair64_t * get_chunk_coordinates(const bam_index_t *idx, int tid, int beg, int end, int* cnt_off)
{
	uint16_t *bins;
	int i, n_bins, n_off;
	pair64_t *off;
	khint_t k;
	khash_t(i) *index;
	uint64_t min_off;

	bins = (uint16_t*)calloc(MAX_BIN, 2);
	n_bins = reg2bins(beg, end, bins);
	index = idx->index[tid];
	min_off = (beg>>BAM_LIDX_SHIFT >= idx->index2[tid].n)? 0 : idx->index2[tid].offset[beg>>BAM_LIDX_SHIFT];
	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 0;
	}
	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);
	{
		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);
	}
	*cnt_off = n_off;
	return off;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, 0x20(%rsp)
movl %ecx, %r13d
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %r15
movl $0x924a, %edi           # imm = 0x924A
movl $0x2, %esi
callq 0x3530
movq %rax, %r14
decl %r13d
movd %ebp, %xmm0
movd %r13d, %xmm1
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
psrld $0x1a, %xmm0
movd %xmm0, %ecx
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %eax
movl $0x1, %ebx
subl %ecx, %eax
jb 0x16d96
addl $0x2, %eax
leaq 0x6(%rax), %rcx
andq $-0x8, %rcx
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movdqa 0xeebd(%rip), %xmm1     # 0x25950
paddd %xmm0, %xmm1
paddd 0xeec1(%rip), %xmm0     # 0x25960
leaq -0x2(%rax), %rdx
movq %rdx, %xmm2
pshufd $0x44, %xmm2, %xmm2     # xmm2 = xmm2[0,1,0,1]
xorl %edx, %edx
movdqa 0xeec9(%rip), %xmm4     # 0x25980
movdqa 0xe831(%rip), %xmm5     # 0x252f0
movdqa 0xe839(%rip), %xmm6     # 0x25300
movdqa 0xe841(%rip), %xmm7     # 0x25310
pxor %xmm7, %xmm2
pcmpeqd %xmm8, %xmm8
movdqa 0xeeaf(%rip), %xmm9     # 0x25990
movq %rdx, %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, %esi
testb $0x1, %sil
je 0x16b62
movd %xmm11, %esi
movw %si, 0x2(%r14,%rdx,2)
pand %xmm13, %xmm12
por %xmm14, %xmm12
packssdw %xmm12, %xmm12
pxor %xmm8, %xmm12
packssdw %xmm12, %xmm12
movd %xmm12, %esi
shrl $0x10, %esi
testb $0x1, %sil
je 0x16b95
pextrw $0x2, %xmm11, %esi
movw %si, 0x4(%r14,%rdx,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, %esi
testb $0x1, %sil
je 0x16bfb
pextrw $0x4, %xmm11, %esi
movw %si, 0x6(%r14,%rdx,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, %esi
testb $0x1, %sil
je 0x16c39
pextrw $0x6, %xmm11, %esi
movw %si, 0x8(%r14,%rdx,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, %esi
testb $0x1, %sil
je 0x16caf
movd %xmm11, %esi
movw %si, 0xa(%r14,%rdx,2)
pand %xmm13, %xmm12
por %xmm14, %xmm12
packssdw %xmm12, %xmm12
pxor %xmm8, %xmm12
packssdw %xmm12, %xmm3
pextrw $0x5, %xmm3, %esi
testb $0x1, %sil
je 0x16cdf
pextrw $0x2, %xmm11, %esi
movw %si, 0xc(%r14,%rdx,2)
por 0xec88(%rip), %xmm10    # 0x25970
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, %esi
testb $0x1, %sil
je 0x16d41
pextrw $0x4, %xmm11, %esi
movw %si, 0xe(%r14,%rdx,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, %esi
testb $0x1, %sil
je 0x16d7d
pextrw $0x6, %xmm11, %esi
movw %si, 0x10(%r14,%rdx,2)
addq $0x8, %rdx
paddd %xmm9, %xmm0
paddd %xmm9, %xmm1
cmpq %rdx, %rcx
jne 0x16ae1
movl %eax, %ebx
movl %ebp, %eax
shrl $0x17, %eax
movl %r13d, %ecx
shrl $0x17, %ecx
cmpl %ecx, %eax
ja 0x16dc0
addl $0x9, %ecx
movl %ebx, %edx
leaq (%r14,%rdx,2), %rdx
addl $0x8, %eax
incl %eax
movw %ax, (%rdx)
addq $0x2, %rdx
incl %ebx
cmpl %ecx, %eax
jb 0x16db1
movl %ebp, %eax
shrl $0x14, %eax
movl %r13d, %ecx
shrl $0x14, %ecx
cmpl %ecx, %eax
ja 0x16dea
addl $0x49, %ecx
movl %ebx, %edx
leaq (%r14,%rdx,2), %rdx
addl $0x48, %eax
incl %eax
movw %ax, (%rdx)
addq $0x2, %rdx
incl %ebx
cmpl %ecx, %eax
jb 0x16ddb
movl %ebp, %eax
shrl $0x11, %eax
movl %r13d, %ecx
shrl $0x11, %ecx
cmpl %ecx, %eax
ja 0x16e19
addl $0x249, %ecx            # imm = 0x249
movl %ebx, %edx
leaq (%r14,%rdx,2), %rdx
addl $0x248, %eax            # imm = 0x248
incl %eax
movw %ax, (%rdx)
addq $0x2, %rdx
incl %ebx
cmpl %ecx, %eax
jb 0x16e0a
movl %ebp, %eax
shrl $0xe, %eax
shrl $0xe, %r13d
cmpl %r13d, %eax
ja 0x16e49
addl $0x1249, %r13d          # imm = 0x1249
movl %ebx, %ecx
leaq (%r14,%rcx,2), %rcx
addl $0x1248, %eax           # imm = 0x1248
incl %eax
movw %ax, (%rcx)
addq $0x2, %rcx
incl %ebx
cmpl %r13d, %eax
jb 0x16e39
movslq %r12d, %rax
movq 0x10(%r15), %rcx
sarl $0xe, %ebp
movq %rax, %rdx
shlq $0x4, %rdx
cmpl (%rcx,%rdx), %ebp
jge 0x16e6f
addq %rdx, %rcx
movq 0x8(%rcx), %rcx
movslq %ebp, %rdx
movq (%rcx,%rdx,8), %r12
jmp 0x16e72
xorl %r12d, %r12d
testl %ebx, %ebx
jle 0x17069
movq 0x8(%r15), %rcx
movq (%rcx,%rax,8), %r13
movl (%r13), %r8d
leal -0x1(%r8), %eax
movl %eax, 0x14(%rsp)
movl %ebx, %eax
movq %rax, 0x18(%rsp)
xorl %r11d, %r11d
xorl %r15d, %r15d
movl $0x0, 0x4(%rsp)
movq %r14, 0x8(%rsp)
movl $0x0, %eax
testl %r8d, %r8d
je 0x16f28
movzwl (%r14,%r15,2), %esi
movl %esi, %eax
xorl %edx, %edx
divl %r8d
movl %edx, %edi
movl %esi, %eax
xorl %edx, %edx
divl 0x14(%rsp)
incl %edx
movq 0x10(%r13), %rbp
movl %edi, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movl (%rbp,%rcx,4), %r14d
leal (%rax,%rax), %ecx
andl $0x1e, %ecx
movl %r14d, %r9d
shrl %cl, %r9d
testb $0x2, %r9b
jne 0x16f18
testb $0x1, %r9b
jne 0x16eff
movq 0x18(%r13), %r9
movl %eax, %r10d
cmpl %esi, (%r9,%r10,4)
je 0x16f18
addl %edx, %eax
cmpl %r8d, %eax
movl %r8d, %ecx
cmovbl %r11d, %ecx
subl %ecx, %eax
cmpl %edi, %eax
jne 0x16ed0
movq 0x8(%rsp), %r14
jmp 0x16f43
shrl %cl, %r14d
testb $0x3, %r14b
cmovnel %r8d, %eax
movq 0x8(%rsp), %r14
cmpl %r8d, %eax
je 0x16f43
movq 0x20(%r13), %rcx
movl %eax, %eax
shlq $0x4, %rax
movl 0x4(%rsp), %edx
addl 0x4(%rcx,%rax), %edx
movl %edx, 0x4(%rsp)
incq %r15
cmpq 0x18(%rsp), %r15
jne 0x16ea8
movl 0x4(%rsp), %eax
testl %eax, %eax
je 0x17069
movslq %eax, %rdi
movl $0x10, %esi
callq 0x3530
movq %rax, %r15
testl %ebx, %ebx
jle 0x17079
movl (%r13), %r8d
leal -0x1(%r8), %eax
movl %eax, 0x4(%rsp)
xorl %r11d, %r11d
xorl %ebx, %ebx
movl $0x0, %eax
testl %r8d, %r8d
je 0x1700c
movzwl (%r14,%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(%r13), %rbp
movl %edi, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movl (%rbp,%rcx,4), %r9d
leal (%rax,%rax), %ecx
andl $0x1e, %ecx
movl %r9d, %r14d
shrl %cl, %r14d
testb $0x2, %r14b
jne 0x16ffc
testb $0x1, %r14b
jne 0x16fdd
movq 0x18(%r13), %r14
movl %eax, %r10d
cmpl %esi, (%r14,%r10,4)
je 0x16ffc
addl %edx, %eax
cmpl %r8d, %eax
movl %r8d, %ecx
movl $0x0, %r9d
cmovbl %r9d, %ecx
subl %ecx, %eax
cmpl %edi, %eax
jne 0x16fae
movq 0x8(%rsp), %r14
jmp 0x17059
shrl %cl, %r9d
testb $0x3, %r9b
cmovnel %r8d, %eax
movq 0x8(%rsp), %r14
cmpl %r8d, %eax
je 0x17059
movq 0x20(%r13), %rcx
movl %eax, %edx
shlq $0x4, %rdx
movl 0x4(%rcx,%rdx), %eax
testq %rax, %rax
je 0x17059
addq %rdx, %rcx
movq 0x8(%rcx), %rcx
addq $0x8, %rcx
xorl %edx, %edx
cmpq %r12, (%rcx)
jbe 0x1704d
leaq -0x8(%rcx), %rsi
movslq %ebx, %rdi
incl %ebx
shlq $0x4, %rdi
movdqu (%rsi), %xmm0
movdqu %xmm0, (%r15,%rdi)
incq %rdx
addq $0x10, %rcx
cmpq %rax, %rdx
jb 0x17031
incq %r11
cmpq 0x18(%rsp), %r11
jne 0x16f86
jmp 0x1707b
movq %r14, %rdi
callq 0x32c0
xorl %r15d, %r15d
jmp 0x1716d
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x32c0
movslq %ebx, %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x157c4
movl $0x1, %eax
cmpl $0x2, %r14d
jl 0x17166
movl %ebx, %ecx
leaq 0x18(%r15), %rsi
shlq $0x4, %rcx
addq $-0x10, %rcx
xorl %edi, %edi
xorl %edx, %edx
movslq %edx, %r8
shlq $0x4, %r8
movq 0x8(%r15,%r8), %r9
cmpq 0x18(%r15,%rdi), %r9
jae 0x170db
leaq (%rsi,%rdi), %r9
addq $-0x8, %r9
incl %edx
movdqu (%r9), %xmm0
movdqu %xmm0, 0x10(%r8,%r15)
addq $0x10, %rdi
cmpq %rdi, %rcx
jne 0x170b2
testl %edx, %edx
jle 0x17166
movl %edx, %ecx
movq %rcx, %rsi
shlq $0x4, %rsi
xorl %edi, %edi
movq 0x10(%r15,%rdi), %r8
cmpq %r8, 0x8(%r15,%rdi)
jb 0x17104
movq %r8, 0x8(%r15,%rdi)
addq $0x10, %rdi
cmpq %rdi, %rsi
jne 0x170f3
testl %edx, %edx
jle 0x17166
shlq $0x4, %rcx
addq $0x10, %rcx
xorl %eax, %eax
movl $0x10, %edx
movslq %eax, %rsi
shlq $0x4, %rsi
movq (%r15,%rdx), %rdi
xorq 0x8(%r15,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0x1714a
addq %r15, %rsi
addq $0x8, %rsi
movq 0x8(%r15,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x1715b
leaq (%r15,%rdx), %rdi
incl %eax
movdqu (%rdi), %xmm0
movdqu %xmm0, 0x10(%rsi,%r15)
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x17120
incl %eax
movq 0x20(%rsp), %rcx
movl %eax, (%rcx)
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq