// File: 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
movq %rsp, %rbp
subq $0x620, %rsp            # imm = 0x620
movq %rdi, -0x5b8(%rbp)
movq %rsi, -0x5b0(%rbp)
movq %rdx, -0x5a8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2f4f0
leaq -0x70(%rbp), %rdi
callq 0x1e230
jmp 0x10b70
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x5c8(%rbp)
callq 0x126e0
movq -0x5a8(%rbp), %rdi
movq -0x5c8(%rbp), %rsi
callq 0x10830
movb %al, -0x5b9(%rbp)
jmp 0x10b9e
movb -0x5b9(%rbp), %al
testb $0x1, %al
jne 0x10be3
jmp 0x10baa
leaq -0x70(%rbp), %rdi
movl $0x6, %esi
movl $0x1, %edx
callq 0x1e690
jmp 0x10bbf
jmp 0x10be3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x111a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x11190
movq -0x5b8(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x2f8b0
movb %al, -0x5c9(%rbp)
jmp 0x10bfb
movb -0x5c9(%rbp), %al
testb $0x1, %al
jne 0x10c1b
jmp 0x10c07
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1e690
jmp 0x10c19
jmp 0x10c1b
leaq -0x208(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x2f6a0
jmp 0x10c2d
leaq -0x20(%rbp), %rdi
callq 0x2f780
movq %rax, -0x5d8(%rbp)
jmp 0x10c3f
movq -0x5d8(%rbp), %rsi
leaq -0x220(%rbp), %rdi
callq 0x12700
jmp 0x10c54
leaq -0x228(%rbp), %rdi
callq 0x2fae0
jmp 0x10c62
movq -0x5b0(%rbp), %rsi
leaq -0x228(%rbp), %rdi
leaq -0x208(%rbp), %rdx
leaq -0x220(%rbp), %rcx
callq 0x2fc70
movb %al, -0x5d9(%rbp)
jmp 0x10c8b
movb -0x5d9(%rbp), %al
testb $0x1, %al
jne 0x10cde
jmp 0x10c97
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1e690
jmp 0x10ca9
jmp 0x10cde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x11184
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x11178
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x1116c
leaq -0x249(%rbp), %rdi
movq %rdi, -0x5e8(%rbp)
callq 0x6a20
movq -0x5e8(%rbp), %rdx
leaq 0x6f477(%rip), %rsi     # 0x80176
leaq -0x248(%rbp), %rdi
callq 0x11570
jmp 0x10d0d
leaq -0x70(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x1e770
jmp 0x10d1f
leaq -0x248(%rbp), %rdi
callq 0x6ba8
leaq -0x249(%rbp), %rdi
callq 0x6600
leaq -0x3a8(%rbp), %rdi
callq 0x2aff0
jmp 0x10d45
leaq -0x20(%rbp), %rdi
leaq -0x3a8(%rbp), %rsi
callq 0x2f700
movb %al, -0x5e9(%rbp)
jmp 0x10d5d
movb -0x5e9(%rbp), %al
testb $0x1, %al
jne 0x10dc8
jmp 0x10d69
leaq -0x70(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x1e690
jmp 0x10d7e
jmp 0x10dc8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x10da6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x6ba8
leaq -0x249(%rbp), %rdi
callq 0x6600
jmp 0x1116c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x11160
leaq -0x3c0(%rbp), %rdi
callq 0x127f0
movl $0x0, -0x3c4(%rbp)
movl $0x0, -0x3c8(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x3a8(%rbp), %rsi
callq 0x2f700
movb %al, -0x5ea(%rbp)
jmp 0x10e00
movb -0x5ea(%rbp), %al
testb $0x1, %al
jne 0x10e0f
jmp 0x11053
movl -0x3c8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c8(%rbp)
movl -0x3c8(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x10f33
leaq -0x550(%rbp), %rdi
callq 0x63a0
jmp 0x10e43
leaq -0x540(%rbp), %rdi
leaq 0x6f34c(%rip), %rsi     # 0x8019d
callq 0x6560
movq %rax, -0x5f8(%rbp)
jmp 0x10e5f
movq -0x5f8(%rbp), %rdi
movl -0x3c4(%rbp), %esi
callq 0x6a00
movq %rax, -0x600(%rbp)
jmp 0x10e7a
movq -0x600(%rbp), %rdi
leaq 0x70d88(%rip), %rsi     # 0x81c10
callq 0x6560
movq %rax, -0x608(%rbp)
jmp 0x10e96
movq -0x608(%rbp), %rdi
movl -0x3c8(%rbp), %esi
callq 0x6a00
jmp 0x10eaa
leaq -0x570(%rbp), %rdi
leaq -0x550(%rbp), %rsi
callq 0x6550
jmp 0x10ebf
leaq -0x70(%rbp), %rdi
leaq -0x570(%rbp), %rsi
callq 0x1e770
jmp 0x10ed1
leaq -0x570(%rbp), %rdi
callq 0x6ba8
leaq -0x550(%rbp), %rdi
callq 0x63f0
jmp 0x10f33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x11154
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x10f22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x570(%rbp), %rdi
callq 0x6ba8
leaq -0x550(%rbp), %rdi
callq 0x63f0
jmp 0x11154
leaq -0x3c0(%rbp), %rdi
callq 0x12810
testb $0x1, %al
jne 0x10f45
jmp 0x10f5f
leaq -0x3c0(%rbp), %rdi
leaq -0x3a8(%rbp), %rsi
callq 0x12830
jmp 0x10f5a
jmp 0x10de8
leaq -0x3c0(%rbp), %rdi
callq 0x128a0
movq %rax, %rdi
leaq -0x3a8(%rbp), %rsi
callq 0x12870
testb $0x1, %al
jne 0x10f83
jmp 0x11035
movl -0x3c4(%rbp), %eax
incl %eax
movl %eax, -0x3c4(%rbp)
leaq -0x588(%rbp), %rdi
movq %rdi, -0x610(%rbp)
callq 0x11c40
movq -0x610(%rbp), %rsi
movl -0x3c4(%rbp), %edx
leaq -0x3c0(%rbp), %rdi
leaq -0xb0(%rbp), %rcx
callq 0xf690
jmp 0x10fc6
leaq -0x588(%rbp), %rdi
leaq -0x228(%rbp), %rsi
leaq -0x220(%rbp), %rdx
callq 0xe9b0
jmp 0x10fe2
leaq -0x3c0(%rbp), %rdi
movq %rdi, -0x618(%rbp)
callq 0x128e0
movq -0x618(%rbp), %rdi
leaq -0x3a8(%rbp), %rsi
callq 0x12830
jmp 0x1100a
leaq -0x588(%rbp), %rdi
callq 0x12560
jmp 0x1104c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x588(%rbp), %rdi
callq 0x12560
jmp 0x11154
leaq -0x3c0(%rbp), %rdi
leaq -0x3a8(%rbp), %rsi
callq 0x12830
jmp 0x1104a
jmp 0x1104c
jmp 0x1104e
jmp 0x10de8
movl -0x3c4(%rbp), %eax
incl %eax
movl %eax, -0x3c4(%rbp)
leaq -0x5a0(%rbp), %rdi
movq %rdi, -0x620(%rbp)
callq 0x11c40
movq -0x620(%rbp), %rsi
movl -0x3c4(%rbp), %edx
leaq -0x3c0(%rbp), %rdi
leaq -0xb0(%rbp), %rcx
callq 0xf690
jmp 0x11096
leaq -0x5a0(%rbp), %rdi
leaq -0x228(%rbp), %rsi
leaq -0x220(%rbp), %rdx
callq 0xe9b0
jmp 0x110b2
leaq -0x20(%rbp), %rdi
callq 0x2f5d0
jmp 0x110bd
leaq -0x228(%rbp), %rdi
callq 0x2fbc0
jmp 0x110cb
leaq -0x5a0(%rbp), %rdi
callq 0x12560
leaq -0x3c0(%rbp), %rdi
callq 0x12910
leaq -0x3a8(%rbp), %rdi
callq 0x2b500
leaq -0x228(%rbp), %rdi
callq 0x2fb50
leaq -0x220(%rbp), %rdi
callq 0x12930
leaq -0x208(%rbp), %rdi
callq 0x303b0
leaq -0xb0(%rbp), %rdi
callq 0x12990
leaq -0x70(%rbp), %rdi
callq 0x129b0
leaq -0x20(%rbp), %rdi
callq 0x2f560
xorl %eax, %eax
addq $0x620, %rsp            # imm = 0x620
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x5a0(%rbp), %rdi
callq 0x12560
leaq -0x3c0(%rbp), %rdi
callq 0x12910
leaq -0x3a8(%rbp), %rdi
callq 0x2b500
leaq -0x228(%rbp), %rdi
callq 0x2fb50
leaq -0x220(%rbp), %rdi
callq 0x12930
leaq -0x208(%rbp), %rdi
callq 0x303b0
leaq -0xb0(%rbp), %rdi
callq 0x12990
leaq -0x70(%rbp), %rdi
callq 0x129b0
leaq -0x20(%rbp), %rdi
callq 0x2f560
movq -0x78(%rbp), %rdi
callq 0x6a10
nopw (%rax,%rax)