// File: /workspace/github_cpp_makefile_star2_O3/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), %r13
testq %r13, %r13
js 0x3fe8e
movq %rdx, %r12
movq %rdi, %r15
movq 0x38(%rdi), %r14
movq 0x40(%rdi), %rax
subq %r14, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rdx
cmpl %edx, %r13d
jge 0x3fe8e
cmpq %r13, %rdx
jbe 0x3ff14
leaq 0x4(%rsp), %rbp
leaq 0x44(%rsp), %rbx
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x26e20
leaq 0x50(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0x48(%rsp), %rcx
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x287a0
leaq (,%r13,2), %rax
addq %r13, %rax
leaq (%r14,%rax,8), %r13
leaq 0x4(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x27760
leaq 0x98(%rsp), %rdx
movq %rax, (%rdx)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r8
movaps %xmm0, (%r8)
movq $0x0, 0x10(%r8)
leaq 0x48(%rsp), %rcx
movq %r15, %rdi
movq %r13, %rsi
callq 0x283f0
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
cmpq %rbp, %r13
je 0x3fdce
movq %rbp, %rax
subq %r13, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x27410
movq %r13, %rdi
movq %rbp, %rsi
callq 0x28980
movq %r12, 0x30(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x265e0
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r12
subq %rbx, %r12
sarq $0x3, %r12
testq %r12, %r12
jle 0x3fda4
leaq 0xe0(%rsp), %r13
movq %r12, %r14
shrq %r14
leaq (%rbx,%r14,8), %rbp
movq 0x8(%r15), %rdi
movq %rbp, %rsi
callq 0x27710
testb %al, %al
je 0x3fdfb
movq 0x8(%r15), %rdi
movq %r13, %rsi
callq 0x26ea0
movq 0x38(%rsp), %rcx
movb %al, (%rcx)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x274a0
movq 0x8(%rsp), %rcx
movl 0x4(%rcx), %edx
leaq 0x8(%rbp), %rcx
movq %r14, %rsi
notq %rsi
addq %rsi, %r12
cmpl %edx, %eax
cmovleq %rcx, %rbx
cmovgq %r14, %r12
testq %r12, %r12
jg 0x3fd36
cmpl %edx, %eax
cmovgq %rbp, %rcx
movq 0x10(%rsp), %rbx
jmp 0x3fda7
movq %rbx, %rcx
xorl %eax, %eax
cmpq %rbx, %rcx
sete %al
movq -0x8(%rcx,%rax,8), %rax
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
leaq 0xe0(%rsp), %rdi
callq 0x285f0
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0x3fddb
movq %r13, %rdi
callq 0x27560
leaq 0x48(%rsp), %rdi
movq 0x10(%rdi), %rsi
callq 0x26440
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r15), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x28df0
leaq 0x15e19(%rip), %rsi     # 0x55c2c
leaq 0xa0(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
callq 0x273f0
movl $0x28, %edi
callq 0x269d0
movq %rax, %r14
leaq 0x88(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1621d(%rip), %rsi     # 0x56065
leaq 0x1622e(%rip), %rdx     # 0x5607d
leaq 0x78(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0x78(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
movq %r14, %rdi
callq 0x28480
xorl %ebp, %ebp
movq 0x2b0be(%rip), %rsi     # 0x6af38
movq 0x2b0f7(%rip), %rdx     # 0x6af78
movq %r14, %rdi
callq 0x28a20
jmp 0x3ff14
movl $0x28, %edi
callq 0x269d0
movq %rax, %r14
leaq 0xf0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1617c(%rip), %rsi     # 0x5602a
leaq 0x16190(%rip), %rdx     # 0x56045
leaq 0xe0(%rsp), %rdi
callq 0x26750
leaq 0x58(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x16173(%rip), %rsi     # 0x56046
leaq 0x1618a(%rip), %rdx     # 0x56064
leaq 0x48(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0xe0(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %r14, %rdi
callq 0x28480
xorl %ebp, %ebp
movq 0x2b033(%rip), %rsi     # 0x6af38
movq 0x2b06c(%rip), %rdx     # 0x6af78
movq %r14, %rdi
callq 0x28a20
leaq 0x15387(%rip), %rdi     # 0x552a2
movq %r13, %rsi
xorl %eax, %eax
callq 0x284e0
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x3ff37
callq 0x27560
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0x3ff49
callq 0x27560
testb %bpl, %bpl
jne 0x3ff77
jmp 0x40011
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0x3ff77
callq 0x27560
jmp 0x3ff77
jmp 0x3ff6c
movq %rax, %rbx
jmp 0x40003
movq %rax, %rbx
movq %r14, %rdi
callq 0x27070
jmp 0x40011
jmp 0x3ffc2
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x3ff98
callq 0x27560
testb %bpl, %bpl
jne 0x3ffa2
jmp 0x3ffaa
movq %rax, %rbx
movq %r14, %rdi
callq 0x27070
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3ffca
callq 0x27560
jmp 0x3ffca
movq %rax, %rbx
jmp 0x3fff4
movq %rax, %rbx
leaq 0xd0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3ffe7
callq 0x27560
jmp 0x3ffe7
jmp 0x3ffe4
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x285f0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x40003
callq 0x27560
leaq 0x48(%rsp), %rdi
movq 0x10(%rdi), %rsi
callq 0x26440
movq %rbx, %rdi
callq 0x28c10
movq %rax, %rdi
callq 0x2f047
nop