// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/bam_index.c
// Function: bam_iter_read
int bam_iter_read(bamFile fp, bam_iter_t iter, bam1_t *b)
{
	int ret;
	if (iter && iter->finished) return -1;
	if (iter == 0 || iter->from_first) {
		ret = bam_read1(fp, b);
		if (ret < 0 && iter) iter->finished = 1;
		return ret;
	}
	if (iter->off == 0) return -1;
	for (;;) {
		if (iter->curr_off == 0 || iter->curr_off >= iter->off[iter->i].v) { // then jump to the next chunk
			if (iter->i == iter->n_off - 1) { ret = -1; break; } // no more chunks
			if (iter->i >= 0) assert(iter->curr_off == iter->off[iter->i].v); // otherwise bug
			if (iter->i < 0 || iter->off[iter->i].v != iter->off[iter->i+1].u) { // not adjacent chunks; then seek
				bam_seek(fp, iter->off[iter->i+1].u, SEEK_SET);
				iter->curr_off = bam_tell(fp);
			}
			++iter->i;
		}
		if ((ret = bam_read1(fp, b)) >= 0) {
			iter->curr_off = bam_tell(fp);
			if (b->core.tid != iter->tid || b->core.pos >= iter->end) { // no need to proceed
				ret = bam_validate1(NULL, b)? -1 : -5; // determine whether end of region or error
				break;
			}
			else if (is_overlap(iter->beg, iter->end, b)) return ret;
		} else break; // end of file or error
	}
	iter->finished = 1;
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
testq %rsi, %rsi
je 0x29a45d
pushq $-0x1
popq %rbp
cmpl $0x0, 0x18(%r14)
jne 0x29a488
cmpl $0x0, (%r14)
je 0x29a499
testq %r14, %r14
sete %bl
movq %r12, %rdi
movq %r15, %rsi
callq 0x294ff7
movl %eax, %r12d
testl %eax, %eax
setns %al
orb %bl, %al
jne 0x29a485
addq $0x18, %r14
movl $0x1, (%r14)
movl %r12d, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x28(%r14)
je 0x29a488
leaq 0x18(%r14), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%r14), %rcx
movl 0x14(%r14), %eax
movslq %eax, %rdx
testq %rcx, %rcx
je 0x29a4cb
movq 0x28(%r14), %rsi
movq %rdx, %rdi
shlq $0x4, %rdi
cmpq 0x8(%rsi,%rdi), %rcx
jb 0x29a532
movl 0x10(%r14), %esi
decl %esi
cmpl %esi, %eax
je 0x29a5d4
movq 0x28(%r14), %rsi
testl %eax, %eax
js 0x29a4ff
movq %rax, %rdx
shlq $0x4, %rdx
cmpq 0x8(%rsi,%rdx), %rcx
jne 0x29a5e2
movq 0x10(%rsi,%rdx), %rsi
cmpq %rsi, %rcx
jne 0x29a508
jmp 0x29a52c
shlq $0x4, %rdx
movq 0x10(%rsi,%rdx), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x29385a
movq 0x30(%r12), %rax
shlq $0x10, %rax
movzwl 0x3c(%r12), %ecx
orq %rax, %rcx
movq %rcx, 0x20(%r14)
movl 0x14(%r14), %eax
incl %eax
movl %eax, 0x14(%r14)
movq %r12, %rdi
movq %r15, %rsi
callq 0x294ff7
movl %eax, %ebp
testl %eax, %eax
js 0x29a5cf
movq 0x30(%r12), %rax
shlq $0x10, %rax
movzwl 0x3c(%r12), %ecx
orq %rax, %rcx
movq %rcx, 0x20(%r14)
movl (%r15), %eax
cmpl 0x4(%r14), %eax
jne 0x29a5b7
movl 0x4(%r15), %r13d
movl 0xc(%r14), %ebx
cmpl %ebx, %r13d
jge 0x29a5b7
movl 0x8(%r14), %ecx
movq 0x8(%r15), %rsi
movq %rsi, %rax
shrq $0x30, %rax
jne 0x29a58a
leal 0x1(%r13), %eax
jmp 0x29a5a1
shrl $0x18, %esi
addq 0x30(%r15), %rsi
movq %r15, %rdi
movl %ecx, 0x14(%rsp)
callq 0x294bb0
movl 0x14(%rsp), %ecx
cmpl %ebx, %r13d
jae 0x29a4a9
cmpl %ecx, %eax
jbe 0x29a4a9
jmp 0x29a488
xorl %r12d, %r12d
xorl %edi, %edi
movq %r15, %rsi
callq 0x295de6
negl %eax
sbbl %r12d, %r12d
orl $-0x5, %r12d
jmp 0x29a5d8
movl %ebp, %r12d
jmp 0x29a5d8
pushq $-0x1
popq %r12
movq 0x8(%rsp), %r14
jmp 0x29a47e
leaq 0xc0d75(%rip), %rdi     # 0x35b35e
leaq 0xc0d95(%rip), %rsi     # 0x35b385
leaq 0xc0db7(%rip), %rcx     # 0x35b3ae
movl $0x2b0, %edx            # imm = 0x2B0
callq 0x2edc0