// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rcx, 0x38(%rsp)
movq %rsi, 0x8(%rsp)
movslq (%rsi), %rsi
testq %rsi, %rsi
js 0x48503
movq %rdx, %r14
movq %rdi, %r15
movq 0x40(%rdi), %rax
subq 0x38(%rdi), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
cmpl %eax, %esi
jge 0x48503
leaq 0x38(%r15), %rdi
callq 0x34be0
movq %rax, %r13
leaq 0x4(%rsp), %rbp
leaq 0x44(%rsp), %rbx
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x324c0
leaq 0x50(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x48(%rsp), %rcx
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x348f0
leaq 0x4(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x331f0
leaq 0xb8(%rsp), %rdx
movq %rax, (%rdx)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r8
movaps %xmm0, (%r8)
andq $0x0, 0x10(%r8)
leaq 0x48(%rsp), %rcx
movq %r15, %rdi
movq %r13, %rsi
callq 0x34430
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
cmpq %rsi, %rdi
je 0x4845d
callq 0x314a0
movq %r14, 0x30(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x318c0
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %r12
subq %r13, %r12
sarq $0x3, %r12
leaq 0xe0(%rsp), %rbp
movq %r13, %r14
testq %r12, %r12
jle 0x48439
movq %r12, %rbx
shrq %rbx
leaq (%r14,%rbx,8), %r13
movq 0x8(%r15), %rdi
movq %r13, %rsi
callq 0x33190
testb %al, %al
je 0x48483
movq 0x8(%r15), %rdi
movq %rbp, %rsi
callq 0x32570
movq 0x38(%rsp), %rcx
movb %al, (%rcx)
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32eb0
leaq 0x8(%r13), %rcx
movq %rbx, %rdx
notq %rdx
addq %rdx, %r12
movq 0x8(%rsp), %rdx
cmpl 0x4(%rdx), %eax
cmovleq %rcx, %r13
cmovleq %rcx, %r14
cmovgq %rbx, %r12
jmp 0x483d4
xorl %eax, %eax
cmpq 0x10(%rsp), %r13
sete %al
movq -0x8(%r13,%rax,8), %rax
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
leaq 0xe0(%rsp), %rdi
callq 0x346b0
leaq 0x10(%rsp), %rdi
callq 0x31670
leaq 0x48(%rsp), %rdi
callq 0x310d0
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r15), %rsi
leaq 0x98(%rsp), %rdi
callq 0x35130
leaq 0x137d6(%rip), %rsi     # 0x5bc71
leaq 0x78(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x32d90
pushq $0x28
popq %rdi
callq 0x31e70
movq %rax, %r14
leaq 0x13b7b(%rip), %rsi     # 0x5c03a
leaq 0xc0(%rsp), %rdi
leaq 0x3(%rsp), %rdx
callq 0x31b30
movb $0x1, %bpl
leaq 0xc0(%rsp), %rsi
leaq 0x78(%rsp), %rdx
movq %r14, %rdi
callq 0x344c0
xorl %ebp, %ebp
movq 0x2ca46(%rip), %rsi     # 0x74f38
movq 0x2ca7f(%rip), %rdx     # 0x74f78
movq %r14, %rdi
callq 0x34c90
jmp 0x48570
pushq $0x28
popq %rdi
callq 0x31e70
movq %rax, %r14
leaq 0x13aea(%rip), %rsi     # 0x5bfff
leaq 0xe0(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x31b30
leaq 0x13aea(%rip), %rsi     # 0x5c01b
leaq 0x48(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x31b30
movb $0x1, %bpl
leaq 0xe0(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %r14, %rdi
callq 0x344c0
xorl %ebp, %ebp
movq 0x2c9d7(%rip), %rsi     # 0x74f38
movq 0x2ca10(%rip), %rdx     # 0x74f78
movq %r14, %rdi
callq 0x34c90
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x33a00
jmp 0x48585
movq %rax, %rbx
movb $0x1, %bpl
leaq 0xe0(%rsp), %rdi
callq 0x33a00
testb %bpl, %bpl
jne 0x485a8
jmp 0x4861f
jmp 0x485a0
jmp 0x485a0
movq %rax, %rbx
jmp 0x48615
movq %rax, %rbx
movq %r14, %rdi
callq 0x327c0
jmp 0x4861f
jmp 0x485b4
movq %rax, %rbx
jmp 0x4860b
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x33a00
testb %bpl, %bpl
jne 0x485d3
jmp 0x485db
movq %rax, %rbx
movq %r14, %rdi
callq 0x327c0
leaq 0x78(%rsp), %rdi
callq 0x33a00
jmp 0x485ea
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x33a00
jmp 0x485fe
jmp 0x485fb
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x346b0
leaq 0x10(%rsp), %rdi
callq 0x31670
leaq 0x48(%rsp), %rdi
callq 0x310d0
movq %rbx, %rdi
callq 0x34ed0
movq %rax, %rdi
callq 0x39b2f
nop