// File: zeeev[P]DASVC/src/varcall.cpp
// Function: varcall(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 varcall(std::string & bname,   /* bam name */
            std::string & tf   ,   /* target fasta */
            std::string & qf    )  /* query fasta */
{

    std::cout << "#target_name\ttarget_pos\tsv_type\ttarget_end\tsv_length\tper_id\tmatching_bases\tquery_name\tquery_start\tquery_end\tsequence" << std::endl;

    errorHandler EH;

    /* target is the ref genome */
    /* query is required for inter alignment insertion */

    FastaReference targetFasta ;
    FastaReference queryFasta  ;

    targetFasta.open(tf.c_str()) ;
    queryFasta.open( qf.c_str()) ;

    BamReader br;

    if(!br.Open(bname)){
        EH.croak(EH.COULD_NOT_OPEN_BAM, true);
    }
    if(!br.LocateIndex()){
        EH.croak(EH.COULD_NOT_OPEN_BAM_INDEX, true);
    }

    EH.printInfo("Starting variant calling");

    RefVector rv     = br.GetReferenceData();

    BamAlignment al;

    std::list<BamAlignment> readBuffer;

    while(br.GetNextAlignment(al)){
        alignmentInternalPrint(al, targetFasta, rv[al.RefID].RefName);

        if(!al.HasTag("AI")){
            EH.croak(EH.BAM_NOT_ANNOTATED, true);
        }

        if(readBuffer.size() == 2){
            largeInsertionPrint(readBuffer, queryFasta,
                                rv[readBuffer.front().RefID].RefName  );
            largeDeletionPrint(readBuffer, targetFasta,
                               rv[readBuffer.front().RefID].RefName  );

            readBuffer.pop_front();
        }

        readBuffer.push_back(al);
    }

    br.Close();

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3a0, %rsp            # imm = 0x3A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq 0x8aa02(%rip), %rdi     # 0xadfd0
leaq 0x5cff3(%rip), %rsi     # 0x805c8
callq 0x6560
movq %rax, %rdi
movq 0x8a9c4(%rip), %rsi     # 0xadfa8
callq 0x65c0
leaq -0x68(%rbp), %rdi
callq 0x1e230
leaq -0xb0(%rbp), %rdi
callq 0x24000
jmp 0x23600
leaq -0x108(%rbp), %rdi
callq 0x24000
jmp 0x2360e
movq -0x10(%rbp), %rdi
callq 0x61f0
movq %rax, -0x350(%rbp)
leaq -0x129(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0x6a20
movq -0x350(%rbp), %rsi
movq -0x348(%rbp), %rdx
leaq -0x128(%rbp), %rdi
callq 0x11570
jmp 0x2364d
leaq -0xb0(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x25ab0
jmp 0x23662
leaq -0x128(%rbp), %rdi
callq 0x6ba8
leaq -0x129(%rbp), %rdi
callq 0x6600
movq -0x18(%rbp), %rdi
callq 0x61f0
movq %rax, -0x360(%rbp)
leaq -0x151(%rbp), %rdi
movq %rdi, -0x358(%rbp)
callq 0x6a20
movq -0x360(%rbp), %rsi
movq -0x358(%rbp), %rdx
leaq -0x150(%rbp), %rdi
callq 0x11570
jmp 0x236b9
leaq -0x108(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x25ab0
jmp 0x236ce
leaq -0x150(%rbp), %rdi
callq 0x6ba8
leaq -0x151(%rbp), %rdi
callq 0x6600
leaq -0x160(%rbp), %rdi
callq 0x2f4f0
jmp 0x236f4
movq -0x8(%rbp), %rsi
leaq -0x160(%rbp), %rdi
callq 0x2f8b0
movb %al, -0x361(%rbp)
jmp 0x2370c
movb -0x361(%rbp), %al
testb $0x1, %al
jne 0x23815
jmp 0x2371c
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1e690
jmp 0x2372e
jmp 0x23815
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x23c05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x23bf9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x23793
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x6ba8
leaq -0x129(%rbp), %rdi
callq 0x6600
jmp 0x23bed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x237d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x6ba8
leaq -0x151(%rbp), %rdi
callq 0x6600
jmp 0x23bed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x23bed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x23be1
movl $0x1, -0x164(%rbp)
leaq -0x160(%rbp), %rdi
leaq -0x164(%rbp), %rsi
callq 0x2f880
movb %al, -0x362(%rbp)
jmp 0x2383a
movb -0x362(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x23848
jmp 0x2385c
leaq -0x68(%rbp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x1e690
jmp 0x2385a
jmp 0x2385c
leaq -0x189(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0x6a20
movq -0x370(%rbp), %rdx
leaq 0x5cdc0(%rip), %rsi     # 0x8063d
leaq -0x188(%rbp), %rdi
callq 0x11570
jmp 0x2388b
leaq -0x68(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x1e770
jmp 0x2389d
leaq -0x188(%rbp), %rdi
callq 0x6ba8
leaq -0x189(%rbp), %rdi
callq 0x6600
leaq -0x160(%rbp), %rdi
callq 0x2f780
movq %rax, -0x378(%rbp)
jmp 0x238ca
movq -0x378(%rbp), %rsi
leaq -0x1a8(%rbp), %rdi
callq 0x12700
jmp 0x238df
leaq -0x300(%rbp), %rdi
callq 0x2aff0
jmp 0x238ed
leaq -0x318(%rbp), %rdi
callq 0x127f0
leaq -0x160(%rbp), %rdi
leaq -0x300(%rbp), %rsi
callq 0x2f700
movb %al, -0x379(%rbp)
jmp 0x23914
movb -0x379(%rbp), %al
testb $0x1, %al
jne 0x23923
jmp 0x23b53
movslq -0x258(%rbp), %rsi
leaq -0x1a8(%rbp), %rdi
callq 0x24040
movq %rax, %rdx
leaq -0x300(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x21af0
jmp 0x2394e
leaq -0x339(%rbp), %rdi
movq %rdi, -0x388(%rbp)
callq 0x6a20
movq -0x388(%rbp), %rdx
leaq 0x5c7ac(%rip), %rsi     # 0x8011b
leaq -0x338(%rbp), %rdi
callq 0x11570
jmp 0x2397d
leaq -0x300(%rbp), %rdi
leaq -0x338(%rbp), %rsi
callq 0x2d670
movb %al, -0x389(%rbp)
jmp 0x23998
movb -0x389(%rbp), %al
xorb $-0x1, %al
movb %al, -0x38a(%rbp)
leaq -0x338(%rbp), %rdi
callq 0x6ba8
leaq -0x339(%rbp), %rdi
callq 0x6600
movb -0x38a(%rbp), %al
testb $0x1, %al
jne 0x239cd
jmp 0x23a9b
leaq -0x68(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1e690
jmp 0x239e2
jmp 0x23a9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x23a19
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x6ba8
leaq -0x189(%rbp), %rdi
callq 0x6600
jmp 0x23be1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x23bd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x23bbd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x23a8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0x338(%rbp), %rdi
callq 0x6ba8
leaq -0x339(%rbp), %rdi
callq 0x6600
jmp 0x23bbd
leaq -0x318(%rbp), %rdi
callq 0x24060
cmpq $0x2, %rax
jne 0x23b39
leaq -0x318(%rbp), %rdi
movq %rdi, -0x398(%rbp)
callq 0x23fd0
movslq 0xa8(%rax), %rsi
leaq -0x1a8(%rbp), %rdi
callq 0x24040
movq -0x398(%rbp), %rdi
movq %rax, %rdx
leaq -0x108(%rbp), %rsi
callq 0x22bc0
jmp 0x23aef
leaq -0x318(%rbp), %rdi
movq %rdi, -0x3a0(%rbp)
callq 0x23fd0
movslq 0xa8(%rax), %rsi
leaq -0x1a8(%rbp), %rdi
callq 0x24040
movq -0x3a0(%rbp), %rdi
movq %rax, %rdx
leaq -0xb0(%rbp), %rsi
callq 0x222f0
jmp 0x23b2d
leaq -0x318(%rbp), %rdi
callq 0x24090
leaq -0x318(%rbp), %rdi
leaq -0x300(%rbp), %rsi
callq 0x12830
jmp 0x23b4e
jmp 0x238f9
leaq -0x160(%rbp), %rdi
callq 0x2f5d0
jmp 0x23b61
leaq -0x318(%rbp), %rdi
callq 0x12910
leaq -0x300(%rbp), %rdi
callq 0x2b500
leaq -0x1a8(%rbp), %rdi
callq 0x12930
leaq -0x160(%rbp), %rdi
callq 0x2f560
leaq -0x108(%rbp), %rdi
callq 0x25e00
leaq -0xb0(%rbp), %rdi
callq 0x25e00
leaq -0x68(%rbp), %rdi
callq 0x129b0
xorl %eax, %eax
addq $0x3a0, %rsp            # imm = 0x3A0
popq %rbp
retq
leaq -0x318(%rbp), %rdi
callq 0x12910
leaq -0x300(%rbp), %rdi
callq 0x2b500
leaq -0x1a8(%rbp), %rdi
callq 0x12930
leaq -0x160(%rbp), %rdi
callq 0x2f560
leaq -0x108(%rbp), %rdi
callq 0x25e00
leaq -0xb0(%rbp), %rdi
callq 0x25e00
leaq -0x68(%rbp), %rdi
callq 0x129b0
movq -0xb8(%rbp), %rdi
callq 0x6a10
nopw (%rax,%rax)