// File: /workspace/github_c_makefile_star2_O0/thegenemyers[P]DASCRUBBER/DASqv.c
// Function: CALCULATE_QVS
static void CALCULATE_QVS(int aread, Overlap *ovls, int novl)
{ static int    nmax = 0;
  static int   *hist = NULL;
  static int   *cist = NULL;
  static uint8 *qvec = NULL;
  static int    partial;

  int  alen, atick;
  int *tick, *cick;
  int  i;

  alen  = DB->reads[aread].rlen;
  atick = (alen + (TRACE_SPACING-1))/TRACE_SPACING;

  if (alen < HGAP_MIN)
    { fwrite(&QV_INDEX,sizeof(int64),1,QV_AFILE);
      return;
    }

#if defined(QV_DEBUG)
  printf("AREAD %d",aread);
  if (novl == 0)
    printf(" EMPTY");
  printf("\n");
#endif

  //  QV SCORES
  //       Allocate or expand data structures for qv calculation as needed

  if (atick > nmax)
    { nmax = atick*1.2 + 100;

      hist = (int *) Realloc(hist,nmax*MAXQV1*sizeof(int),"Allocating histograms");
      cist = (int *) Realloc(cist,nmax*MAXQV1*sizeof(int),"Allocating histograms");
      qvec = (uint8 *) Realloc(qvec,nmax*sizeof(uint8),"Allocating QV vector");

      if (hist == NULL || cist == NULL || qvec == NULL)
        exit (1);

      for (i = MAXQV1*nmax-1; i >= 0; i--)
        hist[i] = cist[i] = 0;
      partial = PARTIAL*TRACE_SPACING;
    }

  //  For every segment, fill histogram of match diffs for every one of the
  //   atick intervals, building separate histograms, hist & cist, for forward
  //   and reverse B-hits

  for (i = 0; i < novl; i++)
    { Path   *path;
      uint16 *trace;
      int    *ht;
      int     tlen, abit;
      int     a, b, x;

      path  = &(ovls[i].path);
      trace = (uint16 *) path->trace;
      tlen  = path->tlen;

      if (COMP(ovls[i].flags))
        ht = cist;
      else
        ht = hist;

      b = 0;
      a = (path->abpos/TRACE_SPACING)*MAXQV1;

      abit = (path->abpos % TRACE_SPACING);
      if (abit != 0)
        { a += MAXQV1;
          b += 2;
        }

      abit = (path->aepos % TRACE_SPACING);
      if (abit != 0)
        tlen -= 2;

      while (b < tlen)
        { x = (int) ((200.*trace[b]) / (TRACE_SPACING + trace[b+1]));
          if (x > MAXQV)
            x = MAXQV;
          ht[a + x] += 1;
          a += MAXQV1;
          b += 2;
        }

      if (path->aepos == alen && abit >= partial)
        { x = (int) ((200.*trace[tlen]) / (abit + trace[tlen+1]));
          if (x > MAXQV)
            x = MAXQV;
          ht[a + x] += 1;
        }
    }

  //  For every segment, qv score is the maximum of the averages of the QV_DEEP lowest
  //    in the forward and reverse directions (if each is QV_DEEP), or the average
  //    of overlap scores (if between MINCOV and QV_DEEP-1), or MAXQV if no overlaps at all.
  //    Reset histogram for segment to zeros.

  tick = hist;
  cick = cist;
  for (i = 0; i < atick; i++)
    { int  v, y;
      int  qvn, qvc;
      int  cntn, cntc;
      int  sumn, sumc;

#ifdef QV_DEBUG
      { int min, max;

        printf("   [%5d,%5d]:",i*TRACE_SPACING,(i+1)*TRACE_SPACING);

        for (v = 0; v <= MAXQV; v++)
          if (tick[v] > 0)
            break;
        min = v;

        for (v = MAXQV; v >= 0; v--)
          if (tick[v] > 0)
            break;
        max = v;

        for (v = min; v <= max; v++)
          if (tick[v] == 1)
            printf(" %2d",v);
          else if (tick[v] > 1)
            printf(" %2d(%d)",v,tick[v]);

        printf("\n                :");

        for (v = 0; v <= MAXQV; v++)
          if (cick[v] > 0)
            break;
        min = v;

        for (v = MAXQV; v >= 0; v--)
          if (cick[v] > 0)
            break;
        max = v;

        for (v = min; v <= max; v++)
          if (cick[v] == 1)
            printf(" %2d",v);
          else if (cick[v] > 1)
            printf(" %2d(%d)",v,cick[v]);
       }
#endif

      for (v = 0; v <= MAXQV; v++)
        sgram[v] += tick[v] + cick[v];
     
      cntn = sumn = 0;
      for (v = 0; v <= MAXQV; v++)
        { y = tick[v];
          tick[v] = 0;
          cntn += y;
          sumn += y*v;
          if (cntn >= QV_DEEP)
            { sumn -= (cntn-QV_DEEP)*v;
              cntn  = QV_DEEP;
              break;
            }
        }
      for (v++; v <= MAXQV; v++)
        tick[v] = 0;
     
      cntc = sumc = 0;
      for (v = 0; v <= MAXQV; v++)
        { y = cick[v];
          cick[v] = 0;
          cntc += y;
          sumc += y*v;
          if (cntc >= QV_DEEP)
            { sumc -= (cntc-QV_DEEP)*v;
              cntc  = QV_DEEP;
              break;
            }
        }
      for (v++; v <= MAXQV; v++)
        cick[v] = 0;

      if (cntn >= MINCOV)
        qvn = sumn/cntn;
      else
        qvn = MAXQV;

      if (cntc >= MINCOV)
        qvc = sumc/cntc;
      else
        qvc = MAXQV;

      if (qvn > qvc)
        qvec[i] = (uint8) qvn;
      else
        qvec[i] = (uint8) qvc;

      tick += MAXQV1;
      cick += MAXQV1;

#ifdef QV_DEBUG
      printf(" >> %2d(%d) %2d(%d) = %2d <<\n",qvn,cntn,qvc,cntc,qvec[i]);
#endif
    }

  //  Accumulate qv histogram and append qv's to .qual file

  for (i = 0; i < atick; i++)
    qgram[qvec[i]] += 1;
  nreads += 1;
  totlen += alen;

  fwrite(qvec,sizeof(uint8),atick,QV_DFILE);
  QV_INDEX += atick;
  fwrite(&QV_INDEX,sizeof(int64),1,QV_AFILE);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq 0x273ec(%rip), %rax     # 0x2b188
movq 0x60(%rax), %rax
movslq -0x4(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl 0x279b6(%rip), %ecx     # 0x2b770
subl $0x1, %ecx
addl %ecx, %eax
cltd
idivl 0x279aa(%rip)           # 0x2b770
movl %eax, -0x1c(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x27636(%rip), %eax     # 0x2b408
jge 0x3df6
movq 0x2797d(%rip), %rcx     # 0x2b758
leaq 0x27986(%rip), %rdi     # 0x2b768
movl $0x8, %esi
movl $0x1, %edx
callq 0x22a0
jmp 0x4431
movl -0x1c(%rbp), %eax
cmpl 0x27a0d(%rip), %eax     # 0x2b80c
jle 0x3f29
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd 0x2220e(%rip), %xmm1    # 0x26020
mulsd %xmm1, %xmm0
movsd 0x221ea(%rip), %xmm1    # 0x26008
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x279e0(%rip)     # 0x2b80c
movq 0x279dd(%rip), %rdi     # 0x2b810
imull $0x33, 0x279d2(%rip), %eax # 0x2b80c
movslq %eax, %rsi
shlq $0x2, %rsi
leaq 0x226f6(%rip), %rdx     # 0x2653e
callq 0x1aae0
movq %rax, 0x279bc(%rip)     # 0x2b810
movq 0x279bd(%rip), %rdi     # 0x2b818
imull $0x33, 0x279aa(%rip), %eax # 0x2b80c
movslq %eax, %rsi
shlq $0x2, %rsi
leaq 0x226ce(%rip), %rdx     # 0x2653e
callq 0x1aae0
movq %rax, 0x2799c(%rip)     # 0x2b818
movq 0x2799d(%rip), %rdi     # 0x2b820
movslq 0x27982(%rip), %rsi     # 0x2b80c
shlq $0x0, %rsi
leaq 0x226bf(%rip), %rdx     # 0x26554
callq 0x1aae0
movq %rax, 0x2797f(%rip)     # 0x2b820
cmpq $0x0, 0x27967(%rip)     # 0x2b810
je 0x3ebf
cmpq $0x0, 0x27965(%rip)     # 0x2b818
je 0x3ebf
cmpq $0x0, 0x27963(%rip)     # 0x2b820
jne 0x3ec9
movl $0x1, %edi
callq 0x2290
imull $0x33, 0x2793c(%rip), %eax # 0x2b80c
subl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jl 0x3f0b
movq 0x27935(%rip), %rax     # 0x2b818
movslq -0x34(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq 0x2791b(%rip), %rax     # 0x2b810
movslq -0x34(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3ed6
cvtsi2sdl 0x2785d(%rip), %xmm1 # 0x2b770
movsd 0x22115(%rip), %xmm0    # 0x26030
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x278ff(%rip)     # 0x2b828
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x410d
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x3f8b
movq 0x27893(%rip), %rax     # 0x2b818
movq %rax, -0x50(%rbp)
jmp 0x3f96
movq 0x2787e(%rip), %rax     # 0x2b810
movq %rax, -0x50(%rbp)
movl $0x0, -0x60(%rbp)
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
cltd
idivl 0x277c5(%rip)           # 0x2b770
imull $0x33, %eax, %eax
movl %eax, -0x5c(%rbp)
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
cltd
idivl 0x277b1(%rip)           # 0x2b770
movl %edx, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
je 0x3fda
movl -0x5c(%rbp), %eax
addl $0x33, %eax
movl %eax, -0x5c(%rbp)
movl -0x60(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x60(%rbp)
movq -0x40(%rbp), %rax
movl 0x18(%rax), %eax
cltd
idivl 0x27788(%rip)           # 0x2b770
movl %edx, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
je 0x3ffa
movl -0x54(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x54(%rbp)
jmp 0x3ffc
movl -0x60(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x4082
movq -0x48(%rbp), %rax
movslq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
cvtsi2sd %eax, %xmm1
movsd 0x2201c(%rip), %xmm0    # 0x26038
mulsd %xmm1, %xmm0
movl 0x2774a(%rip), %eax     # 0x2b770
movq -0x48(%rbp), %rcx
movl -0x60(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzwl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x64(%rbp)
cmpl $0x32, -0x64(%rbp)
jle 0x4055
movl $0x32, -0x64(%rbp)
movq -0x50(%rbp), %rax
movl -0x5c(%rbp), %ecx
addl -0x64(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x5c(%rbp), %eax
addl $0x33, %eax
movl %eax, -0x5c(%rbp)
movl -0x60(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x60(%rbp)
jmp 0x3ffc
movq -0x40(%rbp), %rax
movl 0x18(%rax), %eax
cmpl -0x18(%rbp), %eax
jne 0x40fd
movl -0x58(%rbp), %eax
cmpl 0x27791(%rip), %eax     # 0x2b828
jl 0x40fd
movq -0x48(%rbp), %rax
movslq -0x54(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
cvtsi2sd %eax, %xmm1
movsd 0x21f87(%rip), %xmm0    # 0x26038
mulsd %xmm1, %xmm0
movl -0x58(%rbp), %eax
movq -0x48(%rbp), %rcx
movl -0x54(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzwl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x64(%rbp)
cmpl $0x32, -0x64(%rbp)
jle 0x40e7
movl $0x32, -0x64(%rbp)
movq -0x50(%rbp), %rax
movl -0x5c(%rbp), %ecx
addl -0x64(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x40ff
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3f30
movq 0x276fc(%rip), %rax     # 0x2b810
movq %rax, -0x28(%rbp)
movq 0x276f9(%rip), %rax     # 0x2b818
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x437d
movl $0x0, -0x68(%rbp)
cmpl $0x32, -0x68(%rbp)
jg 0x4181
movq -0x28(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x30(%rbp), %rcx
movslq -0x68(%rbp), %rdx
addl (%rcx,%rdx,4), %eax
movslq %eax, %rdx
movslq -0x68(%rbp), %rcx
leaq 0x27449(%rip), %rax     # 0x2b5b0
addq (%rax,%rcx,8), %rdx
leaq 0x2743e(%rip), %rax     # 0x2b5b0
movq %rdx, (%rax,%rcx,8)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x413d
movl $0x0, -0x80(%rbp)
movl $0x0, -0x78(%rbp)
movl $0x0, -0x68(%rbp)
cmpl $0x32, -0x68(%rbp)
jg 0x4207
movq -0x28(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x6c(%rbp)
movq -0x28(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x6c(%rbp), %eax
addl -0x78(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x6c(%rbp), %eax
imull -0x68(%rbp), %eax
addl -0x80(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x78(%rbp), %eax
cmpl 0x27234(%rip), %eax     # 0x2b40c
jl 0x41fa
movl -0x78(%rbp), %ecx
subl 0x27229(%rip), %ecx     # 0x2b40c
imull -0x68(%rbp), %ecx
movl -0x80(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x80(%rbp)
movl 0x27217(%rip), %eax     # 0x2b40c
movl %eax, -0x78(%rbp)
jmp 0x4207
jmp 0x41fc
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x4196
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
cmpl $0x32, -0x68(%rbp)
jg 0x4230
movq -0x28(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x4210
movl $0x0, -0x84(%rbp)
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x68(%rbp)
cmpl $0x32, -0x68(%rbp)
jg 0x42c5
movq -0x30(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x6c(%rbp)
movq -0x30(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x6c(%rbp), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x6c(%rbp), %eax
imull -0x68(%rbp), %eax
addl -0x84(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x7c(%rbp), %eax
cmpl 0x2717c(%rip), %eax     # 0x2b40c
jl 0x42b8
movl -0x7c(%rbp), %ecx
subl 0x27171(%rip), %ecx     # 0x2b40c
imull -0x68(%rbp), %ecx
movl -0x84(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x84(%rbp)
movl 0x27159(%rip), %eax     # 0x2b40c
movl %eax, -0x7c(%rbp)
jmp 0x42c5
jmp 0x42ba
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x4248
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
cmpl $0x32, -0x68(%rbp)
jg 0x42ee
movq -0x30(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x42ce
cmpl $0x2, -0x78(%rbp)
jl 0x4300
movl -0x80(%rbp), %eax
cltd
idivl -0x78(%rbp)
movl %eax, -0x70(%rbp)
jmp 0x4307
movl $0x32, -0x70(%rbp)
cmpl $0x2, -0x7c(%rbp)
jl 0x431c
movl -0x84(%rbp), %eax
cltd
idivl -0x7c(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x4323
movl $0x32, -0x74(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x74(%rbp), %eax
jle 0x4340
movl -0x70(%rbp), %eax
movb %al, %dl
movq 0x274e9(%rip), %rax     # 0x2b820
movslq -0x34(%rbp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x4353
movl -0x74(%rbp), %eax
movb %al, %dl
movq 0x274d4(%rip), %rax     # 0x2b820
movslq -0x34(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x28(%rbp), %rax
addq $0xcc, %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq $0xcc, %rax
movq %rax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x412a
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x43c2
movq 0x2748d(%rip), %rax     # 0x2b820
movslq -0x34(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x2706c(%rip), %rax     # 0x2b410
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
leaq 0x2705d(%rip), %rax     # 0x2b410
movq %rdx, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4384
movq 0x273af(%rip), %rax     # 0x2b778
addq $0x1, %rax
movq %rax, 0x273a4(%rip)     # 0x2b778
movslq -0x18(%rbp), %rax
addq 0x273a1(%rip), %rax     # 0x2b780
movq %rax, 0x2739a(%rip)     # 0x2b780
movq 0x27433(%rip), %rdi     # 0x2b820
movslq -0x1c(%rbp), %rdx
movq 0x27368(%rip), %rcx     # 0x2b760
movl $0x1, %esi
callq 0x22a0
movslq -0x1c(%rbp), %rax
addq 0x2735b(%rip), %rax     # 0x2b768
movq %rax, 0x27354(%rip)     # 0x2b768
movq 0x2733d(%rip), %rcx     # 0x2b758
leaq 0x27346(%rip), %rdi     # 0x2b768
movl $0x8, %esi
movl $0x1, %edx
callq 0x22a0
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)