// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, 0x78(%rsp)
movq 0x8(%rdi), %rax
cmpq (%rdi), %rax
je 0x8972
movq %rdi, %rbx
movq %rdx, 0x70(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xbb7c
movq (%rbx), %r13
leaq 0x30(%rsp), %r14
leaq 0x4(%rsp), %r15
leaq 0x26b08(%rip), %rbp     # 0x2f029
leaq 0xc(%rsp), %r12
cmpq 0x8(%rbx), %r13
je 0x85f7
movq %r14, %rdi
leaq 0x26ae9(%rip), %rsi     # 0x2f023
movq %r15, %rdx
callq 0x96f2
movq %r13, %rdi
movq %r14, %rsi
leaq 0x8(%rsp), %rdx
callq 0x966e
movq %r14, %rdi
callq 0x58f8
movq %r14, %rdi
leaq 0x26ac2(%rip), %rsi     # 0x2f026
movq %r15, %rdx
callq 0x96f2
movq %r13, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
callq 0x966e
movq %r14, %rdi
callq 0x58f8
movq %r14, %rdi
leaq 0x26a92(%rip), %rsi     # 0x2f020
movq %r15, %rdx
callq 0x96f2
movq %r13, %rdi
movq %r14, %rsi
leaq 0x10(%rsp), %rdx
callq 0x966e
movq %r14, %rdi
callq 0x58f8
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x96f2
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x966e
movq %r14, %rdi
callq 0x58f8
movl 0x10(%rsp), %esi
movl 0xc(%rsp), %edx
movl 0x50(%rsp), %ecx
leaq 0x80(%rsp), %rdi
callq 0xbb14
addq $0x158, %r13            # imm = 0x158
jmp 0x8526
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xbc10
leaq 0x80(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xbcd8
movq 0x429c5(%rip), %rdi     # 0x4aff0
leaq 0x269fa(%rip), %rsi     # 0x2f02c
callq 0x5430
movq %rax, %rdi
movq 0x8(%rbx), %rax
subq (%rbx), %rax
movl $0x158, %ecx            # imm = 0x158
cqto
idivq %rcx
movq %rax, %rsi
callq 0x52c0
leaq 0x269f8(%rip), %rsi     # 0x2f052
movq %rax, %rdi
callq 0x5430
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x2, %rsi
movq %rax, %rdi
callq 0x52c0
leaq 0x28821(%rip), %rsi     # 0x30ea0
movq %rax, %rdi
callq 0x5430
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x53b0
movq %rax, %rdi
callq 0x51f0
andl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r14
leaq 0x30(%rsp), %r15
leaq 0x50(%rsp), %r13
movq 0x18(%rsp), %rsi
cmpq %rsi, %r14
je 0x8705
andl $0x0, 0x50(%rsp)
movslq (%r14), %r12
movq (%rbx), %rbp
movq %r15, %rdi
leaq 0x26959(%rip), %rsi     # 0x2f026
leaq 0x8(%rsp), %rdx
callq 0x96f2
imulq $0x158, %r12, %rax      # imm = 0x158
addq %rax, %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x966e
movq %r15, %rdi
callq 0x58f8
movl 0x50(%rsp), %eax
addl %eax, 0xc(%rsp)
addq $0x4, %r14
jmp 0x86ae
movq 0x10(%rsp), %rdi
callq 0x9e43
leaq 0x8(%rsp), %rax
movl $0x1, (%rax)
movq 0x10(%rsp), %rbp
leaq 0x30(%rsp), %r12
leaq 0x50(%rsp), %r15
leaq 0x3(%rsp), %r14
cmpq 0x18(%rsp), %rbp
je 0x880c
movslq (%rbp), %rax
imulq $0x158, %rax, %r13      # imm = 0x158
addq (%rbx), %r13
movq %r12, %rdi
leaq 0x2690f(%rip), %rsi     # 0x2f060
leaq 0x4(%rsp), %rdx
callq 0x96f2
movq %r15, %rdi
leaq 0x27c45(%rip), %rsi     # 0x303aa
movq %r14, %rdx
callq 0x96f2
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
leaq 0xc(%rsp), %rcx
callq 0x972e
movq %r15, %rdi
callq 0x58f8
movq %r12, %rdi
callq 0x58f8
movslq (%rbp), %rax
imulq $0x158, %rax, %r13      # imm = 0x158
addq (%rbx), %r13
movq %r12, %rdi
leaq 0x268bb(%rip), %rsi     # 0x2f063
leaq 0x4(%rsp), %rdx
callq 0x96f2
movq %r15, %rdi
leaq 0x27bee(%rip), %rsi     # 0x303aa
movq %r14, %rdx
callq 0x96f2
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
leaq 0x8(%rsp), %rcx
callq 0x972e
movq %r15, %rdi
callq 0x58f8
movq %r12, %rdi
callq 0x58f8
incl 0x8(%rsp)
movslq (%rbp), %rax
imulq $0x158, %rax, %rsi      # imm = 0x158
addq (%rbx), %rsi
movq 0x78(%rsp), %rdi
callq 0x12aa4
addq $0x4, %rbp
jmp 0x872e
movq (%rbx), %r14
leaq 0x2680a(%rip), %rsi     # 0x2f020
leaq 0x30(%rsp), %rdi
leaq 0x3(%rsp), %rdx
callq 0x96f2
leaq 0x30(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %r14, %rdi
callq 0x966e
leaq 0x30(%rsp), %rdi
callq 0x58f8
movq 0x8(%rbx), %r14
leaq 0x267dd(%rip), %rsi     # 0x2f029
leaq 0x30(%rsp), %rdi
leaq 0x3(%rsp), %rdx
callq 0x96f2
addq $-0x158, %r14           # imm = 0xFEA8
leaq 0x30(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
callq 0x966e
leaq 0x30(%rsp), %rdi
callq 0x58f8
movq 0x4276b(%rip), %rdi     # 0x4aff0
leaq 0x267da(%rip), %rsi     # 0x2f066
callq 0x5430
movq (%rbx), %rcx
movslq 0xa8(%rcx), %rcx
imulq $0x28, %rcx, %rsi
movq 0x70(%rsp), %rcx
addq (%rcx), %rsi
movq %rax, %rdi
callq 0x53b0
leaq 0x267ce(%rip), %rsi     # 0x2f084
movq %rax, %rdi
callq 0x5430
movq (%rbx), %rcx
movl 0xac(%rcx), %esi
movq %rax, %rdi
callq 0x57b0
leaq 0x267ae(%rip), %rsi     # 0x2f084
movq %rax, %rdi
callq 0x5430
movq %rax, %r14
movq $-0x158, %rdi           # imm = 0xFEA8
addq 0x8(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11df4
movq %r14, %rdi
movl %eax, %esi
callq 0x57b0
leaq 0x2677a(%rip), %rsi     # 0x2f080
movq %rax, %rdi
callq 0x5430
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x53b0
leaq 0x26764(%rip), %rsi     # 0x2f084
movq %rax, %rdi
callq 0x5430
movl 0x50(%rsp), %esi
movq %rax, %rdi
callq 0x57b0
leaq 0x26749(%rip), %rsi     # 0x2f084
movq %rax, %rdi
callq 0x5430
movl 0x4(%rsp), %esi
movq %rax, %rdi
callq 0x57b0
movq %rax, %rdi
callq 0x51f0
leaq 0x10(%rsp), %rdi
callq 0x9e10
leaq 0x80(%rsp), %rdi
callq 0xbbbc
xorl %eax, %eax
jmp 0x8975
pushq $0x1
popq %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x89b5
jmp 0x89b0
jmp 0x89b5
jmp 0x89b0
jmp 0x89b0
jmp 0x89b0
jmp 0x89b0
jmp 0x89b0
jmp 0x89b5
jmp 0x89a1
jmp 0x89b0
jmp 0x89b0
jmp 0x89b5
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x58f8
jmp 0x89b8
movq %rax, %rbx
jmp 0x89c2
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x58f8
leaq 0x10(%rsp), %rdi
callq 0x9e10
jmp 0x89ee
jmp 0x89dc
jmp 0x89eb
jmp 0x89dc
jmp 0x89eb
jmp 0x89eb
jmp 0x89dc
jmp 0x89eb
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x58f8
jmp 0x89ee
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0xbbbc
movq %rbx, %rdi
callq 0x57d0