// File: /workspace/github_c_makefile_star2_O3/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 %rdi, %r15
testq %rsi, %rsi
je 0x112ff
movq %rsi, %rbx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x0, 0x18(%rsi)
jne 0x11449
cmpl $0x0, (%rbx)
je 0x11318
movq %r15, %rdi
movq %r14, %rsi
callq 0x9f3b
movl %eax, %r12d
testl %eax, %eax
jns 0x11449
jmp 0x11442
movq %r15, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9f3b
cmpq $0x0, 0x28(%rbx)
je 0x11449
movq 0x20(%rbx), %rcx
movl 0x14(%rbx), %eax
movslq %eax, %rdx
testq %rcx, %rcx
je 0x11344
movq 0x28(%rbx), %rsi
movq %rdx, %rdi
shlq $0x4, %rdi
cmpq 0x8(%rsi,%rdi), %rcx
jb 0x113a6
movl 0x10(%rbx), %esi
decl %esi
cmpl %esi, %eax
je 0x1143c
movq 0x28(%rbx), %rsi
testl %eax, %eax
js 0x11377
movq %rax, %rdx
shlq $0x4, %rdx
cmpq 0x8(%rsi,%rdx), %rcx
jne 0x1145b
movq 0x10(%rsi,%rdx), %rsi
cmpq %rsi, %rcx
jne 0x11380
jmp 0x113a1
shlq $0x4, %rdx
movq 0x10(%rsi,%rdx), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x189d8
movq 0x10(%r15), %rax
shlq $0x10, %rax
movzwl 0xc(%r15), %ecx
orq %rax, %rcx
movq %rcx, 0x20(%rbx)
movl 0x14(%rbx), %eax
incl %eax
movl %eax, 0x14(%rbx)
movq %r15, %rdi
movq %r14, %rsi
callq 0x9f3b
movl %eax, %r12d
testl %eax, %eax
js 0x11442
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 0x11424
movl 0x4(%r14), %r13d
movl 0xc(%rbx), %ebp
cmpl %ebp, %r13d
jge 0x11424
movl 0x8(%rbx), %ecx
movq 0x8(%r14), %rsi
movq %rsi, %rax
shrq $0x30, %rax
jne 0x113fa
leal 0x1(%r13), %eax
jmp 0x11411
shrl $0x18, %esi
addq 0x30(%r14), %rsi
movq %r14, %rdi
movl %ecx, 0x4(%rsp)
callq 0x9a3c
movl 0x4(%rsp), %ecx
cmpl %ebp, %r13d
jae 0x11323
cmpl %ecx, %eax
jbe 0x11323
jmp 0x11449
xorl %r12d, %r12d
xorl %edi, %edi
movq %r14, %rsi
callq 0xc73a
negl %eax
sbbl %r12d, %r12d
orl $-0x5, %r12d
jmp 0x11442
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0x1, 0x18(%rbx)
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb10c(%rip), %rdi      # 0x1c56e
leaq 0xb12c(%rip), %rsi      # 0x1c595
leaq 0xb131(%rip), %rcx      # 0x1c5a1
movl $0x2b7, %edx            # imm = 0x2B7
callq 0x3230