// File: /workspace/github_c_makefile_star2_O3/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 0x8d87(%rip), %rdi      # 0xb020
leaq 0x8fb9(%rip), %rsi      # 0xb259
callq 0x2a7e
leaq 0xdeec(%rip), %rcx      # 0x10198
movq %rax, (%rcx)
cmpl $0x2, %ebx
jge 0x22db
movq 0xdd3d(%rip), %rcx      # 0xfff8
movq (%rcx), %rdi
leaq 0x8d61(%rip), %rsi      # 0xb026
leaq 0x8fdd(%rip), %rcx      # 0xb2a9
movq %rax, %rdx
xorl %eax, %eax
callq 0x11b0
jmp 0x2982
movl %ebx, %eax
movq %rax, 0x28(%rsp)
movl $0x1, %r13d
leaq 0xc8(%rsp), %rbx
movq %r12, 0x18(%rsp)
movq (%r12,%r13,8), %rdi
movq %rbx, %rsi
callq 0x3a47
testl %eax, %eax
js 0x2982
cmpl $0x1, %eax
je 0x2896
movq %rbx, %rdi
callq 0x4250
movl 0x100(%rsp), %esi
movq 0x110(%rsp), %rbx
testl %esi, %esi
je 0x2389
leaq 0x8f83(%rip), %rdi      # 0xb2b7
movq %rdi, %rdx
callq 0x2d6a
movq %rbx, %rdi
movq %rax, %rsi
leaq 0x8d15(%rip), %rdx      # 0xb05e
leaq 0x8d13(%rip), %rcx      # 0xb063
callq 0x2c63
movq %rax, %rdi
leaq 0x8d0a(%rip), %rsi      # 0xb069
callq 0x2a7e
testq %rax, %rax
je 0x2982
movq %rax, %r15
movq %rax, %rdi
leaq 0x9af2(%rip), %rsi      # 0xbe6c
callq 0x1250
testq %rax, %rax
jne 0x23d9
jmp 0x2935
movq %rbx, %rdi
leaq 0x8f24(%rip), %rsi      # 0xb2b7
leaq 0x8cc4(%rip), %rdx      # 0xb05e
leaq 0x8cc2(%rip), %rcx      # 0xb063
callq 0x2c63
movq %rax, %rdi
leaq 0x8cb9(%rip), %rsi      # 0xb069
callq 0x2a7e
testq %rax, %rax
je 0x2982
movq %rax, %r15
movq %rax, %rdi
leaq 0x9aa1(%rip), %rsi      # 0xbe6c
callq 0x1250
testq %rax, %rax
je 0x2960
movq %rax, %r14
movl $0x4, %esi
movl $0x1, %edx
leaq 0x14(%rsp), %rdi
movq %rax, %rcx
callq 0x10c0
movl $0x4, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rcx
callq 0x10c0
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1240
movq %r14, %rdi
callq 0x11c0
movl 0x14(%rsp), %ecx
incl %ecx
imull 0x10(%rsp), %ecx
subl %eax, %ecx
addl $0x8, %ecx
movslq %ecx, %rsi
movq %r14, %rdi
movl $0x2, %edx
callq 0x1240
movl $0x0, 0xac(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0xa8(%rsp), %rdx
callq 0x635b
testl %eax, %eax
jne 0x28bb
movl $0x0, 0x8c(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0x88(%rsp), %rdx
callq 0x635b
testl %eax, %eax
jne 0x28db
movl $0x0, 0x4c(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0x48(%rsp), %rdx
callq 0x635b
testl %eax, %eax
jne 0x28fb
movl $0x0, 0x6c(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0x68(%rsp), %rdx
callq 0x635b
testl %eax, %eax
jne 0x2918
movq %r14, %rdi
callq 0x10d0
movq %r13, 0x38(%rsp)
movq (%r12,%r13,8), %rdi
leaq 0x8cc6(%rip), %rsi      # 0xb1ab
callq 0x2b9b
movq %rax, %r12
movslq 0xf8(%rsp), %r15
movq 0xf0(%rsp), %rbx
movq 0xc0(%rsp), %rax
movl (%rax), %r14d
movq 0xa0(%rsp), %rax
movl (%rax), %ebp
movq 0x60(%rsp), %r13
movl 0x4c(%rsp), %eax
movq %rax, (%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x8c7c(%rip), %rdi      # 0xb1af
xorl %eax, %eax
callq 0x1110
testl %r14d, %r14d
movq %r12, 0x30(%rsp)
jle 0x25b9
leaq 0x8c6b(%rip), %rdi      # 0xb1b6
movl %r14d, %esi
xorl %eax, %eax
callq 0x1110
leaq 0x8c60(%rip), %rdi      # 0xb1bc
movl %ebp, %esi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1110
movslq 0xf8(%rsp), %rcx
testq %rcx, %rcx
movl $0x0, %r8d
movq 0xda58(%rip), %r12      # 0xffd8
jle 0x25d3
movq 0x128(%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 0x2594
jmp 0x25d3
leaq 0x8bfc(%rip), %rdi      # 0xb1bc
movl %ebp, %esi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1110
movq 0xda05(%rip), %r12      # 0xffd8
leaq 0x8bed(%rip), %rdi      # 0xb1c7
xorl %eax, %eax
callq 0x1110
movq (%r12), %rdx
movq %r15, %rdi
movl $0x7, %esi
callq 0x2e19
leaq 0x8bda(%rip), %rdi      # 0xb1d3
xorl %eax, %eax
callq 0x1110
movq (%r12), %rdx
movq %rbx, %rdi
movl $0xc, %esi
callq 0x2e19
leaq 0x8bc4(%rip), %rdi      # 0xb1dc
xorl %eax, %eax
callq 0x1110
testl %r14d, %r14d
jle 0x2656
leaq 0x8bb8(%rip), %rdi      # 0xb1e3
xorl %eax, %eax
callq 0x1110
movslq 0xf8(%rsp), %rdi
subq %r15, %rdi
movq (%r12), %rdx
xorl %esi, %esi
callq 0x2e19
leaq 0x8b9f(%rip), %rdi      # 0xb1ee
xorl %eax, %eax
callq 0x1110
movl $0xa, %edi
callq 0x1040
movq (%rsp), %rcx
testl %ecx, %ecx
jle 0x2687
xorl %eax, %eax
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x8(%rsp), %rdx
addq (%rdx,%rax,8), %rbx
addq (%r13,%rax,8), %r15
incq %rax
cmpq %rax, %rcx
jne 0x2674
jmp 0x268c
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpl $0x28, %ebp
jl 0x2698
shrl $0x3, %ebp
movl %ebp, %esi
jmp 0x26ad
movl $0x5, %esi
cmpl $0x13, %ebp
jg 0x26ad
leal 0x3(%rbp), %esi
testl %ebp, %ebp
cmovnsl %ebp, %esi
sarl $0x2, %esi
movslq %ecx, %r12
leal -0x1(%r12), %ebp
leaq 0x8b44(%rip), %rdi      # 0xb200
xorl %eax, %eax
callq 0x1110
leaq 0x8bef(%rip), %rdi      # 0xb2b9
callq 0x10a0
movq -0x8(%r13,%r12,8), %r14
movq 0x8(%rsp), %rax
movq -0x8(%rax,%r12,8), %r12
cvtsi2sd %r12, %xmm0
cvtsi2sd %rbx, %xmm1
movsd 0x8918(%rip), %xmm2     # 0xb008
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 0x8b15(%rip), %rdi      # 0xb22c
movl %ebp, %esi
movq %r12, %rdx
movq %r14, %rcx
movb $0x2, %al
callq 0x1110
movq (%rsp), %rax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x2, %eax
jl 0x2839
subq %r14, %r15
addl $-0x2, %eax
subq %r12, %rbx
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
movsd %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
movsd %xmm0, 0x20(%rsp)
movl %eax, %r14d
movq %r14, %rbx
xorl %r15d, %r15d
xorl %r12d, %r12d
movq (%r13,%rbx,8), %rcx
testq %rcx, %rcx
jle 0x27be
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rdx
addq %rdx, %r12
addq %rcx, %r15
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movsd 0x8876(%rip), %xmm2     # 0xb008
mulsd %xmm2, %xmm0
divsd 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
mulsd %xmm2, %xmm1
divsd 0x20(%rsp), %xmm1
leaq 0x8aa5(%rip), %rdi      # 0xb25a
movl %ebx, %esi
movb $0x2, %al
callq 0x1110
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0x2769
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x2, (%rsp)
movq 0x18(%rsp), %r12
jl 0x2843
incq %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movq -0x8(%r13,%r14,8), %rax
testq %rax, %rax
jle 0x2832
addq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movl %r14d, %ecx
mulsd 0x8804(%rip), %xmm0     # 0xb008
movl %r14d, %esi
testl %edx, %edx
js 0x280d
movl %edx, %esi
divsd 0x20(%rsp), %xmm0
ucomisd 0x87f5(%rip), %xmm0     # 0xb010
cmoval %esi, %edx
testl %ebp, %ebp
js 0x2824
movl %ebp, %ecx
ucomisd 0x87ec(%rip), %xmm0     # 0xb018
cmoval %ecx, %ebp
movq %rax, %rcx
decq %r14
jg 0x27e4
jmp 0x2843
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %r12
leaq 0x8a3c(%rip), %rdi      # 0xb286
movl %ebp, %esi
xorl %eax, %eax
callq 0x1110
movq 0x30(%rsp), %rdi
callq 0x1030
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x4a2e
movq 0x38(%rsp), %r13
incq %r13
cmpq 0x28(%rsp), %r13
jne 0x22f5
leaq 0xd911(%rip), %rax      # 0x10198
movq (%rax), %rdi
callq 0x1030
xorl %edi, %edi
callq 0x1280
movq 0xd75b(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xd8f1(%rip), %rax      # 0x10198
movq (%rax), %rdx
movq 0x8(%r12), %rcx
leaq 0x877e(%rip), %rsi      # 0xb034
jmp 0x22cf
movq 0xd736(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xd8cc(%rip), %rax      # 0x10198
movq (%rax), %rdx
leaq 0x87fd(%rip), %rsi      # 0xb0d3
jmp 0x297b
movq 0xd716(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xd8ac(%rip), %rax      # 0x10198
movq (%rax), %rdx
leaq 0x8811(%rip), %rsi      # 0xb107
jmp 0x297b
movq 0xd6f6(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xd88c(%rip), %rax      # 0x10198
movq (%rax), %rdx
leaq 0x8828(%rip), %rsi      # 0xb13e
jmp 0x297b
movq 0xd6d9(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xd86f(%rip), %rax      # 0x10198
movq (%rax), %rdx
leaq 0x883d(%rip), %rsi      # 0xb170
jmp 0x297b
movq 0xd6bc(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xd852(%rip), %rax      # 0x10198
movq (%rax), %rdx
movl 0x100(%rsp), %ecx
leaq 0x8727(%rip), %rsi      # 0xb07e
xorl %eax, %eax
callq 0x11b0
jmp 0x2982
movq 0xd691(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xd827(%rip), %rax      # 0x10198
movq (%rax), %rdx
leaq 0x872f(%rip), %rsi      # 0xb0aa
xorl %eax, %eax
callq 0x11b0
movl $0x1, %edi
callq 0x1280