// File: zeeev[P]DASVC/bamtools/src/api/internal/index/BamStandardIndex_p.cpp
// Function: BamTools::Internal::BamStandardIndex::GetOffset(BamTools::BamRegion const&, long&, bool*)
void BamStandardIndex::GetOffset(const BamRegion& region, int64_t& offset, bool* hasAlignmentsInRegion) {

    // cannot calculate offsets if unknown/invalid reference ID requested
    if ( region.LeftRefID < 0 || region.LeftRefID >= (int)m_indexFileSummary.size() )
        throw BamException("BamStandardIndex::GetOffset", "invalid reference ID requested");

    // retrieve index summary for left bound reference
    const BaiReferenceSummary& refSummary = m_indexFileSummary.at(region.LeftRefID);

    // set up region boundaries based on actual BamReader data
    uint32_t begin;
    uint32_t end;
    AdjustRegion(region, begin, end);

    // retrieve all candidate bin IDs for region
    set<uint16_t> candidateBins;
    CalculateCandidateBins(begin, end, candidateBins);

    // use reference's linear offsets to calculate the minimum offset
    // that must be considered to find overlap
    const uint64_t& minOffset = CalculateMinOffset(refSummary, begin);

    // attempt to use reference summary, minOffset, & candidateBins to calculate offsets
    // no data should not be error, just bail
    vector<int64_t> offsets;
    CalculateCandidateOffsets(refSummary, minOffset, candidateBins, offsets);
    if ( offsets.empty() )
        return;
    
    // ensure that offsets are sorted before processing
    sort( offsets.begin(), offsets.end() );

    // binary search for an overlapping block (may not be first one though)
    BamAlignment al;
    typedef vector<int64_t>::const_iterator OffsetConstIterator;
    OffsetConstIterator offsetFirst = offsets.begin();
    OffsetConstIterator offsetIter  = offsetFirst;
    OffsetConstIterator offsetLast  = offsets.end();
    iterator_traits<OffsetConstIterator>::difference_type count = distance(offsetFirst, offsetLast);
    iterator_traits<OffsetConstIterator>::difference_type step;
    while ( count > 0 ) {
        offsetIter = offsetFirst;
        step = count/2;
        advance(offsetIter, step);

        // attempt seek to candidate offset
        const int64_t& candidateOffset = (*offsetIter);
        if ( !m_reader->Seek(candidateOffset) ) {
            const string readerError = m_reader->GetErrorString();
            const string message = "could not seek in BAM file: \n\t" + readerError;
            throw BamException("BamToolsIndex::GetOffset", message);
        }

        // load first available alignment, setting flag to true if data exists
        *hasAlignmentsInRegion = m_reader->LoadNextAlignment(al);

        // check alignment against region
        if ( al.GetEndPosition() <= region.LeftPosition ) {
            offsetFirst = ++offsetIter;
            count -= step+1;
        } else count = step;
    }

    // step back to the offset before the 'current offset' (to make sure we cover overlaps)
    if ( offsetIter != offsets.begin() )
        --offsetIter;
    offset = (*offsetIter);
}

