// File: /workspace/github_cpp_makefile_star2_O3/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 0xa141
movq %rdi, %rbx
movq %rdx, 0x70(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xe0d2
movq (%rbx), %r12
cmpq 0x8(%rbx), %r12
je 0x9d8e
leaq 0x18(%rsp), %r13
leaq 0x8(%rsp), %r14
leaq 0x50(%rsp), %rbp
leaq 0x4(%rsp), %r15
movq %r13, 0x8(%rsp)
movq %r14, %rdi
leaq 0x2f57d(%rip), %rsi     # 0x39023
leaq 0x2f578(%rip), %rdx     # 0x39025
callq 0xbdac
cmpb $0x0, 0x130(%r12)
jne 0x9af9
movq 0x90(%r12), %rax
testq %rax, %rax
je 0x9af9
movq 0x88(%r12), %rcx
movq %rcx, 0x30(%rsp)
movl %eax, 0x50(%rsp)
movl $0x0, 0x4(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0x16804
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x9b10
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r13, 0x8(%rsp)
movq %r14, %rdi
leaq 0x2f507(%rip), %rsi     # 0x39026
leaq 0x2f502(%rip), %rdx     # 0x39028
callq 0xbdac
cmpb $0x0, 0x130(%r12)
jne 0x9bc1
movq 0x90(%r12), %rax
testq %rax, %rax
je 0x9bc1
movq 0x88(%r12), %rcx
movq %rcx, 0x30(%rsp)
movl %eax, 0x50(%rsp)
movl $0x0, 0x4(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0x16804
testb %al, %al
je 0x9bc1
movq 0x30(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0x9b99
movl $0x1, %edx
cmpl $0x41, %eax
je 0x9baf
cmpl $0x63, %eax
je 0x9baf
jmp 0x9bc1
cmpl $0x69, %eax
je 0x9baa
cmpl $0x73, %eax
jne 0x9bc1
movl $0x2, %edx
jmp 0x9baf
movl $0x4, %edx
movl $0x0, 0x28(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x5380
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x9bd8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r13, 0x8(%rsp)
movq %r14, %rdi
leaq 0x2f439(%rip), %rsi     # 0x39020
leaq 0x2f434(%rip), %rdx     # 0x39022
callq 0xbdac
cmpb $0x0, 0x130(%r12)
jne 0x9c89
movq 0x90(%r12), %rax
testq %rax, %rax
je 0x9c89
movq 0x88(%r12), %rcx
movq %rcx, 0x30(%rsp)
movl %eax, 0x50(%rsp)
movl $0x0, 0x4(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0x16804
testb %al, %al
je 0x9c89
movq 0x30(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0x9c61
movl $0x1, %edx
cmpl $0x41, %eax
je 0x9c77
cmpl $0x63, %eax
je 0x9c77
jmp 0x9c89
cmpl $0x69, %eax
je 0x9c72
cmpl $0x73, %eax
jne 0x9c89
movl $0x2, %edx
jmp 0x9c77
movl $0x4, %edx
movl $0x0, 0x2c(%rsp)
leaq 0x2c(%rsp), %rdi
callq 0x5380
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x9ca0
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r13, 0x8(%rsp)
movq %r14, %rdi
leaq 0x2f37a(%rip), %rsi     # 0x39029
leaq 0x2f375(%rip), %rdx     # 0x3902b
callq 0xbdac
cmpb $0x0, 0x130(%r12)
jne 0x9d4e
movq 0x90(%r12), %rax
testq %rax, %rax
je 0x9d4e
movq 0x88(%r12), %rcx
movq %rcx, 0x30(%rsp)
movl %eax, 0x50(%rsp)
movl $0x0, 0x4(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0x16804
testb %al, %al
je 0x9d4e
movq 0x30(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0x9d29
movl $0x1, %edx
cmpl $0x41, %eax
je 0x9d3f
cmpl $0x63, %eax
je 0x9d3f
jmp 0x9d4e
cmpl $0x69, %eax
je 0x9d3a
cmpl $0x73, %eax
jne 0x9d4e
movl $0x2, %edx
jmp 0x9d3f
movl $0x4, %edx
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x5380
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x9d65
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movl 0x2c(%rsp), %esi
movl (%rsp), %edx
movl 0x28(%rsp), %ecx
leaq 0x80(%rsp), %rdi
callq 0xe048
addq $0x158, %r12            # imm = 0x158
cmpq 0x8(%rbx), %r12
jne 0x9a97
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xe194
leaq 0x80(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xe3f4
movq 0x4622b(%rip), %rdi     # 0x4fff0
leaq 0x2f260(%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 0x461fa(%rip), %rdi     # 0x4fff0
callq 0x52d0
movq %rax, %r14
leaq 0x2f24d(%rip), %rsi     # 0x39052
movl $0xd, %edx
movq %rax, %rdi
callq 0x54f0
movq 0x58(%rsp), %rsi
subq 0x50(%rsp), %rsi
sarq $0x2, %rsi
movq %r14, %rdi
callq 0x52d0
movq %rax, %r14
leaq 0x31079(%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
movl $0x0, 0x2c(%rsp)
movq 0x50(%rsp), %r14
movq 0x58(%rsp), %rbp
cmpq %rbp, %r14
je 0x9f9a
leaq 0x18(%rsp), %r15
leaq 0x2f186(%rip), %r12     # 0x39028
leaq 0x8(%rsp), %r13
movl $0x0, (%rsp)
movslq (%r14), %rax
imulq $0x158, %rax, %rbp      # imm = 0x158
addq (%rbx), %rbp
movq %r15, 0x8(%rsp)
movq %r13, %rdi
leaq 0x2f15c(%rip), %rsi     # 0x39026
movq %r12, %rdx
callq 0xbdac
cmpb $0x0, 0x130(%rbp)
jne 0x9f65
movq 0x90(%rbp), %rax
testq %rax, %rax
je 0x9f65
movq 0x88(%rbp), %rcx
movq %rcx, 0x30(%rsp)
movl %eax, 0x4(%rsp)
movl $0x0, 0x28(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x4(%rsp), %rcx
leaq 0x28(%rsp), %r8
callq 0x16804
testb %al, %al
je 0x9f65
movq 0x30(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0x9f40
movl $0x1, %edx
cmpl $0x41, %eax
je 0x9f56
cmpl $0x63, %eax
je 0x9f56
jmp 0x9f65
cmpl $0x69, %eax
je 0x9f51
cmpl $0x73, %eax
jne 0x9f65
movl $0x2, %edx
jmp 0x9f56
movl $0x4, %edx
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x5380
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x9f7c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movl (%rsp), %eax
addl %eax, 0x2c(%rsp)
addq $0x4, %r14
movq 0x58(%rsp), %rbp
cmpq %rbp, %r14
jne 0x9ea7
movq 0x50(%rsp), %r14
movq %rbx, %r13
cmpq %rbp, %r14
je 0xa14b
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 0xbe41
movq %r14, %rdi
movq %rbp, %rsi
callq 0xbf24
movq 0x50(%rsp), %rbx
movl $0x1, (%rsp)
cmpq 0x58(%rsp), %rbx
je 0xa152
leaq 0x40(%rsp), %rbp
leaq 0x8(%rsp), %r12
leaq 0x30(%rsp), %r14
movslq (%rbx), %rax
imulq $0x158, %rax, %r15      # imm = 0x158
addq (%r13), %r15
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, %rdi
leaq 0x2f045(%rip), %rsi     # 0x39060
leaq 0x2f040(%rip), %rdx     # 0x39062
callq 0xbdac
movq %rbp, 0x30(%rsp)
movq %r14, %rdi
leaq 0x2f078(%rip), %rsi     # 0x390ae
leaq 0x2f072(%rip), %rdx     # 0x390af
callq 0xbdac
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
leaq 0x2c(%rsp), %rcx
callq 0xb5ba
movq 0x30(%rsp), %rdi
cmpq %rbp, %rdi
je 0xa06c
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xa088
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movslq (%rbx), %rax
imulq $0x158, %rax, %r15      # imm = 0x158
addq (%r13), %r15
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, %rdi
leaq 0x2efb9(%rip), %rsi     # 0x39063
leaq 0x2efb4(%rip), %rdx     # 0x39065
callq 0xbdac
movq %rbp, 0x30(%rsp)
movq %r14, %rdi
leaq 0x2efe9(%rip), %rsi     # 0x390ae
leaq 0x2efe3(%rip), %rdx     # 0x390af
callq 0xbdac
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rsp, %rcx
callq 0xb5ba
movq 0x30(%rsp), %rdi
cmpq %rbp, %rdi
je 0xa0f9
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xa115
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
incl (%rsp)
movslq (%rbx), %rax
imulq $0x158, %rax, %rsi      # imm = 0x158
addq (%r13), %rsi
movq 0x78(%rsp), %rdi
callq 0x17a48
addq $0x4, %rbx
cmpq 0x58(%rsp), %rbx
jne 0x9ff9
jmp 0xa152
movl $0x1, %eax
jmp 0xa44e
movl $0x1, (%rsp)
movq (%r13), %r14
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2eeba(%rip), %rsi     # 0x39020
leaq 0x2eeb5(%rip), %rdx     # 0x39022
leaq 0x8(%rsp), %rdi
callq 0xbdac
cmpb $0x0, 0x130(%r14)
jne 0xa209
movq 0x90(%r14), %rax
testq %rax, %rax
je 0xa209
movq 0x88(%r14), %rcx
leaq 0x30(%rsp), %rdx
movq %rcx, (%rdx)
leaq 0x4(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x28(%rsp), %r8
movl $0x0, (%r8)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x16804
testb %al, %al
je 0xa209
movq 0x30(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0xa1e3
movl $0x1, %edx
cmpl $0x41, %eax
je 0xa1f9
cmpl $0x63, %eax
je 0xa1f9
jmp 0xa209
cmpl $0x69, %eax
je 0xa1f4
cmpl $0x73, %eax
jne 0xa209
movl $0x2, %edx
jmp 0xa1f9
movl $0x4, %edx
leaq 0x6c(%rsp), %rdi
movl $0x0, (%rdi)
callq 0x5380
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa220
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%r13), %r14
leaq 0x8(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x2edf6(%rip), %rsi     # 0x39029
leaq 0x2edf1(%rip), %rdx     # 0x3902b
callq 0xbdac
cmpb $0x0, -0x28(%r14)
jne 0xa2d5
movq -0xc8(%r14), %rax
testq %rax, %rax
je 0xa2d5
movq -0xd0(%r14), %rcx
addq $-0x158, %r14           # imm = 0xFEA8
leaq 0x30(%rsp), %rdx
movq %rcx, (%rdx)
leaq 0x4(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x28(%rsp), %r8
movl $0x0, (%r8)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x16804
testb %al, %al
je 0xa2d5
movq 0x30(%rsp), %rsi
movzbl -0x1(%rsi), %eax
cmpl $0x68, %eax
jg 0xa2af
movl $0x1, %edx
cmpl $0x41, %eax
je 0xa2c5
cmpl $0x63, %eax
je 0xa2c5
jmp 0xa2d5
cmpl $0x69, %eax
je 0xa2c0
cmpl $0x73, %eax
jne 0xa2d5
movl $0x2, %edx
jmp 0xa2c5
movl $0x4, %edx
leaq 0x68(%rsp), %rdi
movl $0x0, (%rdi)
callq 0x5380
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa2ec
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x45cfd(%rip), %rdi     # 0x4fff0
leaq 0x2ed6c(%rip), %rsi     # 0x39066
movl $0x1d, %edx
callq 0x54f0
movq 0x70(%rsp), %rcx
movq (%r13), %rax
movslq 0xa8(%rax), %rax
movq (%rcx), %rcx
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rsi
movq 0x8(%rcx,%rax,8), %rdx
movq 0x45cc5(%rip), %rdi     # 0x4fff0
callq 0x54f0
movq %rax, %r14
leaq 0x2ed4a(%rip), %rsi     # 0x39084
movl $0x1, %edx
movq %rax, %rdi
callq 0x54f0
movq (%r13), %rax
movl 0xac(%rax), %esi
movq %r14, %rdi
callq 0x5780
movq %rax, %r14
leaq 0x2ed21(%rip), %rsi     # 0x39084
movl $0x1, %edx
movq %rax, %rdi
callq 0x54f0
movq $-0x158, %rdi           # imm = 0xFEA8
addq 0x8(%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x16c2c
movq %r14, %rdi
movl %eax, %esi
callq 0x5780
movq %rax, %r14
leaq 0x2ece8(%rip), %rsi     # 0x39080
movl $0x3, %edx
movq %rax, %rdi
callq 0x54f0
movq (%r13), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
callq 0x54f0
movq %rax, %rbx
leaq 0x2ecc2(%rip), %rsi     # 0x39084
movl $0x1, %edx
movq %rax, %rdi
callq 0x54f0
movl 0x6c(%rsp), %esi
movq %rbx, %rdi
callq 0x5780
movq %rax, %rbx
leaq 0x2ec9f(%rip), %rsi     # 0x39084
movl $0x1, %edx
movq %rax, %rdi
callq 0x54f0
movl 0x68(%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 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0xa43f
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x5470
leaq 0x80(%rsp), %rdi
callq 0xe12a
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa494
jmp 0xa494
jmp 0xa4fb
jmp 0xa4fb
jmp 0xa4fb
jmp 0xa479
jmp 0xa479
jmp 0xa479
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0xa515
movq 0x18(%rsp), %rsi
incq %rsi
jmp 0xa510
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
jne 0xa4db
jmp 0xa4fe
jmp 0xa4fb
jmp 0xa4fb
jmp 0xa4ad
jmp 0xa4b2
jmp 0xa4fb
movq %rax, %r14
jmp 0xa4cc
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %rbp, %rdi
je 0xa4cc
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xa4fe
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0xa4fe
jmp 0xa4fb
jmp 0xa4fb
jmp 0xa4f6
jmp 0xa4f6
jmp 0xa4f6
jmp 0xa4f6
movq %rax, %r14
jmp 0xa515
movq %rax, %r14
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0xa515
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x5470
leaq 0x80(%rsp), %rdi
callq 0xe12a
movq %r14, %rdi
callq 0x57b0