// File: /workspace/github_cpp_makefile_star2_O1/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 0x43fb0
movq %rcx, %rbx
movq 0x40(%rdi), %rcx
subq 0x38(%rdi), %rcx
shrq $0x4, %rcx
cmpl %ecx, %eax
jge 0x43fb0
movq %rdx, %r14
movq %rsi, %r15
movq %rsp, %rsi
movl %eax, (%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsi)
movq $0x0, 0x18(%rsi)
callq 0x28980
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rdx
movq %rdx, %rsi
subq %rdi, %rsi
movq %rdi, %rax
movq %rdi, %rcx
testq %rsi, %rsi
jle 0x43f57
shrq $0x3, %rsi
movabsq $-0x5555555555555555, %r8 # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %r8
movl 0x4(%r15), %esi
movl 0xc(%r15), %r9d
movq %rdi, %rcx
movq %r8, %r11
shrq %r11
leaq (%r11,%r11,2), %rax
leaq (%rcx,%rax,8), %rax
movb $0x1, %r10b
cmpl %r9d, 0x10(%rax)
jle 0x43f30
movq %r11, %r8
jmp 0x43f4d
cmpl %esi, (%rax)
jle 0x43f40
movq 0x8(%rax), %r10
movq %r10, (%r14)
xorl %r10d, %r10d
jmp 0x43f4d
addq $0x18, %rax
notq %r11
addq %r11, %r8
movq %rax, %rcx
testb %r10b, %r10b
je 0x43f57
testq %r8, %r8
jg 0x43f14
cmpq %rdx, %rax
je 0x43f92
movl 0x4(%r15), %esi
xorl %edx, %edx
cmpq %rcx, %rax
je 0x43f82
movl -0x18(%rax), %r8d
cmpl %esi, %r8d
jg 0x43f79
movq 0x8(%rax), %rdx
movq %rdx, (%r14)
movb $0x1, %dl
addq $-0x18, %rax
cmpl %esi, %r8d
jg 0x43f62
cmpq %rcx, %rax
jne 0x43f94
movq 0x8(%rax), %rax
movq %rax, (%r14)
movb $0x1, %dl
jmp 0x43f94
xorl %edx, %edx
andb $0x1, %dl
movb %dl, (%rbx)
testq %rdi, %rdi
je 0x43fa3
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 0x12098(%rip), %rsi     # 0x56065
leaq 0x120a9(%rip), %rdx     # 0x5607d
movq %rsp, %rdi
callq 0x26750
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x126d5(%rip), %rsi     # 0x566c2
leaq 0x126e6(%rip), %rdx     # 0x566da
leaq 0x20(%rsp), %rdi
callq 0x26750
movb $0x1, %bpl
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x28490
xorl %ebp, %ebp
movq 0x26f1e(%rip), %rsi     # 0x6af38
movq 0x26f57(%rip), %rdx     # 0x6af78
movq %rbx, %rdi
callq 0x28a30
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x44043
callq 0x27560
jmp 0x44043
movq %rax, %r14
movb $0x1, %bpl
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x4406d
callq 0x27560
jmp 0x4406d
movq %rax, %r14
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x4407a
callq 0x27560
jmp 0x4407a
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x4407a
movq %rbx, %rdi
callq 0x27070
movq %r14, %rdi
callq 0x28c20