// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdx, %r15
movq %rdi, %r13
movq %rsi, 0x28(%rsp)
movq 0x8(%rsi), %rax
leaq 0x10(%rsp), %rbp
movq %rax, (%rbp)
xorl %r12d, %r12d
movq %rbp, %rsi
xorl %edx, %edx
callq 0x31f60
leaq 0x8(%r14), %rax
movq %rax, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movq 0x28(%rsp), %rax
cmpl (%rax), %r12d
jge 0x46e4a
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x33330
movzwl 0xc(%rsp), %eax
movw %ax, 0x10(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x32910
movq %rax, 0x30(%rsp)
cmpq 0x20(%rsp), %rax
je 0x46e42
movl %r12d, 0x4(%rsp)
xorl %r14d, %r14d
pushq $0x8
popq %r12
cmpl 0x8(%rsp), %r14d
jge 0x46e24
movq 0x60(%r13), %rcx
movq (%rcx,%r12), %rdx
movb 0x30(%r13), %al
movq %rdx, %rsi
bswapq %rsi
testb %al, %al
cmoveq %rdx, %rsi
cmpq (%r15), %rsi
jb 0x46e1b
movq -0x8(%rcx,%r12), %rcx
movq %rcx, %rdx
bswapq %rdx
testb %al, %al
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x33a60
incl %r14d
addq $0x10, %r12
jmp 0x46dd6
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq 0x30(%rsp), %rsi
callq 0x32110
cmpq $0x0, 0x28(%r14)
movl 0x4(%rsp), %r12d
je 0x46e4a
incl %r12d
jmp 0x46d89
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop