// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/src/annotate.cpp
// Function: annotate(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int annotate(std::string bfName,
             std::string out,
             std::string queryFa)
{
    BamReader reader;

    errorHandler EH;

    std::map<std::string, long int> queryLens;

    if(!loadFai(queryFa, queryLens)){
        EH.croak(EH.COULD_NOT_OPEN_FAI, true );
    }

    if(!reader.Open(bfName)){
        EH.croak(EH.COULD_NOT_OPEN_BAM, true );
    }

    const SamHeader header     = reader.GetHeader();
    const RefVector references = reader.GetReferenceData();

    BamWriter writer;
    if ( !writer.Open(out, header, references) ) {
        EH.croak(EH.COULD_NOT_OPEN_BAM, true);
    }


    EH.printInfo("Starting to annotate alignment blocks.");

    BamAlignment al;

    if(!reader.GetNextAlignment(al)){
        EH.croak(EH.NO_READS, true);
    }

    std::list<BamAlignment> readBuffer;

    int blockId = 0;
    int numberOfAlignments = 0;

    while(reader.GetNextAlignment(al)){

        numberOfAlignments+=1;

        if((numberOfAlignments % 1000) == 0 ){
            std::stringstream ss;
            ss << "Number of contigs / alignments processed: " << blockId << " " << numberOfAlignments;
            EH.printInfo(ss.str());
        }


        if(readBuffer.empty()){
            readBuffer.push_back(al);
            continue;
        }
        else{
            if(readBuffer.back().Name != al.Name){
                blockId += 1;

                std::vector<BamAlignment> filtReads;

                processBlock(readBuffer, filtReads, blockId, queryLens);
                chainBlock(filtReads, writer,  references);

                readBuffer.clear()      ;
                readBuffer.push_back(al);
            }
            else{
                 readBuffer.push_back(al);
            }
        }
    }

    blockId += 1;

    std::vector<BamAlignment> filtReadsB;
    processBlock(readBuffer, filtReadsB, blockId, queryLens );
    chainBlock(filtReadsB, writer, references    );

    reader.Close();
    writer.Close();

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x548, %rsp            # imm = 0x548
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x10(%rsp), %rdi
callq 0x12804
leaq 0xa0(%rsp), %rdi
callq 0xc788
leaq 0x78(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x70(%rsp), %rsi
movq %r15, %rdi
callq 0x905d
testb %al, %al
jne 0x9259
leaq 0xa0(%rsp), %rdi
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0xc93a
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x12944
testb %al, %al
jne 0x927c
leaq 0xa0(%rsp), %rdi
pushq $0x1
popq %rdx
xorl %esi, %esi
callq 0xc93a
leaq 0x3f0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x128bc
leaq 0x10(%rsp), %rdi
callq 0x128f8
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0x9b82
leaq 0x18(%rsp), %rdi
callq 0x12998
leaq 0x18(%rsp), %rdi
leaq 0x3f0(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq %rbx, %rsi
callq 0x12a38
testb %al, %al
jne 0x92df
leaq 0xa0(%rsp), %rdi
pushq $0x1
popq %rdx
xorl %esi, %esi
callq 0xc93a
leaq 0x25dd8(%rip), %rsi     # 0x2f0be
leaq 0x50(%rsp), %rdi
leaq 0x110(%rsp), %rdx
callq 0x96f2
leaq 0xa0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xc9bc
leaq 0x50(%rsp), %rdi
callq 0x58f8
leaq 0x298(%rsp), %rdi
callq 0x112b0
leaq 0x10(%rsp), %rdi
leaq 0x298(%rsp), %rsi
callq 0x128e0
testb %al, %al
jne 0x934a
leaq 0xa0(%rsp), %rdi
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0xc93a
leaq 0x20(%rsp), %r14
movq %r14, 0x8(%r14)
movq %r14, (%r14)
andq $0x0, 0x10(%r14)
movl $0x0, 0xc(%rsp)
leaq 0x10(%rsp), %r13
leaq 0x298(%rsp), %rbp
movl $0x3e8, %ebx            # imm = 0x3E8
leaq 0xf0(%rsp), %r12
xorl %r15d, %r15d
incl %r15d
movq %r13, %rdi
movq %rbp, %rsi
callq 0x128e0
testb %al, %al
je 0x94ca
movl %r15d, %eax
cltd
idivl %ebx
testl %edx, %edx
jne 0x941c
leaq 0x110(%rsp), %rdi
callq 0x52e0
leaq 0x120(%rsp), %rdi
leaq 0x25d29(%rip), %rsi     # 0x2f0e5
callq 0x5430
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x57b0
movq %rax, %rdi
leaq 0x2726d(%rip), %rsi     # 0x30644
callq 0x5430
movq %rax, %rdi
movl %r15d, %esi
callq 0x57b0
movq %r12, %rdi
leaq 0x128(%rsp), %rsi
callq 0x56e0
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
callq 0xc9bc
movq %r12, %rdi
callq 0x58f8
leaq 0x110(%rsp), %rdi
callq 0x5330
cmpq %r14, 0x20(%rsp)
jne 0x9436
incl %r15d
movq %r14, %rdi
movq %rbp, %rsi
callq 0x9bdc
jmp 0x9383
movq 0x28(%rsp), %rdi
addq $0x10, %rdi
movq %rbp, %rsi
callq 0x9be7
testb %al, %al
jne 0x945b
movq %r14, %rdi
movq %rbp, %rsi
callq 0x9bdc
jmp 0x9380
movl 0xc(%rsp), %edx
incl %edx
xorps %xmm0, %xmm0
movaps %xmm0, 0x110(%rsp)
andq $0x0, 0x120(%rsp)
movq %r14, %rdi
leaq 0x110(%rsp), %rsi
movl %edx, 0xc(%rsp)
leaq 0x70(%rsp), %rcx
callq 0x8a03
leaq 0x110(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x84d5
movq %r14, %rdi
callq 0x9bf2
movq %r14, %rdi
movq %rbp, %rsi
callq 0x9bdc
leaq 0x110(%rsp), %rdi
callq 0x9b06
jmp 0x9380
movl 0xc(%rsp), %edx
incl %edx
xorps %xmm0, %xmm0
leaq 0x110(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x20(%rsp), %rdi
leaq 0x70(%rsp), %rcx
callq 0x8a03
leaq 0x110(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x84d5
leaq 0x10(%rsp), %rdi
callq 0x12880
leaq 0x18(%rsp), %rdi
callq 0x12a0e
leaq 0x110(%rsp), %rdi
callq 0x9b06
leaq 0x20(%rsp), %rdi
callq 0x9cae
leaq 0x298(%rsp), %rdi
callq 0x1151e
leaq 0x18(%rsp), %rdi
callq 0x129d0
leaq 0x38(%rsp), %rdi
callq 0x9c0a
leaq 0x3f0(%rsp), %rdi
callq 0x12e2e
leaq 0x70(%rsp), %rdi
callq 0x9ce2
leaq 0xa0(%rsp), %rdi
callq 0x9c2c
leaq 0x10(%rsp), %rdi
callq 0x12842
xorl %eax, %eax
addq $0x548, %rsp            # imm = 0x548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x95d1
jmp 0x9609
jmp 0x95c2
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x58f8
jmp 0x9623
jmp 0x95c2
movq %rax, %rbx
jmp 0x962d
jmp 0x95cc
movq %rax, %rbx
jmp 0x965b
movq %rax, %rbx
jmp 0x9616
movq %rax, %rbx
jmp 0x9623
movq %rax, %rbx
jmp 0x9637
movq %rax, %rbx
jmp 0x9644
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x9b06
jmp 0x960c
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x58f8
jmp 0x95fa
jmp 0x9609
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x5330
jmp 0x960c
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x9cae
leaq 0x298(%rsp), %rdi
callq 0x1151e
leaq 0x18(%rsp), %rdi
callq 0x129d0
leaq 0x38(%rsp), %rdi
callq 0x9c0a
leaq 0x3f0(%rsp), %rdi
callq 0x12e2e
leaq 0x70(%rsp), %rdi
callq 0x9ce2
leaq 0xa0(%rsp), %rdi
callq 0x9c2c
leaq 0x10(%rsp), %rdi
callq 0x12842
movq %rbx, %rdi
callq 0x57d0
nop