// File: /workspace/github_c_makefile_star2_O3/richarddurbin[P]hash10x/moshasm.c
// Function: assembleFromRead
void assembleFromRead (Readset *rs, U32 ix)
{
  Read *x = arrp(rs->reads,ix,Read) ;
  Read *z = new0 (1,Read) ;	/* build the assembly here */
  int io, iy, ih ;

  /* first collect all the hits in all the overlapping reads */
  Array aHits = arrayCreate (1024, AssemblyHit) ;
  AssemblyHit *ah ;
  HASH  hitHash = hashCreate (1024) ;
  Array overlaps = findOverlaps (rs, x, 1) ;
  for (io = 0 ; io < arrayMax (overlaps) ; ++io)
    { Overlap *o = arrp(overlaps,io,Overlap) ;
      Read *y = arrp(rs->reads,o->iy,Read) ;
      int yPos = 0 ;
      Array lastDown = 0 ;
      for (iy = 0 ; iy < y->nHit ; ++iy)
	{ U32 hit = y->hit[iy] & TOPMASK ; yPos += y->dx[iy] ;
	  ih = hashAdd (hitHash, HASH_INT(hit)) ;
	  ah = arrayp(aHits,ih,AssemblyHit) ;
	  if (!ah->count)
	    { ah->hit = hit ;
	      ah->downHits = arrayCreate (8, int) ;
	    }
	  ++ah->count ;
	  if (iy) ++ah->upCount ;
	  if (lastDown) array(lastDown, arrayMax(lastDown), int) = ih ;
	  lastDown = ah->downHits ;
	  if (o->isPlus) ah->pos += o->offset + yPos ;
	  else ah->pos += o->offset - yPos ;
	}
    }

  /* now want to select the longest clearly supported chain */
  /* need a full chain because we want to rely on the order for future matches */

  

  /* reporting */
  
  double totCount = 0. ;
  int i, j, countA[20][20], countB[20][20] ;
  for (i = 20 ; i-- ;) for (j = 20 ; j-- ;) { countA[i][j] = 0 ; countB[i][j] = 0 ; }
  for (ih = 1 ; ih < hashCount(hitHash) ; ++ih)
    { ah = arrp(aHits,ih,AssemblyHit) ;
      ah->pos /= ah->count ;	/* just an estimate for now */
      totCount += ah->count ;
      if (!msIsCopy1 (rs->ms, ah->hit)) continue ;
      i = ah->count ; if (i > 19) i = 19 ;
      j = rs->ms->depth[ah->hit] ; if (j > 19) j = 19 ; ++countA[i][j] ;
      j = (10*ah->count - 1) / rs->ms->depth[ah->hit] ; ++countB[i][j] ;
    }
  totCount /= hashCount(hitHash) ;
  printf ("AR  %d total hits - mean count %.1f\n", hashCount(hitHash), totCount) ;
  for (i = 0 ; i < 20 ; ++i)
    { printf ("AH  %2d\t", i) ;
      for (j = 0 ; j < 20 ; ++j) if (j < i) printf ("    ") ; else printf ("%4d", countA[i][j]) ;
      printf ("    ") ;
      for (j = 0 ; j < 10 ; ++j) printf ("%4d", countB[i][j]) ;
      printf ("\n") ;
    }

  hashDestroy (hitHash) ;
  arrayDestroy (aHits) ;
  arrayDestroy (overlaps) ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xcd8, %rsp            # imm = 0xCD8
movl %esi, %r14d
movq %rdi, %r15
movq 0x8(%rdi), %rax
imull 0x14(%rax), %r14d
addq 0x8(%rax), %r14
movl $0x1, %edi
movl $0x48, %esi
callq 0x6f74
movl $0x400, %edi            # imm = 0x400
movl $0x18, %esi
callq 0x6714
movq %rax, 0x10(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x5b6c
movq %rax, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x2e4d
movl 0x18(%rax), %edx
testl %edx, %edx
movq %rax, 0x20(%rsp)
jle 0x3b07
xorl %esi, %esi
movq %r15, 0x28(%rsp)
movq 0x8(%rax), %rdi
movslq 0x14(%rax), %rcx
movslq %esi, %r8
imulq %rcx, %r8
movq 0x8(%r15), %rcx
movq 0x8(%rcx), %r9
movl 0x14(%rcx), %ecx
movq %rdi, 0x48(%rsp)
movq %r8, 0x40(%rsp)
imull (%rdi,%r8), %ecx
cmpl $0x0, 0x4(%r9,%rcx)
jle 0x3afd
movl %esi, 0x1c(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r9, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq 0x8(%r9,%rcx), %rax
movq 0x10(%r9,%rcx), %rcx
movl (%rax,%rbx,4), %r13d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r13
movzwl (%rcx,%rbx,2), %ecx
movl %ecx, (%rsp)
movq %r13, %rsi
xorq %rax, %rsi
movq 0x8(%rsp), %rdi
callq 0x5d30
movl %eax, %ebp
movq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x69bd
movq %rax, %r15
movl 0x4(%rax), %eax
testl %eax, %eax
jne 0x3a87
movl %r13d, (%r15)
movl $0x8, %edi
movl $0x4, %esi
callq 0x6714
movq %rax, 0x10(%r15)
movl 0x4(%r15), %eax
incl %eax
movl %eax, 0x4(%r15)
testq %rbx, %rbx
je 0x3a96
incl 0xc(%r15)
testq %r14, %r14
je 0x3aa9
movl 0x18(%r14), %esi
movq %r14, %rdi
callq 0x69bd
movl %ebp, (%rax)
addl (%rsp), %r12d
movl %r12d, %eax
negl %eax
movq 0x48(%rsp), %rcx
movq 0x40(%rsp), %rdx
cmpb $0x0, 0xc(%rcx,%rdx)
cmovnel %r12d, %eax
addl 0x8(%rcx,%rdx), %eax
movq 0x10(%r15), %r14
addl %eax, 0x8(%r15)
incq %rbx
movq 0x38(%rsp), %r9
movq 0x30(%rsp), %rcx
movslq 0x4(%r9,%rcx), %rax
cmpq %rax, %rbx
jl 0x3a28
movq 0x20(%rsp), %rax
movl 0x18(%rax), %edx
movq 0x28(%rsp), %r15
movl 0x1c(%rsp), %esi
incl %esi
cmpl %edx, %esi
jl 0x39dd
leaq 0x690(%rsp), %r13
movl $0x640, %edx            # imm = 0x640
movq %r13, %rdi
xorl %esi, %esi
callq 0x10e0
leaq 0x50(%rsp), %rbp
movl $0x640, %edx            # imm = 0x640
movq %rbp, %rdi
xorl %esi, %esi
callq 0x10e0
movq 0x8(%rsp), %rdi
callq 0x60af
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
cmpl $0x2, %eax
jl 0x3c18
movl $0x1, %ebx
movsd %xmm0, (%rsp)
movl $0x13, %r14d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movslq 0x14(%rax), %rax
movslq %ebx, %r8
imulq %rax, %r8
movl (%rdi,%r8), %esi
movl 0x4(%rdi,%r8), %ecx
movl 0x8(%rdi,%r8), %eax
xorl %edx, %edx
divl %ecx
movl %eax, 0x8(%rdi,%r8)
movq (%r15), %rax
movq 0x38(%rax), %rdx
movb (%rdx,%rsi), %dl
andb $0x3, %dl
cmpb $0x1, %dl
jne 0x3bee
cmpl %r14d, %ecx
movl $0x13, %edx
cmovll %ecx, %edx
movq 0x30(%rax), %rax
movzwl (%rax,%rsi,2), %esi
cmpw $0x13, %si
movl $0x13, %eax
cmovbl %esi, %eax
movslq %edx, %rdx
shlq $0x4, %rdx
leaq (%rdx,%rdx,4), %rdi
leaq (%rsp,%rdi), %rdx
addq $0x690, %rdx            # imm = 0x690
incl (%rdx,%rax,4)
leal (%rcx,%rcx,4), %eax
leal -0x1(,%rax,2), %eax
xorl %edx, %edx
divl %esi
cltq
leaq (%rsp,%rdi), %rdx
addq $0x50, %rdx
incl (%rdx,%rax,4)
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
incl %ebx
movq 0x8(%rsp), %rdi
callq 0x60af
cmpl %eax, %ebx
jl 0x3b5e
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x60af
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd (%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %rbx, %rdi
callq 0x60af
leaq 0x48ab(%rip), %rdi      # 0x84f4
movl %eax, %esi
movsd (%rsp), %xmm0
movb $0x1, %al
callq 0x10c0
leaq 0x48c4(%rip), %r15      # 0x8522
leaq 0x48c2(%rip), %r12      # 0x8527
xorl %r14d, %r14d
leaq 0x48aa(%rip), %rdi      # 0x8519
movl %r14d, %esi
xorl %eax, %eax
callq 0x10c0
xorl %ebx, %ebx
cmpq %r14, %rbx
jae 0x3c8c
movq %r15, %rdi
xorl %eax, %eax
callq 0x10c0
jmp 0x3c9b
movl (%r13,%rbx,4), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x10c0
incq %rbx
cmpq $0x14, %rbx
jne 0x3c7b
movq %r15, %rdi
xorl %eax, %eax
callq 0x10c0
xorl %ebx, %ebx
movl (%rbp,%rbx,4), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x10c0
incq %rbx
cmpq $0xa, %rbx
jne 0x3cb0
movl $0xa, %edi
callq 0x1050
incq %r14
addq $0x50, %r13
addq $0x50, %rbp
cmpq $0x14, %r14
jne 0x3c68
movq 0x8(%rsp), %rdi
callq 0x5c28
movq 0x10(%rsp), %rdi
callq 0x6862
movq 0x20(%rsp), %rdi
addq $0xcd8, %rsp            # imm = 0xCD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6862