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

    // return false ref ID is not a valid index in file summary data
    if ( region.LeftRefID < 0 || region.LeftRefID >= (int)m_indexFileSummary.size() )
        throw BamException("BamToolsIndex::GetOffset", "invalid region requested");

    // retrieve reference index data for left bound reference
    BtiReferenceEntry refEntry(region.LeftRefID);
    ReadReferenceEntry(refEntry);

    // binary search for an overlapping block (may not be first one though)
    bool found = false;
    typedef BtiBlockVector::const_iterator BtiBlockConstIterator;
    BtiBlockConstIterator blockFirst = refEntry.Blocks.begin();
    BtiBlockConstIterator blockIter  = blockFirst;
    BtiBlockConstIterator blockLast  = refEntry.Blocks.end();
    iterator_traits<BtiBlockConstIterator>::difference_type count = distance(blockFirst, blockLast);
    iterator_traits<BtiBlockConstIterator>::difference_type step;
    while ( count > 0 ) {
        blockIter = blockFirst;
        step = count/2;
        advance(blockIter, step);

        const BtiBlock& block = (*blockIter);
        if ( block.StartPosition <= region.RightPosition ) {
            if ( block.MaxEndPosition > region.LeftPosition ) {
                offset = block.StartOffset;
                break;
            }
            blockFirst = ++blockIter;
            count -= step+1;
        }
        else count = step;
    }

    // if we didn't search "off the end" of the blocks
    if ( blockIter != blockLast ) {

        // "walk back" until we've gone too far
        while ( blockIter != blockFirst ) {
            const BtiBlock& currentBlock = (*blockIter);

            --blockIter;
            const BtiBlock& previousBlock = (*blockIter);
            if ( previousBlock.MaxEndPosition <= region.LeftPosition ) {
                offset = currentBlock.StartOffset;
                found = true;
                break;
            }
        }

        // if we walked all the way to first block, just return that and let the reader's
        // region overlap parsing do the rest
        if ( blockIter == blockFirst ) {
            const BtiBlock& block = (*blockIter);
            offset = block.StartOffset;
            found = true;
        }
    }


    // sets to false if blocks container is empty, or if no matching block could be found
    *hasAlignmentsInRegion = found;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x40, %rsp
movl (%rsi), %eax
testl %eax, %eax
js 0x43f47
movq %rcx, %rbx
movq 0x40(%rdi), %rcx
subq 0x38(%rdi), %rcx
shrq $0x4, %rcx
cmpl %ecx, %eax
jge 0x43f47
movq %rdx, %r14
movq %rsi, %r15
movq %rsp, %rsi
movl %eax, (%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsi)
movq $0x0, 0x18(%rsi)
callq 0x28970
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
movq %rax, %rsi
subq %rdi, %rsi
movq %rdi, %rcx
movq %rdi, %rdx
testq %rsi, %rsi
jle 0x43ef4
shrq $0x3, %rsi
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %r10
movl 0x4(%r15), %esi
movl 0xc(%r15), %r8d
movq %rdi, %rcx
movq %r10, %r9
shrq %r9
leaq (%r9,%r9,2), %rdx
leaq (%rcx,%rdx,8), %rdx
cmpl %r8d, 0x10(%rdx)
jg 0x43ee3
cmpl %esi, (%rdx)
jg 0x43eed
addq $0x18, %rdx
notq %r9
addq %r10, %r9
movq %rdx, %rcx
movq %r9, %r10
testq %r9, %r9
jg 0x43ec1
jmp 0x43ef4
movq 0x8(%rdx), %rsi
movq %rsi, (%r14)
cmpq %rax, %rdx
je 0x43f1e
movl 0x4(%r15), %esi
cmpq %rcx, %rdx
je 0x43f22
leaq -0x18(%rdx), %rax
cmpl %esi, -0x18(%rdx)
movq %rax, %rdx
jg 0x43efd
movq 0x20(%rax), %rdx
movq %rdx, (%r14)
movb $0x1, %dl
cmpq %rcx, %rax
jne 0x43f2e
jmp 0x43f25
xorl %edx, %edx
jmp 0x43f2e
movq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, (%r14)
movb $0x1, %dl
movb %dl, (%rbx)
testq %rdi, %rdi
je 0x43f3a
callq 0x27560
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
callq 0x269d0
movq %rax, %rbx
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x12101(%rip), %rsi     # 0x56065
leaq 0x12112(%rip), %rdx     # 0x5607d
movq %rsp, %rdi
callq 0x26750
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x1273e(%rip), %rsi     # 0x566c2
leaq 0x1274f(%rip), %rdx     # 0x566da
leaq 0x20(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x28480
xorl %ebp, %ebp
movq 0x26f87(%rip), %rsi     # 0x6af38
movq 0x26fc0(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a20
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x43fd2
callq 0x27560
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x43fe0
callq 0x27560
testb %bpl, %bpl
jne 0x44011
jmp 0x44019
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x44011
callq 0x27560
jmp 0x44011
movq %rax, %r14
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x44019
callq 0x27560
jmp 0x44019
movq %rax, %r14
movq %rbx, %rdi
callq 0x27070
movq %r14, %rdi
callq 0x28c10
nop