// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movq %rsi, 0x88(%rsp)
movq 0x8(%rdi), %rax
cmpq (%rdi), %rax
je 0xa20c
movq %rdi, %rbx
movq %rdx, 0x80(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xe152
movq (%rbx), %r12
cmpq 0x8(%rbx), %r12
je 0x9da8
leaq 0x20(%rsp), %r13
leaq 0x10(%rsp), %r14
leaq 0x60(%rsp), %rbp
leaq 0x8(%rsp), %r15
movq %r13, 0x10(%rsp)
movq %r14, %rdi
leaq 0x2f567(%rip), %rsi     # 0x39023
leaq 0x2f562(%rip), %rdx     # 0x39025
callq 0xbe08
cmpb $0x0, 0x130(%r12)
jne 0x9b0f
movq 0x90(%r12), %rax
testq %rax, %rax
je 0x9b0f
movq 0x88(%r12), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, 0x60(%rsp)
movl $0x0, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0x1693a
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x9b26
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r13, 0x10(%rsp)
movq %r14, %rdi
leaq 0x2f4f1(%rip), %rsi     # 0x39026
leaq 0x2f4ec(%rip), %rdx     # 0x39028
callq 0xbe08
cmpb $0x0, 0x130(%r12)
jne 0x9bd7
movq 0x90(%r12), %rax
testq %rax, %rax
je 0x9bd7
movq 0x88(%r12), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, 0x60(%rsp)
movl $0x0, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0x1693a
testb %al, %al
je 0x9bd7
movq 0x40(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0x9baf
movl $0x1, %edx
cmpl $0x41, %eax
je 0x9bc5
cmpl $0x63, %eax
je 0x9bc5
jmp 0x9bd7
cmpl $0x69, %eax
je 0x9bc0
cmpl $0x73, %eax
jne 0x9bd7
movl $0x2, %edx
jmp 0x9bc5
movl $0x4, %edx
movl $0x0, 0xc(%rsp)
leaq 0xc(%rsp), %rdi
callq 0x5380
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x9bee
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r13, 0x10(%rsp)
movq %r14, %rdi
leaq 0x2f423(%rip), %rsi     # 0x39020
leaq 0x2f41e(%rip), %rdx     # 0x39022
callq 0xbe08
cmpb $0x0, 0x130(%r12)
jne 0x9c9f
movq 0x90(%r12), %rax
testq %rax, %rax
je 0x9c9f
movq 0x88(%r12), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, 0x60(%rsp)
movl $0x0, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0x1693a
testb %al, %al
je 0x9c9f
movq 0x40(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0x9c77
movl $0x1, %edx
cmpl $0x41, %eax
je 0x9c8d
cmpl $0x63, %eax
je 0x9c8d
jmp 0x9c9f
cmpl $0x69, %eax
je 0x9c88
cmpl $0x73, %eax
jne 0x9c9f
movl $0x2, %edx
jmp 0x9c8d
movl $0x4, %edx
movl $0x0, 0x34(%rsp)
leaq 0x34(%rsp), %rdi
callq 0x5380
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x9cb6
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r13, 0x10(%rsp)
movq %r14, %rdi
leaq 0x2f364(%rip), %rsi     # 0x39029
leaq 0x2f35f(%rip), %rdx     # 0x3902b
callq 0xbe08
cmpb $0x0, 0x130(%r12)
jne 0x9d67
movq 0x90(%r12), %rax
testq %rax, %rax
je 0x9d67
movq 0x88(%r12), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, 0x60(%rsp)
movl $0x0, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0x1693a
testb %al, %al
je 0x9d67
movq 0x40(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0x9d3f
movl $0x1, %edx
cmpl $0x41, %eax
je 0x9d55
cmpl $0x63, %eax
je 0x9d55
jmp 0x9d67
cmpl $0x69, %eax
je 0x9d50
cmpl $0x73, %eax
jne 0x9d67
movl $0x2, %edx
jmp 0x9d55
movl $0x4, %edx
movl $0x0, 0x4(%rsp)
leaq 0x4(%rsp), %rdi
callq 0x5380
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x9d7e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
movl 0x34(%rsp), %esi
movl 0x4(%rsp), %edx
movl 0xc(%rsp), %ecx
leaq 0x90(%rsp), %rdi
callq 0xe0cc
addq $0x158, %r12            # imm = 0x158
cmpq 0x8(%rbx), %r12
jne 0x9aad
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xe210
leaq 0x90(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xe486
movq 0x46211(%rip), %rdi     # 0x4fff0
leaq 0x2f246(%rip), %rsi     # 0x3902c
movl $0x25, %edx
callq 0x54f0
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x3, %rax
movabsq $-0x7d05f417d05f417d, %rsi # imm = 0x82FA0BE82FA0BE83
imulq %rax, %rsi
movq 0x461e0(%rip), %rdi     # 0x4fff0
callq 0x52d0
movq %rax, %r14
leaq 0x2f233(%rip), %rsi     # 0x39052
movl $0xd, %edx
movq %rax, %rdi
callq 0x54f0
movq 0x68(%rsp), %rsi
subq 0x60(%rsp), %rsi
sarq $0x2, %rsi
movq %r14, %rdi
callq 0x52d0
movq %rax, %r14
leaq 0x3105f(%rip), %rsi     # 0x3aeab
movl $0x5, %edx
movq %rax, %rdi
callq 0x54f0
movq (%rbx), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
callq 0x54f0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5430
movsbl %al, %esi
movq %r14, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x52e0
movq %rbx, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %rbp
cmpq %rbp, %rbx
je 0x9fc1
leaq 0x20(%rsp), %r13
leaq 0x2f165(%rip), %r14     # 0x39026
leaq 0x2f160(%rip), %r15     # 0x39028
leaq 0x10(%rsp), %r12
movl $0x0, 0x4(%rsp)
movslq (%rbx), %rax
imulq $0x158, %rax, %rbp      # imm = 0x158
movq 0x38(%rsp), %rax
addq (%rax), %rbp
movq %r13, 0x10(%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xbe08
cmpb $0x0, 0x130(%rbp)
jne 0x9f90
movq 0x90(%rbp), %rax
testq %rax, %rax
je 0x9f90
movq 0x88(%rbp), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
leaq 0x40(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
callq 0x1693a
testb %al, %al
je 0x9f90
movq 0x40(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0x9f68
movl $0x1, %edx
cmpl $0x41, %eax
je 0x9f7e
cmpl $0x63, %eax
je 0x9f7e
jmp 0x9f90
cmpl $0x69, %eax
je 0x9f79
cmpl $0x73, %eax
jne 0x9f90
movl $0x2, %edx
jmp 0x9f7e
movl $0x4, %edx
movl $0x0, 0x4(%rsp)
leaq 0x4(%rsp), %rdi
callq 0x5380
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x9fa7
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
movl 0x4(%rsp), %eax
addl %eax, 0x34(%rsp)
addq $0x4, %rbx
movq 0x68(%rsp), %rbp
cmpq %rbp, %rbx
jne 0x9ecd
movq 0x60(%rsp), %r14
cmpq %rbp, %r14
je 0x9ff8
movq %rbp, %rax
subq %r14, %rax
sarq $0x2, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xbe98
movq %r14, %rdi
movq %rbp, %rsi
callq 0xbf7b
movl $0x1, 0x4(%rsp)
movq 0x60(%rsp), %rbp
cmpq 0x68(%rsp), %rbp
je 0xa16b
leaq 0x20(%rsp), %rbx
leaq 0x50(%rsp), %r13
leaq 0x10(%rsp), %r12
leaq 0x40(%rsp), %r14
movslq (%rbp), %rax
imulq $0x158, %rax, %r15      # imm = 0x158
movq 0x38(%rsp), %rax
addq (%rax), %r15
movq %rbx, 0x10(%rsp)
movq %r12, %rdi
leaq 0x2f01a(%rip), %rsi     # 0x39060
leaq 0x2f015(%rip), %rdx     # 0x39062
callq 0xbe08
movq %r13, 0x40(%rsp)
movq %r14, %rdi
leaq 0x2f04d(%rip), %rsi     # 0x390ae
leaq 0x2f047(%rip), %rdx     # 0x390af
callq 0xbe08
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
leaq 0x34(%rsp), %rcx
callq 0xb628
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0xa097
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0xa0ae
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
movslq (%rbp), %rax
imulq $0x158, %rax, %r15      # imm = 0x158
movq 0x38(%rsp), %rax
addq (%rax), %r15
movq %rbx, 0x10(%rsp)
movq %r12, %rdi
leaq 0x2ef93(%rip), %rsi     # 0x39063
leaq 0x2ef8e(%rip), %rdx     # 0x39065
callq 0xbe08
movq %r13, 0x40(%rsp)
movq %r14, %rdi
leaq 0x2efc3(%rip), %rsi     # 0x390ae
leaq 0x2efbd(%rip), %rdx     # 0x390af
callq 0xbe08
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
leaq 0x4(%rsp), %rcx
callq 0xb628
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0xa121
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0xa138
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
incl 0x4(%rsp)
movslq (%rbp), %rax
imulq $0x158, %rax, %rsi      # imm = 0x158
movq 0x38(%rsp), %rax
addq (%rax), %rsi
movq 0x88(%rsp), %rdi
callq 0x17ba0
addq $0x4, %rbp
cmpq 0x68(%rsp), %rbp
jne 0xa024
movq 0x38(%rsp), %rax
movq (%rax), %r14
leaq 0x20(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x2ee9d(%rip), %rsi     # 0x39020
leaq 0x2ee98(%rip), %rdx     # 0x39022
leaq 0x10(%rsp), %rdi
callq 0xbe08
cmpb $0x0, 0x130(%r14)
movq 0x80(%rsp), %r15
jne 0xa23c
movq 0x90(%r14), %rax
testq %rax, %rax
je 0xa23c
movq 0x88(%r14), %rcx
leaq 0x40(%rsp), %rdx
movq %rcx, (%rdx)
leaq 0x8(%rsp), %rcx
movl %eax, (%rcx)
leaq 0xc(%rsp), %r8
movl $0x0, (%r8)
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x1693a
testb %al, %al
je 0xa23c
movq 0x40(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0xa216
movl $0x1, %edx
cmpl $0x41, %eax
je 0xa22c
cmpl $0x63, %eax
je 0xa22c
jmp 0xa23c
movl $0x1, %eax
jmp 0xa483
cmpl $0x69, %eax
je 0xa227
cmpl $0x73, %eax
jne 0xa23c
movl $0x2, %edx
jmp 0xa22c
movl $0x4, %edx
leaq 0x7c(%rsp), %rdi
movl $0x0, (%rdi)
callq 0x5380
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0xa253
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x38(%rsp), %rax
movq 0x8(%rax), %r14
leaq 0x10(%rsp), %rdi
movq %rbx, (%rdi)
leaq 0x2edbe(%rip), %rsi     # 0x39029
leaq 0x2edb9(%rip), %rdx     # 0x3902b
callq 0xbe08
cmpb $0x0, -0x28(%r14)
jne 0xa30d
movq -0xc8(%r14), %rax
testq %rax, %rax
je 0xa30d
movq -0xd0(%r14), %rcx
addq $-0x158, %r14           # imm = 0xFEA8
leaq 0x40(%rsp), %rdx
movq %rcx, (%rdx)
leaq 0x8(%rsp), %rcx
movl %eax, (%rcx)
leaq 0xc(%rsp), %r8
movl $0x0, (%r8)
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x1693a
testb %al, %al
je 0xa30d
movq 0x40(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0xa2e7
movl $0x1, %edx
cmpl $0x41, %eax
je 0xa2fd
cmpl $0x63, %eax
je 0xa2fd
jmp 0xa30d
cmpl $0x69, %eax
je 0xa2f8
cmpl $0x73, %eax
jne 0xa30d
movl $0x2, %edx
jmp 0xa2fd
movl $0x4, %edx
leaq 0x78(%rsp), %rdi
movl $0x0, (%rdi)
callq 0x5380
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0xa324
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x45cc5(%rip), %rdi     # 0x4fff0
leaq 0x2ed34(%rip), %rsi     # 0x39066
movl $0x1d, %edx
callq 0x54f0
movq 0x38(%rsp), %rbx
movq (%rbx), %rax
movslq 0xa8(%rax), %rax
movq (%r15), %rcx
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rsi
movq 0x8(%rcx,%rax,8), %rdx
movq 0x45c8e(%rip), %rdi     # 0x4fff0
callq 0x54f0
movq %rax, %r14
leaq 0x2ed13(%rip), %rsi     # 0x39084
movl $0x1, %edx
movq %rax, %rdi
callq 0x54f0
movq (%rbx), %rax
movl 0xac(%rax), %esi
movq %r14, %rdi
callq 0x5780
movq %rax, %r14
leaq 0x2eceb(%rip), %rsi     # 0x39084
movl $0x1, %edx
movq %rax, %rdi
callq 0x54f0
movq $-0x158, %rdi           # imm = 0xFEA8
addq 0x8(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x16d74
movq %r14, %rdi
movl %eax, %esi
callq 0x5780
movq %rax, %r14
leaq 0x2ecb2(%rip), %rsi     # 0x39080
movl $0x3, %edx
movq %rax, %rdi
callq 0x54f0
movq (%rbx), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
callq 0x54f0
movq %rax, %rbx
leaq 0x2ec8d(%rip), %rsi     # 0x39084
movl $0x1, %edx
movq %rax, %rdi
callq 0x54f0
movl 0x7c(%rsp), %esi
movq %rbx, %rdi
callq 0x5780
movq %rax, %rbx
leaq 0x2ec6a(%rip), %rsi     # 0x39084
movl $0x1, %edx
movq %rax, %rdi
callq 0x54f0
movl 0x78(%rsp), %esi
movq %rbx, %rdi
callq 0x5780
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5430
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x52e0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xa474
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x5470
leaq 0x90(%rsp), %rdi
callq 0xe1aa
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa4e4
jmp 0xa4e4
jmp 0xa52b
jmp 0xa52b
jmp 0xa52b
jmp 0xa4ae
jmp 0xa4ae
jmp 0xa4ae
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0xa545
movq 0x20(%rsp), %rsi
incq %rsi
jmp 0xa540
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
jne 0xa50d
jmp 0xa52e
jmp 0xa52b
jmp 0xa52b
jmp 0xa52b
jmp 0xa4e4
jmp 0xa4e9
jmp 0xa52b
movq %rax, %r14
jmp 0xa503
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0xa503
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0xa52e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0xa52e
jmp 0xa52b
jmp 0xa526
jmp 0xa526
jmp 0xa526
jmp 0xa526
movq %rax, %r14
jmp 0xa545
movq %rax, %r14
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xa545
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x5470
leaq 0x90(%rsp), %rdi
callq 0xe1aa
movq %r14, %rdi
callq 0x57b0