// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movl %esi, 0x74(%rsp)
movl %edx, 0x70(%rsp)
movl %ecx, 0x6c(%rsp)
movq $0x0, 0x28(%rsp)
cmpl $0x0, 0x70(%rsp)
jge 0x4733d0
movl $0x0, 0x70(%rsp)
movl 0x6c(%rsp), %eax
cmpl 0x70(%rsp), %eax
jge 0x4733eb
movq $0x0, 0x80(%rsp)
jmp 0x473a17
movl $0x1, %edi
movl $0x30, %esi
callq 0x32f50
movq %rax, 0x28(%rsp)
movl 0x74(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x4(%rax)
movl 0x70(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x8(%rax)
movl 0x6c(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x28(%rsp), %rax
movl $0xffffffff, 0x14(%rax) # imm = 0xFFFFFFFF
movl $0x924a, %edi           # imm = 0x924A
movl $0x2, %esi
callq 0x32f50
movq %rax, 0x60(%rsp)
movl 0x70(%rsp), %edi
movl 0x6c(%rsp), %esi
movq 0x60(%rsp), %rdx
callq 0x473a30
movl %eax, 0x58(%rsp)
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x74(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x74(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jle 0x4735eb
movl 0x70(%rsp), %eax
sarl $0xe, %eax
movq 0x78(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq 0x74(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jl 0x4734ee
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x74(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq 0x78(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq 0x74(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, (%rsp)
jmp 0x473519
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x74(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl 0x70(%rsp), %ecx
sarl $0xe, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, (%rsp)
movq (%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x4735e9
movl 0x70(%rsp), %eax
sarl $0xe, %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movq 0x78(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq 0x74(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jle 0x473571
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x74(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x5c(%rsp)
cmpl $0x0, 0x5c(%rsp)
jl 0x4735b9
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x74(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movslq 0x5c(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x4735aa
jmp 0x4735b9
jmp 0x4735ac
movl 0x5c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x47357c
cmpl $0x0, 0x5c(%rsp)
jl 0x4735e7
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x74(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movslq 0x5c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
jmp 0x4735e9
jmp 0x4735f4
movq $0x0, 0x30(%rsp)
movl $0x0, 0x54(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x58(%rsp), %eax
jge 0x473661
movq 0x38(%rsp), %rdi
movq 0x60(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movzwl (%rax,%rcx,2), %esi
callq 0x473210
movl %eax, 0x44(%rsp)
movq 0x38(%rsp), %rcx
cmpl (%rcx), %eax
je 0x473652
movq 0x38(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x44(%rsp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl 0x54(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x473654
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x473604
cmpl $0x0, 0x54(%rsp)
jne 0x473684
movq 0x60(%rsp), %rdi
callq 0x34078
movq 0x28(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x473a17
movslq 0x54(%rsp), %rdi
movl $0x10, %esi
callq 0x32f50
movq %rax, 0x48(%rsp)
movl $0x0, 0x54(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x58(%rsp), %eax
jge 0x473790
movq 0x38(%rsp), %rdi
movq 0x60(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movzwl (%rax,%rcx,2), %esi
callq 0x473210
movl %eax, 0x44(%rsp)
movq 0x38(%rsp), %rcx
cmpl (%rcx), %eax
je 0x47377e
movq 0x38(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x44(%rsp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x47377c
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x20(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
cmpq 0x30(%rsp), %rax
jbe 0x47376d
movq 0x48(%rsp), %rax
movl 0x54(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x54(%rsp)
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
movslq 0x20(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x47376f
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x473700
jmp 0x47377e
jmp 0x473780
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x4736a8
movq 0x60(%rsp), %rdi
callq 0x34078
cmpl $0x0, 0x54(%rsp)
jne 0x4737bd
movq 0x48(%rsp), %rdi
callq 0x34078
movq 0x28(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x473a17
movl $0x1, %edi
movl $0x38, %esi
callq 0x32f50
movq %rax, 0x10(%rsp)
movslq 0x54(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x470a30
movl $0x1, 0x5c(%rsp)
movl $0x0, 0xc(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x47386e
movq 0x48(%rsp), %rax
movslq 0xc(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq 0x48(%rsp), %rcx
movslq 0x5c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jae 0x47385f
movq 0x48(%rsp), %rax
movl 0xc(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, 0xc(%rsp)
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x48(%rsp), %rcx
movslq 0x5c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x473861
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x4737f0
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
movl $0x1, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x4738f8
movq 0x48(%rsp), %rax
movl 0x5c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq 0x48(%rsp), %rcx
movslq 0x5c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jb 0x4738e9
movq 0x48(%rsp), %rax
movslq 0x5c(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq 0x48(%rsp), %rax
movl 0x5c(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x4738eb
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x473881
movl $0x1, 0x5c(%rsp)
movl $0x0, 0xc(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x4739c3
movq 0x48(%rsp), %rax
movslq 0xc(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0x10, %rax
movq 0x48(%rsp), %rcx
movslq 0x5c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
shrq $0x10, %rcx
cmpq %rcx, %rax
jne 0x473978
movq 0x48(%rsp), %rax
movslq 0x5c(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq 0x48(%rsp), %rax
movslq 0xc(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x4739b1
movq 0x48(%rsp), %rax
movl 0xc(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, 0xc(%rsp)
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x48(%rsp), %rcx
movslq 0x5c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x4739b3
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x473908
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x4739ee
movq 0x10(%rsp), %rax
movq 0x30(%rax), %rdi
callq 0x34078
movq 0x10(%rsp), %rdi
callq 0x34078
jmp 0x4739f0
movl 0x54(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x48(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x28(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
addq $0x88, %rsp
retq
nopw (%rax,%rax)