// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/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
pushq %rax
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
testq %rsi, %rsi
je 0x10c3e
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x0, 0x18(%rbx)
jne 0x10c6e
testq %rbx, %rbx
je 0x10c48
cmpl $0x0, (%rbx)
je 0x10c80
testq %rbx, %rbx
setne %bpl
movq %r15, %rdi
movq %r14, %rsi
callq 0x9b44
movl %eax, %r12d
testl %eax, %eax
sets %al
testb %al, %bpl
je 0x10c6e
movl $0x1, 0x18(%rbx)
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x28(%rbx)
je 0x10d9d
movq 0x20(%rbx), %rcx
testq %rcx, %rcx
je 0x10ca7
movq 0x28(%rbx), %rax
movslq 0x14(%rbx), %rdx
shlq $0x4, %rdx
cmpq 0x8(%rax,%rdx), %rcx
jb 0x10d1c
movl 0x10(%rbx), %edx
movl 0x14(%rbx), %eax
decl %edx
cmpl %edx, %eax
je 0x10dc3
testl %eax, %eax
js 0x10cd1
movq 0x28(%rbx), %rdx
movq %rax, %rsi
shlq $0x4, %rsi
cmpq 0x8(%rdx,%rsi), %rcx
jne 0x10dce
testl %eax, %eax
js 0x10cec
movq 0x28(%rbx), %rcx
movq %rax, %rdx
shlq $0x4, %rdx
movq 0x8(%rcx,%rdx), %rsi
cmpq 0x10(%rcx,%rdx), %rsi
je 0x10d19
movq 0x28(%rbx), %rcx
cltq
shlq $0x4, %rax
movq 0x10(%rcx,%rax), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x18145
movq 0x10(%r15), %rax
shlq $0x10, %rax
movzwl 0xc(%r15), %ecx
orq %rax, %rcx
movq %rcx, 0x20(%rbx)
incl 0x14(%rbx)
movq %r15, %rdi
movq %r14, %rsi
callq 0x9b44
movl %eax, %r12d
testl %eax, %eax
js 0x10c67
movq 0x10(%r15), %rax
shlq $0x10, %rax
movzwl 0xc(%r15), %ecx
orq %rax, %rcx
movq %rcx, 0x20(%rbx)
movl (%r14), %eax
cmpl 0x4(%rbx), %eax
jne 0x10da8
movl 0x4(%r14), %r13d
movl 0xc(%rbx), %ebp
cmpl %ebp, %r13d
jge 0x10da8
movl 0x8(%rbx), %ecx
movq 0x8(%r14), %rsi
movq %rsi, %rax
shrq $0x30, %rax
jne 0x10d70
leal 0x1(%r13), %eax
jmp 0x10d87
shrl $0x18, %esi
addq 0x30(%r14), %rsi
movq %r14, %rdi
movl %ecx, 0x4(%rsp)
callq 0x9628
movl 0x4(%rsp), %ecx
cmpl %ebp, %r13d
jae 0x10c8b
cmpl %ecx, %eax
jbe 0x10c8b
jmp 0x10c6e
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x10c6e
xorl %r12d, %r12d
xorl %edi, %edi
movq %r14, %rsi
callq 0xc210
negl %eax
sbbl %r12d, %r12d
orl $-0x5, %r12d
jmp 0x10c67
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x10c67
leaq 0xa689(%rip), %rdi      # 0x1b45e
leaq 0xa6a9(%rip), %rsi      # 0x1b485
leaq 0xa6ae(%rip), %rcx      # 0x1b491
movl $0x2b7, %edx            # imm = 0x2B7
callq 0x3230