// File: /workspace/github_cpp_makefile_star2_O3/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 %rsi, %r15
movl %edx, 0x6c(%rsp)
xorl %r12d, %r12d
movq %r12, 0x80(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
leaq 0xb0(%rsp), %rbp
movl $0x0, (%rbp)
movq %r12, 0x8(%rbp)
movq %rbp, 0x10(%rbp)
movq %rbp, 0x18(%rbp)
movq %r12, 0x20(%rbp)
movq (%rdi), %rbx
movabsq $-0x7d05f417d05f417d, %r13 # imm = 0x82FA0BE82FA0BE83
movq %rdi, 0xa0(%rsp)
cmpq %rdi, %rbx
je 0xac5a
movq %r15, 0x90(%rsp)
leaq 0x8(%rcx), %rax
movq %rax, 0x98(%rsp)
leaq 0x18(%rsp), %r15
movq %rcx, 0x60(%rsp)
movq %r15, %r13
movq $0x0, 0x50(%rsp)
leaq 0xc8(%rbx), %rbp
movq 0xc8(%rbx), %r15
movq 0xd0(%rbx), %rax
cmpq %rax, %r15
je 0xa60c
movzbl (%r15), %ecx
cmpl $0x53, %ecx
je 0xa5e4
cmpl $0x48, %ecx
jne 0xa617
movl 0x4(%r15), %esi
movsbl %cl, %edi
leaq 0x50(%rsp), %rdx
movl $0x1, %ecx
callq 0xebed
addq $0x8, %r15
movq 0xd0(%rbx), %rax
cmpq %rax, %r15
jne 0xa5d6
jmp 0xa61a
movq $0x0, 0x58(%rsp)
jmp 0xa67b
movq %rax, %r15
movq (%rbp), %r14
movq $0x0, 0x58(%rsp)
cmpq %r15, %r14
je 0xa67b
movb (%r14), %al
cmpb $0x48, %al
setne %cl
cmpb $0x53, %al
setne %dl
andb %cl, %dl
movzbl %dl, %r15d
addq $0x8, %r14
cmpb $0x53, %al
je 0xa64f
movzbl %al, %ecx
cmpl $0x48, %ecx
jne 0xa657
testl %r15d, %r15d
jg 0xa67b
incl %r15d
movl -0x4(%r14), %esi
movsbl %al, %edi
leaq 0x58(%rsp), %rdx
movl $0x1, %ecx
callq 0xebed
cmpq 0xd0(%rbx), %r14
je 0xa67b
movb (%r14), %al
jmp 0xa63f
movl $0x0, 0x4c(%rsp)
movl 0x30(%rbx), %esi
movq %rbp, %rdi
leaq 0x4c(%rsp), %rdx
callq 0xeb74
movsd %xmm0, 0x88(%rsp)
leaq 0x38(%rsp), %r14
leaq 0x28(%rsp), %r12
movq %r13, %r15
leaq 0xb8(%rbx), %rsi
movq 0xb8(%rsp), %r8
testq %r8, %r8
je 0xa702
movl (%rsi), %eax
leaq 0xb0(%rsp), %r10
movq %r10, %r9
movq %r9, %rcx
movq %r8, %rdx
movl 0x20(%r8), %edi
xorl %r8d, %r8d
cmpl %eax, %edi
setl %r8b
movq %rdx, %r9
cmovlq %rcx, %r9
movq 0x10(%rdx,%r8,8), %r8
testq %r8, %r8
jne 0xa6ca
cmpq %r10, %r9
je 0xa702
cmpl %eax, %edi
cmovlq %rcx, %rdx
cmpl 0x20(%rdx), %eax
jge 0xabd4
movl 0x4c(%rsp), %ebp
leaq 0xa8(%rsp), %rdi
callq 0xb76e
movl %ebp, (%rax)
leaq 0x10(%rbx), %rbp
testb $0x10, 0xc4(%rbx)
je 0xa77b
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0xc3ce
cmpq 0x98(%rsp), %rax
je 0xad01
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0xb7d4
movq (%rax), %rax
subq 0x50(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0xb7d4
movq (%rax), %rax
subq 0x58(%rsp), %rax
movslq 0x50(%rsp), %rcx
movq %rax, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
movq %r14, 0x28(%rsp)
movq %r12, %rdi
leaq 0x2e896(%rip), %rsi     # 0x39020
leaq 0x2e891(%rip), %rdx     # 0x39022
callq 0xbdac
movq %r15, 0x8(%rsp)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x2e904(%rip), %rsi     # 0x390ae
leaq 0x2e8fe(%rip), %rdx     # 0x390af
callq 0xbdac
movl 0x50(%rsp), %eax
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
leaq 0x4(%rsp), %rcx
callq 0xb5ba
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa7e8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xa7ff
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r14, 0x28(%rsp)
movq %r12, %rdi
leaq 0x2e81b(%rip), %rsi     # 0x39029
leaq 0x2e816(%rip), %rdx     # 0x3902b
callq 0xbdac
movq %r15, 0x8(%rsp)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x2e880(%rip), %rsi     # 0x390ae
leaq 0x2e87a(%rip), %rdx     # 0x390af
callq 0xbdac
movl 0x58(%rsp), %eax
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
leaq 0x4(%rsp), %rcx
callq 0xb5ba
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa86c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xa883
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r14, 0x28(%rsp)
movq %r12, %rdi
leaq 0x2e791(%rip), %rsi     # 0x39023
leaq 0x2e78c(%rip), %rdx     # 0x39025
callq 0xbdac
movq %r15, 0x8(%rsp)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x2e7fc(%rip), %rsi     # 0x390ae
leaq 0x2e7f6(%rip), %rdx     # 0x390af
callq 0xbdac
movl 0x58(%rsp), %eax
subl 0x50(%rsp), %eax
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
leaq 0x4(%rsp), %rcx
callq 0xb5ba
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa8f4
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xa90b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r14, 0x28(%rsp)
movq %r12, %rdi
leaq 0x2e796(%rip), %rsi     # 0x390b0
leaq 0x2e791(%rip), %rdx     # 0x390b2
callq 0xbdac
movq %r15, 0x8(%rsp)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x2e774(%rip), %rsi     # 0x390ae
leaq 0x2e76e(%rip), %rdx     # 0x390af
callq 0xbdac
leaq 0xbc(%rbx), %rcx
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xb5ba
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa972
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xa989
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r14, 0x28(%rsp)
movq %r12, %rdi
leaq 0x2e71b(%rip), %rsi     # 0x390b3
leaq 0x2e716(%rip), %rdx     # 0x390b5
callq 0xbdac
movq %r15, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
leaq 0x2e6f9(%rip), %rsi     # 0x390ae
leaq 0x2e6f3(%rip), %rdx     # 0x390af
callq 0xbdac
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x16c2c
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0xb5ba
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa9fd
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xaa14
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r14, 0x28(%rsp)
movq %r12, %rdi
leaq 0x2e603(%rip), %rsi     # 0x39026
leaq 0x2e5fe(%rip), %rdx     # 0x39028
callq 0xbdac
movq %r15, 0x8(%rsp)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x2e66b(%rip), %rsi     # 0x390ae
leaq 0x2e665(%rip), %rdx     # 0x390af
callq 0xbdac
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
leaq 0x4c(%rsp), %rcx
callq 0xb5ba
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xaa79
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xaa90
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r14, 0x28(%rsp)
movq %r12, %rdi
leaq 0x2e617(%rip), %rsi     # 0x390b6
leaq 0x2e612(%rip), %rdx     # 0x390b8
callq 0xbdac
movq %r15, 0x8(%rsp)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x2e5fa(%rip), %rsi     # 0x390b9
leaq 0x2e5f4(%rip), %rdx     # 0x390ba
callq 0xbdac
cvtsd2ss 0x88(%rsp), %xmm0
movss %xmm0, 0x4(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
leaq 0x4(%rsp), %rcx
callq 0xb850
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xab04
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xab1b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movq %r14, 0x28(%rsp)
movq %r12, %rdi
leaq 0x2e591(%rip), %rsi     # 0x390bb
leaq 0x2e58c(%rip), %rdx     # 0x390bd
callq 0xbdac
movq %r15, 0x8(%rsp)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x2e564(%rip), %rsi     # 0x390ae
leaq 0x2e55e(%rip), %rdx     # 0x390af
callq 0xbdac
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
leaq 0x6c(%rsp), %rcx
callq 0xb5ba
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xab80
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0xab97
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
movsd 0x88(%rsp), %xmm0
ucomisd 0x2e460(%rip), %xmm0    # 0x39008
jbe 0xabc1
cmpl $0xc9, 0x4c(%rsp)
jl 0xabc1
leaq 0x70(%rsp), %rdi
movq %rbp, %rsi
callq 0xba0c
movq (%rbx), %rbx
cmpq 0xa0(%rsp), %rbx
jne 0xa5b0
jmp 0xabec
movl 0x4c(%rsp), %ebp
leaq 0xa8(%rsp), %rdi
callq 0xb76e
addl %ebp, (%rax)
jmp 0xa715
movq 0x70(%rsp), %rbx
movq 0x78(%rsp), %r14
cmpq %r14, %rbx
movq 0x90(%rsp), %r15
movl $0x0, %r12d
movabsq $-0x7d05f417d05f417d, %r13 # imm = 0x82FA0BE82FA0BE83
leaq 0xb0(%rsp), %rbp
je 0xac5a
movq %r14, %rax
subq %rbx, %rax
sarq $0x3, %rax
imulq %r13, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x1411(%rip), %rcx     # 0x982c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xc9fa
leaq -0x1423(%rip), %rdx     # 0x982c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xcb00
movq 0xc0(%rsp), %rax
cmpq %rbp, %rax
je 0xac86
xorl %ebx, %ebx
xorl %r12d, %r12d
movl 0x24(%rax), %ecx
cmpl %ebx, %ecx
jle 0xac79
movl 0x20(%rax), %r12d
movl %ecx, %ebx
movq %rax, %rdi
callq 0x52f0
cmpq %rbp, %rax
jne 0xac6c
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rax
cmpq %rsi, %rax
je 0xacd6
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpl %r12d, 0xa8(%rsi,%rbx)
jne 0xacb9
addq %rbx, %rsi
movq %r15, %rdi
callq 0xba0c
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rax
incq %r14
movq %rax, %rcx
subq %rsi, %rcx
sarq $0x3, %rcx
imulq %r13, %rcx
addq $0x158, %rbx            # imm = 0x158
cmpq %r14, %rcx
ja 0xac9a
leaq 0xa8(%rsp), %rdi
callq 0xbc16
leaq 0x70(%rsp), %rdi
callq 0xba3c
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x452e8(%rip), %rdi     # 0x4fff0
leaq 0x2e377(%rip), %rsi     # 0x39086
movl $0x29, %edx
callq 0x54f0
movq 0x452d0(%rip), %rdi     # 0x4fff0
callq 0x5200
movl $0x1, %edi
callq 0x5560
jmp 0xada4
jmp 0xada4
jmp 0xada4
jmp 0xada4
jmp 0xad68
jmp 0xada4
jmp 0xad63
jmp 0xad68
jmp 0xada4
jmp 0xad63
jmp 0xad68
jmp 0xada4
jmp 0xad63
jmp 0xad68
jmp 0xada4
jmp 0xad63
jmp 0xad68
jmp 0xada4
jmp 0xad63
jmp 0xada4
jmp 0xad63
jmp 0xad68
jmp 0xada4
jmp 0xad63
jmp 0xad68
jmp 0xada4
movq %rax, %r14
jmp 0xad82
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xad82
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5470
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xada7
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0xada7
jmp 0xada4
jmp 0xada4
movq %rax, %r14
leaq 0xa8(%rsp), %rdi
callq 0xbc16
leaq 0x70(%rsp), %rdi
callq 0xba3c
movq %r14, %rdi
callq 0x57b0