// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_index.c
// Function: bam_fetch
int bam_fetch(bamFile fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func)
{
	int n_off;
	pair64_t *off = get_chunk_coordinates(idx, tid, beg, end, &n_off);
	if (off == 0) return 0;
	{
		// retrive alignments
		uint64_t curr_off;
		int i, ret, n_seeks;
		n_seeks = 0; i = -1; curr_off = 0;
		bam1_t *b = (bam1_t*)calloc(1, sizeof(bam1_t));
		for (;;) {
			if (curr_off == 0 || curr_off >= off[i].v) { // then jump to the next chunk
				if (i == n_off - 1) break; // no more chunks
				if (i >= 0) assert(curr_off == off[i].v); // otherwise bug
				if (i < 0 || off[i].v != off[i+1].u) { // not adjacent chunks; then seek
					bam_seek(fp, off[i+1].u, SEEK_SET);
					curr_off = bam_tell(fp);
					++n_seeks;
				}
				++i;
			}
			if ((ret = bam_read1(fp, b)) > 0) {
				curr_off = bam_tell(fp);
				if (b->core.tid != tid || b->core.pos >= end) break; // no need to proceed
				else if (is_overlap(beg, end, b)) func(b, data);
			} else break; // end of file
		}
//		fprintf(stderr, "[bam_fetch] # seek calls: %d\n", n_seeks);
		bam_destroy1(b);
	}
	free(off);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x30(%rsp)
movl %r8d, %eax
movq %rdi, 0x10(%rsp)
leaq 0x24(%rsp), %r8
movq %rsi, %rdi
movl %edx, 0x20(%rsp)
movl %edx, %esi
movl %ecx, 0x1c(%rsp)
movl %ecx, %edx
movl %eax, 0xc(%rsp)
movl %eax, %ecx
callq 0x16a1c
testq %rax, %rax
je 0x172f7
movq %rax, %r13
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %r14
movq %r13, %rax
addq $0x10, %rax
movq %rax, 0x28(%rsp)
movl 0x24(%rsp), %eax
decl %eax
movl %eax, 0x18(%rsp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r12d, %r12d
testq %r12, %r12
je 0x17209
movslq %r15d, %rax
shlq $0x4, %rax
cmpq 0x8(%r13,%rax), %r12
jb 0x1725a
cmpl 0x18(%rsp), %r15d
je 0x172d9
testl %r15d, %r15d
js 0x1723b
movl %r15d, %eax
shlq $0x4, %rax
cmpq 0x8(%r13,%rax), %r12
jne 0x17308
movq 0x28(%rsp), %rcx
movq (%rcx,%rax), %rsi
cmpq %rsi, %r12
jne 0x1724b
jmp 0x17257
movslq %r15d, %rax
shlq $0x4, %rax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax), %rsi
movq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0x10ef6
incl %r15d
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x126b5
testl %eax, %eax
jle 0x172d9
movl 0x20(%rsp), %eax
cmpl %eax, (%r14)
jne 0x172de
movl 0x4(%r14), %ebx
cmpl 0xc(%rsp), %ebx
jge 0x172de
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rbp
movzwl 0x34(%rax), %r12d
movq 0x8(%r14), %rsi
movq %rsi, %rax
shrq $0x30, %rax
jne 0x1729e
leal 0x1(%rbx), %eax
jmp 0x172ad
shrl $0x18, %esi
addq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0x12268
shlq $0x10, %rbp
orq %rbp, %r12
cmpl 0xc(%rsp), %ebx
jae 0x171f6
cmpl 0x1c(%rsp), %eax
jbe 0x171f6
movq %r14, %rdi
movq 0x30(%rsp), %rsi
callq *0x70(%rsp)
jmp 0x171f6
testq %r14, %r14
je 0x172ef
movq 0x30(%r14), %rdi
callq 0x32c0
movq %r14, %rdi
callq 0x32c0
movq %r13, %rdi
callq 0x32c0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe855(%rip), %rdi      # 0x25b64
leaq 0xe863(%rip), %rsi      # 0x25b79
leaq 0xe868(%rip), %rcx      # 0x25b85
movl $0x22b, %edx            # imm = 0x22B
callq 0x3140