// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movl %ecx, %r13d
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %r15
movl $0x924a, %edi           # imm = 0x924A
movl $0x2, %esi
callq 0x3530
movq %rax, %rdi
decl %r13d
movw $0x0, (%rax)
movl %ebp, %eax
shrl $0x1a, %eax
movl %r13d, %ecx
shrl $0x1a, %ecx
movl $0x1, %ebx
subl %eax, %ecx
jb 0x165c0
addl $0x2, %ecx
movl $0x1, %ebx
leal (%rax,%rbx), %edx
movw %dx, (%rdi,%rbx,2)
incq %rbx
cmpq %rbx, %rcx
jne 0x165b1
movl %ebp, %eax
shrl $0x17, %eax
movl %r13d, %ecx
shrl $0x17, %ecx
cmpl %ecx, %eax
ja 0x165ea
addl $0x9, %ecx
movl %ebx, %edx
leaq (%rdi,%rdx,2), %rdx
addl $0x8, %eax
incl %eax
movw %ax, (%rdx)
addq $0x2, %rdx
incl %ebx
cmpl %ecx, %eax
jb 0x165db
movl %ebp, %eax
shrl $0x14, %eax
movl %r13d, %ecx
shrl $0x14, %ecx
cmpl %ecx, %eax
ja 0x16614
addl $0x49, %ecx
movl %ebx, %edx
leaq (%rdi,%rdx,2), %rdx
addl $0x48, %eax
incl %eax
movw %ax, (%rdx)
addq $0x2, %rdx
incl %ebx
cmpl %ecx, %eax
jb 0x16605
movl %ebp, %eax
shrl $0x11, %eax
movl %r13d, %ecx
shrl $0x11, %ecx
cmpl %ecx, %eax
ja 0x16643
addl $0x249, %ecx            # imm = 0x249
movl %ebx, %edx
leaq (%rdi,%rdx,2), %rdx
addl $0x248, %eax            # imm = 0x248
incl %eax
movw %ax, (%rdx)
addq $0x2, %rdx
incl %ebx
cmpl %ecx, %eax
jb 0x16634
movl %ebp, %eax
shrl $0xe, %eax
shrl $0xe, %r13d
cmpl %r13d, %eax
ja 0x16673
addl $0x1249, %r13d          # imm = 0x1249
movl %ebx, %ecx
leaq (%rdi,%rcx,2), %rcx
addl $0x1248, %eax           # imm = 0x1248
incl %eax
movw %ax, (%rcx)
addq $0x2, %rcx
incl %ebx
cmpl %r13d, %eax
jb 0x16663
movslq %r12d, %rax
movq 0x8(%r15), %rcx
movq 0x10(%r15), %rdx
sarl $0xe, %ebp
movq %rax, %rsi
shlq $0x4, %rsi
cmpl (%rdx,%rsi), %ebp
movq %r14, 0x20(%rsp)
jge 0x166aa
movq $0x0, (%rsp)
addq %rsi, %rdx
movq 0x8(%rdx), %rdx
movslq %ebp, %rsi
movq (%rdx,%rsi,8), %r12
jmp 0x166b5
movq $0x0, (%rsp)
xorl %r12d, %r12d
movq (%rcx,%rax,8), %r13
movl %ebx, %eax
movq %rax, 0x18(%rsp)
testl %ebx, %ebx
movq %rdi, 0x8(%rsp)
jle 0x1678c
movl (%r13), %r9d
leal -0x1(%r9), %eax
movl %eax, 0x14(%rsp)
xorl %r11d, %r11d
xorl %r14d, %r14d
movq $0x0, (%rsp)
movl $0x0, %eax
testl %r9d, %r9d
je 0x16763
movzwl (%rdi,%r14,2), %esi
movl %esi, %eax
xorl %edx, %edx
divl %r9d
movl %edx, %edi
movl %esi, %eax
xorl %edx, %edx
divl 0x14(%rsp)
incl %edx
movq 0x10(%r13), %r15
movl %edi, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movl (%r15,%rcx,4), %ebp
leal (%rax,%rax), %ecx
andl $0x1e, %ecx
movl %ebp, %r10d
shrl %cl, %r10d
testb $0x2, %r10b
jne 0x16754
testb $0x1, %r10b
jne 0x1673d
movq 0x18(%r13), %r10
movl %eax, %r8d
cmpl %esi, (%r10,%r8,4)
je 0x16754
addl %edx, %eax
cmpl %r9d, %eax
movl %r9d, %ecx
cmovbl %r11d, %ecx
subl %ecx, %eax
cmpl %edi, %eax
jne 0x1670f
movl %r9d, %eax
jmp 0x1675e
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %r9d, %eax
movq 0x8(%rsp), %rdi
cmpl %r9d, %eax
je 0x1677e
movq 0x20(%r13), %rcx
movl %eax, %eax
shlq $0x4, %rax
movq (%rsp), %rdx
addl 0x4(%rcx,%rax), %edx
movq %rdx, (%rsp)
incq %r14
cmpq 0x18(%rsp), %r14
jne 0x166e7
movq (%rsp), %rax
testl %eax, %eax
je 0x169a6
movslq %eax, %rdi
movl $0x10, %esi
callq 0x3530
movq %rax, %r15
movl $0x0, %r14d
testl %ebx, %ebx
jle 0x168a2
xorl %r9d, %r9d
xorl %r14d, %r14d
movl (%r13), %r10d
movl $0x0, %eax
testl %r10d, %r10d
je 0x16842
movq 0x8(%rsp), %rax
movzwl (%rax,%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), %ebx
leal (%rax,%rax), %ecx
andl $0x1e, %ecx
movl %ebx, %ebp
shrl %cl, %ebp
testb $0x2, %bpl
jne 0x16839
testb $0x1, %bpl
jne 0x1681c
movq 0x18(%r13), %rbp
movl %eax, %r8d
cmpl %esi, (%rbp,%r8,4)
je 0x16839
addl %edx, %eax
cmpl %r10d, %eax
movl %r10d, %ecx
movl $0x0, %r8d
cmovbl %r8d, %ecx
subl %ecx, %eax
cmpl %edi, %eax
jne 0x167ef
movl %r10d, %eax
jmp 0x16842
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %r10d, %eax
cmpl %r10d, %eax
je 0x16894
movq 0x20(%r13), %rcx
movl %eax, %eax
shlq $0x4, %rax
cmpl $0x0, 0x4(%rcx,%rax)
je 0x16894
addq %rax, %rcx
movl $0x8, %eax
xorl %edx, %edx
movq 0x8(%rcx), %rsi
cmpq %r12, (%rsi,%rax)
jbe 0x16885
addq %rax, %rsi
addq $-0x8, %rsi
movslq %r14d, %rdi
incl %r14d
shlq $0x4, %rdi
movups (%rsi), %xmm0
movups %xmm0, (%r15,%rdi)
incq %rdx
movl 0x4(%rcx), %esi
addq $0x10, %rax
cmpq %rsi, %rdx
jb 0x16862
incq %r9
cmpq 0x18(%rsp), %r9
jne 0x167bc
movq 0x8(%rsp), %rdi
callq 0x32c0
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %rbx
movslq %r14d, %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x15301
cmpl $0x2, %r12d
movl $0x0, %r8d
jl 0x1691c
movl %r14d, %eax
leaq 0x18(%r15), %rcx
shlq $0x4, %rax
addq $-0x10, %rax
xorl %edx, %edx
xorl %r8d, %r8d
movslq %r8d, %rsi
shlq $0x4, %rsi
movq 0x8(%r15,%rsi), %rdi
cmpq 0x18(%r15,%rdx), %rdi
jae 0x16913
leaq (%rcx,%rdx), %rdi
addq $-0x8, %rdi
incl %r8d
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r15)
addq $0x10, %rdx
cmpq %rdx, %rax
jne 0x168ec
movl %r8d, %eax
testl %r8d, %r8d
movq 0x20(%rsp), %r14
jle 0x1694c
movq %rax, %rcx
shlq $0x4, %rcx
xorl %edx, %edx
movq 0x10(%r15,%rdx), %rsi
cmpq %rsi, 0x8(%r15,%rdx)
jb 0x16943
movq %rsi, 0x8(%r15,%rdx)
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x16932
testl %r8d, %r8d
jle 0x169b0
shlq $0x4, %rax
addq $0x10, %rax
xorl %ebp, %ebp
movl $0x10, %ecx
movslq %ebp, %rdx
shlq $0x4, %rdx
movq (%r15,%rcx), %rsi
xorq 0x8(%r15,%rdx), %rsi
cmpq $0xffff, %rsi           # imm = 0xFFFF
ja 0x1698a
addq %r15, %rdx
addq $0x8, %rdx
movq 0x8(%r15,%rcx), %rsi
movq %rsi, (%rdx)
jmp 0x16999
leaq (%r15,%rcx), %rsi
incl %ebp
movups (%rsi), %xmm0
movups %xmm0, 0x10(%rdx,%r15)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x16960
incl %ebp
jmp 0x169b5
callq 0x32c0
xorl %r15d, %r15d
jmp 0x169ce
movl $0x1, %ebp
testq %rbx, %rbx
je 0x169cb
movq 0x30(%rbx), %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0x32c0
movl %ebp, (%r14)
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq