// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq 0x8(%rdi), %rax
imull 0x14(%rax), %r14d
addq 0x8(%rax), %r14
movl $0x1, %edi
movl $0x48, %esi
callq 0x6cbc
movl $0x400, %edi            # imm = 0x400
movl $0x18, %esi
callq 0x6434
movq %rax, 0x10(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x588c
movq %rax, 0x8(%rsp)
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x2cbe
cmpl $0x0, 0x18(%rax)
movq %rax, 0x20(%rsp)
jle 0x3966
xorl %ecx, %ecx
movq 0x8(%rax), %rdx
movslq 0x14(%rax), %rax
movl %ecx, 0x1c(%rsp)
movslq %ecx, %rsi
imulq %rax, %rsi
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rcx
movl 0x14(%rax), %edi
imull (%rdx,%rsi), %edi
cmpl $0x0, 0x4(%rcx,%rdi)
jle 0x3952
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %r8d, %r8d
movq %rdx, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
movl %r8d, (%rsp)
movq 0x8(%rcx,%rdi), %rax
movq 0x10(%rcx,%rdi), %rcx
movl (%rax,%rbx,4), %r12d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r12
movzwl (%rcx,%rbx,2), %r15d
movq %r12, %rsi
xorq %rax, %rsi
movq 0x8(%rsp), %rdi
callq 0x5a50
movl %eax, %ebp
movq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x66ef
movq %rax, %r14
cmpl $0x0, 0x4(%rax)
jne 0x38e1
movl %r12d, (%r14)
movl $0x8, %edi
movl $0x4, %esi
callq 0x6434
movq %rax, 0x10(%r14)
incl 0x4(%r14)
testq %rbx, %rbx
je 0x38ee
incl 0xc(%r14)
testq %r13, %r13
je 0x3901
movl 0x18(%r13), %esi
movq %r13, %rdi
callq 0x66ef
movl %ebp, (%rax)
movl (%rsp), %r8d
addl %r15d, %r8d
movq 0x10(%r14), %r13
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rsi
cmpb $0x0, 0xc(%rdx,%rsi)
movl 0x8(%r14), %eax
je 0x392a
movl 0x8(%rdx,%rsi), %ecx
addl %r8d, %ecx
jmp 0x3931
movl 0x8(%rdx,%rsi), %ecx
subl %r8d, %ecx
movq 0x30(%rsp), %rdi
addl %eax, %ecx
movl %ecx, 0x8(%r14)
incq %rbx
movq 0x38(%rsp), %rcx
movslq 0x4(%rcx,%rdi), %rax
cmpq %rax, %rbx
jl 0x3885
movl 0x1c(%rsp), %ecx
incl %ecx
movq 0x20(%rsp), %rax
cmpl 0x18(%rax), %ecx
jl 0x3837
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 0x5dd2
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
cmpl $0x2, %eax
jl 0x3a7c
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 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x38(%rax), %rdx
movb (%rdx,%rsi), %dl
andb $0x3, %dl
cmpb $0x1, %dl
jne 0x3a52
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 0x5dd2
cmpl %eax, %ebx
jl 0x39bd
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x5dd2
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd (%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %rbx, %rdi
callq 0x5dd2
leaq 0x4a45(%rip), %rdi      # 0x84f2
movl %eax, %esi
movsd (%rsp), %xmm0
movb $0x1, %al
callq 0x10c0
leaq 0x4a5e(%rip), %r12      # 0x8520
leaq 0x4a5c(%rip), %r15      # 0x8525
xorl %r14d, %r14d
leaq 0x4a44(%rip), %rdi      # 0x8517
movl %r14d, %esi
xorl %eax, %eax
callq 0x10c0
xorl %ebx, %ebx
cmpq %r14, %rbx
jae 0x3af0
movq %r12, %rdi
xorl %eax, %eax
callq 0x10c0
jmp 0x3aff
movl (%r13,%rbx,4), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10c0
incq %rbx
cmpq $0x14, %rbx
jne 0x3adf
movq %r12, %rdi
xorl %eax, %eax
callq 0x10c0
xorl %ebx, %ebx
movl (%rbp,%rbx,4), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10c0
incq %rbx
cmpq $0xa, %rbx
jne 0x3b14
movl $0xa, %edi
callq 0x1050
incq %r14
addq $0x50, %r13
addq $0x50, %rbp
cmpq $0x14, %r14
jne 0x3acc
movq 0x8(%rsp), %rdi
callq 0x5948
movq 0x10(%rsp), %rdi
callq 0x658d
movq 0x20(%rsp), %rdi
addq $0xcd8, %rsp            # imm = 0xCD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x658d