// File: /workspace/github_cpp_makefile_star2_O1/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 0x3fee8
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 0x3fee8
cmpq %r13, %rdx
jbe 0x3ff6e
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 0x287b0
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 0x28400
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
cmpq %rbp, %r13
je 0x3fe26
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 0x28990
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 0x3fe02
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 0x3fe55
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 0x3fd98
cmpl %edx, %eax
cmovgq %rbp, %rcx
movq %rcx, %rbx
xorl %eax, %eax
cmpq 0x10(%rsp), %rbx
sete %al
movq -0x8(%rbx,%rax,8), %rax
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
leaq 0xe0(%rsp), %rdi
callq 0x28600
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3fe35
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 0x28e00
leaq 0x15dbf(%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 0x161c3(%rip), %rsi     # 0x56065
leaq 0x161d4(%rip), %rdx     # 0x5607d
leaq 0x78(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0x78(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
movq %r14, %rdi
callq 0x28490
xorl %ebp, %ebp
movq 0x2b064(%rip), %rsi     # 0x6af38
movq 0x2b09d(%rip), %rdx     # 0x6af78
movq %r14, %rdi
callq 0x28a30
jmp 0x3ff6e
movl $0x28, %edi
callq 0x269d0
movq %rax, %r14
leaq 0xf0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x16122(%rip), %rsi     # 0x5602a
leaq 0x16136(%rip), %rdx     # 0x56045
leaq 0xe0(%rsp), %rdi
callq 0x26750
leaq 0x58(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x16119(%rip), %rsi     # 0x56046
leaq 0x16130(%rip), %rdx     # 0x56064
leaq 0x48(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
leaq 0xe0(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %r14, %rdi
callq 0x28490
xorl %ebp, %ebp
movq 0x2afd9(%rip), %rsi     # 0x6af38
movq 0x2b012(%rip), %rdx     # 0x6af78
movq %r14, %rdi
callq 0x28a30
leaq 0x1532d(%rip), %rdi     # 0x552a2
movq %r13, %rsi
xorl %eax, %eax
callq 0x284f0
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x3ff99
callq 0x27560
jmp 0x3ff99
movq %rax, %rbx
movb $0x1, %bpl
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0x3ffbd
callq 0x27560
jmp 0x3ffbd
jmp 0x3ffaf
movq %rax, %rbx
jmp 0x40055
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x40063
movq %r14, %rdi
callq 0x27070
jmp 0x40063
jmp 0x40014
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x3ffef
callq 0x27560
jmp 0x3ffef
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x3fffc
movq %r14, %rdi
callq 0x27070
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4001c
callq 0x27560
jmp 0x4001c
movq %rax, %rbx
jmp 0x40046
movq %rax, %rbx
leaq 0xd0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x40039
callq 0x27560
jmp 0x40039
jmp 0x40036
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x28600
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x40055
callq 0x27560
leaq 0x48(%rsp), %rdi
movq 0x10(%rdi), %rsi
callq 0x26440
movq %rbx, %rdi
callq 0x28c20
movq %rax, %rdi
callq 0x2f0bd
nop