// File: /workspace/github_cpp_makefile_star2_O1/zeeev[P]DASVC/src/annotate.cpp
// Function: processBlock(std::__cxx11::list<BamTools::BamAlignment, std::allocator<BamTools::BamAlignment>>&, std::vector<BamTools::BamAlignment, std::allocator<BamTools::BamAlignment>>&, int, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, long, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, long>>>&)
int processBlock(std::list< BamAlignment > & readBuffer ,
                 std::vector< BamAlignment > & processed,
                 int blockId,
                 std::map<std::string, long int> & queryLens )
{

    int totalAlignedBases = 0;

    std::vector<BamAlignment> reads;

    std::map<int, int> matchesPerContig;

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

        long int queryStart = 0;

        for(std::vector<CigarOp>::iterator iz = it->CigarData.begin() ;
            iz != it->CigarData.end() ; iz++){
            /* first matching query position */
            if( (iz->Type != 'H') && (iz->Type != 'S') ){
                break;
            }
            advanceQuery(iz->Type, iz->Length, &queryStart, true);
        }

        long int queryEnd   = 0;
        int seen            = 0;

        /* if alignment starts without clip bump the stop condition */
        if(it->CigarData.front().Type != 'H'
           && it->CigarData.front().Type != 'S'){
            seen += 1;
        }


        for(std::vector<CigarOp>::iterator iz = it->CigarData.begin() ;
            iz != it->CigarData.end() ; iz++){
            /* first matching query position */

            if( (iz->Type == 'H') || (iz->Type == 'S') ){
                seen += 1;
            }
            if(seen > 1){
                break;
            }

            advanceQuery(iz->Type, iz->Length, &queryEnd, true);
        }

        int matchingBases = 0;

        double pctID = percentID(it->CigarData, it->Length, &matchingBases);

        if(matchesPerContig.find(it->RefID) != matchesPerContig.end() ){
            matchesPerContig[it->RefID] += matchingBases;
        }
        else{
            matchesPerContig[it->RefID] = matchingBases;
        }

        /* reverse strand needs to flip */

        if(it->AlignmentFlag & 16){
            if(queryLens.find(it->Name) == queryLens.end()){
                std::cerr << "FATAL: query name not found in query .fai" << std::endl;
                exit(1);
            }

            queryStart = queryLens[it->Name] - queryStart;
            queryEnd   = queryLens[it->Name] - queryEnd  ;

            int tmp = queryStart;
            queryStart = queryEnd;
            queryEnd   = tmp;
        }

        /* position where the alignment starts matching */

        it->AddTag<int>( "QS", "i", queryStart      );

        /* position in the aligment where it stops matching */

        it->AddTag<int>( "QE", "i", queryEnd        );

        /* lenght of the alignment block */

        it->AddTag<int>( "QL", "i", (queryEnd - queryStart));


        it->AddTag<int>( "TS", "i", it->Position);
        it->AddTag<int>( "TE", "i", it->GetEndPosition());

        /* number of matching bases M= */

        it->AddTag<int>( "MB", "i", matchingBases);

        /* percent ID */

        it->AddTag<float>( "PI", "f", pctID);

        /* alignment group ; just another way of saying query name */

        it->AddTag<int>( "BI", "i", blockId);

        if(matchingBases > 200 && pctID > 0.85 ){
            totalAlignedBases += matchingBases;
            reads.push_back(*it);
        }
    }

    sort(reads.begin(), reads.end(),  qStartSort);

    /* best contig match */

    int maxRefId = 0;
    int maxValue = 0;

    for(std::map<int, int>::iterator mp = matchesPerContig.begin();
        mp != matchesPerContig.end(); mp++){
        if(mp->second > maxValue){
            maxRefId = mp->first;
            maxValue = mp->second;
        }
    }

    for(int i = 0; i < reads.size(); i++){

        if(reads[i].RefID != maxRefId){
            continue;
        }
        processed.push_back(reads[i]);
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rcx, 0x60(%rsp)
movq %rsi, 0x88(%rsp)
movl %edx, 0x6c(%rsp)
xorl %eax, %eax
movq %rax, 0x80(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
leaq 0xb0(%rsp), %rcx
movl $0x0, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movq (%rdi), %rbx
movq %rdi, 0xa0(%rsp)
cmpq %rdi, %rbx
je 0xac40
movq 0x60(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x98(%rsp)
leaq 0x38(%rsp), %r14
leaq 0x54(%rsp), %r15
leaq 0x8(%rsp), %r13
movq %r13, %r12
movq %r14, %r13
movq %r15, %r14
movq $0x0, 0x48(%rsp)
movq 0xc8(%rbx), %r15
cmpq 0xd0(%rbx), %r15
je 0xa62c
movzbl (%r15), %eax
cmpl $0x53, %eax
je 0xa610
cmpl $0x48, %eax
jne 0xa62c
movl 0x4(%r15), %esi
movsbl %al, %edi
leaq 0x48(%rsp), %rdx
movl $0x1, %ecx
callq 0xecad
addq $0x8, %r15
jmp 0xa5f9
movq $0x0, 0x58(%rsp)
movq 0xc8(%rbx), %rbp
cmpq 0xd0(%rbx), %rbp
je 0xa691
movb (%rbp), %al
cmpb $0x48, %al
setne %cl
cmpb $0x53, %al
setne %al
andb %cl, %al
movzbl %al, %r15d
movzbl (%rbp), %eax
cmpl $0x53, %eax
je 0xa666
cmpl $0x48, %eax
jne 0xa669
incl %r15d
cmpl $0x1, %r15d
jg 0xa691
movl 0x4(%rbp), %esi
movsbl %al, %edi
leaq 0x58(%rsp), %rdx
movl $0x1, %ecx
callq 0xecad
addq $0x8, %rbp
cmpq 0xd0(%rbx), %rbp
jne 0xa658
leaq 0xc8(%rbx), %rdi
movl $0x0, 0x54(%rsp)
movl 0x30(%rbx), %esi
movq %r14, %r15
movq %r14, %rdx
callq 0xec34
movsd %xmm0, 0x90(%rsp)
leaq 0xb0(%rsp), %r8
movq %r13, %r14
movq %r12, %r13
leaq 0xb8(%rbx), %rsi
movq 0xb8(%rsp), %rcx
movq %r8, %rax
testq %rcx, %rcx
je 0xa6f8
movl (%rsi), %edx
movq %r8, %rax
xorl %edi, %edi
cmpl %edx, 0x20(%rcx)
setl %dil
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdi,8), %rcx
testq %rcx, %rcx
jne 0xa6e1
movq %r8, %rcx
cmpq %r8, %rax
je 0xa70c
movl (%rsi), %ecx
cmpl 0x20(%rax), %ecx
cmovlq %r8, %rax
movq %rax, %rcx
movl 0x54(%rsp), %ebp
cmpq %r8, %rcx
je 0xa726
leaq 0xa8(%rsp), %rdi
callq 0xb7dc
addl %ebp, (%rax)
jmp 0xa735
leaq 0xa8(%rsp), %rdi
callq 0xb7dc
movl %ebp, (%rax)
leaq 0x10(%rbx), %rbp
testb $0x10, 0xc4(%rbx)
je 0xa79b
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0xc44c
cmpq 0x98(%rsp), %rax
je 0xad5c
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0xb840
movq (%rax), %rax
subq 0x48(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0xb840
movq (%rax), %rax
subq 0x58(%rsp), %rax
movslq 0x48(%rsp), %rcx
movq %rax, 0x48(%rsp)
movq %rcx, 0x58(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x2e871(%rip), %rsi     # 0x39020
leaq 0x2e86c(%rip), %rdx     # 0x39022
callq 0xbe08
movq %r14, 0x28(%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
leaq 0x2e8df(%rip), %rsi     # 0x390ae
leaq 0x2e8d9(%rip), %rdx     # 0x390af
callq 0xbe08
movl 0x48(%rsp), %eax
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
leaq 0x4(%rsp), %rcx
callq 0xb628
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xa80d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xa829
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x2e7ec(%rip), %rsi     # 0x39029
leaq 0x2e7e7(%rip), %rdx     # 0x3902b
callq 0xbe08
movq %r14, 0x28(%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
leaq 0x2e851(%rip), %rsi     # 0x390ae
leaq 0x2e84b(%rip), %rdx     # 0x390af
callq 0xbe08
movl 0x58(%rsp), %eax
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
leaq 0x4(%rsp), %rcx
callq 0xb628
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xa89b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xa8b7
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x2e758(%rip), %rsi     # 0x39023
leaq 0x2e753(%rip), %rdx     # 0x39025
callq 0xbe08
movq %r14, 0x28(%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
leaq 0x2e7c3(%rip), %rsi     # 0x390ae
leaq 0x2e7bd(%rip), %rdx     # 0x390af
callq 0xbe08
movl 0x58(%rsp), %eax
subl 0x48(%rsp), %eax
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
leaq 0x4(%rsp), %rcx
callq 0xb628
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xa92d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xa949
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x2e753(%rip), %rsi     # 0x390b0
leaq 0x2e74e(%rip), %rdx     # 0x390b2
callq 0xbe08
movq %r14, 0x28(%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
leaq 0x2e731(%rip), %rsi     # 0x390ae
leaq 0x2e72b(%rip), %rdx     # 0x390af
callq 0xbe08
leaq 0xbc(%rbx), %rcx
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xb628
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xa9b5
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xa9d1
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x2e6ce(%rip), %rsi     # 0x390b3
leaq 0x2e6c9(%rip), %rdx     # 0x390b5
callq 0xbe08
movq %r14, 0x28(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x2e6ac(%rip), %rsi     # 0x390ae
leaq 0x2e6a6(%rip), %rdx     # 0x390af
callq 0xbe08
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x16d74
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0xb628
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xaa4a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xaa66
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x2e5ac(%rip), %rsi     # 0x39026
leaq 0x2e5a7(%rip), %rdx     # 0x39028
callq 0xbe08
movq %r14, 0x28(%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
leaq 0x2e614(%rip), %rsi     # 0x390ae
leaq 0x2e60e(%rip), %rdx     # 0x390af
callq 0xbe08
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0xb628
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xaace
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xaaea
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x2e5b8(%rip), %rsi     # 0x390b6
leaq 0x2e5b3(%rip), %rdx     # 0x390b8
callq 0xbe08
movq %r14, 0x28(%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
leaq 0x2e59b(%rip), %rsi     # 0x390b9
leaq 0x2e595(%rip), %rdx     # 0x390ba
callq 0xbe08
cvtsd2ss 0x90(%rsp), %xmm0
movss %xmm0, 0x4(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
leaq 0x4(%rsp), %rcx
callq 0xb8bc
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xab63
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xab7f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x2e528(%rip), %rsi     # 0x390bb
leaq 0x2e523(%rip), %rdx     # 0x390bd
callq 0xbe08
movq %r14, 0x28(%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
leaq 0x2e4fb(%rip), %rsi     # 0x390ae
leaq 0x2e4f5(%rip), %rdx     # 0x390af
callq 0xbe08
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
leaq 0x6c(%rsp), %rcx
callq 0xb628
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xabe9
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xac05
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movsd 0x90(%rsp), %xmm0
ucomisd 0x2e3f2(%rip), %xmm0    # 0x39008
jbe 0xac2f
cmpl $0xc9, 0x54(%rsp)
jl 0xac2f
leaq 0x70(%rsp), %rdi
movq %rbp, %rsi
callq 0xba78
movq (%rbx), %rbx
cmpq 0xa0(%rsp), %rbx
jne 0xa5e0
movq 0x70(%rsp), %rbx
movq 0x78(%rsp), %r14
movabsq $-0x7d05f417d05f417d, %r15 # imm = 0x82FA0BE82FA0BE83
cmpq %r14, %rbx
je 0xac98
movq %r14, %rax
subq %rbx, %rax
sarq $0x3, %rax
imulq %r15, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x143f(%rip), %rcx     # 0x983c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xca66
leaq -0x1451(%rip), %rdx     # 0x983c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xcb6c
movq 0xc0(%rsp), %rax
leaq 0xb0(%rsp), %rcx
cmpq %rcx, %rax
je 0xacdc
xorl %ebp, %ebp
xorl %ebx, %ebx
movq 0x88(%rsp), %r13
movl 0x24(%rax), %ecx
cmpl %ebp, %ecx
jle 0xacc5
movl 0x20(%rax), %ebx
movl %ecx, %ebp
movq %rax, %rdi
callq 0x52f0
leaq 0xb0(%rsp), %rcx
cmpq %rcx, %rax
jne 0xacb9
jmp 0xace6
xorl %ebx, %ebx
movq 0x88(%rsp), %r13
movq 0x70(%rsp), %rsi
cmpq %rsi, 0x78(%rsp)
je 0xad31
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpl %ebx, 0xa8(%rsi,%r14)
jne 0xad0d
addq %r14, %rsi
movq %r13, %rdi
callq 0xba78
incq %r12
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rax
subq %rsi, %rax
sarq $0x3, %rax
imulq %r15, %rax
addq $0x158, %r14            # imm = 0x158
cmpq %r12, %rax
ja 0xacf8
leaq 0xa8(%rsp), %rdi
callq 0xbc78
leaq 0x70(%rsp), %rdi
callq 0xbaa8
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4528d(%rip), %rdi     # 0x4fff0
leaq 0x2e31c(%rip), %rsi     # 0x39086
movl $0x29, %edx
callq 0x54f0
movq 0x45275(%rip), %rdi     # 0x4fff0
callq 0x5200
movl $0x1, %edi
callq 0x5560
jmp 0xadff
jmp 0xadff
jmp 0xadff
jmp 0xadff
jmp 0xadc3
jmp 0xadff
jmp 0xadbe
jmp 0xadc3
jmp 0xadff
jmp 0xadbe
jmp 0xadc3
jmp 0xadff
jmp 0xadbe
jmp 0xadc3
jmp 0xadff
jmp 0xadbe
jmp 0xadc3
jmp 0xadff
jmp 0xadbe
jmp 0xadc3
jmp 0xadff
jmp 0xadbe
jmp 0xadff
jmp 0xadbe
jmp 0xadc3
jmp 0xadff
movq %rax, %r15
jmp 0xaddd
movq %rax, %r15
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xaddd
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xae02
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0xae02
jmp 0xadff
jmp 0xadff
movq %rax, %r15
leaq 0xa8(%rsp), %rdi
callq 0xbc78
leaq 0x70(%rsp), %rdi
callq 0xbaa8
movq %r15, %rdi
callq 0x57b0