// File: /workspace/github_c_makefile_star2_O3/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, 0x44(%rsp)
movq %rsi, %r15
movq %rdi, %r14
movl $0x100, %edi            # imm = 0x100
movl $0x10, %esi
callq 0x6714
movq %rax, %rbx
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
callq 0x69bd
movl %ebp, 0x4(%rax)
movq 0x8(%r14), %rax
movslq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x6f74
movq %rax, %rbp
movq %r14, 0x38(%rsp)
movq (%r14), %rax
movl 0x40(%rax), %edi
incl %edi
movl $0x2, %esi
callq 0x6f74
movq %rax, %r12
movq 0x8(%r15), %r14
movq 0x10(%r15), %r13
movslq 0x4(%r15), %rax
leaq 0x4(,%rax,4), %rdi
callq 0x6f4c
movq %rbx, %rdi
movq %r15, %rbx
movq %rax, 0x48(%rsp)
xorl %ecx, %ecx
movl %ecx, (%rax)
movl $0x0, %edx
cmpl %ecx, 0x4(%r15)
movq %rdi, 0x18(%rsp)
movq %r12, 0x30(%rsp)
jle 0x2ffa
movq %r12, %rsi
xorl %r9d, %r9d
xorl %edx, %edx
movq %rbx, 0x58(%rsp)
movl (%r14), %r15d
movzwl (%r13), %eax
movq 0x48(%rsp), %rcx
addl (%rcx,%r9,4), %eax
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl %eax, 0x4(%rcx,%r9,4)
incq %r9
movq 0x38(%rsp), %rax
movq (%rax), %rcx
movq 0x38(%rcx), %rax
movb (%rax,%r15), %al
andb $0x3, %al
cmpb $0x1, %al
jne 0x2fe5
cmpw $0x0, (%rsi,%r15,2)
je 0x2f50
incl %edx
orb $0x1, 0x18(%rbx)
jmp 0x2fe5
movl %edx, 0x14(%rsp)
leal (%r15,%r15), %eax
movw %r9w, (%rsi,%rax)
movq 0x30(%rcx), %rdx
cmpw $0x0, (%rdx,%rax)
je 0x2fe1
movq %r9, 0x28(%rsp)
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%r15,8), %rbx
xorl %r12d, %r12d
movl (%rbx,%r12,4), %eax
movslq (%rbp,%rax,4), %rdx
testq %rdx, %rdx
je 0x2f99
movslq 0x14(%rdi), %rax
imulq %rdx, %rax
addq 0x8(%rdi), %rax
jmp 0x2fbd
movl 0x18(%rdi), %esi
movl %esi, (%rbp,%rax,4)
callq 0x69bd
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %rdi
movl (%rbx,%r12,4), %ecx
movl %ecx, (%rax)
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
incl 0x4(%rax)
movq 0x30(%rcx), %rax
movzwl (%rax,%r15,2), %eax
incq %r12
cmpl %eax, %r12d
jb 0x2f7d
movq 0x58(%rsp), %rbx
movl 0x14(%rsp), %edx
movq 0x28(%rsp), %r9
jmp 0x2fe5
movl 0x14(%rsp), %edx
addq $0x4, %r14
addq $0x2, %r13
movslq 0x4(%rbx), %rax
cmpq %rax, %r9
jl 0x2f03
movl %edx, 0x14(%rsp)
movq %rbp, %rdi
callq 0x1040
movq 0x8(%rbx), %rax
movq %rax, 0x60(%rsp)
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x18(%rsp), %rax
movslq 0x18(%rax), %rsi
movq 0x18(%rsp), %rax
movslq 0x14(%rax), %rdx
leaq 0x404(%rip), %rcx       # 0x3435
callq 0x1070
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rdx
movl $0x1, %r14d
cmpl $0x2, 0x18(%rdx)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
jl 0x3348
movq 0x8(%rdx), %r13
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movl 0x4(%r13), %eax
cmpl $0x3, %eax
jl 0x3348
movq 0x38(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rsi
movl 0x14(%rcx), %r8d
imull (%r13), %r8d
cmpb $0x0, 0x18(%rsi,%r8)
jne 0x3337
movq 0x8(%rsi,%r8), %rcx
movq %rsi, 0x28(%rsp)
movq %r8, %r15
movl 0x4(%rsi,%r8), %edx
testl %edx, %edx
movq 0x60(%rsp), %r10
jle 0x30f5
xorl %esi, %esi
xorl %ebp, %ebp
xorl %r12d, %r12d
movl (%rcx,%rsi,4), %r9d
leal (%r9,%r9), %r8d
movzwl (%rdi,%r8), %r8d
testw %r8w, %r8w
je 0x30ec
decl %r8d
movzwl %r8w, %r8d
xorl (%r10,%r8,4), %r9d
js 0x30ea
incl %r12d
jmp 0x30ec
incl %ebp
incq %rsi
cmpl %esi, %edx
jne 0x30c5
jmp 0x30fa
xorl %r12d, %r12d
xorl %ebp, %ebp
movb $0x0, 0xd(%r13)
movq 0x28(%rsp), %rsi
movq 0x10(%rsi,%r15), %rsi
movzwl (%rsi), %r8d
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
testl %r12d, %r12d
sete %r9b
testl %ebp, %ebp
setne %r8b
orb %r9b, %r8b
je 0x3161
testl %ebp, %ebp
sete %r9b
testl %r12d, %r12d
setne %r8b
orb %r9b, %r8b
je 0x31e2
testl %r12d, %r12d
setne %cl
testl %ebp, %ebp
setne %dl
andb %cl, %dl
xorpd %xmm0, %xmm0
cmpb $0x1, %dl
jne 0x3263
movb $0x1, 0xd(%r13)
jmp 0x3263
movb $0x1, 0xc(%r13)
testl %edx, %edx
jle 0x325d
xorpd %xmm0, %xmm0
xorl %r10d, %r10d
xorl %r8d, %r8d
xorpd %xmm3, %xmm3
movl (%rcx,%r10,4), %r9d
addl %r9d, %r9d
movzwl (%rdi,%r9), %r9d
testl %r9d, %r9d
je 0x31c1
cmpl %r9d, %r8d
jle 0x319a
movb $0x1, 0xd(%r13)
decl %r12d
movl %r9d, %r8d
movq 0x48(%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,%r10,2), %r9d
xorps %xmm2, %xmm2
cvtsi2sd %r9d, %xmm2
addsd %xmm2, %xmm1
incq %r10
cmpl %r10d, %edx
jne 0x317c
xorl %ebp, %ebp
jmp 0x3267
xorl %r12d, %r12d
movb $0x0, 0xc(%r13)
testl %edx, %edx
jle 0x325f
movl 0x4(%rbx), %r8d
xorpd %xmm0, %xmm0
xorl %r10d, %r10d
xorpd %xmm3, %xmm3
movl (%rcx,%r10,4), %r9d
addl %r9d, %r9d
movzwl (%rdi,%r9), %r9d
testl %r9d, %r9d
je 0x3241
cmpl %r9d, %r8d
jge 0x321a
movb $0x1, 0xd(%r13)
decl %ebp
movl %r9d, %r8d
movq 0x48(%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,%r10,2), %r9d
xorps %xmm2, %xmm2
cvtsi2sd %r9d, %xmm2
addsd %xmm2, %xmm1
incq %r10
cmpl %r10d, %edx
jne 0x31fd
jmp 0x3267
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm3
divsd %xmm1, %xmm0
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
ucomisd 0x4da6(%rip), %xmm0     # 0x8030
movsd %xmm3, 0x50(%rsp)
jb 0x3298
sqrtsd %xmm0, %xmm0
jmp 0x32a3
callq 0x1240
movsd 0x50(%rsp), %xmm3
movb 0xd(%r13), %al
xorl %ecx, %ecx
xorl %edx, %edx
testb %al, %al
setne %cl
sete %dl
addl %edx, 0x20(%rsp)
addl %ecx, 0x24(%rsp)
cvttsd2si %xmm3, %ecx
movl %ecx, 0x8(%r13)
cmpl $0x2, 0x44(%rsp)
jl 0x332d
testb %al, %al
movq 0xa09d(%rip), %rdi      # 0xd370
movl (%r13), %edx
movq 0x28(%rsp), %rax
movl (%rax,%r15), %ecx
leaq 0x508c(%rip), %r8       # 0x8373
leaq 0x5089(%rip), %rax      # 0x8377
cmoveq %rax, %r8
movl %ebp, (%rsp)
leaq 0x5053(%rip), %rsi      # 0x834f
movl %r12d, %r9d
xorl %eax, %eax
movsd %xmm0, 0x28(%rsp)
callq 0x1130
movq 0xa05d(%rip), %rdi      # 0xd370
leaq 0x5062(%rip), %rsi      # 0x837c
movsd 0x50(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movb $0x2, %al
callq 0x1130
movq 0x18(%rsp), %rdx
movq 0x30(%rsp), %rdi
incl %r14d
addq $0x10, %r13
cmpl 0x18(%rdx), %r14d
jl 0x3074
movl %r14d, 0x18(%rdx)
movl 0x20(%rsp), %r14d
movl %r14d, %eax
movl 0x24(%rsp), %ebp
orl %ebp, %eax
jne 0x337c
movb 0x18(%rbx), %al
movl %eax, %ecx
orb $0x8, %cl
movb %cl, 0x18(%rbx)
movb $0x18, %cl
cmpl $0xa, 0x4(%rbx)
jl 0x3377
movb $0x28, %cl
cmpl $0x9, 0x28(%rbx)
jg 0x337c
orb %cl, %al
movb %al, 0x18(%rbx)
cmpl $0x0, 0x44(%rsp)
jle 0x341c
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq %rbx, %rcx
subq 0x8(%rax), %rcx
js 0x33b4
shrq $0x3, %rcx
movabsq $-0x71c71c71c71c71c7, %rdx # imm = 0x8E38E38E38E38E39
imulq %rcx, %rdx
movslq 0x18(%rax), %rax
cmpq %rax, %rdx
jle 0x33b6
xorl %edx, %edx
movq 0x9fb3(%rip), %rdi      # 0xd370
movl (%rbx), %ecx
movl 0x4(%rbx), %r8d
movl 0x1c(%rbx), %r9d
leaq 0x4fc3(%rip), %rsi      # 0x8391
xorl %eax, %eax
callq 0x1130
movq 0x9f94(%rip), %rdi      # 0xd370
movl 0x24(%rbx), %edx
movl 0x28(%rbx), %ecx
movl 0x2c(%rbx), %r8d
movl 0x30(%rbx), %r9d
leaq 0x4fc2(%rip), %rsi      # 0x83b3
xorl %eax, %eax
callq 0x1130
movq 0x9f71(%rip), %rdi      # 0xd370
leaq 0x4fbf(%rip), %rsi      # 0x83c5
movl 0x14(%rsp), %edx
movl %r14d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x1130
movq 0x30(%rsp), %rdi
callq 0x1040
movq 0x18(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq