// File: /workspace/github_c_makefile_star2_O1/thegenemyers[P]DASCRUBBER/REPqv.c
// Function: main
int main(int argc, char *argv[])
{ int c;

  //  Process arguments

  Prog_Name = Strdup("REPqv","");

  if (argc < 2)
    { fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage);
      exit (1);
    }

  //  Open trimmed DB and the qual-track

  for (c = 1; c < argc; c++)

    { DAZZ_DB    _DB, *DB  = &_DB;
      DAZZ_EXTRA ex_hgap, ex_cest, ex_qvs, ex_dif;

      //  Load DB

      { int status;

        status = Open_DB(argv[c],DB);
        if (status < 0)
          exit (1);
        if (status == 1)
          { fprintf(stderr,"%s: Cannot be called on a .dam index: %s\n",Prog_Name,argv[1]);
            exit (1);
          }
        Trim_DB(DB);
      }

      //  Get .qual track extras

      { FILE *afile;
        char *aname;
        int   extra, tracklen, size;

        afile = NULL;
        if (DB->part)
          { aname = Strdup(Catenate(DB->path,Numbered_Suffix(".",DB->part,"."),"qual",".anno"),
                           "Allocating anno file");
            if (aname == NULL)
              exit (1);
            afile = fopen(aname,"r");
            if (afile == NULL)
              { fprintf(stderr,"%s: Must have a 'qual.%d' track, run DASqv\n",Prog_Name,DB->part);
                exit (1);
              }
          }
        else
          { aname = Strdup(Catenate(DB->path,".","qual",".anno"),"Allocating anno file");
            if (aname == NULL)
              exit (1);
            afile  = fopen(aname,"r");
            if (afile == NULL)
              { fprintf(stderr,"%s: Must have a 'qual' track, run DASqv\n",Prog_Name);
                exit (1);
              }
          }

        fread(&tracklen,sizeof(int),1,afile);
        fread(&size,sizeof(int),1,afile);
        fseeko(afile,0,SEEK_END);
        extra = ftell(afile) - (size*(tracklen+1) + 2*sizeof(int));
        fseeko(afile,-extra,SEEK_END);
        ex_hgap.nelem = 0;
        if (Read_Extra(afile,aname,&ex_hgap) != 0)
          { fprintf(stderr,"%s: Hgap threshold extra missing from .qual track?\n",Prog_Name);
            exit (1);
          }
        ex_cest.nelem = 0;
        if (Read_Extra(afile,aname,&ex_cest) != 0)
          { fprintf(stderr,"%s: Coverage estimate extra missing from .qual track?\n",Prog_Name);
            exit (1);
          }
        ex_qvs.nelem = 0;
        if (Read_Extra(afile,aname,&ex_qvs) != 0)
          { fprintf(stderr,"%s: QV histogram extra missing from .qual track?\n",Prog_Name);
            exit (1);
          }
        ex_dif.nelem = 0;
        if (Read_Extra(afile,aname,&ex_dif) != 0)
          { fprintf(stderr,"%s: Differences histogram extra missing from .qual track?\n",Prog_Name);
            exit (1);
          }
        fclose(afile);
      }

      //  Generate display

      { char       *root;
        int64       nreads, totlen;
        int         hgap_min, cover;
        int64      *qgram, *sgram;
        int         maxqv;

        //  Get relevant variables

        root   = Root(argv[c],".db");
        nreads = DB->nreads;
        totlen = DB->totlen;

        hgap_min = (int) ((int64 *) (ex_hgap.value))[0];
        cover    = (int) ((int64 *) (ex_cest.value))[0];
        qgram    = (int64 *) (ex_qvs.value);
        maxqv    = ex_qvs.nelem - 1;
        sgram    = (int64 *) (ex_dif.value);

        printf("\nDASqv");
        if (hgap_min > 0)
          printf(" -H%d",hgap_min);
        printf(" -c%d %s\n\n",cover,root);

        if (hgap_min > 0) 
          { int i;

            for (i = 0; i < DB->nreads; i++)
              if (DB->reads[i].rlen < hgap_min)
                { nreads -= 1;
                  totlen -= DB->reads[i].rlen;
                }
          }

        //  Display histograms

        printf("\n  Input:  ");
        Print_Number(nreads,7,stdout);
        printf("reads,  ");
        Print_Number(totlen,12,stdout);
        printf(" bases");
        if (hgap_min > 0)
          { printf(" (another ");
            Print_Number(DB->nreads - nreads,0,stdout);
            printf(" were < H-length)");
          }
        printf("\n");

        { int64 ssum, qsum;
          int64 stotal, qtotal;
          int   qv_deep;
          int   gval, bval;
          int   i;

          stotal = qtotal = 0;
          for (i = 0; i <= maxqv; i++)
            { stotal += sgram[i];
              qtotal += qgram[i];
            }

          if (cover >= 40)
            qv_deep = cover/8;
          else if (cover >= 20)
            qv_deep = 5;
          else
            qv_deep = cover/4;

          printf("\n  Histogram of q-values (average %d best)\n",qv_deep);
          printf("\n                 Input                 QV\n");
          qsum = qgram[maxqv];
          ssum = sgram[maxqv];
          printf("\n    %2d:  %9lld  %5.1f%%    %9lld  %5.1f%%\n\n",
                 maxqv,sgram[maxqv],(100.*ssum)/stotal,qgram[maxqv],(100.*qsum)/qtotal);

          qtotal -= qsum;
          stotal -= ssum;
          ssum = qsum = 0;
          for (i = maxqv-1; i >= 0; i--)
            if (qgram[i] > 0)
              { ssum += sgram[i];
                qsum += qgram[i];
                printf("    %2d:  %9lld  %5.1f%%    %9lld  %5.1f%%\n",
                       i,sgram[i],(100.*ssum)/stotal,
                         qgram[i],(100.*qsum)/qtotal);
              }

          //  Estimate -g and -b parameters

          bval = gval = -1;
          qsum = 0;
          for (i = maxqv-1; i >= 0; i--)
            if (qgram[i] > 0)
              { qsum += qgram[i];
                if ((100.*qsum)/qtotal > 7. && bval < 0)
                  bval = i+1;
                if ((100.*qsum)/qtotal > 20. && gval < 0)
                  gval = i+1;
              }
  
          printf("\n  Recommend \'DAStrim -g%d -b%d'\n\n",gval,bval);
        }

        free(root);
        Close_DB(DB);
      }
    }

  free(Prog_Name);
  exit (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rsi, %r12
movl %edi, %ebx
leaq 0x7d17(%rip), %rdi      # 0xa020
leaq 0x7f49(%rip), %rsi      # 0xa259
callq 0x2aee
leaq 0xce7c(%rip), %rcx      # 0xf198
movq %rax, (%rcx)
cmpl $0x2, %ebx
jge 0x234b
movq 0xcccd(%rip), %rcx      # 0xeff8
movq (%rcx), %rdi
leaq 0x7cf1(%rip), %rsi      # 0xa026
leaq 0x7f6d(%rip), %rcx      # 0xa2a9
movq %rax, %rdx
xorl %eax, %eax
callq 0x11b0
jmp 0x29f1
movl %ebx, %eax
movq %rax, 0x28(%rsp)
movl $0x1, %r13d
leaq 0x68(%rsp), %rbx
movq %r12, 0x30(%rsp)
movq (%r12,%r13,8), %rdi
movq %rbx, %rsi
callq 0x39ec
testl %eax, %eax
js 0x29f1
cmpl $0x1, %eax
je 0x2905
movq %rbx, %rdi
callq 0x41b1
movl 0xa0(%rsp), %esi
testl %esi, %esi
je 0x23f6
movq 0xb0(%rsp), %rbx
leaq 0x7f16(%rip), %rdi      # 0xa2b7
movq %rdi, %rdx
callq 0x2db9
movq %rbx, %rdi
movq %rax, %rsi
leaq 0x7ca8(%rip), %rdx      # 0xa05e
leaq 0x7ca6(%rip), %rcx      # 0xa063
callq 0x2cc9
movq %rax, %rdi
leaq 0x7c9d(%rip), %rsi      # 0xa069
callq 0x2aee
testq %rax, %rax
je 0x29f1
movq %rax, %r15
movq %rax, %rdi
leaq 0x8a25(%rip), %rsi      # 0xae0c
callq 0x1250
testq %rax, %rax
jne 0x244b
jmp 0x29a4
movq 0xb0(%rsp), %rdi
leaq 0x7eb2(%rip), %rsi      # 0xa2b7
leaq 0x7c52(%rip), %rdx      # 0xa05e
leaq 0x7c50(%rip), %rcx      # 0xa063
callq 0x2cc9
movq %rax, %rdi
leaq 0x7c47(%rip), %rsi      # 0xa069
callq 0x2aee
testq %rax, %rax
je 0x29f1
movq %rax, %r15
movq %rax, %rdi
leaq 0x89cf(%rip), %rsi      # 0xae0c
callq 0x1250
testq %rax, %rax
je 0x29cf
movq %rax, %r14
movl $0x4, %esi
movl $0x1, %edx
leaq 0x1c(%rsp), %rdi
movq %rax, %rcx
callq 0x10c0
movl $0x4, %esi
movl $0x1, %edx
leaq 0x18(%rsp), %rdi
movq %r14, %rcx
callq 0x10c0
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1240
movq %r14, %rdi
callq 0x11c0
movl 0x1c(%rsp), %ecx
incl %ecx
imull 0x18(%rsp), %ecx
subl %eax, %ecx
addl $0x8, %ecx
movslq %ecx, %rsi
movq %r14, %rdi
movl $0x2, %edx
callq 0x1240
movl $0x0, 0x11c(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0x118(%rsp), %rdx
callq 0x632d
testl %eax, %eax
jne 0x292a
movl $0x0, 0xfc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0xf8(%rsp), %rdx
callq 0x632d
testl %eax, %eax
jne 0x294a
movl $0x0, 0x4c(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0x48(%rsp), %rdx
callq 0x632d
testl %eax, %eax
jne 0x296a
movl $0x0, 0xdc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0xd8(%rsp), %rdx
callq 0x632d
testl %eax, %eax
jne 0x2987
movq %r14, %rdi
callq 0x10d0
movq %r13, 0x38(%rsp)
movq (%r12,%r13,8), %rdi
leaq 0x7c4e(%rip), %rsi      # 0xa1ab
callq 0x2c04
movq %rax, 0x10(%rsp)
movslq 0x98(%rsp), %r15
movq 0x90(%rsp), %rbx
movq 0x130(%rsp), %rax
movl (%rax), %r14d
movq 0x110(%rsp), %rax
movl (%rax), %r12d
movq 0x60(%rsp), %r13
movl 0x4c(%rsp), %eax
movq %rax, 0x20(%rsp)
movq 0xf0(%rsp), %rbp
leaq 0x7c05(%rip), %rdi      # 0xa1af
xorl %eax, %eax
callq 0x1110
testl %r14d, %r14d
movq %r12, (%rsp)
jle 0x2632
leaq 0x7bf5(%rip), %rdi      # 0xa1b6
movl %r14d, %esi
xorl %eax, %eax
callq 0x1110
leaq 0x7bea(%rip), %rdi      # 0xa1bc
movl %r12d, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
movslq 0x98(%rsp), %rcx
testq %rcx, %rcx
movl $0x0, %r8d
movq 0xc9df(%rip), %r12      # 0xefd8
jle 0x264f
movq 0xc8(%rsp), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
movl 0x4(%rax,%rdx), %esi
xorl %edi, %edi
cmpl %r14d, %esi
setl %dil
cmovgel %r8d, %esi
movslq %esi, %rsi
subq %rsi, %rbx
subq %rdi, %r15
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0x260d
jmp 0x264f
leaq 0x7b83(%rip), %rdi      # 0xa1bc
movl %r12d, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
movq 0xc989(%rip), %r12      # 0xefd8
leaq 0x7b71(%rip), %rdi      # 0xa1c7
xorl %eax, %eax
callq 0x1110
movq (%r12), %rdx
movq %r15, %rdi
movl $0x7, %esi
callq 0x2e6c
leaq 0x7b5e(%rip), %rdi      # 0xa1d3
xorl %eax, %eax
callq 0x1110
movq (%r12), %rdx
movq %rbx, %rdi
movl $0xc, %esi
callq 0x2e6c
leaq 0x7b48(%rip), %rdi      # 0xa1dc
xorl %eax, %eax
callq 0x1110
testl %r14d, %r14d
jle 0x26d2
leaq 0x7b3c(%rip), %rdi      # 0xa1e3
xorl %eax, %eax
callq 0x1110
movslq 0x98(%rsp), %rdi
subq %r15, %rdi
movq (%r12), %rdx
xorl %esi, %esi
callq 0x2e6c
leaq 0x7b23(%rip), %rdi      # 0xa1ee
xorl %eax, %eax
callq 0x1110
movl $0xa, %edi
callq 0x1040
movq 0x20(%rsp), %rcx
testl %ecx, %ecx
jle 0x2700
xorl %eax, %eax
xorl %r15d, %r15d
xorl %ebx, %ebx
addq (%rbp,%rax,8), %rbx
addq (%r13,%rax,8), %r15
incq %rax
cmpq %rax, %rcx
jne 0x26ec
jmp 0x2705
xorl %ebx, %ebx
xorl %r15d, %r15d
movq (%rsp), %rax
cmpl $0x28, %eax
jl 0x2715
shrl $0x3, %eax
movl %eax, %esi
jmp 0x272a
movl $0x5, %esi
cmpl $0x13, %eax
jg 0x272a
leal 0x3(%rax), %esi
testl %eax, %eax
cmovnsl %eax, %esi
sarl $0x2, %esi
movslq %ecx, %r12
leal -0x1(%r12), %eax
movl %eax, (%rsp)
leaq 0x7ac4(%rip), %rdi      # 0xa200
xorl %eax, %eax
callq 0x1110
leaq 0x7b6f(%rip), %rdi      # 0xa2b9
callq 0x10a0
movq -0x8(%r13,%r12,8), %r14
movq -0x8(%rbp,%r12,8), %rdx
cvtsi2sd %rdx, %xmm0
cvtsi2sd %rbx, %xmm1
movsd 0x789d(%rip), %xmm2     # 0xa008
mulsd %xmm2, %xmm0
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
mulsd %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r15, %xmm2
divsd %xmm2, %xmm1
leaq 0x7a9a(%rip), %rdi      # 0xa22c
movl (%rsp), %esi
movq %rdx, (%rsp)
movq %r14, %rcx
movb $0x2, %al
callq 0x1110
subq %r14, %r15
leal -0x2(%r12), %eax
movl %eax, 0xc(%rsp)
cmpl $0x2, %r12d
jl 0x283e
subq (%rsp), %rbx
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
movsd %xmm0, (%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0xc(%rsp), %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
movq (%r13,%rbx,8), %rcx
testq %rcx, %rcx
jle 0x2832
movq (%rbp,%rbx,8), %rdx
addq %rdx, %r12
addq %rcx, %r14
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movsd 0x7801(%rip), %xmm2     # 0xa008
mulsd %xmm2, %xmm0
divsd (%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
mulsd %xmm2, %xmm1
divsd 0x40(%rsp), %xmm1
leaq 0x7a31(%rip), %rdi      # 0xa25a
movl %ebx, %esi
movb $0x2, %al
callq 0x1110
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0x27e2
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x2, 0x20(%rsp)
movq 0x30(%rsp), %r12
leaq 0x68(%rsp), %rbx
jl 0x28bc
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
movl 0xc(%rsp), %eax
incq %rax
xorl %edi, %edi
movq -0x8(%r13,%rax,8), %rcx
testq %rcx, %rcx
jle 0x28b7
addq %rdi, %rcx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
movl %eax, %edi
mulsd 0x777f(%rip), %xmm1     # 0xa008
movl %eax, %r8d
testl %edx, %edx
js 0x2893
movl %edx, %r8d
divsd %xmm0, %xmm1
ucomisd 0x7771(%rip), %xmm1     # 0xa010
cmoval %r8d, %edx
testl %esi, %esi
js 0x28a9
movl %esi, %edi
ucomisd 0x7767(%rip), %xmm1     # 0xa018
cmoval %edi, %esi
movq %rcx, %rdi
decq %rax
jg 0x286a
leaq 0x79c3(%rip), %rdi      # 0xa286
xorl %eax, %eax
callq 0x1110
movq 0x10(%rsp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x49c4
movq 0x38(%rsp), %r13
incq %r13
cmpq 0x28(%rsp), %r13
jne 0x2362
leaq 0xc8a2(%rip), %rax      # 0xf198
movq (%rax), %rdi
callq 0x1030
xorl %edi, %edi
callq 0x1280
movq 0xc6ec(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xc882(%rip), %rax      # 0xf198
movq (%rax), %rdx
movq 0x8(%r12), %rcx
leaq 0x770f(%rip), %rsi      # 0xa034
jmp 0x233f
movq 0xc6c7(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xc85d(%rip), %rax      # 0xf198
movq (%rax), %rdx
leaq 0x778e(%rip), %rsi      # 0xa0d3
jmp 0x29ea
movq 0xc6a7(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xc83d(%rip), %rax      # 0xf198
movq (%rax), %rdx
leaq 0x77a2(%rip), %rsi      # 0xa107
jmp 0x29ea
movq 0xc687(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xc81d(%rip), %rax      # 0xf198
movq (%rax), %rdx
leaq 0x77b9(%rip), %rsi      # 0xa13e
jmp 0x29ea
movq 0xc66a(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xc800(%rip), %rax      # 0xf198
movq (%rax), %rdx
leaq 0x77ce(%rip), %rsi      # 0xa170
jmp 0x29ea
movq 0xc64d(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xc7e3(%rip), %rax      # 0xf198
movq (%rax), %rdx
movl 0xa0(%rsp), %ecx
leaq 0x76b8(%rip), %rsi      # 0xa07e
xorl %eax, %eax
callq 0x11b0
jmp 0x29f1
movq 0xc622(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xc7b8(%rip), %rax      # 0xf198
movq (%rax), %rdx
leaq 0x76c0(%rip), %rsi      # 0xa0aa
xorl %eax, %eax
callq 0x11b0
movl $0x1, %edi
callq 0x1280
nop