// File: 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:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq %rcx, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq 0x90(%rsp), %rdi
movq %rdi, (%rsp)
movq 0x88(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %rsi
xorl %edx, %edx
callq 0x73a20
leaq 0x58(%rsp), %rdi
callq 0x77560
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
movq 0x88(%rsp), %rcx
cmpl (%rcx), %eax
jge 0xb11d4
movq (%rsp), %rdi
leaq 0x64(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x76d70
movq 0x78(%rsp), %rdi
movl 0x64(%rsp), %eax
movw %ax, 0x46(%rsp)
leaq 0x46(%rsp), %rsi
callq 0x78810
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x78(%rsp), %rdi
callq 0x744d0
movq %rax, 0x38(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x76e90
testb $0x1, %al
jne 0xb10d6
jmp 0xb10db
jmp 0xb11c4
movq $0x0, 0x30(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x60(%rsp), %eax
jge 0xb1195
movq (%rsp), %rax
movq 0x60(%rax), %rcx
movq 0x30(%rsp), %rdx
movq (%rcx,%rdx), %rcx
movq %rcx, 0x28(%rsp)
movq 0x30(%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0x30(%rsp)
movq 0x60(%rax), %rcx
movq 0x30(%rsp), %rdx
movq (%rcx,%rdx), %rcx
movq %rcx, 0x20(%rsp)
movq 0x30(%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0x30(%rsp)
testb $0x1, 0x30(%rax)
je 0xb1158
leaq 0x28(%rsp), %rdi
callq 0x72ec0
leaq 0x20(%rsp), %rdi
callq 0x72ec0
movq 0x20(%rsp), %rax
movq 0x80(%rsp), %rcx
cmpq (%rcx), %rax
jb 0xb1183
movq 0x70(%rsp), %rdi
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x10(%rsp), %rsi
callq 0x781a0
jmp 0xb1185
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xb10ec
movq 0x78(%rsp), %rdi
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rsi
callq 0x74080
movq 0x78(%rsp), %rdi
callq 0x7bbd0
testb $0x1, %al
jne 0xb11be
jmp 0xb11c0
jmp 0xb11d4
jmp 0xb11c2
jmp 0xb11c4
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0xb1064
addq $0x98, %rsp
retq
nopl (%rax)