// File: /workspace/github_cpp_makefile_star2_O3/zeeev[P]DASVC/bamtools/src/api/internal/index/BamStandardIndex_p.cpp
// Function: BamTools::Internal::BamStandardIndex::CalculateCandidateOffsets(BamTools::Internal::BaiReferenceSummary const&, unsigned long const&, std::set<unsigned short, std::less<unsigned short>, std::allocator<unsigned short>>&, std::vector<long, std::allocator<long>>&)
void BamStandardIndex::CalculateCandidateOffsets(const BaiReferenceSummary& refSummary,
                                                 const uint64_t& minOffset,
                                                 set<uint16_t>& candidateBins,
                                                 vector<int64_t>& offsets)
{
    // seek to first bin
    Seek(refSummary.FirstBinFilePosition, SEEK_SET);

    // iterate over reference bins
    uint32_t binId;
    int32_t numAlignmentChunks;
    set<uint16_t>::iterator candidateBinIter;
    for ( int i = 0; i < refSummary.NumBins; ++i ) {

        // read bin contents (if successful, alignment chunks are now in m_buffer)
        ReadBinIntoBuffer(binId, numAlignmentChunks);

        // see if bin is a 'candidate bin'
        candidateBinIter = candidateBins.find(binId);

        // if not, move on to next bin
        if ( candidateBinIter == candidateBins.end() )
            continue;

        // otherwise, check bin's contents against for overlap
        else {

            size_t offset = 0;
            uint64_t chunkStart;
            uint64_t chunkStop;

            // iterate over alignment chunks
            for ( int j = 0; j < numAlignmentChunks; ++j ) {

                // read chunk start & stop from buffer
                memcpy((char*)&chunkStart, m_resources.Buffer+offset, sizeof(uint64_t));
                offset += sizeof(uint64_t);
                memcpy((char*)&chunkStop, m_resources.Buffer+offset, sizeof(uint64_t));
                offset += sizeof(uint64_t);

                // swap endian-ness if necessary
                if ( m_isBigEndian ) {
                    SwapEndian_64(chunkStart);
                    SwapEndian_64(chunkStop);
                }

                // store alignment chunk's start offset
                // if its stop offset is larger than our 'minOffset'
                if ( chunkStop >= minOffset )
                    offsets.push_back(chunkStart);
            }

            // 'pop' bin ID from candidate bins set
            candidateBins.erase(candidateBinIter);

            // quit if no more candidates
            if ( candidateBins.empty() )
                break;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, 0x18(%rsp)
movq %rsi, %r12
movq %rdi, %r13
movq 0x8(%rsi), %rax
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x26a90
cmpl $0x0, (%r12)
jle 0x3e237
leaq 0x8(%r15), %rax
movq %rax, 0x8(%rsp)
movq %r15, 0x30(%rsp)
movq %r12, 0x28(%rsp)
movq %r13, %rdi
leaq 0x14(%rsp), %rsi
leaq 0x4(%rsp), %rdx
callq 0x27860
movq 0x10(%r15), %rcx
testq %rcx, %rcx
je 0x3e22a
movzwl 0x14(%rsp), %eax
movq 0x8(%rsp), %rbp
xorl %edx, %edx
cmpw %ax, 0x20(%rcx)
setb %dl
cmovaeq %rcx, %rbp
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x3e154
cmpq 0x8(%rsp), %rbp
je 0x3e22a
cmpw %ax, 0x20(%rbp)
cmovaq 0x8(%rsp), %rbp
ja 0x3e22a
movl 0x4(%rsp), %eax
testl %eax, %eax
movq 0x18(%rsp), %r8
jle 0x3e205
xorl %r12d, %r12d
movl $0x8, %r15d
movq 0x60(%r13), %rdx
movq -0x8(%rdx,%r15), %rsi
movq %rsi, %rcx
bswapq %rcx
movq (%rdx,%r15), %rdx
movq %rdx, %rdi
bswapq %rdi
cmpb $0x0, 0x30(%r13)
cmoveq %rsi, %rcx
cmoveq %rdx, %rdi
cmpq (%r8), %rdi
jb 0x3e1f9
movq %rcx, 0x20(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x3e1e3
movq %rcx, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x3e1f9
movq %rbx, %rdi
leaq 0x20(%rsp), %rdx
callq 0x284f0
movq 0x18(%rsp), %r8
movl 0x4(%rsp), %eax
incl %r12d
addq $0x10, %r15
cmpl %eax, %r12d
jl 0x3e19c
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0x26d70
movq %rax, %rdi
callq 0x27560
movq 0x30(%rsp), %r15
decq 0x28(%r15)
movq 0x28(%rsp), %r12
je 0x3e237
incl %r14d
cmpl (%r12), %r14d
jl 0x3e12b
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq