// File: /workspace/github_cpp_makefile_star2_O3/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 0x3dfab(%rip), %r12     # 0x4ffd0
leaq 0x2729e(%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 0xec74
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 0xf9d0
leaq 0x158(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x13854
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x120f8
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x5470
movq (%r14), %rsi
leaq 0x80(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
callq 0xf9d0
leaq 0x110(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x13854
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12146
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x8(%rsp), %rdi
callq 0x17794
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x178da
testb %al, %al
jne 0x12175
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xeea0
leaq 0x1a0(%rsp), %rsi
movl $0x1, (%rsi)
leaq 0x8(%rsp), %rdi
callq 0x178d2
testb %al, %al
jne 0x121a8
leaq 0xc0(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0xeea0
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x27187(%rip), %rsi     # 0x3933f
leaq 0x27198(%rip), %rdx     # 0x39357
leaq 0x60(%rsp), %rdi
callq 0xbdac
leaq 0xc0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xef90
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x121f2
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0x8(%rsp), %rdi
callq 0x1788c
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0xbae4
leaq 0x1a0(%rsp), %rdi
callq 0x15e70
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 0x17874
testb %al, %al
je 0x12358
movslq 0x248(%rsp), %rax
leaq (%rax,%rax,4), %rdx
shlq $0x3, %rdx
addq 0x28(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x101cc
movq %r14, 0x40(%rsp)
movq %r13, %rdi
leaq 0x26ddc(%rip), %rsi     # 0x39063
leaq 0x26dd7(%rip), %rdx     # 0x39065
callq 0xbdac
movq %r15, %rdi
movq %r13, %rsi
callq 0x17086
movl %eax, %ebp
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x122b7
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5470
testb %bpl, %bpl
jne 0x122d3
leaq 0xc0(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xeea0
cmpq $0x2, 0x20(%rsp)
jne 0x12335
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 0x1145b
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 0x10a23
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x124fe
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe004
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5080
movq 0x20(%rsp), %rax
incq %rax
jmp 0x1223e
leaq 0x8(%rsp), %rdi
callq 0x17814
leaq 0x10(%rsp), %rdi
callq 0xbc66
leaq 0x1a0(%rsp), %rdi
callq 0x161d0
leaq 0x28(%rsp), %rdi
callq 0xbb7e
leaq 0x8(%rsp), %rdi
callq 0x177d2
leaq 0x110(%rsp), %rdi
callq 0x13b6a
leaq 0x158(%rsp), %rdi
callq 0x13b6a
leaq 0x100(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x123c8
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x5470
leaq 0xd8(%rsp), %rdi
callq 0xba86
leaq 0xc0(%rsp), %rdi
callq 0xba86
xorl %eax, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x124a0
movq %rax, %rbx
jmp 0x124ba
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x124c4
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0x124c4
jmp 0x1247d
jmp 0x1247d
jmp 0x12476
movq %rax, %rbx
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x124ce
movq 0x90(%rsp), %rsi
jmp 0x1246c
jmp 0x12476
movq %rax, %rbx
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x124ce
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0x124ce
movq %rax, %rbx
jmp 0x124ce
jmp 0x1247d
movq %rax, %rbx
jmp 0x124c4
jmp 0x124a0
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x124a3
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5470
jmp 0x124a3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xbc66
leaq 0x1a0(%rsp), %rdi
callq 0x161d0
leaq 0x28(%rsp), %rdi
callq 0xbb7e
leaq 0x8(%rsp), %rdi
callq 0x177d2
leaq 0x110(%rsp), %rdi
callq 0x13b6a
leaq 0x158(%rsp), %rdi
callq 0x13b6a
leaq 0xc0(%rsp), %rdi
callq 0xbbdc
movq %rbx, %rdi
callq 0x57b0
nop