// File: /workspace/github_cpp_makefile_star2_O2/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 $0xe8, %rsp
movq %rsi, 0xa0(%rsp)
movq %rdi, %r12
leaq 0x54(%rsp), %rax
movl %edx, (%rax)
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rax
andq $0x0, 0x10(%rax)
leaq 0xc0(%rsp), %rdx
andl $0x0, (%rdx)
andq $0x0, 0x8(%rdx)
movq %rdx, 0x10(%rdx)
movq %rdx, 0x18(%rdx)
andq $0x0, 0x20(%rdx)
movaps %xmm0, (%rax)
movq %rcx, 0x48(%rsp)
leaq 0x8(%rcx), %rax
movq %rax, 0xa8(%rsp)
pushq $0x1
popq %rbp
leaq 0x18(%rsp), %r13
movq %rdi, 0xb0(%rsp)
movq (%r12), %r12
cmpq 0xb0(%rsp), %r12
je 0x8f01
andq $0x0, 0x18(%rsp)
leaq 0x10(%r12), %r14
leaq 0xc8(%r12), %r15
movq 0xc8(%r12), %rbx
movq 0xd0(%r12), %rax
cmpq %rax, %rbx
je 0x8ad1
movzbl (%rbx), %ecx
cmpl $0x53, %ecx
je 0x8abb
cmpl $0x48, %ecx
jne 0x8ad1
movl 0x4(%rbx), %esi
movsbl %cl, %edi
movq %r13, %rdx
movl %ebp, %ecx
callq 0xc703
addq $0x8, %rbx
jmp 0x8aa1
andq $0x0, 0x20(%rsp)
movq (%r15), %rbx
movb (%rbx), %cl
cmpb $0x48, %cl
setne %dl
cmpb $0x53, %cl
setne %cl
andb %dl, %cl
movzbl %cl, %r13d
cmpq %rax, %rbx
je 0x8b28
movzbl (%rbx), %eax
cmpl $0x53, %eax
je 0x8b00
cmpl $0x48, %eax
jne 0x8b08
testl %r13d, %r13d
jg 0x8b28
incl %r13d
movl 0x4(%rbx), %esi
movsbl %al, %edi
leaq 0x20(%rsp), %rdx
movl %ebp, %ecx
callq 0xc703
addq $0x8, %rbx
movq 0xd0(%r12), %rax
jmp 0x8aee
andl $0x0, 0x14(%rsp)
movl 0x30(%r12), %esi
movq %r15, %rdi
leaq 0x14(%rsp), %rdx
callq 0xc6a4
movsd %xmm0, 0x58(%rsp)
leaq 0xb8(%r12), %r15
leaq 0xb8(%rsp), %rdi
movq %r15, %rsi
callq 0xa2f2
movl 0x14(%rsp), %ebx
leaq 0xc0(%rsp), %rcx
cmpq %rcx, %rax
je 0x8b82
leaq 0xb8(%rsp), %rdi
movq %r15, %rsi
callq 0x98a4
addl %ebx, (%rax)
jmp 0x8b94
leaq 0xb8(%rsp), %rdi
movq %r15, %rsi
callq 0x98a4
movl %ebx, (%rax)
testb $0x10, 0xc4(%r12)
je 0x8bf8
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0xa594
cmpq 0xa8(%rsp), %rax
je 0x8fbd
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x98f2
movq (%rax), %rax
subq 0x18(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x98f2
movq (%rax), %rax
subq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq %rax, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x2641c(%rip), %rsi     # 0x2f020
leaq 0xe(%rsp), %rdx
callq 0x96f2
leaq 0x80(%rsp), %r15
movq %r15, %rdi
leaq 0x2778a(%rip), %rsi     # 0x303aa
leaq 0xf(%rsp), %rdx
callq 0x96f2
movl 0x18(%rsp), %eax
movl %eax, 0x10(%rsp)
movq %r14, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
leaq 0x10(%rsp), %rcx
callq 0x972e
movq %r15, %rdi
callq 0x58f8
movq %rbx, %rdi
callq 0x58f8
movq %rbx, %rdi
leaq 0x263c5(%rip), %rsi     # 0x2f029
leaq 0xe(%rsp), %rdx
callq 0x96f2
movq %r15, %rdi
leaq 0x27732(%rip), %rsi     # 0x303aa
leaq 0xf(%rsp), %rdx
callq 0x96f2
movl 0x20(%rsp), %eax
movl %eax, 0x10(%rsp)
movq %r14, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
leaq 0x10(%rsp), %rcx
callq 0x972e
movq %r15, %rdi
callq 0x58f8
movq %rbx, %rdi
callq 0x58f8
movq %rbx, %rdi
leaq 0x26367(%rip), %rsi     # 0x2f023
leaq 0xe(%rsp), %rdx
callq 0x96f2
movq %r15, %rdi
leaq 0x276da(%rip), %rsi     # 0x303aa
leaq 0xf(%rsp), %rdx
callq 0x96f2
movl 0x20(%rsp), %eax
subl 0x18(%rsp), %eax
movl %eax, 0x10(%rsp)
movq %r14, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
leaq 0x10(%rsp), %r13
movq %r13, %rcx
callq 0x972e
movq %r15, %rdi
callq 0x58f8
movq %rbx, %rdi
callq 0x58f8
movq %rbx, %rdi
leaq 0x26395(%rip), %rsi     # 0x2f0b0
movq %r13, %rdx
callq 0x96f2
movq %r15, %rdi
leaq 0x2767d(%rip), %rsi     # 0x303aa
leaq 0xe(%rsp), %rdx
callq 0x96f2
leaq 0xbc(%r12), %rcx
movq %r14, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
callq 0x972e
movq %r15, %rdi
callq 0x58f8
movq %rbx, %rdi
callq 0x58f8
movq %rbx, %rdi
leaq 0x26347(%rip), %rsi     # 0x2f0b3
leaq 0xe(%rsp), %rdx
callq 0x96f2
movq %r15, %rdi
leaq 0x2762a(%rip), %rsi     # 0x303aa
leaq 0xf(%rsp), %rdx
callq 0x96f2
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11df4
leaq 0x28(%rsp), %rbx
leaq 0x80(%rsp), %r15
leaq 0x10(%rsp), %r13
movl %eax, 0x10(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x972e
movq %r15, %rdi
callq 0x58f8
movq %rbx, %rdi
callq 0x58f8
movq %rbx, %rdi
leaq 0x2624f(%rip), %rsi     # 0x2f026
movq %r13, %rdx
callq 0x96f2
movq %r15, %rdi
leaq 0x275c1(%rip), %rsi     # 0x303aa
leaq 0xe(%rsp), %rdx
callq 0x96f2
movq %r14, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
leaq 0x14(%rsp), %rcx
callq 0x972e
movq %r15, %rdi
callq 0x58f8
movq %rbx, %rdi
callq 0x58f8
movq %rbx, %rdi
leaq 0x26291(%rip), %rsi     # 0x2f0b6
leaq 0xe(%rsp), %rdx
callq 0x96f2
movq %r15, %rdi
leaq 0x26280(%rip), %rsi     # 0x2f0b9
leaq 0xf(%rsp), %rdx
callq 0x96f2
cvtsd2ss 0x58(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movq %r14, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
leaq 0x10(%rsp), %r13
movq %r13, %rcx
callq 0x9958
movq %r15, %rdi
callq 0x58f8
movq %rbx, %rdi
callq 0x58f8
movq %rbx, %rdi
leaq 0x26237(%rip), %rsi     # 0x2f0bb
movq %r13, %rdx
callq 0x96f2
movq %r15, %rdi
leaq 0x27514(%rip), %rsi     # 0x303aa
leaq 0xe(%rsp), %rdx
callq 0x96f2
leaq 0x18(%rsp), %r13
movq %r14, %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
leaq 0x54(%rsp), %rcx
callq 0x972e
movq %r15, %rdi
callq 0x58f8
movq %rbx, %rdi
callq 0x58f8
movsd 0x58(%rsp), %xmm0
ucomisd 0x2612d(%rip), %xmm0    # 0x2f008
jbe 0x8a74
cmpl $0xc9, 0x14(%rsp)
jl 0x8a74
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x9ad6
jmp 0x8a74
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
leaq -0xaf6(%rip), %rdx      # 0x841c
callq 0xabc2
xorl %ebx, %ebx
movq 0xd0(%rsp), %rax
xorl %ebp, %ebp
leaq 0xc0(%rsp), %r14
cmpq %r14, %rax
je 0x8f46
movl 0x24(%rax), %ecx
cmpl %ebp, %ecx
jle 0x8f3c
movl 0x20(%rax), %ebx
movl %ecx, %ebp
movq %rax, %rdi
callq 0x52d0
jmp 0x8f2b
xorl %r14d, %r14d
movl $0x158, %r15d           # imm = 0x158
xorl %r12d, %r12d
movq 0xa0(%rsp), %r13
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rax
subq %rsi, %rax
cqto
idivq %r15
cmpq %r12, %rax
jbe 0x8f92
cmpl %ebx, 0xa8(%rsi,%r14)
jne 0x8f86
addq %r14, %rsi
movq %r13, %rdi
callq 0x9ad6
incq %r12
addq $0x158, %r14            # imm = 0x158
jmp 0x8f5a
leaq 0xb8(%rsp), %rdi
callq 0x9c56
leaq 0x60(%rsp), %rdi
callq 0x9b06
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4202c(%rip), %rdi     # 0x4aff0
leaq 0x260bb(%rip), %rsi     # 0x2f086
callq 0x5430
movq %rax, %rdi
callq 0x51f0
pushq $0x1
popq %rdi
callq 0x5550
jmp 0x903b
jmp 0x903b
jmp 0x903b
jmp 0x903b
jmp 0x903b
jmp 0x9010
jmp 0x901b
jmp 0x9010
jmp 0x903b
jmp 0x901b
jmp 0x9010
jmp 0x903b
jmp 0x901b
jmp 0x9010
jmp 0x903b
jmp 0x903b
jmp 0x9010
jmp 0x9010
jmp 0x901b
jmp 0x901b
jmp 0x903b
jmp 0x9010
jmp 0x901b
jmp 0x903b
movq %rax, %rbx
jmp 0x902b
jmp 0x901b
jmp 0x903b
jmp 0x903b
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x58f8
leaq 0x28(%rsp), %rdi
callq 0x58f8
jmp 0x903e
jmp 0x903b
jmp 0x903b
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x9c56
leaq 0x60(%rsp), %rdi
callq 0x9b06
movq %rbx, %rdi
callq 0x57d0