// File: /workspace/github_c_makefile_star2_O2/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 0x7c47(%rip), %rdi      # 0x9020
leaq 0x7e79(%rip), %rsi      # 0x9259
callq 0x1b58
leaq 0xcdac(%rip), %rcx      # 0xe198
movq %rax, (%rcx)
cmpl $0x2, %ebx
jge 0x141b
movq 0xcbfd(%rip), %rcx      # 0xdff8
movq (%rcx), %rdi
leaq 0x7c21(%rip), %rsi      # 0x9026
leaq 0x7e9d(%rip), %rcx      # 0x92a9
movq %rax, %rdx
xorl %eax, %eax
callq 0x11b0
jmp 0x1a64
movl %ebx, %eax
movq %rax, 0x40(%rsp)
pushq $0x1
popq %r13
leaq 0xc8(%rsp), %rbx
movq %r12, 0x38(%rsp)
cmpq 0x40(%rsp), %r13
je 0x1962
movq (%r12,%r13,8), %rdi
movq %rbx, %rsi
callq 0x2940
testl %eax, %eax
js 0x1a64
cmpl $0x1, %eax
je 0x1978
movq %rbx, %rdi
callq 0x31e6
movl 0x100(%rsp), %esi
movq 0x110(%rsp), %rbx
testl %esi, %esi
movq %r13, 0x30(%rsp)
je 0x14d7
leaq 0x7e35(%rip), %rdi      # 0x92b7
movq %rdi, %rdx
callq 0x1e5e
movq %rbx, %rdi
movq %rax, %rsi
leaq 0x7bc7(%rip), %rdx      # 0x905e
leaq 0x7bc5(%rip), %rcx      # 0x9063
callq 0x1d37
movq %rax, %rdi
leaq 0x7bbc(%rip), %rsi      # 0x9069
callq 0x1b58
testq %rax, %rax
je 0x1a64
movq %rax, %r14
movq %rax, %rdi
leaq 0x8944(%rip), %rsi      # 0x9e0c
callq 0x1250
testq %rax, %rax
jne 0x1527
jmp 0x1a17
movq %rbx, %rdi
leaq 0x7dd6(%rip), %rsi      # 0x92b7
leaq 0x7b76(%rip), %rdx      # 0x905e
leaq 0x7b74(%rip), %rcx      # 0x9063
callq 0x1d37
movq %rax, %rdi
leaq 0x7b6b(%rip), %rsi      # 0x9069
callq 0x1b58
testq %rax, %rax
je 0x1a64
movq %rax, %r14
movq %rax, %rdi
leaq 0x88f3(%rip), %rsi      # 0x9e0c
callq 0x1250
testq %rax, %rax
je 0x1a42
movq %rax, %r15
leaq 0x2c(%rsp), %rdi
pushq $0x4
popq %r13
movq %r13, %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
movq %rax, %rcx
callq 0x10c0
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x10c0
movq %r15, %rdi
xorl %esi, %esi
pushq $0x2
popq %rbx
movl %ebx, %edx
callq 0x1240
movq %r15, %rdi
callq 0x11c0
movl 0x2c(%rsp), %ecx
incl %ecx
imull 0x28(%rsp), %ecx
subl %eax, %ecx
addl $0x8, %ecx
movslq %ecx, %rsi
movq %r15, %rdi
movl %ebx, %edx
callq 0x1240
andl $0x0, 0xac(%rsp)
movq %r15, %rdi
movq %r14, %rsi
leaq 0xa8(%rsp), %rdx
callq 0x5239
testl %eax, %eax
jne 0x199d
andl $0x0, 0x8c(%rsp)
movq %r15, %rdi
movq %r14, %rsi
leaq 0x88(%rsp), %rdx
callq 0x5239
testl %eax, %eax
jne 0x19bd
andl $0x0, 0x4c(%rsp)
movq %r15, %rdi
movq %r14, %rsi
leaq 0x48(%rsp), %rdx
callq 0x5239
testl %eax, %eax
jne 0x19dd
andl $0x0, 0x6c(%rsp)
movq %r15, %rdi
movq %r14, %rsi
leaq 0x68(%rsp), %rdx
callq 0x5239
testl %eax, %eax
jne 0x19fa
movq %r15, %rdi
callq 0x10d0
movq 0x30(%rsp), %rax
movq (%r12,%rax,8), %rdi
leaq 0x7b88(%rip), %rsi      # 0x91ab
callq 0x1c73
movq %rax, 0x8(%rsp)
movslq 0xf8(%rsp), %r13
movq 0xf0(%rsp), %rbx
movq 0xc0(%rsp), %rax
movl (%rax), %r15d
movq 0xa0(%rsp), %rax
movl (%rax), %ebp
movq 0x60(%rsp), %r14
movslq 0x4c(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x80(%rsp), %r12
leaq 0x7b3f(%rip), %rdi      # 0x91af
xorl %eax, %eax
callq 0x1110
testl %r15d, %r15d
movl %ebp, 0x20(%rsp)
jle 0x16ec
leaq 0x7b2f(%rip), %rdi      # 0x91b6
movl %r15d, %esi
xorl %eax, %eax
callq 0x1110
leaq 0x7b24(%rip), %rdi      # 0x91bc
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
movl 0xf8(%rsp), %ecx
movq 0x128(%rsp), %rax
testl %ecx, %ecx
movl $0x0, %r8d
cmovlel %r8d, %ecx
imulq $0x28, %rcx, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x1701
movl 0x4(%rax,%rdx), %esi
xorl %edi, %edi
cmpl %r15d, %esi
setl %dil
cmovgel %r8d, %esi
movslq %esi, %rsi
subq %rsi, %rbx
subq %rdi, %r13
addq $0x28, %rdx
jmp 0x16c7
leaq 0x7ac9(%rip), %rdi      # 0x91bc
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
leaq 0x7abf(%rip), %rdi      # 0x91c7
xorl %eax, %eax
callq 0x1110
movq 0xc8c2(%rip), %rbp      # 0xdfd8
movq (%rbp), %rdx
movq %r13, %rdi
pushq $0x7
popq %rsi
callq 0x1f2d
leaq 0x7aa7(%rip), %rdi      # 0x91d3
xorl %eax, %eax
callq 0x1110
movq (%rbp), %rdx
movq %rbx, %rdi
pushq $0xc
popq %rsi
callq 0x1f2d
leaq 0x7a93(%rip), %rdi      # 0x91dc
xorl %eax, %eax
callq 0x1110
testl %r15d, %r15d
jle 0x1787
leaq 0x7a87(%rip), %rdi      # 0x91e3
xorl %eax, %eax
callq 0x1110
movslq 0xf8(%rsp), %rdi
subq %r13, %rdi
movq (%rbp), %rdx
xorl %esi, %esi
callq 0x1f2d
leaq 0x7a6e(%rip), %rdi      # 0x91ee
xorl %eax, %eax
callq 0x1110
movq 0x10(%rsp), %rbx
leal -0x1(%rbx), %eax
movl %eax, 0x18(%rsp)
pushq $0xa
popq %rdi
callq 0x1040
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq %rcx, %rax
je 0x17bd
addq (%r12,%rcx,8), %rbx
addq (%r14,%rcx,8), %rbp
incq %rcx
jmp 0x17ab
movl 0x20(%rsp), %eax
cmpl $0x28, %eax
jl 0x17cb
shrl $0x3, %eax
jmp 0x17d9
pushq $0x5
popq %rsi
cmpl $0x13, %eax
jg 0x17db
cltd
pushq $0x4
popq %rcx
idivl %ecx
movl %eax, %esi
leaq 0x7a1e(%rip), %rdi      # 0x9200
xorl %eax, %eax
callq 0x1110
leaq 0x7ac9(%rip), %rdi      # 0x92b9
callq 0x10a0
movq 0x10(%rsp), %rax
movq -0x8(%r14,%rax,8), %r15
movq -0x8(%r12,%rax,8), %r13
cvtsi2sd %r13, %xmm0
cvtsi2sd %rbx, %xmm1
movsd 0x77f2(%rip), %xmm2     # 0x9008
mulsd %xmm2, %xmm0
divsd %xmm1, %xmm0
cvtsi2sd %r15, %xmm1
mulsd %xmm2, %xmm1
cvtsi2sd %rbp, %xmm2
divsd %xmm2, %xmm1
leaq 0x79f5(%rip), %rdi      # 0x922c
movl 0x18(%rsp), %esi
movq %r13, %rdx
movq %r15, %rcx
movb $0x2, %al
callq 0x1110
subq %r15, %rbp
subq %r13, %rbx
cvtsi2sd %rbx, %xmm0
movsd %xmm0, 0x18(%rsp)
cvtsi2sd %rbp, %xmm3
movq 0x10(%rsp), %rax
leal -0x2(%rax), %r15d
xorl %r13d, %r13d
xorl %ebp, %ebp
movl %r15d, %ebx
movsd %xmm3, 0x20(%rsp)
testl %ebx, %ebx
js 0x18cc
movl %ebx, %eax
movq (%r14,%rax,8), %rcx
testq %rcx, %rcx
jle 0x18c8
movq (%r12,%rax,8), %rdx
addq %rdx, %r13
addq %rcx, %rbp
cvtsi2sd %r13, %xmm0
movsd 0x776d(%rip), %xmm2     # 0x9008
mulsd %xmm2, %xmm0
divsd 0x18(%rsp), %xmm0
cvtsi2sd %rbp, %xmm1
mulsd %xmm2, %xmm1
divsd %xmm3, %xmm1
leaq 0x79a1(%rip), %rdi      # 0x925a
movl %ebx, %esi
movb $0x2, %al
callq 0x1110
movsd 0x20(%rsp), %xmm3
decl %ebx
jmp 0x1875
xorl %eax, %eax
pushq $-0x1
popq %rdx
movl %edx, %esi
movq 0x38(%rsp), %r12
leaq 0xc8(%rsp), %rbx
movq 0x30(%rsp), %r13
testl %r15d, %r15d
js 0x193a
movl %r15d, %ecx
movq (%r14,%rcx,8), %rcx
testq %rcx, %rcx
jle 0x1935
addq %rax, %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x7702(%rip), %xmm0     # 0x9008
leal 0x1(%r15), %eax
movl %eax, %edi
testl %edx, %edx
js 0x1912
movl %edx, %edi
divsd %xmm3, %xmm0
ucomisd 0x76f2(%rip), %xmm0     # 0x9010
cmoval %edi, %edx
testl %esi, %esi
js 0x1927
movl %esi, %eax
ucomisd 0x76e9(%rip), %xmm0     # 0x9018
cmoval %eax, %esi
movq %rcx, %rax
decl %r15d
jmp 0x18e5
leaq 0x7945(%rip), %rdi      # 0x9286
xorl %eax, %eax
callq 0x1110
movq 0x8(%rsp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x399f
incq %r13
jmp 0x1433
leaq 0xc82f(%rip), %rax      # 0xe198
movq (%rax), %rdi
callq 0x1030
xorl %edi, %edi
callq 0x1280
movq 0xc679(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0xc80f(%rip), %rax      # 0xe198
movq (%rax), %rdx
movq 0x8(%r12), %rcx
leaq 0x769c(%rip), %rsi      # 0x9034
jmp 0x140f
movq 0xc654(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0xc7ea(%rip), %rax      # 0xe198
movq (%rax), %rdx
leaq 0x771b(%rip), %rsi      # 0x90d3
jmp 0x1a5d
movq 0xc634(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0xc7ca(%rip), %rax      # 0xe198
movq (%rax), %rdx
leaq 0x772f(%rip), %rsi      # 0x9107
jmp 0x1a5d
movq 0xc614(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0xc7aa(%rip), %rax      # 0xe198
movq (%rax), %rdx
leaq 0x7746(%rip), %rsi      # 0x913e
jmp 0x1a5d
movq 0xc5f7(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0xc78d(%rip), %rax      # 0xe198
movq (%rax), %rdx
leaq 0x775b(%rip), %rsi      # 0x9170
jmp 0x1a5d
movq 0xc5da(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0xc770(%rip), %rax      # 0xe198
movq (%rax), %rdx
movl 0x100(%rsp), %ecx
leaq 0x7645(%rip), %rsi      # 0x907e
xorl %eax, %eax
callq 0x11b0
jmp 0x1a64
movq 0xc5af(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0xc745(%rip), %rax      # 0xe198
movq (%rax), %rdx
leaq 0x764d(%rip), %rsi      # 0x90aa
xorl %eax, %eax
callq 0x11b0
pushq $0x1
popq %rdi
callq 0x1280