// Assembly:
subq $0x3d8, %rsp            # imm = 0x3D8
movq %rdi, 0x3d0(%rsp)
movq %rsi, 0x3c8(%rsp)
movq %rdx, 0x3c0(%rsp)
movq %rcx, 0x3b8(%rsp)
movq 0x3d0(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x3c8(%rsp), %rax
cmpl $0x0, (%rax)
jl 0xb3998
movq 0xb8(%rsp), %rdi
movq 0x3c8(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xb4(%rsp)
addq $0x38, %rdi
callq 0x7ae80
movq %rax, %rcx
movl 0xb4(%rsp), %eax
cmpl %ecx, %eax
jl 0xb3af9
movb $0x1, 0x35e(%rsp)
movl $0x28, %edi
callq 0x737c0
movq %rax, 0xa0(%rsp)
leaq 0x397(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0x7b5f0
movq 0xa8(%rsp), %rdx
leaq 0x378a1(%rip), %rsi     # 0xeb277
leaq 0x398(%rsp), %rdi
callq 0x78e70
jmp 0xb39e5
leaq 0x35f(%rsp), %rdi
movq %rdi, 0x98(%rsp)
callq 0x7b5f0
movq 0x98(%rsp), %rdx
leaq 0x3788a(%rip), %rsi     # 0xeb293
leaq 0x360(%rsp), %rdi
callq 0x78e70
jmp 0xb3a18
movq 0xa0(%rsp), %rdi
leaq 0x398(%rsp), %rsi
leaq 0x360(%rsp), %rdx
callq 0x79cd0
jmp 0xb3a37
movq 0xa0(%rsp), %rdi
movb $0x0, 0x35e(%rsp)
movq 0x5e4c2(%rip), %rsi     # 0x111f10
movq 0x5e50b(%rip), %rdx     # 0x111f60
callq 0x7b010
jmp 0xb4162
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x388(%rsp)
movl %eax, 0x384(%rsp)
jmp 0xb3ace
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x388(%rsp)
movl %eax, 0x384(%rsp)
jmp 0xb3ab0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x388(%rsp)
movl %eax, 0x384(%rsp)
leaq 0x360(%rsp), %rdi
callq 0x7cce8
jmp 0xb3aae
jmp 0xb3ab0
leaq 0x35f(%rsp), %rdi
callq 0x76f80
leaq 0x398(%rsp), %rdi
callq 0x7cce8
jmp 0xb3acc
jmp 0xb3ace
leaq 0x397(%rsp), %rdi
callq 0x76f80
testb $0x1, 0x35e(%rsp)
jne 0xb3ae7
jmp 0xb3af4
movq 0xa0(%rsp), %rdi
callq 0x74e00
jmp 0xb414d
movq 0xb8(%rsp), %rdi
addq $0x38, %rdi
movq 0x3c8(%rsp), %rax
movslq (%rax), %rsi
callq 0x7ae70
movq 0xb8(%rsp), %rdi
movq %rax, 0x350(%rsp)
movq 0x3c8(%rsp), %rsi
leaq 0x34c(%rsp), %rdx
movq %rdx, 0x80(%rsp)
leaq 0x348(%rsp), %rcx
movq %rcx, 0x88(%rsp)
callq 0x74680
leaq 0x318(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x74ab0
movq 0xb8(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
movq 0x90(%rsp), %rcx
callq 0x7a630
jmp 0xb3b8e
movq 0xb8(%rsp), %rdi
movq 0x350(%rsp), %rsi
leaq 0x34c(%rsp), %rdx
callq 0x76ac0
movq %rax, 0x78(%rsp)
jmp 0xb3bb2
movq 0x78(%rsp), %rax
movq %rax, 0x308(%rsp)
leaq 0x308(%rsp), %rax
movq %rax, 0x310(%rsp)
leaq 0x2f0(%rsp), %rdi
callq 0x7b340
jmp 0xb3bde
movq 0xb8(%rsp), %rdi
movq 0x350(%rsp), %rsi
movq 0x310(%rsp), %rdx
leaq 0x318(%rsp), %rcx
leaq 0x2f0(%rsp), %r8
callq 0x79b50
jmp 0xb3c0d
leaq 0x2f0(%rsp), %rdi
callq 0x71f70
movb %al, 0x77(%rsp)
jmp 0xb3c20
movb 0x77(%rsp), %al
testb $0x1, %al
jne 0xb3c2a
jmp 0xb3c6c
movl $0x1, 0x2ec(%rsp)
jmp 0xb40f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x388(%rsp)
movl %eax, 0x384(%rsp)
jmp 0xb413c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x388(%rsp)
movl %eax, 0x384(%rsp)
jmp 0xb412b
leaq 0x2f0(%rsp), %rdi
callq 0x7c3f0
movq %rax, 0x68(%rsp)
jmp 0xb3c80
movq 0x68(%rsp), %rax
movq %rax, 0x2e0(%rsp)
leaq 0x2f0(%rsp), %rdi
callq 0x78460
movq %rax, 0x60(%rsp)
jmp 0xb3ca1
movq 0x60(%rsp), %rax
movq %rax, 0x2d8(%rsp)
movq 0x2e0(%rsp), %rdi
movq 0x2d8(%rsp), %rsi
callq 0x7ac50
jmp 0xb3cc5
leaq 0x180(%rsp), %rdi
callq 0x72830
jmp 0xb3cd4
leaq 0x2f0(%rsp), %rdi
callq 0x7c3f0
movq %rax, 0x58(%rsp)
jmp 0xb3ce8
movq 0x58(%rsp), %rax
movq %rax, 0x170(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x73a60
jmp 0xb3d0c
movq 0x178(%rsp), %rax
movq %rax, 0x168(%rsp)
leaq 0x2f0(%rsp), %rdi
callq 0x78460
movq %rax, 0x50(%rsp)
jmp 0xb3d30
movq 0x50(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x160(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x73a60
jmp 0xb3d54
movq 0x178(%rsp), %rax
movq %rax, 0x148(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x148(%rsp), %rdi
movq 0x140(%rsp), %rsi
callq 0x75060
movq %rax, 0x48(%rsp)
jmp 0xb3d90
movq 0x48(%rsp), %rax
movq %rax, 0x150(%rsp)
cmpq $0x0, 0x150(%rsp)
jle 0xb4060
movq 0x178(%rsp), %rax
movq %rax, 0x168(%rsp)
movq 0x150(%rsp), %rax
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rcx, %rax
sarq %rax
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0x720e0
jmp 0xb3df0
leaq 0x168(%rsp), %rdi
callq 0x752f0
movq %rax, 0x40(%rsp)
jmp 0xb3e04
movq 0xb8(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, 0x130(%rsp)
movq 0x8(%rax), %rdi
movq 0x130(%rsp), %rsi
callq 0x76a30
movb %al, 0x3f(%rsp)
jmp 0xb3e30
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0xb3fae
jmp 0xb3e3e
movq 0xb8(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x110(%rsp), %rdi
callq 0x7bbf0
jmp 0xb3e59
leaq 0x36fb4(%rip), %rsi     # 0xeae14
leaq 0xf0(%rsp), %rdi
leaq 0x110(%rsp), %rdx
callq 0x75e70
jmp 0xb3e77
movb $0x1, 0xce(%rsp)
movl $0x28, %edi
callq 0x737c0
movq %rax, 0x28(%rsp)
leaq 0xcf(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x7b5f0
movq 0x30(%rsp), %rdx
leaq 0x37406(%rip), %rsi     # 0xeb2b2
leaq 0xd0(%rsp), %rdi
callq 0x78e70
jmp 0xb3ebb
movq 0x28(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
leaq 0xf0(%rsp), %rdx
callq 0x79cd0
jmp 0xb3ed7
movq 0x28(%rsp), %rdi
movb $0x0, 0xce(%rsp)
movq 0x5e025(%rip), %rsi     # 0x111f10
movq 0x5e06e(%rip), %rdx     # 0x111f60
callq 0x7b010
jmp 0xb4162
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x388(%rsp)
movl %eax, 0x384(%rsp)
jmp 0xb411a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x388(%rsp)
movl %eax, 0x384(%rsp)
jmp 0xb3f9a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x388(%rsp)
movl %eax, 0x384(%rsp)
jmp 0xb3f66
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x388(%rsp)
movl %eax, 0x384(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x7cce8
jmp 0xb3f64
jmp 0xb3f66
leaq 0xcf(%rsp), %rdi
callq 0x76f80
testb $0x1, 0xce(%rsp)
jne 0xb3f7f
jmp 0xb3f89
movq 0x28(%rsp), %rdi
callq 0x74e00
leaq 0xf0(%rsp), %rdi
callq 0x7cce8
jmp 0xb3f98
jmp 0xb3f9a
leaq 0x110(%rsp), %rdi
callq 0x7cce8
jmp 0xb3fa9
jmp 0xb411a
movq 0xb8(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x180(%rsp), %rsi
callq 0x74860
movb %al, 0x27(%rsp)
jmp 0xb3fcd
movb 0x27(%rsp), %cl
movq 0x3b8(%rsp), %rax
andb $0x1, %cl
movb %cl, (%rax)
leaq 0x180(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x76240
movl %eax, 0x20(%rsp)
jmp 0xb3ff5
movl 0x20(%rsp), %eax
movq 0x3c8(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jg 0xb404b
leaq 0x168(%rsp), %rdi
callq 0x713e0
movq %rax, 0x18(%rsp)
jmp 0xb401a
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x178(%rsp)
movq 0x138(%rsp), %rcx
addq $0x1, %rcx
movq 0x150(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x150(%rsp)
jmp 0xb405b
movq 0x138(%rsp), %rax
movq %rax, 0x150(%rsp)
jmp 0xb3d9d
leaq 0x2f0(%rsp), %rdi
callq 0x7c3f0
movq %rax, 0x10(%rsp)
jmp 0xb4074
movq 0x10(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x74cc0
movb %al, 0xf(%rsp)
jmp 0xb409c
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0xb40a6
jmp 0xb40b7
leaq 0x168(%rsp), %rdi
callq 0x74bc0
jmp 0xb40b5
jmp 0xb40b7
leaq 0x168(%rsp), %rdi
callq 0x752f0
movq %rax, (%rsp)
jmp 0xb40ca
movq (%rsp), %rax
movq (%rax), %rcx
movq 0x3c0(%rsp), %rax
movq %rcx, (%rax)
leaq 0x180(%rsp), %rdi
callq 0x7a0d0
jmp 0xb40eb
movl $0x0, 0x2ec(%rsp)
leaq 0x2f0(%rsp), %rdi
callq 0x7b860
jmp 0xb4105
leaq 0x318(%rsp), %rdi
callq 0x7b390
addq $0x3d8, %rsp            # imm = 0x3D8
retq
leaq 0x180(%rsp), %rdi
callq 0x7a0d0
jmp 0xb4129
jmp 0xb412b
leaq 0x2f0(%rsp), %rdi
callq 0x7b860
jmp 0xb413a
jmp 0xb413c
leaq 0x318(%rsp), %rdi
callq 0x7b390
jmp 0xb414b
jmp 0xb414d
movq 0x388(%rsp), %rdi
callq 0x7b550
movq %rax, %rdi
callq 0x88ef0
nopw %cs:(%rax,%rax)