// File: /workspace/github_c_makefile_star2_O0/thegenemyers[P]DAMASKER/REPmask.c
// Function: PARTITION
static void PARTITION(int aread, Overlap *ovls, int novl)
{ int   ntrim, *trim;

#if defined(DEBUG_BLOCKS) || defined(DEBUG_GAP_MERGE)
  printf("\nAREAD %d (%d)\n",aread,DB->reads[aread].rlen);
#endif

  if (novl <= 0)
    { fwrite(&MSK_INDEX,sizeof(int64),1,MSK_AFILE);
      return;
    }

  //  Merge overlap pairs that appear to have a low-quality induced gap between them

  { int   i, j, k;
    int   bread, agap, bgap;
    Path *ipath, *jpath;

#ifdef DEBUG_GAP_MERGE
    printf("\nGAPS\n");
    ipath = &(ovls[0].path);
    bread = ovls[0].bread;
    printf("    %5d %5d  [%5d,%5d] [%5d,%5d]\n",
           aread,bread,ipath->abpos,ipath->aepos,ipath->bbpos,ipath->bepos);
#endif

    k = 0;
    for (i = 1; i < novl; i++)
      { ipath = &(ovls[i].path);
        bread = ovls[i].bread;
#ifdef DEBUG_GAP_MERGE
        printf("    %5d %5d  [%5d,%5d] [%5d,%5d]\n",
               aread,bread,ipath->abpos,ipath->aepos,ipath->bbpos,ipath->bepos);
#endif
        for (j = k; j >= 0; j--)
          if (bread == ovls[j].bread && COMP(ovls[i].flags) == COMP(ovls[j].flags))
            { jpath = &(ovls[j].path);
              if (jpath->aepos < ipath->abpos+PEEL_BACK)
                { agap = ipath->abpos - jpath->aepos;
                  bgap = ipath->bbpos - jpath->bepos;
                  if (abs(agap-bgap) < .2*(agap+bgap) + 200)
                    { if (ipath->aepos > jpath->aepos)
                        jpath->aepos = ipath->aepos;
                      if (ipath->bepos > jpath->bepos)
                        jpath->bepos = ipath->bepos;
                      jpath->tlen += agap;
#ifdef DEBUG_GAP_MERGE
                      printf("  Span off = %d ->%4d %4d",k-j,agap,bgap);
                      printf("    %5d %5d  [%5d,%5d] [%5d,%5d]\n",
                             aread,bread,jpath->abpos,jpath->aepos,jpath->bbpos,jpath->bepos);
#endif
                      break;
                    }
                }
            }
          else
            { j = -1;
              break;
            }
        if (j < 0)
          { k += 1;
            ovls[k] = ovls[i];
          }
      }
    novl = k+1;
  }

  //  For reads that come from the same well, take only the las from the one that
  //    covers A the most (in both directions).

  { int   i, j, k, t;
    int   bread, lread, cssr;
    int   DBreads, alow, ahigh;
    Path *ipath;
    int   best, score;
    int   bnbeg, snbeg;
#ifdef DEBUG_WELL_SELECTION
    int   multi;
#endif

    DBreads = DB->nreads;
    for (j = aread+1; j < DBreads; j++)
      if ((Reads[j].flags & DB_CCS) == 0)
        break;
    ahigh = j;
    for (j = aread; j >= 0; j--)
      if ((Reads[j].flags & DB_CCS) == 0)
        break;
    alow = j;

    k = 0;
    for (i = 0; i < novl; i = j)
      { bread = ovls[i].bread;

        if (alow <= bread && bread < ahigh)
          { j += 1;
            continue;
          }

        for (j = bread+1; j < DBreads; j++)
          if ((Reads[j].flags & DB_CCS) == 0)
            break;
        cssr = j;

#ifdef DEBUG_WELL_SELECTION
        multi = (cssr-bread > 1);
        if (multi)
          { printf(" CLUSTER: %d - %d:",bread,cssr);
            for (j = bread+1; j < cssr; j++)
              { printf(" %d",Reads[j].fpulse - (Reads[j-1].fpulse+Reads[j-1].rlen));
                if (Reads[j].fpulse - (Reads[j-1].fpulse+Reads[j-1].rlen) > 60)
                  printf(" XXX");
              }
            printf(" (%d)\n",Reads[bread].origin);
          }
#endif

        lread = -1;
        score = best = 0;
        for (j = i; bread < cssr; j++)
          { if (j < novl)
              { bread = ovls[j].bread;
                ipath = &(ovls[j].path);
              }

            if (j >= novl || bread != lread)
              { if (score > best)
                  { best  = score;
                    bnbeg = snbeg;
                  }
                score = 0;
                snbeg = j;
              }

            if (j >= novl || bread >= cssr)
              break;

#ifdef DEBUG_WELL_SELECTION
            if (multi)
              printf("    %3d: %5d %5d  [%5d,%5d] [%5d,%5d] %c\n",
                     j,aread,bread,ipath->abpos,ipath->aepos,ipath->bbpos,ipath->bepos,
                     COMP(ovls[j].flags) ? 'c' : 'n');
#endif

            score += ipath->aepos - ipath->abpos;
            lread = bread;
          }

        if (best > 0)
          { bread = ovls[bnbeg].bread;
            for (t = bnbeg; t < j; t++)
              if (ovls[t].bread == bread)
                ovls[k++] = ovls[t];
#ifdef DEBUG_WELL_SELECTION
            if (multi)
              printf("    %3d -> %d\n",bnbeg,best);
#endif
          }
      }
    novl = k;
  }

  //  Find the high-coverage intervals over the pair-merged alignment intervals

  trim = blocks(ovls,novl,&ntrim);

  if (VERBOSE)
    { int i;

      for (i = 0; i < ntrim; i += 2)
        masked += trim[i+1]-trim[i];
      nmasks += ntrim/2;
      nreads += 1;
      totlen += DB->reads[aread].rlen;
    }

  //  Write out the trim intervals for the read

  { int i;

    for (i = 0; i < ntrim; i += 2)
      { fwrite(trim+i,sizeof(int),1,MSK_DFILE);
        fwrite(trim+(i+1),sizeof(int),1,MSK_DFILE);
      }
    MSK_INDEX += ntrim*sizeof(int);
    fwrite(&MSK_INDEX,sizeof(int64),1,MSK_AFILE);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jg 0x37ad
movq 0x27ca6(%rip), %rcx     # 0x2b438
leaq 0x27caf(%rip), %rdi     # 0x2b448
movl $0x8, %esi
movl $0x1, %edx
callq 0x22b0
jmp 0x3d94
movl $0x0, -0x2c(%rbp)
movl $0x1, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3985
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jl 0x393e
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
movslq -0x28(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x28(%rcx), %eax
jne 0x3925
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x1, %eax
movq -0x10(%rbp), %rcx
movslq -0x28(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
movl 0x20(%rcx), %ecx
andl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3925
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x40(%rbp), %rcx
movl 0x10(%rcx), %ecx
addl $0x12c, %ecx            # imm = 0x12C
cmpl %ecx, %eax
jge 0x3923
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x48(%rbp), %rcx
subl 0x18(%rcx), %eax
movl %eax, -0x34(%rbp)
movq -0x40(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x48(%rbp), %rcx
subl 0x1c(%rcx), %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cvtsi2sd %eax, %xmm1
movl -0x34(%rbp), %eax
addl -0x38(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x22760(%rip), %xmm2    # 0x26020
mulsd %xmm2, %xmm0
movsd 0x2275c(%rip), %xmm2    # 0x26028
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3921
movq -0x40(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x48(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jle 0x38f4
movq -0x40(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x48(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jle 0x3912
movq -0x40(%rbp), %rax
movl 0x1c(%rax), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl -0x34(%rbp), %ecx
movq -0x48(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
jmp 0x393e
jmp 0x3923
jmp 0x392e
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
jmp 0x393e
jmp 0x3930
movl -0x28(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x37f5
cmpl $0x0, -0x28(%rbp)
jge 0x3975
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x2c(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movslq -0x24(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rsi
movl $0x30, %edx
callq 0x21e0
jmp 0x3977
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x37bb
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movq 0x277f3(%rip), %rax     # 0x2b188
movl 0x30(%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x68(%rbp), %eax
jge 0x39da
movq 0x27a4d(%rip), %rax     # 0x2b400
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
jne 0x39cd
jmp 0x39da
jmp 0x39cf
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x39a4
movl -0x50(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x4(%rbp), %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jl 0x3a1a
movq 0x27a0d(%rip), %rax     # 0x2b400
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
jne 0x3a0d
jmp 0x3a1a
jmp 0x3a0f
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x39e6
movl -0x50(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3c14
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
movl %eax, -0x5c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jg 0x3a6d
movl -0x5c(%rbp), %eax
cmpl -0x70(%rbp), %eax
jge 0x3a6d
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3c09
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x68(%rbp), %eax
jge 0x3aac
movq 0x2797b(%rip), %rax     # 0x2b400
movslq -0x50(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
jne 0x3a9f
jmp 0x3aac
jmp 0x3aa1
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3a76
movl -0x50(%rbp), %eax
movl %eax, -0x64(%rbp)
movl $0xffffffff, -0x60(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x3b7d
movl -0x50(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3b09
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3b19
movl -0x5c(%rbp), %eax
cmpl -0x60(%rbp), %eax
je 0x3b43
movl -0x80(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jle 0x3b33
movl -0x80(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x84(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3b53
movl -0x5c(%rbp), %eax
cmpl -0x64(%rbp), %eax
jl 0x3b55
jmp 0x3b7d
movq -0x78(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x78(%rbp), %rcx
subl 0x10(%rcx), %eax
addl -0x80(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3acd
cmpl $0x0, -0x7c(%rbp)
jle 0x3c07
movq -0x10(%rbp), %rax
movslq -0x84(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
movl %eax, -0x5c(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x3c05
movq -0x10(%rbp), %rax
movslq -0x58(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
cmpl -0x5c(%rbp), %eax
jne 0x3bf8
movq -0x10(%rbp), %rdi
movl -0x54(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x54(%rbp)
cltq
imulq $0x30, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movslq -0x58(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rsi
movl $0x30, %edx
callq 0x21e0
jmp 0x3bfa
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x3ba8
jmp 0x3c07
jmp 0x3c09
movl -0x50(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x3a2e
movl -0x54(%rbp), %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
leaq -0x18(%rbp), %rdx
callq 0x3da0
movq %rax, -0x20(%rbp)
cmpl $0x0, 0x277c7(%rip)     # 0x2b3fc
je 0x3ce7
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x3c92
movq -0x20(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movq -0x20(%rbp), %rcx
movslq -0x8c(%rbp), %rdx
subl (%rcx,%rdx,4), %eax
cltq
addq 0x2779e(%rip), %rax     # 0x2b418
movq %rax, 0x27797(%rip)     # 0x2b418
movl -0x8c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x8c(%rbp)
jmp 0x3c45
movl -0x18(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cltq
addq 0x2777a(%rip), %rax     # 0x2b420
movq %rax, 0x27773(%rip)     # 0x2b420
movq 0x27754(%rip), %rax     # 0x2b408
addq $0x1, %rax
movq %rax, 0x27749(%rip)     # 0x2b408
movq 0x274c2(%rip), %rax     # 0x2b188
movq 0x60(%rax), %rax
movslq -0x4(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
addq 0x27730(%rip), %rax     # 0x2b410
movq %rax, 0x27729(%rip)     # 0x2b410
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x3d61
movq -0x20(%rbp), %rdi
movslq -0x90(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
movq 0x2772b(%rip), %rcx     # 0x2b440
movl $0x4, %esi
movl $0x1, %edx
callq 0x22b0
movq -0x20(%rbp), %rdi
movl -0x90(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
movq 0x276ff(%rip), %rcx     # 0x2b440
movl $0x4, %esi
movl $0x1, %edx
callq 0x22b0
movl -0x90(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x90(%rbp)
jmp 0x3cf1
movslq -0x18(%rbp), %rax
shlq $0x2, %rax
addq 0x276d8(%rip), %rax     # 0x2b448
movq %rax, 0x276d1(%rip)     # 0x2b448
movq 0x276ba(%rip), %rcx     # 0x2b438
leaq 0x276c3(%rip), %rdi     # 0x2b448
movl $0x8, %esi
movl $0x1, %edx
callq 0x22b0
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)