// File: /workspace/github_c_makefile_star2_O2/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, 0x34(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
pushq $0x10
popq %rsi
movl $0x100, %edi            # imm = 0x100
callq 0x5d68
xorl %r14d, %r14d
movq %rax, 0x20(%rsp)
movq %rax, %rdi
xorl %esi, %esi
callq 0x6000
andl $0x0, 0x4(%rax)
movq 0x8(%r15), %rax
movslq 0x18(%rax), %rdi
pushq $0x4
popq %rsi
callq 0x65c4
movq %rax, %rbp
movq %r15, 0x28(%rsp)
movq (%r15), %rax
movl 0x40(%rax), %edi
incl %edi
pushq $0x2
popq %rsi
callq 0x65c4
movq %rax, 0x58(%rsp)
movq 0x8(%rbx), %r12
movq 0x10(%rbx), %r13
movslq 0x4(%rbx), %rax
leaq 0x4(,%rax,4), %rdi
callq 0x659c
movq %rax, 0x38(%rsp)
andl $0x0, (%rax)
xorl %edx, %edx
movq %rbx, 0x8(%rsp)
movslq 0x4(%rbx), %rax
cmpq %rax, %r14
jge 0x22c1
movl (%r12), %r15d
movzwl (%r13), %eax
movq 0x38(%rsp), %rcx
addl (%rcx,%r14,4), %eax
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl %eax, 0x4(%rcx,%r14,4)
incq %r14
movq 0x28(%rsp), %rax
movq (%rax), %rcx
movq 0x38(%rcx), %rax
movb (%rax,%r15), %al
andb $0x3, %al
cmpb $0x1, %al
jne 0x22b4
movq 0x58(%rsp), %rax
cmpw $0x0, (%rax,%r15,2)
je 0x2232
incl %edx
orb $0x1, 0x18(%rbx)
jmp 0x22b4
movq %r12, 0x18(%rsp)
movl %edx, 0x14(%rsp)
movw %r14w, (%rax,%r15,2)
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%r15,8), %rbx
xorl %r12d, %r12d
movq 0x20(%rsp), %rdi
movq 0x30(%rcx), %rax
movzwl (%rax,%r15,2), %eax
cmpl %eax, %r12d
jae 0x22a6
movl (%rbx,%r12,4), %eax
movslq (%rbp,%rax,4), %rdx
testq %rdx, %rdx
je 0x227f
movslq 0x14(%rdi), %rax
imulq %rdx, %rax
addq 0x8(%rdi), %rax
jmp 0x229e
movl 0x18(%rdi), %esi
movl %esi, (%rbp,%rax,4)
callq 0x6000
movq 0x20(%rsp), %rdi
movl (%rbx,%r12,4), %ecx
movl %ecx, (%rax)
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
incl 0x4(%rax)
incq %r12
jmp 0x2255
movq 0x8(%rsp), %rbx
movl 0x14(%rsp), %edx
movq 0x18(%rsp), %r12
addq $0x4, %r12
addq $0x2, %r13
jmp 0x21d2
movl %edx, 0x14(%rsp)
movq %rbp, %rdi
callq 0x1040
movq 0x8(%rbx), %rax
movq %rax, 0x60(%rsp)
movq 0x20(%rsp), %rbx
movq 0x8(%rbx), %rdi
movslq 0x18(%rbx), %rsi
movslq 0x14(%rbx), %rdx
leaq 0x3de(%rip), %rcx       # 0x26cc
callq 0x1070
movq %rbx, %rdx
movq 0x8(%rbx), %r12
pushq $0x1
popq %rbx
xorl %r11d, %r11d
xorl %r13d, %r13d
movq 0x58(%rsp), %r15
cmpl 0x18(%rdx), %ebx
jge 0x25d9
movl 0x4(%r12), %eax
cmpl $0x3, %eax
jl 0x25d9
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rsi
movl 0x14(%rcx), %edi
imull (%r12), %edi
cmpb $0x0, 0x18(%rsi,%rdi)
jne 0x25ce
movl 0x4(%rsi,%rdi), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
movq %rsi, 0x18(%rsp)
movq %rdi, 0x50(%rsp)
movq 0x8(%rsi,%rdi), %rdx
xorl %esi, %esi
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x60(%rsp), %r9
cmpl %esi, %ecx
je 0x2394
movl (%rdx,%rsi,4), %edi
leal (%rdi,%rdi), %r8d
movzwl (%r15,%r8), %r8d
testw %r8w, %r8w
je 0x238f
decl %r8d
movzwl %r8w, %r8d
xorl (%r9,%r8,4), %edi
js 0x238c
incl %ebp
jmp 0x238f
incl %r14d
incq %rsi
jmp 0x2365
movb $0x0, 0xd(%r12)
movq 0x18(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq 0x10(%rsi,%rdi), %rsi
movzwl (%rsi), %edi
cvtsi2sd %edi, %xmm1
testl %ebp, %ebp
sete %dil
testl %r14d, %r14d
setne %r8b
orb %dil, %r8b
je 0x23fd
testl %r14d, %r14d
sete %dil
testl %ebp, %ebp
setne %r8b
orb %dil, %r8b
je 0x2471
testl %ebp, %ebp
setne %cl
testl %r14d, %r14d
setne %dl
andb %cl, %dl
xorpd %xmm0, %xmm0
cmpb $0x1, %dl
jne 0x23f4
movb $0x1, 0xd(%r12)
xorpd %xmm3, %xmm3
jmp 0x24e7
movb $0x1, 0xc(%r12)
xorl %r14d, %r14d
xorpd %xmm3, %xmm3
xorl %edi, %edi
xorpd %xmm0, %xmm0
xorl %r8d, %r8d
cmpl %edi, %ecx
je 0x24e7
movl (%rdx,%rdi,4), %r9d
addl %r9d, %r9d
movzwl (%r15,%r9), %r9d
testl %r9d, %r9d
je 0x245d
cmpl %r9d, %r8d
jle 0x2439
movb $0x1, 0xd(%r12)
decl %ebp
movl %r9d, %r8d
movq 0x38(%rsp), %r10
movl (%r10,%r8,4), %r8d
cvtsi2sd %r8, %xmm2
subsd %xmm1, %xmm2
addsd %xmm2, %xmm3
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
movl %r9d, %r8d
movzwl 0x2(%rsi,%rdi,2), %r9d
cvtsi2sd %r9d, %xmm2
addsd %xmm2, %xmm1
incq %rdi
jmp 0x2413
movb $0x0, 0xc(%r12)
movq 0x8(%rsp), %rdi
movl 0x4(%rdi), %r8d
xorpd %xmm3, %xmm3
xorl %edi, %edi
xorpd %xmm0, %xmm0
cmpl %edi, %ecx
je 0x24e5
movl (%rdx,%rdi,4), %r9d
addl %r9d, %r9d
movzwl (%r15,%r9), %r9d
testl %r9d, %r9d
je 0x24d1
cmpl %r9d, %r8d
jge 0x24ad
movb $0x1, 0xd(%r12)
decl %r14d
movl %r9d, %r8d
movq 0x38(%rsp), %r10
movl (%r10,%r8,4), %r8d
cvtsi2sd %r8, %xmm2
addsd %xmm1, %xmm2
addsd %xmm2, %xmm3
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
movl %r9d, %r8d
movzwl 0x2(%rsi,%rdi,2), %r9d
cvtsi2sd %r9d, %xmm2
addsd %xmm2, %xmm1
incq %rdi
jmp 0x248a
xorl %ebp, %ebp
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm3
divsd %xmm1, %xmm0
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
ucomisd 0x4b29(%rip), %xmm0     # 0x7030
movsd %xmm3, 0x48(%rsp)
jb 0x2515
sqrtsd %xmm0, %xmm0
jmp 0x2534
movl %ebx, 0x44(%rsp)
movl %r13d, %ebx
movl %r11d, %r13d
callq 0x1240
movsd 0x48(%rsp), %xmm3
movl %r13d, %r11d
movl %ebx, %r13d
movl 0x44(%rsp), %ebx
movb 0xd(%r12), %al
xorl %ecx, %ecx
xorl %edx, %edx
testb %al, %al
setne %cl
sete %dl
addl %edx, %r11d
addl %ecx, %r13d
cvttsd2si %xmm3, %ecx
movl %ecx, 0x8(%r12)
cmpl $0x2, 0x34(%rsp)
jl 0x25c9
testb %al, %al
movq 0x9e0c(%rip), %rdi      # 0xc370
movl (%r12), %edx
movq 0x18(%rsp), %rax
movq 0x50(%rsp), %rcx
movl (%rax,%rcx), %ecx
leaq 0x4df7(%rip), %r8       # 0x7373
leaq 0x4df4(%rip), %rax      # 0x7377
cmoveq %rax, %r8
movl %r14d, (%rsp)
leaq 0x4dbd(%rip), %rsi      # 0x734f
movl %ebp, %r9d
xorl %eax, %eax
movl %r11d, %ebp
movsd %xmm0, 0x18(%rsp)
callq 0x1130
movq 0x9dc4(%rip), %rdi      # 0xc370
leaq 0x4dc9(%rip), %rsi      # 0x737c
movsd 0x48(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movb $0x2, %al
callq 0x1130
movl %ebp, %r11d
movq 0x20(%rsp), %rdx
incl %ebx
addq $0x10, %r12
jmp 0x2308
movl %ebx, 0x18(%rdx)
movl %r11d, %eax
orl %r13d, %eax
jne 0x2613
movq 0x8(%rsp), %rdx
movb 0x18(%rdx), %al
movl %eax, %ecx
orb $0x8, %cl
movb %cl, 0x18(%rdx)
movb $0x18, %cl
cmpl $0xa, 0x4(%rdx)
jl 0x2609
movb $0x28, %cl
movq 0x8(%rsp), %rdx
cmpl $0x9, 0x28(%rdx)
jg 0x2613
orb %cl, %al
movq 0x8(%rsp), %rcx
movb %al, 0x18(%rcx)
cmpl $0x0, 0x34(%rsp)
jle 0x26b0
movl %r11d, %ebp
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x8(%rsp), %rax
subq 0x8(%rcx), %rax
js 0x2649
pushq $0x48
popq %rsi
xorl %edx, %edx
divq %rsi
movq %rax, %rdx
movslq 0x18(%rcx), %rax
cmpq %rax, %rdx
jle 0x264b
xorl %edx, %edx
movq 0x9d1e(%rip), %rdi      # 0xc370
movq 0x8(%rsp), %rbx
movl (%rbx), %ecx
movl 0x4(%rbx), %r8d
movl 0x1c(%rbx), %r9d
leaq 0x4d29(%rip), %rsi      # 0x7391
xorl %eax, %eax
callq 0x1130
movq 0x9cfa(%rip), %rdi      # 0xc370
movl 0x24(%rbx), %edx
movl 0x28(%rbx), %ecx
movl 0x2c(%rbx), %r8d
movl 0x30(%rbx), %r9d
leaq 0x4d28(%rip), %rsi      # 0x73b3
xorl %eax, %eax
callq 0x1130
movq 0x9cd7(%rip), %rdi      # 0xc370
leaq 0x4d25(%rip), %rsi      # 0x73c5
movl 0x14(%rsp), %edx
movl %ebp, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x1130
movq %r15, %rdi
callq 0x1040
movq 0x20(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq