// File: zeeev[P]DASVC/bamtools/src/api/internal/index/BamStandardIndex_p.cpp
// Function: BamTools::Internal::BamStandardIndex::MergeAlignmentChunks(std::vector<BamTools::Internal::BaiAlignmentChunk, std::allocator<BamTools::Internal::BaiAlignmentChunk>>&)
void BamStandardIndex::MergeAlignmentChunks(BaiAlignmentChunkVector& chunks) {

    // skip if chunks are empty, nothing to merge
    if ( chunks.empty() )
        return;

    // set up merged alignment chunk container
    BaiAlignmentChunkVector mergedChunks;
    mergedChunks.push_back( chunks[0] );

    // iterate over chunks
    int i = 0;
    BaiAlignmentChunkVector::iterator chunkIter = chunks.begin();
    BaiAlignmentChunkVector::iterator chunkEnd  = chunks.end();
    for ( ++chunkIter; chunkIter != chunkEnd; ++chunkIter) {

        // get 'currentMergeChunk' based on numeric index
        BaiAlignmentChunk& currentMergeChunk = mergedChunks[i];

        // get sourceChunk based on source vector iterator
        BaiAlignmentChunk& sourceChunk = (*chunkIter);

        // if currentMergeChunk ends where sourceChunk starts, then merge the two
        if ( currentMergeChunk.Stop>>16 == sourceChunk.Start>>16 )
            currentMergeChunk.Stop = sourceChunk.Stop;

        // otherwise
        else {
            // append sourceChunk after currentMergeChunk
            mergedChunks.push_back(sourceChunk);

            // update i, so the next iteration will consider the
            // recently-appended sourceChunk as new mergeChunk candidate
            ++i;
        }
    }

    // saved newly-merged chunks into (parameter) chunks
    chunks = mergedChunks;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq 0x78(%rsp), %rdi
callq 0x76ed0
testb $0x1, %al
jne 0xb4784
jmp 0xb4789
jmp 0xb48f6
leaq 0x60(%rsp), %rdi
callq 0x72150
movq 0x78(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x763f0
movq %rax, 0x28(%rsp)
jmp 0xb47a8
movq 0x28(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x775d0
jmp 0xb47b9
movl $0x0, 0x50(%rsp)
movq 0x78(%rsp), %rdi
callq 0x77940
movq %rax, 0x20(%rsp)
jmp 0xb47d2
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x78(%rsp), %rdi
callq 0x75050
movq %rax, 0x18(%rsp)
jmp 0xb47ed
movq 0x18(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x7c390
jmp 0xb4803
jmp 0xb4805
leaq 0x48(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x760f0
movb %al, 0x17(%rsp)
jmp 0xb481a
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0xb4827
jmp 0xb48db
movslq 0x50(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x763f0
movq %rax, 0x8(%rsp)
jmp 0xb483d
movq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x72d10
movq %rax, (%rsp)
jmp 0xb4857
movq (%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
shrq $0x10, %rax
movq 0x30(%rsp), %rcx
movq (%rcx), %rcx
shrq $0x10, %rcx
cmpq %rcx, %rax
jne 0xb48ac
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x8(%rax)
jmp 0xb48c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x77ee0
jmp 0xb48fe
movq 0x30(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x775d0
jmp 0xb48bd
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0xb48ca
leaq 0x48(%rsp), %rdi
callq 0x7c390
jmp 0xb48d6
jmp 0xb4805
movq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x79160
jmp 0xb48ec
leaq 0x60(%rsp), %rdi
callq 0x77ee0
addq $0x88, %rsp
retq
jmp 0xb4900
movq 0x58(%rsp), %rdi
callq 0x7b550
movq %rax, %rdi
callq 0x88ef0
nopw %cs:(%rax,%rax)