// File: /workspace/github_cpp_makefile_star2_O1/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 0x1655f
testq %rax, %rax
je 0x16b64
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, %r12d      # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movslq %r12d, %rax
testq %r15, %r15
je 0x16a6d
movq %rax, %rcx
shlq $0x4, %rcx
cmpq 0x8(%r13,%rcx), %r15
jb 0x16ac7
cmpl 0x18(%rsp), %r12d
je 0x16b46
movl %r12d, %ecx
testl %r12d, %r12d
js 0x16a92
movq %rcx, %rdx
shlq $0x4, %rdx
cmpq 0x8(%r13,%rdx), %r15
jne 0x16b75
testl %r12d, %r12d
js 0x16aab
shlq $0x4, %rcx
movq 0x8(%r13,%rcx), %rdx
movq 0x28(%rsp), %rsi
cmpq (%rsi,%rcx), %rdx
je 0x16ac4
shlq $0x4, %rax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax), %rsi
movq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0x106da
incl %r12d
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1204b
testl %eax, %eax
jle 0x16b46
movl 0x20(%rsp), %eax
cmpl %eax, (%r14)
jne 0x16b46
movl 0x4(%r14), %ebx
cmpl 0xc(%rsp), %ebx
jge 0x16b46
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rbp
movzwl 0x34(%rax), %r15d
movq 0x8(%r14), %rsi
movq %rsi, %rax
shrq $0x30, %rax
jne 0x16b0b
leal 0x1(%rbx), %eax
jmp 0x16b1a
shrl $0x18, %esi
addq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0x11c08
shlq $0x10, %rbp
orq %rbp, %r15
cmpl 0xc(%rsp), %ebx
jae 0x16a57
cmpl 0x1c(%rsp), %eax
jbe 0x16a57
movq %r14, %rdi
movq 0x30(%rsp), %rsi
callq *0x70(%rsp)
jmp 0x16a57
testq %r14, %r14
je 0x16b5c
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 0xfef7(%rip), %rdi      # 0x26a73
leaq 0xff05(%rip), %rsi      # 0x26a88
leaq 0xff0a(%rip), %rcx      # 0x26a94
movl $0x22b, %edx            # imm = 0x22B
callq 0x3140