// File: /workspace/github_cpp_makefile_star2_O1/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 $0x538, %rsp            # imm = 0x538
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
movq %rsp, %rdi
callq 0x178ec
leaq 0x80(%rsp), %rdi
callq 0xed40
leaq 0xd8(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0xd0(%rsp), %rsi
movq %r15, %rdi
callq 0xae21
testb %al, %al
jne 0xb105
leaq 0x80(%rsp), %rdi
movl $0x6, %esi
movl $0x1, %edx
callq 0xef6c
movq %rsp, %rdi
movq %r14, %rsi
callq 0x17a32
testb %al, %al
jne 0xb128
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xef6c
leaq 0x3e0(%rsp), %rdi
movq %rsp, %rsi
callq 0x179a8
movq %rsp, %rdi
callq 0x179e4
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0xbb48
leaq 0x8(%rsp), %rdi
callq 0x17a84
leaq 0x8(%rsp), %rdi
leaq 0x3e0(%rsp), %rdx
leaq 0x48(%rsp), %rcx
movq %rbx, %rsi
callq 0x17b2a
testb %al, %al
jne 0xb189
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xef6c
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2df25(%rip), %rsi     # 0x390be
leaq 0x2df44(%rip), %rdx     # 0x390e4
leaq 0x60(%rsp), %rdi
callq 0xbe08
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xf05c
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xb1d3
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x288(%rsp), %rdi
callq 0x15f60
movq %rsp, %rdi
leaq 0x288(%rsp), %rsi
callq 0x179cc
testb %al, %al
jne 0xb20b
leaq 0x80(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0xef6c
leaq 0x10(%rsp), %r14
movq %r14, 0x8(%r14)
movq %r14, (%r14)
movq $0x0, 0x10(%r14)
xorl %ebx, %ebx
movq %rsp, %rbp
leaq 0x288(%rsp), %r12
xorl %r13d, %r13d
incl %r13d
movq %rbp, %rdi
movq %r12, %rsi
callq 0x179cc
testb %al, %al
je 0xb40a
imull $0x26e978d5, %r13d, %eax # imm = 0x26E978D5
addl $0x10624d8, %eax        # imm = 0x10624D8
rorl $0x3, %eax
cmpl $0x418936, %eax         # imm = 0x418936
ja 0xb317
leaq 0x100(%rsp), %rdi
callq 0x5300
movl $0x2a, %edx
leaq 0x110(%rsp), %rdi
leaq 0x2de65(%rip), %rsi     # 0x390e5
callq 0x54f0
leaq 0x110(%rsp), %rdi
movl %ebx, %esi
callq 0x5780
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2f3ad(%rip), %rsi     # 0x3a653
callq 0x54f0
movq %r15, %rdi
movl %r13d, %esi
callq 0x5780
leaq 0x28(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x56c0
leaq 0x80(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xf05c
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xb2f6
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x100(%rsp), %rdi
movq 0x44ca3(%rip), %rsi     # 0x4ffa8
callq 0x5340
leaq 0x180(%rsp), %rdi
callq 0x5160
cmpq %r14, 0x10(%rsp)
jne 0xb33e
movq %r14, %rdi
movq %r12, %rsi
callq 0xe086
movq %rax, %rdi
movq %r14, %rsi
callq 0x5080
incq 0x20(%rsp)
jmp 0xb22f
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdx
cmpq 0x290(%rsp), %rdx
jne 0xb378
testq %rdx, %rdx
je 0xb36b
movq 0x288(%rsp), %rsi
movq 0x10(%rax), %rdi
callq 0x5480
testl %eax, %eax
jne 0xb378
movq %r14, %rdi
movq %r12, %rsi
callq 0xe086
jmp 0xb329
incl %ebx
xorps %xmm0, %xmm0
movaps %xmm0, 0x100(%rsp)
movq $0x0, 0x110(%rsp)
movq %r14, %rdi
leaq 0x100(%rsp), %rsi
movl %ebx, %edx
leaq 0xd0(%rsp), %rcx
callq 0xa55a
leaq 0x100(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x9a4e
movq %r14, %rdi
callq 0xbcc8
movq %r14, 0x18(%rsp)
movq %r14, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0xe086
movq %rax, %rdi
movq %r14, %rsi
callq 0x5080
incq 0x20(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xbaa8
jmp 0xb22f
incl %ebx
xorps %xmm0, %xmm0
leaq 0x100(%rsp), %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
leaq 0x10(%rsp), %rdi
leaq 0xd0(%rsp), %rcx
movl %ebx, %edx
callq 0xa55a
leaq 0x100(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x9a4e
movq %rsp, %rdi
callq 0x1796c
leaq 0x8(%rsp), %rdi
callq 0x17b00
leaq 0x100(%rsp), %rdi
callq 0xbaa8
leaq 0x10(%rsp), %rdi
callq 0xbcc8
leaq 0x288(%rsp), %rdi
callq 0x162c0
leaq 0x8(%rsp), %rdi
callq 0x17abe
leaq 0x48(%rsp), %rdi
callq 0xbbe2
leaq 0x3e0(%rsp), %rdi
callq 0x17ff4
leaq 0xd0(%rsp), %rdi
callq 0xbd00
leaq 0xc0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb4d2
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x98(%rsp), %rdi
callq 0xbaec
leaq 0x80(%rsp), %rdi
callq 0xbaec
movq %rsp, %rdi
callq 0x1792a
xorl %eax, %eax
addq $0x538, %rsp            # imm = 0x538
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xb568
jmp 0xb5c3
jmp 0xb550
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xb5dd
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0xb5dd
jmp 0xb550
movq %rax, %rbx
jmp 0xb5e7
jmp 0xb560
movq %rax, %rbx
jmp 0xb618
movq %rax, %rbx
jmp 0xb5d0
movq %rax, %rbx
jmp 0xb5dd
movq %rax, %rbx
jmp 0xb5f1
movq %rax, %rbx
jmp 0xb5fe
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0xbaa8
jmp 0xb5c6
jmp 0xb5c3
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xb5a0
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0xb5a0
movq %rax, %rbx
movq 0x44a01(%rip), %rsi     # 0x4ffa8
leaq 0x100(%rsp), %rdi
callq 0x5340
leaq 0x180(%rsp), %rdi
callq 0x5160
jmp 0xb5c6
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xbcc8
leaq 0x288(%rsp), %rdi
callq 0x162c0
leaq 0x8(%rsp), %rdi
callq 0x17abe
leaq 0x48(%rsp), %rdi
callq 0xbbe2
leaq 0x3e0(%rsp), %rdi
callq 0x17ff4
leaq 0xd0(%rsp), %rdi
callq 0xbd00
leaq 0x80(%rsp), %rdi
callq 0xbc3e
movq %rsp, %rdi
callq 0x1792a
movq %rbx, %rdi
callq 0x57b0