// File: /workspace/github_cpp_makefile_star2_O2/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 $0x50, %rsp
movl (%rsi), %eax
testl %eax, %eax
js 0x4c089
movq %rcx, %rbx
movq 0x40(%rdi), %rcx
subq 0x38(%rdi), %rcx
shrq $0x4, %rcx
cmpl %ecx, %eax
jge 0x4c089
movq %rdx, %r14
movq %rsi, %r12
leaq 0x18(%rsp), %r15
movl %eax, -0x8(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
leaq 0x10(%rsp), %rsi
callq 0x34bb0
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %rsi
movq %rsi, %rax
subq %rcx, %rax
pushq $0x18
popq %rdi
cqto
idivq %rdi
movl 0x4(%r12), %edi
movl 0xc(%r12), %r8d
movq %rcx, %rdx
testq %rax, %rax
jle 0x4c035
movq %rax, %r9
shrq %rax
imulq $0x18, %rax, %r10
leaq (%rcx,%r10), %rdx
cmpl %r8d, 0x10(%rcx,%r10)
jg 0x4c001
cmpl %edi, (%rdx)
jg 0x4c02e
addq $0x18, %rdx
notq %rax
addq %r9, %rax
movq %rdx, %rcx
jmp 0x4c001
movq 0x8(%rdx), %rax
movq %rax, (%r14)
cmpq %rsi, %rdx
je 0x4c05e
movl 0x4(%r12), %eax
cmpq %rcx, %rdx
je 0x4c062
leaq -0x18(%rdx), %rsi
cmpl %eax, -0x18(%rdx)
movq %rsi, %rdx
jg 0x4c03f
movq 0x20(%rsi), %rax
movq %rax, (%r14)
movb $0x1, %al
movq %rsi, %rdx
jmp 0x4c064
xorl %eax, %eax
jmp 0x4c072
xorl %eax, %eax
cmpq %rcx, %rdx
jne 0x4c072
movq 0x8(%rdx), %rax
movq %rax, (%r14)
movb $0x1, %al
movb %al, (%rbx)
movq %r15, %rdi
callq 0x33fb0
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x28
popq %rdi
callq 0x31e70
movq %rax, %rbx
leaq 0xff9f(%rip), %rsi      # 0x5c03a
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x31b30
leaq 0x10602(%rip), %rsi     # 0x5c6b3
leaq 0x30(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x31b30
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x344c0
xorl %ebp, %ebp
movq 0x28e5a(%rip), %rsi     # 0x74f38
movq 0x28e93(%rip), %rdx     # 0x74f78
movq %rbx, %rdi
callq 0x34c90
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x33a00
jmp 0x4c102
movq %rax, %r14
movb $0x1, %bpl
leaq 0x10(%rsp), %rdi
callq 0x33a00
testb %bpl, %bpl
jne 0x4c123
jmp 0x4c12b
movq %rax, %r14
movq %r15, %rdi
callq 0x33fb0
jmp 0x4c12b
movq %rax, %r14
movq %rbx, %rdi
callq 0x327c0
movq %r14, %rdi
callq 0x34ed0
movq %rax, %rdi
callq 0x39b2f
nop