// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, %r13
movq 0x8(%rsi), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x26a90
movq %rbp, 0x28(%rsp)
cmpl $0x0, (%rbp)
jle 0x3e2b9
leaq 0x8(%r12), %rbp
movq %r12, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x27860
movzwl 0xc(%rsp), %eax
movq 0x10(%r12), %rdx
movq %rbp, %rcx
testq %rdx, %rdx
je 0x3e1f0
movq %rbp, %rcx
xorl %esi, %esi
cmpw %ax, 0x20(%rdx)
setb %sil
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x3e1d8
movq %rbp, %rdx
cmpq %rbp, %rcx
je 0x3e203
cmpw %ax, 0x20(%rcx)
cmovaq %rbp, %rcx
movq %rcx, %rdx
cmpq %rbp, %rdx
je 0x3e2a8
movq %rdx, 0x30(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x3e282
xorl %r12d, %r12d
movl $0x8, %ebp
movq 0x60(%r13), %rcx
movq -0x8(%rcx,%rbp), %rdx
movq %rdx, %rax
bswapq %rax
movq (%rcx,%rbp), %rcx
movq %rcx, %rsi
bswapq %rsi
cmpb $0x0, 0x30(%r13)
cmoveq %rdx, %rax
cmoveq %rcx, %rsi
cmpq (%r15), %rsi
jb 0x3e274
movq %rax, 0x10(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x3e267
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x3e274
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
callq 0x28500
incl %r12d
addq $0x10, %rbp
cmpl 0x8(%rsp), %r12d
jl 0x3e220
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rsi
callq 0x26d70
movq %rax, %rdi
callq 0x27560
movq 0x20(%rsp), %r12
decq 0x28(%r12)
je 0x3e2b9
incl %r14d
movq 0x28(%rsp), %rax
cmpl (%rax), %r14d
jl 0x3e1b1
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq