// File: zeeev[P]DASVC/src/annotate.cpp
// Function: chainBlock(std::vector<BamTools::BamAlignment, std::allocator<BamTools::BamAlignment>>&, BamTools::BamWriter&, std::vector<BamTools::RefData, std::allocator<BamTools::RefData>> const&)
int chainBlock(std::vector<BamAlignment> & reads,
               BamWriter & br,
                const RefVector & references){

    if(reads.size() == 0){
        return 1;
    }

    chain qChain;

    for(std::vector<BamAlignment>::iterator it = reads.begin();
        it != reads.end(); it++){

        int match ;
        int qStart;
        int qEnd  ;
        int qLen  ;

        it->GetTag<int>("QL", qLen   );
        it->GetTag<int>("MB", match  );
        it->GetTag<int>("QS", qStart );
        it->GetTag<int>("QE", qEnd   );

        //        br.SaveAlignment(*it);

        qChain.addAlignment(qStart, qEnd, match);

    }

 std::vector<int> indiciesOfAlignments;
 qChain.buildLinks();
 qChain.traceback(indiciesOfAlignments);

 std::cerr << " INFO: n alignments before chaining :"
           << reads.size() << " and after : "
           << indiciesOfAlignments.size()
           << " for " << reads.front().Name << std::endl;


 int totalMatchingBases = 0;

 for(std::vector<int>::iterator it = indiciesOfAlignments.begin();
     it != indiciesOfAlignments.end(); it++){
     int mb = 0;
     reads[*it].GetTag<int>("MB", mb);
     totalMatchingBases += mb;
 }

 sort(indiciesOfAlignments.begin(), indiciesOfAlignments.end());

 int index = 1;

 for(std::vector<int>::iterator it = indiciesOfAlignments.begin();
     it != indiciesOfAlignments.end(); it++){
     reads[*it].AddTag<int>("TM", "i", totalMatchingBases);
     reads[*it].AddTag<int>("AI", "i", index);
     index++;

     br.SaveAlignment(reads[*it]);
 }

 int qs;
 int qe;

 reads.front().GetTag<int>("QS", qs);
 reads.back().GetTag<int>("QE",  qe);

 std::cerr << " ANNOTATION: T_S_E : Q_S_E : "
           << references[reads.front().RefID].RefName
           << "_"
           << reads.front().Position
           << "_"
           << reads.back().GetEndPosition()
           << " : "
           << reads.front().Name
           << "_"
           << qs
           << "_"
           << qe << std::endl;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x420, %rsp            # imm = 0x420
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x11620
cmpq $0x0, %rax
jne 0xe9e2
movl $0x1, -0x4(%rbp)
jmp 0xf66e
leaq -0x48(%rbp), %rdi
callq 0x1b260
movq -0x10(%rbp), %rdi
callq 0x11650
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x116c0
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x11680
testb $0x1, %al
jne 0xea1b
jmp 0xed2b
leaq -0x50(%rbp), %rdi
callq 0x116f0
movq %rax, -0x2c0(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
callq 0x6a20
movq -0x2b8(%rbp), %rdx
leaq 0x7168f(%rip), %rsi     # 0x800db
leaq -0x88(%rbp), %rdi
callq 0x11570
jmp 0xea5a
movq -0x2c0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x111c0
jmp 0xea73
leaq -0x88(%rbp), %rdi
callq 0x6ba8
leaq -0x89(%rbp), %rdi
callq 0x6600
leaq -0x50(%rbp), %rdi
callq 0x116f0
movq %rax, -0x2d0(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0x6a20
movq -0x2c8(%rbp), %rdx
leaq 0x71622(%rip), %rsi     # 0x800de
leaq -0xc0(%rbp), %rdi
callq 0x11570
jmp 0xeaca
movq -0x2d0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
callq 0x111c0
jmp 0xeae3
leaq -0xc0(%rbp), %rdi
callq 0x6ba8
leaq -0xc1(%rbp), %rdi
callq 0x6600
leaq -0x50(%rbp), %rdi
callq 0x116f0
movq %rax, -0x2e0(%rbp)
leaq -0xe9(%rbp), %rdi
movq %rdi, -0x2d8(%rbp)
callq 0x6a20
movq -0x2d8(%rbp), %rdx
leaq 0x715ac(%rip), %rsi     # 0x800d8
leaq -0xe8(%rbp), %rdi
callq 0x11570
jmp 0xeb3a
movq -0x2e0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x111c0
jmp 0xeb53
leaq -0xe8(%rbp), %rdi
callq 0x6ba8
leaq -0xe9(%rbp), %rdi
callq 0x6600
leaq -0x50(%rbp), %rdi
callq 0x116f0
movq %rax, -0x2f0(%rbp)
leaq -0x111(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
callq 0x6a20
movq -0x2e8(%rbp), %rdx
leaq 0x71545(%rip), %rsi     # 0x800e1
leaq -0x110(%rbp), %rdi
callq 0x11570
jmp 0xebaa
movq -0x2f0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
leaq -0x64(%rbp), %rdx
callq 0x111c0
jmp 0xebc3
leaq -0x110(%rbp), %rdi
callq 0x6ba8
leaq -0x111(%rbp), %rdi
callq 0x6600
movl -0x60(%rbp), %esi
movl -0x64(%rbp), %edx
movl -0x5c(%rbp), %ecx
leaq -0x48(%rbp), %rdi
callq 0x1b190
jmp 0xebef
jmp 0xebf1
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x11710
movq %rax, -0x120(%rbp)
jmp 0xe9f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xec3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x6ba8
leaq -0x89(%rbp), %rdi
callq 0x6600
jmp 0xf663
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xec7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x6ba8
leaq -0xc1(%rbp), %rdi
callq 0x6600
jmp 0xf663
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xecc0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x6ba8
leaq -0xe9(%rbp), %rdi
callq 0x6600
jmp 0xf663
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xed03
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x6ba8
leaq -0x111(%rbp), %rdi
callq 0x6600
jmp 0xf663
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xf663
leaq -0x138(%rbp), %rdi
callq 0x11750
leaq -0x48(%rbp), %rdi
callq 0x1b370
jmp 0xed42
leaq -0x48(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x1b550
jmp 0xed54
movq 0x9f295(%rip), %rdi     # 0xadff0
leaq 0x71382(%rip), %rsi     # 0x800e4
callq 0x6560
movq %rax, -0x2f8(%rbp)
jmp 0xed70
movq -0x10(%rbp), %rdi
callq 0x11620
movq -0x2f8(%rbp), %rdi
movq %rax, %rsi
callq 0x6130
movq %rax, -0x300(%rbp)
jmp 0xed91
movq -0x300(%rbp), %rdi
leaq 0x7136b(%rip), %rsi     # 0x8010a
callq 0x6560
movq %rax, -0x308(%rbp)
jmp 0xedad
leaq -0x138(%rbp), %rdi
callq 0x11770
movq -0x308(%rbp), %rdi
movq %rax, %rsi
callq 0x6130
movq %rax, -0x310(%rbp)
jmp 0xedd1
movq -0x310(%rbp), %rdi
leaq 0x7368d(%rip), %rsi     # 0x8246c
callq 0x6560
movq %rax, -0x318(%rbp)
jmp 0xeded
movq -0x10(%rbp), %rdi
callq 0x11790
movq -0x318(%rbp), %rdi
movq %rax, %rsi
callq 0x64c0
movq %rax, -0x320(%rbp)
jmp 0xee0e
movq -0x320(%rbp), %rdi
movq 0x9f18c(%rip), %rsi     # 0xadfa8
callq 0x65c0
jmp 0xee23
movl $0x0, -0x13c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x117c0
movq %rax, -0x148(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x11830
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x117f0
testb $0x1, %al
jne 0xee6f
jmp 0xef8f
movl $0x0, -0x154(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x338(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x11880
movq -0x338(%rbp), %rdi
movslq (%rax), %rsi
callq 0x11860
movq %rax, -0x330(%rbp)
leaq -0x179(%rbp), %rdi
movq %rdi, -0x328(%rbp)
callq 0x6a20
movq -0x328(%rbp), %rdx
leaq 0x71217(%rip), %rsi     # 0x800de
leaq -0x178(%rbp), %rdi
callq 0x11570
jmp 0xeed5
movq -0x330(%rbp), %rdi
leaq -0x178(%rbp), %rsi
leaq -0x154(%rbp), %rdx
callq 0x111c0
jmp 0xeef1
leaq -0x178(%rbp), %rdi
callq 0x6ba8
leaq -0x179(%rbp), %rdi
callq 0x6600
movl -0x154(%rbp), %eax
addl -0x13c(%rbp), %eax
movl %eax, -0x13c(%rbp)
leaq -0x148(%rbp), %rdi
xorl %esi, %esi
callq 0x118a0
movq %rax, -0x188(%rbp)
jmp 0xee40
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xf657
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xef7e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x6ba8
leaq -0x179(%rbp), %rdi
callq 0x6600
jmp 0xf657
leaq -0x138(%rbp), %rdi
movq %rdi, -0x340(%rbp)
callq 0x117c0
movq -0x340(%rbp), %rdi
movq %rax, -0x190(%rbp)
callq 0x11830
movq %rax, -0x198(%rbp)
movq -0x190(%rbp), %rdi
movq -0x198(%rbp), %rsi
callq 0x118e0
jmp 0xefd1
movl $0x1, -0x19c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x117c0
movq %rax, -0x1a8(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x11830
movq %rax, -0x1b0(%rbp)
leaq -0x1a8(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x117f0
testb $0x1, %al
jne 0xf01d
jmp 0xf323
movq -0x10(%rbp), %rax
movq %rax, -0x358(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x11880
movq -0x358(%rbp), %rdi
movslq (%rax), %rsi
callq 0x11860
movq %rax, -0x350(%rbp)
leaq -0x1d1(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0x6a20
movq -0x348(%rbp), %rdx
leaq 0x710ad(%rip), %rsi     # 0x80118
leaq -0x1d0(%rbp), %rdi
callq 0x11570
jmp 0xf079
leaq -0x1f9(%rbp), %rdi
movq %rdi, -0x360(%rbp)
callq 0x6a20
movq -0x360(%rbp), %rdx
leaq 0x728dc(%rip), %rsi     # 0x81976
leaq -0x1f8(%rbp), %rdi
callq 0x11570
jmp 0xf0a8
movq -0x350(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
leaq -0x1f8(%rbp), %rdx
leaq -0x13c(%rbp), %rcx
callq 0x11920
jmp 0xf0cb
leaq -0x1f8(%rbp), %rdi
callq 0x6ba8
leaq -0x1f9(%rbp), %rdi
callq 0x6600
leaq -0x1d0(%rbp), %rdi
callq 0x6ba8
leaq -0x1d1(%rbp), %rdi
callq 0x6600
movq -0x10(%rbp), %rax
movq %rax, -0x378(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x11880
movq -0x378(%rbp), %rdi
movslq (%rax), %rsi
callq 0x11860
movq %rax, -0x370(%rbp)
leaq -0x221(%rbp), %rdi
movq %rdi, -0x368(%rbp)
callq 0x6a20
movq -0x368(%rbp), %rdx
leaq 0x70fd2(%rip), %rsi     # 0x8011b
leaq -0x220(%rbp), %rdi
callq 0x11570
jmp 0xf157
leaq -0x249(%rbp), %rdi
movq %rdi, -0x380(%rbp)
callq 0x6a20
movq -0x380(%rbp), %rdx
leaq 0x727fe(%rip), %rsi     # 0x81976
leaq -0x248(%rbp), %rdi
callq 0x11570
jmp 0xf186
movq -0x370(%rbp), %rdi
leaq -0x220(%rbp), %rsi
leaq -0x248(%rbp), %rdx
leaq -0x19c(%rbp), %rcx
callq 0x11920
jmp 0xf1a9
leaq -0x248(%rbp), %rdi
callq 0x6ba8
leaq -0x249(%rbp), %rdi
callq 0x6600
leaq -0x220(%rbp), %rdi
callq 0x6ba8
leaq -0x221(%rbp), %rdi
callq 0x6600
movl -0x19c(%rbp), %eax
incl %eax
movl %eax, -0x19c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x388(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x390(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x11880
movq -0x390(%rbp), %rdi
movslq (%rax), %rsi
callq 0x11860
movq -0x388(%rbp), %rdi
movq %rax, %rsi
callq 0x2fd20
jmp 0xf229
jmp 0xf22b
leaq -0x1a8(%rbp), %rdi
xorl %esi, %esi
callq 0x118a0
movq %rax, -0x258(%rbp)
jmp 0xefee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xf2a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xf28b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x6ba8
leaq -0x1f9(%rbp), %rdi
callq 0x6600
leaq -0x1d0(%rbp), %rdi
callq 0x6ba8
leaq -0x1d1(%rbp), %rdi
callq 0x6600
jmp 0xf657
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xf312
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xf2fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x6ba8
leaq -0x249(%rbp), %rdi
callq 0x6600
leaq -0x220(%rbp), %rdi
callq 0x6ba8
leaq -0x221(%rbp), %rdi
callq 0x6600
jmp 0xf657
movq -0x10(%rbp), %rdi
callq 0x11790
movq %rax, -0x3a0(%rbp)
leaq -0x281(%rbp), %rdi
movq %rdi, -0x398(%rbp)
callq 0x6a20
movq -0x398(%rbp), %rdx
leaq 0x70d84(%rip), %rsi     # 0x800d8
leaq -0x280(%rbp), %rdi
callq 0x11570
jmp 0xf362
movq -0x3a0(%rbp), %rdi
leaq -0x280(%rbp), %rsi
leaq -0x25c(%rbp), %rdx
callq 0x111c0
jmp 0xf37e
leaq -0x280(%rbp), %rdi
callq 0x6ba8
leaq -0x281(%rbp), %rdi
callq 0x6600
movq -0x10(%rbp), %rdi
callq 0x11b80
movq %rax, -0x3b0(%rbp)
leaq -0x2a9(%rbp), %rdi
movq %rdi, -0x3a8(%rbp)
callq 0x6a20
movq -0x3a8(%rbp), %rdx
leaq 0x70d1a(%rip), %rsi     # 0x800e1
leaq -0x2a8(%rbp), %rdi
callq 0x11570
jmp 0xf3d5
movq -0x3b0(%rbp), %rdi
leaq -0x2a8(%rbp), %rsi
leaq -0x260(%rbp), %rdx
callq 0x111c0
jmp 0xf3f1
leaq -0x2a8(%rbp), %rdi
callq 0x6ba8
leaq -0x2a9(%rbp), %rdi
callq 0x6600
movq 0x9ebe0(%rip), %rdi     # 0xadff0
leaq 0x70d07(%rip), %rsi     # 0x8011e
callq 0x6560
movq %rax, -0x3b8(%rbp)
jmp 0xf425
movq -0x20(%rbp), %rax
movq %rax, -0x3c8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x11790
movq -0x3c8(%rbp), %rdi
movslq 0xa8(%rax), %rsi
callq 0x11bc0
movq -0x3b8(%rbp), %rdi
movq %rax, %rsi
callq 0x64c0
movq %rax, -0x3c0(%rbp)
jmp 0xf464
movq -0x3c0(%rbp), %rdi
leaq 0x70cca(%rip), %rsi     # 0x8013c
callq 0x6560
movq %rax, -0x3d0(%rbp)
jmp 0xf480
movq -0x10(%rbp), %rdi
callq 0x11790
movq -0x3d0(%rbp), %rdi
movl 0xac(%rax), %esi
callq 0x6a00
movq %rax, -0x3d8(%rbp)
jmp 0xf4a4
movq -0x3d8(%rbp), %rdi
leaq 0x70c8a(%rip), %rsi     # 0x8013c
callq 0x6560
movq %rax, -0x3e0(%rbp)
jmp 0xf4c0
movq -0x10(%rbp), %rdi
callq 0x11b80
movq %rax, %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x2cdf0
movl %eax, -0x3e4(%rbp)
jmp 0xf4dd
movl -0x3e4(%rbp), %esi
movq -0x3e0(%rbp), %rdi
callq 0x6a00
movq %rax, -0x3f0(%rbp)
jmp 0xf4f8
movq -0x3f0(%rbp), %rdi
leaq 0x70c32(%rip), %rsi     # 0x80138
callq 0x6560
movq %rax, -0x3f8(%rbp)
jmp 0xf514
movq -0x10(%rbp), %rdi
callq 0x11790
movq -0x3f8(%rbp), %rdi
movq %rax, %rsi
callq 0x64c0
movq %rax, -0x400(%rbp)
jmp 0xf535
movq -0x400(%rbp), %rdi
leaq 0x70bf9(%rip), %rsi     # 0x8013c
callq 0x6560
movq %rax, -0x408(%rbp)
jmp 0xf551
movq -0x408(%rbp), %rdi
movl -0x25c(%rbp), %esi
callq 0x6a00
movq %rax, -0x410(%rbp)
jmp 0xf56c
movq -0x410(%rbp), %rdi
leaq 0x70bc2(%rip), %rsi     # 0x8013c
callq 0x6560
movq %rax, -0x418(%rbp)
jmp 0xf588
movq -0x418(%rbp), %rdi
movl -0x260(%rbp), %esi
callq 0x6a00
movq %rax, -0x420(%rbp)
jmp 0xf5a3
movq -0x420(%rbp), %rdi
movq 0x9e9f7(%rip), %rsi     # 0xadfa8
callq 0x65c0
jmp 0xf5b8
movl $0x0, -0x4(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x11be0
leaq -0x48(%rbp), %rdi
callq 0x1b2d0
jmp 0xf66e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xf60b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x6ba8
leaq -0x281(%rbp), %rdi
callq 0x6600
jmp 0xf657
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0xf64b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x6ba8
leaq -0x2a9(%rbp), %rdi
callq 0x6600
leaq -0x138(%rbp), %rdi
callq 0x11be0
leaq -0x48(%rbp), %rdi
callq 0x1b2d0
jmp 0xf67a
movl -0x4(%rbp), %eax
addq $0x420, %rsp            # imm = 0x420
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x6a10
nopw %cs:(%rax,%rax)