// File: zeeev[P]DASVC/bamtools/src/api/internal/bam/BamReader_p.cpp
// Function: BamTools::Internal::BamReaderPrivate::GetNextAlignmentCore(BamTools::BamAlignment&)
bool BamReaderPrivate::GetNextAlignmentCore(BamAlignment& alignment) {

    // skip if stream not opened
    if ( !m_stream.IsOpen() )
        return false;

    try {

        // skip if region is set but has no alignments
        if ( m_randomAccessController.HasRegion() &&
             !m_randomAccessController.RegionHasAlignments() )
        {
            return false;
        }

        // if can't read next alignment
        if ( !LoadNextAlignment(alignment) )
            return false;

        // check alignment's region-overlap state
        BamRandomAccessController::RegionState state = m_randomAccessController.AlignmentState(alignment);

        // if alignment starts after region, no need to keep reading
        if ( state == BamRandomAccessController::AfterRegion )
            return false;

        // read until overlap is found
        while ( state != BamRandomAccessController::OverlapsRegion ) {

            // if can't read next alignment
            if ( !LoadNextAlignment(alignment) )
                return false;

            // check alignment's region-overlap state
            state = m_randomAccessController.AlignmentState(alignment);

            // if alignment starts after region, no need to keep reading
            if ( state == BamRandomAccessController::AfterRegion )
                return false;
        }

        // if we get here, we found the next 'valid' alignment
        // (e.g. overlaps current region if one was set, simply the next alignment if not)
        alignment.SupportData.HasCoreOnly = true;
        return true;

    } catch ( BamException& e ) {
        const string streamError = e.what();
        const string message = string("encountered error reading BAM alignment: \n\t") + streamError;
        SetErrorString("BamReader::GetNextAlignmentCore", message);
        return false;
    }
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0xf8(%rsp)
movq %rsi, 0xf0(%rsp)
movq 0xf8(%rsp), %rdi
movq %rdi, 0x38(%rsp)
addq $0x1e8, %rdi            # imm = 0x1E8
callq 0x7a940
testb $0x1, %al
jne 0xaa7d1
movb $0x0, 0x107(%rsp)
jmp 0xaab9f
movq 0x38(%rsp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
callq 0x75aa0
movb %al, 0x37(%rsp)
jmp 0xaa7e8
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0xaa7f5
jmp 0xaa97b
movq 0x38(%rsp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
callq 0x7c680
movb %al, 0x36(%rsp)
jmp 0xaa80c
movb 0x36(%rsp), %al
testb $0x1, %al
jne 0xaa97b
jmp 0xaa81a
movb $0x0, 0x107(%rsp)
jmp 0xaab9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0xaabb0
movq 0xe8(%rsp), %rdi
callq 0x72fc0
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, 0x20(%rsp)
leaq 0xb7(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x7b5f0
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0xb8(%rsp), %rdi
callq 0x78e70
jmp 0xaa8a5
leaq 0xb7(%rsp), %rdi
callq 0x76f80
leaq 0x6f(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x7b5f0
movq 0x18(%rsp), %rdx
leaq 0x40429(%rip), %rsi     # 0xeacf6
leaq 0x70(%rsp), %rdi
callq 0x78e70
jmp 0xaa8d9
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0x7c720
jmp 0xaa8f5
leaq 0x70(%rsp), %rdi
callq 0x7cce8
jmp 0xaa901
leaq 0x6f(%rsp), %rdi
callq 0x76f80
leaq 0x47(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x7b5f0
movq 0x10(%rsp), %rdx
leaq 0x403fc(%rip), %rsi     # 0xead22
leaq 0x48(%rsp), %rdi
callq 0x78e70
jmp 0xaa932
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x774d0
jmp 0xaa94b
leaq 0x48(%rsp), %rdi
callq 0x7cce8
jmp 0xaa957
leaq 0x47(%rsp), %rdi
callq 0x76f80
movb $0x0, 0x107(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x7cce8
jmp 0xaab41
movq 0x38(%rsp), %rdi
movq 0xf0(%rsp), %rsi
callq 0x74860
movb %al, 0xf(%rsp)
jmp 0xaa993
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0xaa9aa
jmp 0xaa99d
movb $0x0, 0x107(%rsp)
jmp 0xaab9f
movq 0x38(%rsp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
movq 0xf0(%rsp), %rsi
callq 0x7bbb0
movl %eax, 0x8(%rsp)
jmp 0xaa9c9
movl 0x8(%rsp), %eax
movl %eax, 0xe0(%rsp)
cmpl $0x2, 0xe0(%rsp)
jne 0xaa9eb
movb $0x0, 0x107(%rsp)
jmp 0xaab9f
jmp 0xaa9ed
cmpl $0x1, 0xe0(%rsp)
je 0xaaa67
movq 0x38(%rsp), %rdi
movq 0xf0(%rsp), %rsi
callq 0x74860
movb %al, 0x7(%rsp)
jmp 0xaaa0f
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0xaaa26
jmp 0xaaa19
movb $0x0, 0x107(%rsp)
jmp 0xaab9f
movq 0x38(%rsp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
movq 0xf0(%rsp), %rsi
callq 0x7bbb0
movl %eax, (%rsp)
jmp 0xaaa44
movl (%rsp), %eax
movl %eax, 0xe0(%rsp)
cmpl $0x2, 0xe0(%rsp)
jne 0xaaa65
movb $0x0, 0x107(%rsp)
jmp 0xaab9f
jmp 0xaa9ed
movq 0xf0(%rsp), %rax
movb $0x1, 0x130(%rax)
movb $0x1, 0x107(%rsp)
jmp 0xaab9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0xb7(%rsp), %rdi
callq 0x76f80
jmp 0xaab96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0xaaae1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x7cce8
jmp 0xaaadf
jmp 0xaaae1
leaq 0x6f(%rsp), %rdi
callq 0x76f80
jmp 0xaab85
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0xaab28
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x7cce8
jmp 0xaab26
jmp 0xaab28
leaq 0x47(%rsp), %rdi
callq 0x76f80
leaq 0x90(%rsp), %rdi
callq 0x7cce8
jmp 0xaab66
leaq 0xb8(%rsp), %rdi
callq 0x7cce8
jmp 0xaab68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0xaab85
jmp 0xaab85
callq 0x7a790
jmp 0xaab9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0xaab96
leaq 0xb8(%rsp), %rdi
callq 0x7cce8
jmp 0xaab94
jmp 0xaab96
callq 0x7a790
jmp 0xaab9d
jmp 0xaabb0
movb 0x107(%rsp), %al
andb $0x1, %al
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xe8(%rsp), %rdi
callq 0x7b550
movq %rax, %rdi
callq 0x88ef0
nopw %cs:(%rax,%rax)