// File: /workspace/github_c_makefile_star2_O2/thegenemyers[P]DAMASKER/tandem.c
// Function: Match_Self
void Match_Self(char *aname, DAZZ_DB *ablock, Align_Spec *aspec)
{ THREAD     threads[NTHREADS];
  Merge_Arg  parmm[NTHREADS];
  Lex_Arg    parmx[NTHREADS];
  Report_Arg parmr[NTHREADS];
  int        pairsort[16];

  int64     nfilt, ncheck;

  KmerPos  *asort, *osort;
  int       alen;
  int64     atot;

  //  Setup 

  atot = ablock->totlen;

  nfilt = ncheck = 0;

  { int64 powr;
    int   i, nbyte;

    for (i = 0; i < NTHREADS; i++)
      parmx[i].sptr = (int64 *) alloca(NTHREADS*BPOWR*sizeof(int64));

    for (i = 0; i < 16; i++)
      pairsort[i] = 0;

    powr = 1;
    for (nbyte = 0; powr < ablock->maxlen; nbyte += 1)
      powr <<= 8;
    for (i = 8; i < 8+nbyte; i++)
      pairsort[i] = 1;

    powr = 1;
    for (nbyte = 0; powr < ablock->nreads; nbyte += 1)
      powr <<= 8;
    for (i = 12; i < 12+nbyte; i++)
      pairsort[i] = 1;
  }

  //  Build K-mer sorted index of ablock

  if (VERBOSE)
    printf("\nIndexing %s\n",aname);

  osort = NULL;   //  Just to shut up dumb compilers
  asort = Sort_Kmers(ablock,&alen,&osort);

  if (VERBOSE)
    printf("\nComparing %s to itself\n",aname);

  if (alen == 0)
    goto zerowork;

  //  Determine equal K-mer links

  { int    i, p;
    uint64 c;

    parmm[0].abeg = 0;
    for (i = 1; i < NTHREADS; i++)
      { p = (int) ((((int64) alen) * i) >> NSHIFT);
        if (p > 0)
          { c = asort[p-1].code;
            while (asort[p].code == c)
              p += 1;
          }
        parmm[i].abeg = parmm[i-1].aend = p;
      }
    parmm[NTHREADS-1].aend = alen;

    for (i = 0; i < NTHREADS; i++)
      { parmm[i].kptr = parmx[i].tptr;
        for (p = 0; p < BPOWR; p++)
          parmm[i].kptr[p] = 0;
      }

    MG_alist = asort;

    for (i = 0; i < NTHREADS; i++)
      pthread_create(threads+i,NULL,count_thread,parmm+i);

    for (i = 0; i < NTHREADS; i++)
      pthread_join(threads[i],NULL);

#ifdef TEST_PAIRS
    printf("\nCROSS SORT %d:\n",alen);
    for (i = 0; i < HOW_MANY && i <= alen; i++)
      { KmerPos *c = asort+i;
        printf(" %5d / %5d / %10lld\n",c->read,c->rpos,c->code);
        fflush(stdout);
      }
#endif
  }

  //  Resort the list on read,rpos

  { int      i;
    KmerPos *rez;

    for (i = 0; i < NTHREADS; i++)
      { parmx[i].beg = parmm[i].abeg;
        parmx[i].end = parmm[i].aend;
      }

    rez = (KmerPos *) lex_sort(pairsort,(Double *) asort,(Double *) osort,parmx);
    if (rez != asort)
      { osort = asort;
        asort = rez;
      }

#ifdef TEST_CSORT
    printf("\nCROSS SORT %d:\n",alen);
    for (i = 0; i < HOW_MANY && i <= alen; i++)
      { KmerPos *c = asort+i;
        printf(" %5d / %5d / %10lld\n",c->read,c->rpos,c->code);
        fflush(stdout);
      }
#endif
  }

  //  Apply the diagonal filter and find local alignments about seed hits

  { int    i, w;
    int64  p;
    int    ar;
    int   *counters;

    MG_alist  = asort;
    MR_ablock = ablock;
    MR_spec   = aspec;
    MR_tspace = Trace_Spacing(aspec);

    asort[alen].read = ablock->nreads;
    parmr[0].beg = 0;
    for (i = 1; i < NTHREADS; i++)
      { p = (int) ((((int64) alen) * i) >> NSHIFT);
        if (p > 0)
          { ar = asort[p-1].read;
            while ((asort[p].read) == ar)
              p += 1;
          }
        parmr[i].beg = parmr[i-1].end = p;
      }
    parmr[NTHREADS-1].end = alen;

    w = ((ablock->maxlen >> Binshift) - ((-ablock->maxlen) >> Binshift)) + 1;
    counters = (int *) Malloc(NTHREADS*3*w*sizeof(int),"Allocating diagonal buckets");
    if (counters == NULL)
      Clean_Exit(1);

    for (i = 0; i < 3*w*NTHREADS; i++)
      counters[i] = 0;
    for (i = 0; i < NTHREADS; i++)
      { if (i == 0)
          parmr[i].score = counters - ((-ablock->maxlen) >> Binshift);
        else
          parmr[i].score = parmr[i-1].lasta + w;
        parmr[i].lastp = parmr[i].score + w;
        parmr[i].lasta = parmr[i].lastp + w;
        parmr[i].work  = New_Work_Data();

        parmr[i].ofile =
             Fopen(Catenate(SORT_PATH,"/",aname,Numbered_Suffix(".T",i+1,".las")),"w");
        if (parmr[i].ofile == NULL)
          Clean_Exit(1);
      }

#ifdef NOTHREAD

    for (i = 0; i < NTHREADS; i++)
      report_thread(parmr+i);

#else

    for (i = 0; i < NTHREADS; i++)
      pthread_create(threads+i,NULL,report_thread,parmr+i);

    for (i = 0; i < NTHREADS; i++)
      pthread_join(threads[i],NULL);

#endif

    if (VERBOSE)
      for (i = 0; i < NTHREADS; i++)
        { nfilt  += parmr[i].nfilt;
          ncheck += parmr[i].ncheck;
        }

    for (i = 0; i < NTHREADS; i++)
      Free_Work_Data(parmr[i].work);
    free(counters);
  }

  //  Finish up

  goto epilogue;

zerowork:
  { FILE *ofile;
    int   i;

    nfilt  = 0;
    for (i = 0; i < NTHREADS; i++)
      { ofile = Fopen(Catenate(SORT_PATH,"/",aname,Numbered_Suffix(".T",i+1,".las")),"w");
        fwrite(&nfilt,sizeof(int64),1,ofile);
        fwrite(&MR_tspace,sizeof(int),1,ofile);
        fclose(ofile);
      }
  }

epilogue:

  free(asort);
  free(osort);

  if (VERBOSE)
    { int width;

      if (nfilt <= 0)
        width = 1;
      else
        width = ((int) log10((double) nfilt)) + 1;
      width += (width-1)/3;

      printf("\n     ");
      Print_Number(nfilt,width,stdout);
      printf(" seed hits (%e of matrix)\n     ",(1.*nfilt/atot)/atot);
      Print_Number(ncheck,width,stdout);
      printf(" confirmed hits (%e of matrix)\n",(1.*ncheck/atot)/atot);
      fflush(stdout);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, -0x48(%rbp)
movq %rdi, -0x40(%rbp)
movl 0x1f1f0(%rip), %ecx     # 0x221f8
movslq %ecx, %rax
movq %rsp, %r15
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
subq %rdx, %r15
movq %r15, %rsp
movq %rsp, %r12
movq %rcx, %rdx
shlq $0x4, %rdx
subq %rdx, %r12
movq %r12, %rsp
movq %rsp, %r13
imulq $0x818, %rcx, %rdx      # imm = 0x818
addq $0xf, %rdx
andq $-0x10, %rdx
subq %rdx, %r13
movq %r13, %rsp
movq %rsp, %r14
imulq $0x48, %rcx, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
subq %rcx, %r14
movq %r14, %rsp
movq %rsi, -0x30(%rbp)
movq 0x28(%rsi), %rbx
andq $0x0, -0x58(%rbp)
movq %rax, %rcx
shlq $0xb, %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
imulq $0x818, %rax, %rax      # imm = 0x818
addq $0xf, %rcx
andq $-0x10, %rcx
cmpq %rdx, %rax
je 0x30a6
movq %rsp, %rsi
subq %rcx, %rsi
movq %rsi, %rsp
movq %rsi, 0x810(%r13,%rdx)
addq $0x818, %rdx            # imm = 0x818
jmp 0x3087
xorl %eax, %eax
cmpq $0x10, %rax
je 0x30bb
andl $0x0, -0xc0(%rbp,%rax,4)
incq %rax
jmp 0x30a8
movq -0x30(%rbp), %rax
movslq 0x20(%rax), %rax
pushq $0x1
popq %rdx
pushq $0x8
popq %rcx
cmpq %rax, %rdx
jge 0x30d6
shlq $0x8, %rdx
incl %ecx
jmp 0x30c9
movl %ecx, %eax
pushq $0x8
popq %rcx
cmpq %rcx, %rax
je 0x30f0
movl $0x1, -0xc0(%rbp,%rcx,4)
incq %rcx
jmp 0x30db
movq -0x30(%rbp), %rax
movslq 0x30(%rax), %rax
pushq $0x1
popq %rdx
pushq $0xc
popq %rcx
cmpq %rax, %rdx
jge 0x310b
shlq $0x8, %rdx
incl %ecx
jmp 0x30fe
movl %ecx, %eax
pushq $0xc
popq %rcx
cmpq %rcx, %rax
je 0x3125
movl $0x1, -0xc0(%rbp,%rcx,4)
incq %rcx
jmp 0x3110
leaq 0x1f0a8(%rip), %rax     # 0x221d4
cmpl $0x0, (%rax)
je 0x3143
leaq 0x184e8(%rip), %rdi     # 0x1b620
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x2150
leaq -0x68(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0x6c(%rbp), %rsi
movq -0x30(%rbp), %rdi
callq 0x3774
movq %rax, -0x38(%rbp)
leaq 0x1f071(%rip), %rax     # 0x221d4
cmpl $0x0, (%rax)
je 0x317a
leaq 0x184bf(%rip), %rdi     # 0x1b62e
movq -0x40(%rbp), %rsi
xorl %eax, %eax
callq 0x2150
movslq -0x6c(%rbp), %r11
testq %r11, %r11
movq %rbx, -0x80(%rbp)
je 0x35f6
andl $0x0, (%r12)
movl 0x1f062(%rip), %eax     # 0x221f8
movl 0x1f060(%rip), %ecx     # 0x221fc
movslq %eax, %rdx
pushq $0x1
popq %rsi
cmpq %rdx, %rsi
jge 0x3201
movq %rsi, %rdi
imulq %r11, %rdi
sarq %cl, %rdi
testl %edi, %edi
jle 0x31ec
leal -0x1(%rdi), %r8d
shlq $0x4, %r8
movq -0x38(%rbp), %r9
movq (%r9,%r8), %r8
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl %edi, %r10d
decl %r10d
shlq $0x4, %rdi
addq %r9, %rdi
movq %rdi, %r9
movl %r10d, %edi
incl %edi
leaq 0x10(%r9), %r10
cmpq %r8, (%r9)
movq %r10, %r9
je 0x31de
movq %rsi, %r8
shlq $0x4, %r8
movl %edi, -0xc(%r12,%r8)
movl %edi, (%r12,%r8)
incq %rsi
jmp 0x31a2
movq %rdx, %rcx
shlq $0x4, %rcx
movl %r11d, -0xc(%r12,%rcx)
xorl %ecx, %ecx
testl %edx, %edx
cmovlel %ecx, %edx
leaq 0x10(%r13), %rsi
cmpq %rdx, %rcx
je 0x3258
imulq $0x818, %rcx, %rdi      # imm = 0x818
addq %r13, %rdi
addq $0x10, %rdi
movq %rcx, %r8
shlq $0x4, %r8
movq %rdi, 0x8(%r12,%r8)
xorl %edi, %edi
cmpq $0x100, %rdi            # imm = 0x100
je 0x324c
andq $0x0, (%rsi,%rdi,8)
incq %rdi
jmp 0x3239
incq %rcx
addq $0x818, %rsi            # imm = 0x818
jmp 0x3218
movq %r11, -0x78(%rbp)
movq %r14, -0x50(%rbp)
movq -0x38(%rbp), %rcx
movq %rcx, 0x1ef9d(%rip)     # 0x22208
xorl %ebx, %ebx
movq %r15, -0x60(%rbp)
movq %r15, %r14
movq %r12, %r15
movslq %eax, %rcx
cmpq %rcx, %rbx
jge 0x32a6
movq %r14, %rdi
xorl %esi, %esi
leaq 0x89c(%rip), %rdx       # 0x3b27
movq %r15, %rcx
callq 0x2280
incq %rbx
movl 0x1ef5c(%rip), %eax     # 0x221f8
addq $0x10, %r15
addq $0x8, %r14
jmp 0x3277
xorl %ebx, %ebx
movq -0x50(%rbp), %r14
movq -0x60(%rbp), %r15
movslq %eax, %rcx
cmpq %rcx, %rbx
jge 0x32ce
movq (%r15,%rbx,8), %rdi
xorl %esi, %esi
callq 0x2300
incq %rbx
movl 0x1ef2c(%rip), %eax     # 0x221f8
jmp 0x32b0
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
imulq $0x818, %rax, %rax      # imm = 0x818
addq $0x4, %r12
cmpq %rcx, %rax
je 0x3305
movslq -0x4(%r12), %rdx
movq %rdx, (%r13,%rcx)
movslq (%r12), %rdx
movq %rdx, 0x8(%r13,%rcx)
addq $0x818, %rcx            # imm = 0x818
addq $0x10, %r12
jmp 0x32e0
movq -0x68(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
movq -0x38(%rbp), %r15
movq %r15, %rsi
movq %r13, %rcx
callq 0x3bb5
cmpq %r15, %rax
je 0x332b
movq %r15, -0x68(%rbp)
movq %rax, %r15
movq %r15, 0x1eed6(%rip)     # 0x22208
movq -0x30(%rbp), %rbx
movq %rbx, 0x1eed3(%rip)     # 0x22210
movq -0x48(%rbp), %rdi
movq %rdi, 0x1eed0(%rip)     # 0x22218
callq 0x52c4
movl %eax, 0x1eead(%rip)     # 0x22200
movl 0x30(%rbx), %eax
movq -0x78(%rbp), %r11
movq %r11, %rcx
shlq $0x4, %rcx
movl %eax, 0xc(%r15,%rcx)
andq $0x0, (%r14)
movslq 0x1ee87(%rip), %rax     # 0x221f8
movl 0x1ee85(%rip), %ecx     # 0x221fc
leaq 0xc(%r15), %rdx
pushq $0x1
popq %rsi
cmpq %rax, %rsi
jge 0x33d0
movq %rsi, %rdi
imulq %r11, %rdi
sarq %cl, %rdi
movslq %edi, %rdi
testq %rdi, %rdi
jle 0x33be
leaq -0x1(%rdi), %r9
shlq $0x4, %rdi
addq %rdx, %rdi
movq %rdi, %r8
movq %r9, %rdi
shlq $0x4, %r9
movl 0xc(%r15,%r9), %r9d
incq %rdi
leaq 0x10(%r8), %r10
cmpl %r9d, (%r8)
movq %r10, %r8
je 0x33af
imulq $0x48, %rsi, %r8
movq %rdi, -0x40(%r14,%r8)
movq %rdi, (%r14,%r8)
incq %rsi
jmp 0x337e
imulq $0x48, %rax, %rcx
movq %r11, -0x40(%r14,%rcx)
movq -0x30(%rbp), %rcx
movl 0x20(%rcx), %edx
movb 0x1edfe(%rip), %cl      # 0x221e4
movl %edx, %ebx
sarl %cl, %ebx
negl %edx
sarl %cl, %edx
subl %edx, %ebx
incl %ebx
imull %ebx, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
shlq $0x2, %rdi
leaq 0x18241(%rip), %rsi     # 0x1b647
callq 0x134dd
movq %rax, %r12
testq %rax, %rax
jne 0x341b
pushq $0x1
popq %rdi
callq 0x242c
movl 0x1edd7(%rip), %eax     # 0x221f8
movl %ebx, %ecx
imull %eax, %ecx
leal (%rcx,%rcx,2), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
cmpq %rdx, %rcx
je 0x343f
andl $0x0, (%r12,%rdx,4)
incq %rdx
jmp 0x3430
movq %r12, -0x48(%rbp)
movq %r15, -0x38(%rbp)
movslq %ebx, %r12
leaq 0x30(%r14), %r15
leaq (,%r12,4), %rbx
xorl %r14d, %r14d
movslq %eax, %rcx
cmpq %rcx, %r14
jge 0x3521
testq %r14, %r14
je 0x3477
movq -0x58(%r15), %rax
addq %rbx, %rax
movq %rax, -0x20(%r15)
jmp 0x349e
xorl %eax, %eax
movq -0x30(%rbp), %rcx
subl 0x20(%rcx), %eax
movb 0x1ed5e(%rip), %cl      # 0x221e4
sarl %cl, %eax
movslq %eax, %rcx
shlq $0x2, %rcx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq -0x50(%rbp), %rcx
movq %rax, 0x10(%rcx)
leaq (%rax,%r12,4), %rax
movq %rax, -0x18(%r15)
leaq (%rax,%r12,4), %rax
movq %rax, -0x10(%r15)
xorl %eax, %eax
callq 0x4fec
movq %rax, -0x8(%r15)
leaq 0x1ed08(%rip), %rax     # 0x221c8
movq (%rax), %r13
leal 0x1(%r14), %esi
leaq 0x18197(%rip), %rdi     # 0x1b665
leaq 0x17feb(%rip), %rdx     # 0x1b4c0
callq 0x1389e
movq %r13, %rdi
leaq 0x1817f(%rip), %rsi     # 0x1b663
movq -0x40(%rbp), %rdx
movq %rax, %rcx
callq 0x13777
movq %rax, %rdi
leaq 0x18b8b(%rip), %rsi     # 0x1c085
callq 0x135fb
movq %rax, (%r15)
testq %rax, %rax
jne 0x350f
pushq $0x1
popq %rdi
callq 0x242c
movl 0x1ece3(%rip), %eax     # 0x221f8
addq $0x48, %r15
incq %r14
jmp 0x3459
xorl %r12d, %r12d
leaq 0x9cb(%rip), %rbx       # 0x3ef6
movq -0x60(%rbp), %r14
movq -0x50(%rbp), %r15
movslq %eax, %rcx
cmpq %rcx, %r12
jge 0x355e
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
incq %r12
movl 0x1eca4(%rip), %eax     # 0x221f8
addq $0x48, %r15
addq $0x8, %r14
jmp 0x3533
xorl %ebx, %ebx
movq -0x50(%rbp), %r14
movq -0x60(%rbp), %r15
movq -0x38(%rbp), %r12
movslq %eax, %rcx
cmpq %rcx, %rbx
jge 0x358a
movq (%r15,%rbx,8), %rdi
xorl %esi, %esi
callq 0x2300
incq %rbx
movl 0x1ec70(%rip), %eax     # 0x221f8
jmp 0x356c
xorl %r15d, %r15d
leaq 0x1ec40(%rip), %rcx     # 0x221d4
cmpl $0x0, (%rcx)
je 0x35c4
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
imulq $0x48, %rcx, %rcx
xorl %esi, %esi
xorl %edx, %edx
cmpq %rsi, %rcx
je 0x35c0
addq 0x38(%r14,%rsi), %rdx
addq 0x40(%r14,%rsi), %r15
addq $0x48, %rsi
jmp 0x35ab
movq %rdx, -0x58(%rbp)
addq $0x28, %r14
xorl %ebx, %ebx
cltq
cmpq %rax, %rbx
jge 0x35e8
movq (%r14), %rdi
callq 0x5037
incq %rbx
movl 0x1ec16(%rip), %eax     # 0x221f8
addq $0x48, %r14
jmp 0x35ca
movq -0x48(%rbp), %rdi
callq 0x2030
jmp 0x369b
leaq -0x58(%rbp), %rax
andq $0x0, (%rax)
pushq $0x1
popq %r14
xorl %r15d, %r15d
pushq $0x1
popq %rbx
pushq $0x4
popq %r13
leal -0x1(%r14), %eax
cmpl 0x1ebe2(%rip), %eax     # 0x221f8
jge 0x3697
leaq 0x1eba9(%rip), %rax     # 0x221c8
movq (%rax), %r12
leaq 0x1803c(%rip), %rdi     # 0x1b665
movl %r14d, %esi
leaq 0x17e8d(%rip), %rdx     # 0x1b4c0
callq 0x1389e
movq %r12, %rdi
leaq 0x18021(%rip), %rsi     # 0x1b663
movq -0x40(%rbp), %rdx
movq %rax, %rcx
callq 0x13777
movq %rax, %rdi
leaq 0x18a2d(%rip), %rsi     # 0x1c085
callq 0x135fb
movq %rax, %r12
leaq -0x58(%rbp), %rdi
pushq $0x8
popq %rsi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x22f0
leaq 0x1eb87(%rip), %rdi     # 0x22200
movq %r13, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x22f0
movq %r12, %rdi
callq 0x2100
incl %r14d
jmp 0x360c
movq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0x2030
movq -0x68(%rbp), %rdi
callq 0x2030
leaq 0x1eb21(%rip), %rax     # 0x221d4
cmpl $0x0, (%rax)
je 0x3765
movq -0x58(%rbp), %r14
testq %r14, %r14
jle 0x36d7
cvtsi2sd %r14, %xmm0
callq 0x21f0
cvttsd2si %xmm0, %ebx
incl %ebx
jmp 0x36da
pushq $0x1
popq %rbx
leal -0x1(%rbx), %eax
pushq $0x3
popq %rcx
cltd
idivl %ecx
addl %eax, %ebx
leaq 0x17f95(%rip), %rdi     # 0x1b681
xorl %eax, %eax
callq 0x2150
movq 0x1e8de(%rip), %r12     # 0x21fd8
movq (%r12), %rdx
movq %r14, %rdi
movl %ebx, %esi
callq 0x1396d
cvtsi2sd %r14, %xmm0
cvtsi2sdq -0x80(%rbp), %xmm1
movsd %xmm1, -0x40(%rbp)
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
leaq 0x17f41(%rip), %rdi     # 0x1b668
movb $0x1, %al
callq 0x2150
movq (%r12), %rdx
movq %r15, %rdi
movl %ebx, %esi
callq 0x1396d
cvtsi2sd %r15, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
leaq 0x17f33(%rip), %rdi     # 0x1b688
movb $0x1, %al
callq 0x2150
movq (%r12), %rdi
callq 0x2250
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq