// File: /workspace/github_cpp_makefile_star2_O3/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 0x17794
leaq 0x80(%rsp), %rdi
callq 0xec74
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 0xadc6
testb %al, %al
jne 0xb097
leaq 0x80(%rsp), %rdi
movl $0x6, %esi
movl $0x1, %edx
callq 0xeea0
movq %rsp, %rdi
movq %r14, %rsi
callq 0x178da
testb %al, %al
jne 0xb0ba
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xeea0
leaq 0x3e0(%rsp), %rdi
movq %rsp, %rsi
callq 0x17850
movq %rsp, %rdi
callq 0x1788c
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0xbae4
leaq 0x8(%rsp), %rdi
callq 0x1792c
leaq 0x8(%rsp), %rdi
leaq 0x3e0(%rsp), %rdx
leaq 0x48(%rsp), %rcx
movq %rbx, %rsi
callq 0x179d2
testb %al, %al
jne 0xb11b
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xeea0
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2df93(%rip), %rsi     # 0x390be
leaq 0x2dfb2(%rip), %rdx     # 0x390e4
leaq 0x60(%rsp), %rdi
callq 0xbdac
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xef90
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xb165
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x288(%rsp), %rdi
callq 0x15e70
movq %rsp, %rdi
leaq 0x288(%rsp), %rsi
callq 0x17874
testb %al, %al
jne 0xb19d
leaq 0x80(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0xeea0
leaq 0x10(%rsp), %r14
movq %r14, 0x8(%r14)
movq %r14, (%r14)
movq $0x0, 0x10(%r14)
xorl %ebp, %ebp
movq %rsp, %r13
leaq 0x288(%rsp), %rbx
xorl %r12d, %r12d
incl %r12d
movq %r13, %rdi
movq %rbx, %rsi
callq 0x17874
testb %al, %al
je 0xb39c
imull $0x26e978d5, %r12d, %eax # imm = 0x26E978D5
addl $0x10624d8, %eax        # imm = 0x10624D8
rorl $0x3, %eax
cmpl $0x418936, %eax         # imm = 0x418936
ja 0xb2a9
leaq 0x100(%rsp), %rdi
callq 0x5300
movl $0x2a, %edx
leaq 0x110(%rsp), %rdi
leaq 0x2ded3(%rip), %rsi     # 0x390e5
callq 0x54f0
leaq 0x110(%rsp), %rdi
movl %ebp, %esi
callq 0x5780
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2f41b(%rip), %rsi     # 0x3a653
callq 0x54f0
movq %r15, %rdi
movl %r12d, %esi
callq 0x5780
leaq 0x28(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x56c0
leaq 0x80(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xef90
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xb288
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x100(%rsp), %rdi
movq 0x44d11(%rip), %rsi     # 0x4ffa8
callq 0x5340
leaq 0x180(%rsp), %rdi
callq 0x5160
cmpq %r14, 0x10(%rsp)
jne 0xb2d0
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe004
movq %rax, %rdi
movq %r14, %rsi
callq 0x5080
incq 0x20(%rsp)
jmp 0xb1c1
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdx
cmpq 0x290(%rsp), %rdx
jne 0xb30a
testq %rdx, %rdx
je 0xb2fd
movq 0x288(%rsp), %rsi
movq 0x10(%rax), %rdi
callq 0x5480
testl %eax, %eax
jne 0xb30a
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe004
jmp 0xb2bb
incl %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x100(%rsp)
movq $0x0, 0x110(%rsp)
movq %r14, %rdi
leaq 0x100(%rsp), %rsi
movl %ebp, %edx
leaq 0xd0(%rsp), %rcx
callq 0xa52a
leaq 0x100(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x9a3e
movq %r14, %rdi
callq 0xbc66
movq %r14, 0x18(%rsp)
movq %r14, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe004
movq %rax, %rdi
movq %r14, %rsi
callq 0x5080
incq 0x20(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xba3c
jmp 0xb1c1
incl %ebp
xorps %xmm0, %xmm0
leaq 0x100(%rsp), %rsi
movaps %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
leaq 0x10(%rsp), %rdi
leaq 0xd0(%rsp), %rcx
movl %ebp, %edx
callq 0xa52a
leaq 0x100(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x9a3e
movq %rsp, %rdi
callq 0x17814
leaq 0x8(%rsp), %rdi
callq 0x179a8
leaq 0x100(%rsp), %rdi
callq 0xba3c
leaq 0x10(%rsp), %rdi
callq 0xbc66
leaq 0x288(%rsp), %rdi
callq 0x161d0
leaq 0x8(%rsp), %rdi
callq 0x17966
leaq 0x48(%rsp), %rdi
callq 0xbb7e
leaq 0x3e0(%rsp), %rdi
callq 0x17e9c
leaq 0xd0(%rsp), %rdi
callq 0xbc9e
leaq 0xc0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb464
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x98(%rsp), %rdi
callq 0xba86
leaq 0x80(%rsp), %rdi
callq 0xba86
movq %rsp, %rdi
callq 0x177d2
xorl %eax, %eax
addq $0x538, %rsp            # imm = 0x538
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xb4fa
jmp 0xb555
jmp 0xb4e2
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xb56f
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0xb56f
jmp 0xb4e2
movq %rax, %rbx
jmp 0xb579
jmp 0xb4f2
movq %rax, %rbx
jmp 0xb5aa
movq %rax, %rbx
jmp 0xb562
movq %rax, %rbx
jmp 0xb56f
movq %rax, %rbx
jmp 0xb583
movq %rax, %rbx
jmp 0xb590
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0xba3c
jmp 0xb558
jmp 0xb555
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xb532
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0xb532
movq %rax, %rbx
movq 0x44a6f(%rip), %rsi     # 0x4ffa8
leaq 0x100(%rsp), %rdi
callq 0x5340
leaq 0x180(%rsp), %rdi
callq 0x5160
jmp 0xb558
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xbc66
leaq 0x288(%rsp), %rdi
callq 0x161d0
leaq 0x8(%rsp), %rdi
callq 0x17966
leaq 0x48(%rsp), %rdi
callq 0xbb7e
leaq 0x3e0(%rsp), %rdi
callq 0x17e9c
leaq 0xd0(%rsp), %rdi
callq 0xbc9e
leaq 0x80(%rsp), %rdi
callq 0xbbdc
movq %rsp, %rdi
callq 0x177d2
movq %rbx, %rdi
callq 0x57b0