// File: /workspace/github_cpp_makefile_star2_O2/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 0x3ca58(%rip), %rdi     # 0x4afd0
leaq 0x20d07(%rip), %rsi     # 0x2f286
callq 0x5430
movq %rax, %rdi
callq 0x51f0
leaq 0xa0(%rsp), %rdi
callq 0xc788
xorl %eax, %eax
leaq 0x150(%rsp), %rcx
movb %al, -0x18(%rcx)
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %al, (%rcx)
andq $0x0, 0x10(%rcx)
andq $0x0, 0x28(%rcx)
leaq 0x108(%rsp), %rcx
movb %al, -0x18(%rcx)
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %al, (%rcx)
andq $0x0, 0x10(%rcx)
andq $0x0, 0x28(%rcx)
movq (%r15), %rsi
leaq 0x80(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
callq 0x96f2
leaq 0x138(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xf774
leaq 0x80(%rsp), %rdi
callq 0x58f8
movq (%r14), %rsi
leaq 0x60(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
callq 0x96f2
leaq 0xf0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xf774
leaq 0x60(%rsp), %rdi
callq 0x58f8
movq %rsp, %rdi
callq 0x12804
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x12944
testb %al, %al
jne 0xe66f
leaq 0xa0(%rsp), %rdi
pushq $0x1
popq %rdx
xorl %esi, %esi
callq 0xc93a
leaq 0x1a0(%rsp), %rsi
movl $0x1, (%rsi)
movq %rsp, %rdi
callq 0x1293c
testb %al, %al
jne 0xe69b
leaq 0xa0(%rsp), %rdi
pushq $0x1
popq %rdx
movl %edx, %esi
callq 0xc93a
leaq 0x20c59(%rip), %rsi     # 0x2f2fb
leaq 0x40(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
callq 0x96f2
leaq 0xa0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0xc9bc
leaq 0x40(%rsp), %rdi
callq 0x58f8
movq %rsp, %rdi
callq 0x128f8
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x9b82
leaq 0x1a0(%rsp), %rdi
callq 0x112b0
leaq 0x10(%rsp), %rbx
movq %rbx, 0x8(%rbx)
movq %rbx, (%rbx)
andq $0x0, 0x10(%rbx)
leaq 0x1a0(%rsp), %r15
leaq 0x138(%rsp), %r12
leaq 0x180(%rsp), %rbp
leaq 0xf(%rsp), %r13
movq %rsp, %rdi
movq %r15, %rsi
callq 0x128e0
testb %al, %al
je 0xe7f4
movslq 0x248(%rsp), %rax
imulq $0x28, %rax, %rdx
addq 0x28(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0xd7d4
movq %rbp, %rdi
leaq 0x2090a(%rip), %rsi     # 0x2f063
movq %r13, %rdx
callq 0x96f2
movq %r15, %rdi
movq %rbp, %rsi
callq 0x12182
movl %eax, %r14d
movq %rbp, %rdi
callq 0x58f8
testb %r14b, %r14b
jne 0xe78f
leaq 0xa0(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0xc93a
cmpq $0x2, 0x20(%rsp)
jne 0xe7e4
movq 0x10(%rsp), %rax
movslq 0xb8(%rax), %rax
imulq $0x28, %rax, %rdx
addq 0x28(%rsp), %rdx
movq %rbx, %rdi
leaq 0xf0(%rsp), %rsi
callq 0xe076
movq 0x10(%rsp), %rax
movslq 0xb8(%rax), %rax
imulq $0x28, %rax, %rdx
addq 0x28(%rsp), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdc09
movq %rbx, %rdi
callq 0xe9c0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9bdc
jmp 0xe720
movq %rsp, %rdi
callq 0x12880
leaq 0x10(%rsp), %rdi
callq 0x9cae
leaq 0x1a0(%rsp), %rdi
callq 0x1151e
leaq 0x28(%rsp), %rdi
callq 0x9c0a
movq %rsp, %rdi
callq 0x12842
leaq 0xf0(%rsp), %rdi
callq 0xf940
leaq 0x138(%rsp), %rdi
callq 0xf940
leaq 0xa0(%rsp), %rdi
callq 0x9c2c
xorl %eax, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xe8ba
movq %rax, %rbx
jmp 0xe8d4
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x58f8
jmp 0xe8de
jmp 0xe8a1
jmp 0xe8a1
jmp 0xe89a
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
jmp 0xe893
jmp 0xe89a
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x58f8
jmp 0xe8e6
movq %rax, %rbx
jmp 0xe8e6
jmp 0xe8a1
movq %rax, %rbx
jmp 0xe8de
jmp 0xe8ba
movq %rax, %rbx
leaq 0x180(%rsp), %rdi
callq 0x58f8
jmp 0xe8bd
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x9cae
leaq 0x1a0(%rsp), %rdi
callq 0x1151e
leaq 0x28(%rsp), %rdi
callq 0x9c0a
movq %rsp, %rdi
callq 0x12842
leaq 0xf0(%rsp), %rdi
callq 0xf940
leaq 0x138(%rsp), %rdi
callq 0xf940
leaq 0xa0(%rsp), %rdi
callq 0x9c2c
movq %rbx, %rdi
callq 0x57d0
nop