// File: /workspace/github_c_makefile_star2_O1/richarddurbin[P]hash10x/moshasm.c
// Function: findOverlaps
Array findOverlaps (Readset *rs, Read *x, int reportLevel) /* returns array of Overlap */
/* reportLevel 0 for none, 1 for per read (RR lines), 2 for per overlap (RR and RH lines) */
{
  int nRepeat = 0 ; /* number of repeat moshes */
  Array olap = arrayCreate (256, Overlap) ;
  arrayp(olap,0,Overlap)->nHit = 0 ; /* burn the 0 position */
  int *omap = new0 (arrayMax(rs->reads), int) ; /* map from read2 id to index in olap */
  U16 *hmap = new0 (rs->ms->max+1, U16) ; /* map from hashes to index in read1 hits */
  int j, k ;	/* j is index in list of read hits, k is index over reads containing hxx */
  Overlap *o ;
  U32 *hx = x->hit ; U16 *dx = x->dx ;
  U32 *xPos = new(x->nHit+1,U32) ; xPos[0] = 0 ; /* convert dx into absolute position NB +1 */
  for (j = 0 ; j < x->nHit ; ++j, ++hx, ++dx)
    { U32 hxx = *hx & TOPMASK ;
      xPos[j+1] = xPos[j] + *dx ; /* j+1 not j because hmap[] below is j+1 */
      if (msIsCopy1 (rs->ms, hxx))
	{ if (hmap[hxx]) { ++nRepeat ; x->badRepeat = 1 ; continue ; }
	  hmap[hxx] = j+1 ; /* note the +1 : needed to distinguish from missing */
	  U32 *r2 = rs->inv[hxx] ;
	  for (k = 0 ; k < rs->ms->depth[hxx] ; ++k, ++r2)
	    { if (!omap[*r2])
		{ o = arrayp(olap, omap[*r2] = arrayMax(olap), Overlap) ;
		  o->iy = *r2 ;
		}
	      else o = arrp(olap, omap[*r2], Overlap) ;
	      ++o->nHit ;
	    }
	}
    }
  free (omap) ;

  /* now consider overlap for each read with at least 3 hits */
  int nGood = 0, nBad = 0 ;
  hx = x->hit ;
  arraySort (olap, compareOverlap) ;
  o = arrp(olap, 0, Overlap) ;
  for (k = 1 ; k < arrayMax(olap) ; ++k, ++o) /* NB drop out of loop before reach end */
    { if (o->nHit < 3) break ;
      Read *y = arrp(rs->reads,o->iy,Read) ;
      if (y->bad) continue ;
      int nPlus = 0, nMinus = 0 ;
      double d = 0.0, d2 = 0.0 ;
      U16 ihx ;
      U32 *hy = y->hit ;
      for (j = 0 ; j < y->nHit ; ++j, ++hy) /* run through once to find direction */
	if ((ihx = hmap[*hy & TOPMASK]))
	  { if ((*hy & TOPBIT) == (hx[--ihx] & TOPBIT)) ++nPlus ; else ++nMinus ; }
      o->isBad = FALSE ;
      hy = y->hit ; /* reset hy for loop to come */
      U16 *dy = y->dx ; double yPos = *dy ;
      if (nPlus && !nMinus)
	{ o->isPlus = TRUE ;
	  int last = 0 ;
	  for (j = 0 ; j < y->nHit ; ++j, ++hy, yPos += *++dy)
	    if ((ihx = hmap[*hy & TOPMASK]))
	      { if (ihx < last) { o->isBad = TRUE ; --nPlus ; } last = ihx ;
		double z = xPos[ihx] - yPos ; d += z ; d2 += z*z ;
	      }
	}
      else if (nMinus && !nPlus)
	{ o->isPlus = FALSE ;
	  int last = x->nHit ;
	  for (j = 0 ; j < y->nHit ; ++j, ++hy, yPos += *++dy)
	    if ((ihx = hmap[*hy & TOPMASK]))
	      { if (ihx > last) { o->isBad = TRUE ; --nMinus ; } last = ihx ;
		double z = xPos[ihx] + yPos ; d += z ; d2 += z*z ;
	      }
	}
      if (nPlus && nMinus) o->isBad = TRUE ;
      d /= o->nHit ; d2 = sqrt (d2 / o->nHit - d*d) ;
      o->offset = d ;
      if (o->isBad) ++nBad ; else ++nGood ;
      
      if (reportLevel > 1)
	{ fprintf (outFile, "RH\t%u\tlen %d\t%s\tnPlus %d\tnMinus %d\t",
		   o->iy, y->len, o->isBad ? "BAD" : "GOOD", nPlus, nMinus) ;
	  fprintf (outFile, "offset %.1f\tsd %.1f\n", d, d2) ;
	}
    }
  arrayMax(olap) = k ;
  
  if (!nGood && !nBad)
    { x->badNoMatch = 1 ;
      if (x->nHit < 10) x->badLowHit = 1 ;
      else if (x->nCopy[1] < 10) x->badLowCopy1 = 1 ;
    }

  if (reportLevel > 0)
    { I64 iix = x - arrp(rs->reads,0,Read) ;
      if (iix < 0 || iix > arrayMax(rs->reads)) iix = 0 ; /* read not in readset */
      U32 ix = iix ;
      fprintf (outFile, "RR %6u\tlen %d\tnHit %3d\tnMiss %3d\t", ix, x->len, x->nHit, x->nMiss) ;
      fprintf (outFile, "nCpy %d %d %d %d\t", x->nCopy[0], x->nCopy[1], x->nCopy[2], x->nCopy[3]);
      fprintf (outFile, "nRepeatMosh %d\tnGood %4d\tnBad %4d\n", nRepeat, nGood, nBad) ;
    }

  free (hmap) ;
  return olap ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, 0x28(%rsp)
movq %rsi, %r15
movq %rdi, %r13
movl $0x100, %edi            # imm = 0x100
movl $0x10, %esi
callq 0x6434
movq %rax, %rbx
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
callq 0x66ef
movl %ebp, 0x4(%rax)
movq 0x8(%r13), %rax
movslq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x6cbc
movq %rax, %r12
movq (%r13), %rax
movl 0x40(%rax), %edi
incl %edi
movl $0x2, %esi
callq 0x6cbc
movq %rax, 0x30(%rsp)
movq 0x8(%r15), %r14
movq 0x10(%r15), %rbp
movslq 0x4(%r15), %rax
leaq 0x4(,%rax,4), %rdi
callq 0x6c94
movq %r12, %r8
movq %rbx, %rdi
movq %r15, %rbx
movq %rax, 0x40(%rsp)
xorl %ecx, %ecx
movl %ecx, (%rax)
movl $0x0, 0x1c(%rsp)
cmpl %ecx, 0x4(%r15)
movq %rdi, 0x10(%rsp)
movq %r13, 0x38(%rsp)
jle 0x2e62
xorl %esi, %esi
movl $0x0, 0x1c(%rsp)
movq %rbx, 0x58(%rsp)
movl (%r14), %r15d
movzwl (%rbp), %eax
movq 0x40(%rsp), %rcx
addl (%rcx,%rsi,4), %eax
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl %eax, 0x4(%rcx,%rsi,4)
incq %rsi
movq (%r13), %rax
movq 0x38(%rax), %rax
movb (%rax,%r15), %al
andb $0x3, %al
cmpb $0x1, %al
jne 0x2e4d
movq 0x30(%rsp), %rcx
cmpw $0x0, (%rcx,%r15,2)
je 0x2dc7
incl 0x1c(%rsp)
orb $0x1, 0x18(%rbx)
jmp 0x2e4d
leal (%r15,%r15), %eax
movw %si, (%rcx,%rax)
movq (%r13), %rcx
movq 0x30(%rcx), %rcx
cmpw $0x0, (%rcx,%rax)
je 0x2e4d
movq %rsi, 0x20(%rsp)
movq 0x18(%r13), %rax
movq (%rax,%r15,8), %rbx
xorl %r12d, %r12d
movl (%rbx,%r12,4), %eax
movslq (%r8,%rax,4), %rcx
testq %rcx, %rcx
je 0x2e09
movslq 0x14(%rdi), %rax
imulq %rcx, %rax
addq 0x8(%rdi), %rax
jmp 0x2e2b
movl 0x18(%rdi), %esi
movl %esi, (%r8,%rax,4)
movq %r8, %r13
callq 0x66ef
movq %r13, %r8
movq 0x38(%rsp), %r13
movq 0x10(%rsp), %rdi
movl (%rbx,%r12,4), %ecx
movl %ecx, (%rax)
incl 0x4(%rax)
movq (%r13), %rax
movq 0x30(%rax), %rax
movzwl (%rax,%r15,2), %eax
incq %r12
cmpl %eax, %r12d
jb 0x2dee
movq 0x58(%rsp), %rbx
movq 0x20(%rsp), %rsi
addq $0x4, %r14
addq $0x2, %rbp
movslq 0x4(%rbx), %rax
cmpq %rax, %rsi
jl 0x2d7a
movq %r8, %rdi
callq 0x1040
movq 0x8(%rbx), %rax
movq %rax, 0x60(%rsp)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x10(%rsp), %rax
movslq 0x18(%rax), %rsi
movq 0x10(%rsp), %rax
movslq 0x14(%rax), %rdx
leaq 0x3e3(%rip), %rcx       # 0x3278
callq 0x1070
movq 0x38(%rsp), %rsi
movq 0x10(%rsp), %rdx
movl $0x1, %edi
cmpl $0x2, 0x18(%rdx)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
jl 0x3191
movq 0x8(%rdx), %rbp
movl $0x0, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
movl 0x4(%rbp), %eax
cmpl $0x3, %eax
jl 0x3191
movq 0x8(%rsi), %rcx
movq 0x8(%rcx), %r8
movl 0x14(%rcx), %r9d
imull (%rbp), %r9d
cmpb $0x0, 0x18(%r8,%r9)
jne 0x3182
movq 0x8(%r8,%r9), %rcx
movq %r8, %r15
movq %r9, %r13
movl 0x4(%r8,%r9), %edx
testl %edx, %edx
movq 0x60(%rsp), %r9
movl %edi, 0x2c(%rsp)
jle 0x2f5a
xorl %esi, %esi
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x30(%rsp), %r10
movl (%rcx,%rsi,4), %edi
leal (%rdi,%rdi), %r8d
movzwl (%r10,%r8), %r8d
testw %r8w, %r8w
je 0x2f51
decl %r8d
movzwl %r8w, %r8d
xorl (%r9,%r8,4), %edi
js 0x2f4e
incl %r12d
jmp 0x2f51
incl %r14d
incq %rsi
cmpl %esi, %edx
jne 0x2f2a
jmp 0x2f65
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x30(%rsp), %r10
movb $0x0, 0xd(%rbp)
movq 0x10(%r15,%r13), %rsi
movzwl (%rsi), %edi
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
testl %r12d, %r12d
sete %dil
testl %r14d, %r14d
setne %r8b
orb %dil, %r8b
je 0x3025
testl %r14d, %r14d
sete %dil
testl %r12d, %r12d
setne %r8b
orb %dil, %r8b
xorpd %xmm3, %xmm3
jne 0x30a3
movb $0x0, 0xc(%rbp)
xorl %r12d, %r12d
testl %edx, %edx
jle 0x30a3
movl 0x4(%rbx), %r8d
xorpd %xmm0, %xmm0
xorl %edi, %edi
movl (%rcx,%rdi,4), %r9d
addl %r9d, %r9d
movzwl (%r10,%r9), %r9d
testl %r9d, %r9d
je 0x3007
cmpl %r9d, %r8d
jge 0x2fe0
movb $0x1, 0xd(%rbp)
decl %r14d
movl %r9d, %r8d
movq 0x40(%rsp), %r11
movl (%r11,%r8,4), %r8d
xorps %xmm2, %xmm2
cvtsi2sd %r8, %xmm2
addsd %xmm1, %xmm2
addsd %xmm2, %xmm3
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
movl %r9d, %r8d
movzwl 0x2(%rsi,%rdi,2), %r9d
xorps %xmm2, %xmm2
cvtsi2sd %r9d, %xmm2
addsd %xmm2, %xmm1
incq %rdi
cmpl %edi, %edx
jne 0x2fc3
jmp 0x30a7
movb $0x1, 0xc(%rbp)
testl %edx, %edx
jle 0x309c
xorpd %xmm0, %xmm0
xorl %edi, %edi
xorl %r8d, %r8d
xorpd %xmm3, %xmm3
movl (%rcx,%rdi,4), %r9d
addl %r9d, %r9d
movzwl (%r10,%r9), %r9d
testl %r9d, %r9d
je 0x307e
cmpl %r9d, %r8d
jle 0x3057
movb $0x1, 0xd(%rbp)
decl %r12d
movl %r9d, %r8d
movq 0x40(%rsp), %r11
movl (%r11,%r8,4), %r8d
xorps %xmm2, %xmm2
cvtsi2sd %r8, %xmm2
subsd %xmm1, %xmm2
addsd %xmm2, %xmm3
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
movl %r9d, %r8d
movzwl 0x2(%rsi,%rdi,2), %r9d
xorps %xmm2, %xmm2
cvtsi2sd %r9d, %xmm2
addsd %xmm2, %xmm1
incq %rdi
cmpl %edi, %edx
jne 0x303a
xorl %r14d, %r14d
jmp 0x30a7
xorpd %xmm3, %xmm3
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
testl %r12d, %r12d
je 0x30b5
testl %r14d, %r14d
je 0x30b5
movb $0x1, 0xd(%rbp)
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm3
divsd %xmm1, %xmm0
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
ucomisd 0x4f58(%rip), %xmm0     # 0x8030
movsd %xmm3, 0x48(%rsp)
jb 0x30e6
sqrtsd %xmm0, %xmm0
jmp 0x30f1
callq 0x1240
movsd 0x48(%rsp), %xmm3
movb 0xd(%rbp), %al
xorl %ecx, %ecx
xorl %edx, %edx
testb %al, %al
setne %cl
sete %dl
addl %edx, 0x20(%rsp)
addl %ecx, 0x18(%rsp)
cvttsd2si %xmm3, %ecx
movl %ecx, 0x8(%rbp)
cmpl $0x2, 0x28(%rsp)
jl 0x3174
testb %al, %al
movq 0xa251(%rip), %rdi      # 0xd370
movl (%rbp), %edx
movl (%r15,%r13), %ecx
leaq 0x5244(%rip), %r8       # 0x8371
leaq 0x5241(%rip), %rax      # 0x8375
cmoveq %rax, %r8
movl %r14d, (%rsp)
leaq 0x520a(%rip), %rsi      # 0x834d
movl %r12d, %r9d
xorl %eax, %eax
movsd %xmm0, 0x50(%rsp)
callq 0x1130
movq 0xa216(%rip), %rdi      # 0xd370
leaq 0x5219(%rip), %rsi      # 0x837a
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movb $0x2, %al
callq 0x1130
movq 0x10(%rsp), %rdx
movq 0x38(%rsp), %rsi
movl 0x2c(%rsp), %edi
incl %edi
addq $0x10, %rbp
cmpl 0x18(%rdx), %edi
jl 0x2ed7
movl %edi, 0x18(%rdx)
movl 0x20(%rsp), %eax
movl 0x18(%rsp), %ebp
orl %ebp, %eax
jne 0x31c0
movb 0x18(%rbx), %al
movl %eax, %ecx
orb $0x8, %cl
movb %cl, 0x18(%rbx)
movb $0x18, %cl
cmpl $0xa, 0x4(%rbx)
jl 0x31bb
movb $0x28, %cl
cmpl $0x9, 0x28(%rbx)
jg 0x31c0
orb %cl, %al
movb %al, 0x18(%rbx)
cmpl $0x0, 0x28(%rsp)
movq 0x30(%rsp), %r14
jle 0x325c
movq 0x8(%rsi), %rax
movq %rbx, %rcx
subq 0x8(%rax), %rcx
js 0x31f8
shrq $0x3, %rcx
movabsq $-0x71c71c71c71c71c7, %rdx # imm = 0x8E38E38E38E38E39
imulq %rcx, %rdx
movslq 0x18(%rax), %rax
cmpq %rax, %rdx
jle 0x31fa
xorl %edx, %edx
movq 0xa16f(%rip), %rdi      # 0xd370
movl (%rbx), %ecx
movl 0x4(%rbx), %r8d
movl 0x1c(%rbx), %r9d
leaq 0x517d(%rip), %rsi      # 0x838f
xorl %eax, %eax
callq 0x1130
movq 0xa150(%rip), %rdi      # 0xd370
movl 0x24(%rbx), %edx
movl 0x28(%rbx), %ecx
movl 0x2c(%rbx), %r8d
movl 0x30(%rbx), %r9d
leaq 0x517c(%rip), %rsi      # 0x83b1
xorl %eax, %eax
callq 0x1130
movq 0xa12d(%rip), %rdi      # 0xd370
leaq 0x5179(%rip), %rsi      # 0x83c3
movl 0x1c(%rsp), %edx
movl 0x20(%rsp), %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x1130
movq %r14, %rdi
callq 0x1040
movq 0x10(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq