// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq 0x3deb3(%rip), %r12     # 0x4ffd0
leaq 0x271a6(%rip), %rsi     # 0x392ca
movl $0x74, %edx
movq %r12, %rdi
callq 0x54f0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x5430
movsbl %al, %esi
movq %r12, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x52e0
leaq 0xc0(%rsp), %rdi
callq 0xed40
xorl %eax, %eax
leaq 0x170(%rsp), %rcx
movb %al, -0x18(%rcx)
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq %rax, 0x10(%rcx)
movq %rax, 0x28(%rcx)
leaq 0x128(%rsp), %rcx
movb %al, -0x18(%rcx)
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq %rax, 0x10(%rcx)
movq %rax, 0x28(%rcx)
movq (%r15), %rsi
leaq 0xa0(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
callq 0xfacc
leaq 0x158(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1396a
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x121f0
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x5470
movq (%r14), %rsi
leaq 0x80(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
callq 0xfacc
leaq 0x110(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1396a
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1223e
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x8(%rsp), %rdi
callq 0x178ec
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x17a32
testb %al, %al
jne 0x1226d
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xef6c
leaq 0x1a0(%rsp), %rsi
movl $0x1, (%rsi)
leaq 0x8(%rsp), %rdi
callq 0x17a2a
testb %al, %al
jne 0x122a0
leaq 0xc0(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0xef6c
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2708f(%rip), %rsi     # 0x3933f
leaq 0x270a0(%rip), %rdx     # 0x39357
leaq 0x60(%rsp), %rdi
callq 0xbe08
leaq 0xc0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xf05c
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x122ea
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x8(%rsp), %rdi
callq 0x179e4
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0xbb48
leaq 0x1a0(%rsp), %rdi
callq 0x15f60
leaq 0x10(%rsp), %rbx
movq %rbx, 0x8(%rbx)
movq %rbx, (%rbx)
leaq 0x50(%rsp), %r14
xorl %eax, %eax
leaq 0x1a0(%rsp), %r15
leaq 0x158(%rsp), %r12
leaq 0x40(%rsp), %r13
movq %rax, 0x20(%rsp)
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x179cc
testb %al, %al
je 0x12450
movslq 0x248(%rsp), %rax
leaq (%rax,%rax,4), %rdx
shlq $0x3, %rdx
addq 0x28(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x102c8
movq %r14, 0x40(%rsp)
movq %r13, %rdi
leaq 0x26ce4(%rip), %rsi     # 0x39063
leaq 0x26cdf(%rip), %rdx     # 0x39065
callq 0xbe08
movq %r15, %rdi
movq %r13, %rsi
callq 0x171d4
movl %eax, %ebp
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x123af
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5470
testb %bpl, %bpl
jne 0x123cb
leaq 0xc0(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xef6c
cmpq $0x2, 0x20(%rsp)
jne 0x1242d
movq 0x10(%rsp), %rax
movslq 0xb8(%rax), %rax
leaq (%rax,%rax,4), %rdx
shlq $0x3, %rdx
addq 0x28(%rsp), %rdx
movq %rbx, %rdi
leaq 0x110(%rsp), %rsi
callq 0x11554
movq 0x10(%rsp), %rax
movslq 0xb8(%rax), %rax
leaq (%rax,%rax,4), %rdx
shlq $0x3, %rdx
addq 0x28(%rsp), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x10b1b
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x125f6
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe086
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5080
movq 0x20(%rsp), %rax
incq %rax
jmp 0x12336
leaq 0x8(%rsp), %rdi
callq 0x1796c
leaq 0x10(%rsp), %rdi
callq 0xbcc8
leaq 0x1a0(%rsp), %rdi
callq 0x162c0
leaq 0x28(%rsp), %rdi
callq 0xbbe2
leaq 0x8(%rsp), %rdi
callq 0x1792a
leaq 0x110(%rsp), %rdi
callq 0x13c80
leaq 0x158(%rsp), %rdi
callq 0x13c80
leaq 0x100(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x124c0
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0xd8(%rsp), %rdi
callq 0xbaec
leaq 0xc0(%rsp), %rdi
callq 0xbaec
xorl %eax, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x12598
movq %rax, %rbx
jmp 0x125b2
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x125bc
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0x125bc
jmp 0x12575
jmp 0x12575
jmp 0x1256e
movq %rax, %rbx
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x125c6
movq 0x90(%rsp), %rsi
jmp 0x12564
jmp 0x1256e
movq %rax, %rbx
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x125c6
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0x125c6
movq %rax, %rbx
jmp 0x125c6
jmp 0x12575
movq %rax, %rbx
jmp 0x125bc
jmp 0x12598
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x1259b
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0x1259b
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xbcc8
leaq 0x1a0(%rsp), %rdi
callq 0x162c0
leaq 0x28(%rsp), %rdi
callq 0xbbe2
leaq 0x8(%rsp), %rdi
callq 0x1792a
leaq 0x110(%rsp), %rdi
callq 0x13c80
leaq 0x158(%rsp), %rdi
callq 0x13c80
leaq 0xc0(%rsp), %rdi
callq 0xbc3e
movq %rbx, %rdi
callq 0x57b0
nop