// File: /workspace/github_c_makefile_star2_O2/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, %r15d
movq %rdi, %r14
movq 0x8(%rdi), %rax
imull 0x14(%rax), %r15d
addq 0x8(%rax), %r15
pushq $0x1
popq %rdi
pushq $0x48
popq %rsi
callq 0x65c4
pushq $0x18
popq %rsi
movl $0x400, %edi            # imm = 0x400
callq 0x5d68
movq %rax, 0x10(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x5288
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rbp
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x2142
xorl %ecx, %ecx
movq %rax, 0x20(%rsp)
movq %r14, 0x28(%rsp)
cmpl 0x18(%rax), %ecx
jge 0x2d29
movq 0x8(%rax), %rdx
movslq 0x14(%rax), %rax
movl %ecx, 0x1c(%rsp)
movslq %ecx, %rcx
imulq %rax, %rcx
movq 0x8(%r14), %rax
movl 0x14(%rax), %esi
movq %rdx, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
imull (%rdx,%rcx), %esi
movq 0x8(%rax), %rcx
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rsi, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movslq 0x4(%rcx,%rsi), %rax
cmpq %rax, %rbx
jge 0x2d11
movq 0x8(%rcx,%rsi), %rax
movq 0x10(%rcx,%rsi), %rcx
movl (%rax,%rbx,4), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r14
movzwl (%rcx,%rbx,2), %ecx
movl %ecx, (%rsp)
movq %r14, %rsi
xorq %rax, %rsi
movq 0x8(%rsp), %rdi
callq 0x542f
movl %eax, %r13d
movq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x6000
movq %rax, %rbp
movl 0x4(%rax), %eax
testl %eax, %eax
jne 0x2cb7
movl %r14d, (%rbp)
pushq $0x8
popq %rdi
pushq $0x4
popq %rsi
callq 0x5d68
movq %rax, 0x10(%rbp)
movl 0x4(%rbp), %eax
incl %eax
movl %eax, 0x4(%rbp)
testq %rbx, %rbx
je 0x2cc4
incl 0xc(%rbp)
testq %r15, %r15
je 0x2cd8
movl 0x18(%r15), %esi
movq %r15, %rdi
callq 0x6000
movl %r13d, (%rax)
addl (%rsp), %r12d
movq 0x10(%rbp), %r15
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
addl %eax, 0x8(%rbp)
incq %rbx
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rcx
jmp 0x2c4d
movl 0x1c(%rsp), %ecx
incl %ecx
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %r14
pushq $0x1
popq %rbp
jmp 0x2c06
movl $0x63c, %eax            # imm = 0x63C
leaq 0x50(%rsp), %rcx
addq %rax, %rcx
leaq 0x690(%rsp), %r15
addq %r15, %rax
pushq $0x14
popq %rdx
subq $0x1, %rdx
jb 0x2d69
xorl %esi, %esi
cmpq $-0x14, %rsi
je 0x2d5f
andl $0x0, (%rcx,%rsi,4)
andl $0x0, (%rax,%rsi,4)
decq %rsi
jmp 0x2d4c
addq $-0x50, %rcx
addq $-0x50, %rax
jmp 0x2d44
xorps %xmm0, %xmm0
movsd %xmm0, (%rsp)
pushq $0x13
popq %rbx
movq 0x8(%rsp), %rdi
callq 0x575c
cmpl %eax, %ebp
jge 0x2e20
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movslq 0x14(%rax), %rax
movslq %ebp, %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 (%r14), %rax
movq 0x38(%rax), %rdx
movb (%rdx,%rsi), %dl
andb $0x3, %dl
cmpb $0x1, %dl
jne 0x2e06
cmpl %ebx, %ecx
movl %ebx, %edx
cmovll %ecx, %edx
movq 0x30(%rax), %rax
movzwl (%rax,%rsi,2), %esi
cmpw $0x13, %si
movl %ebx, %eax
cmovbl %esi, %eax
movslq %edx, %rdx
imulq $0x50, %rdx, %rdi
leaq (%rsp,%rdi), %rdx
addq $0x50, %rdx
incl (%rdx,%rax,4)
imull $0xa, %ecx, %eax
decl %eax
xorl %edx, %edx
divl %esi
cltq
leaq (%rsp,%rdi), %rdx
addq $0x690, %rdx            # imm = 0x690
incl (%rdx,%rax,4)
cvtsi2sd %rcx, %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
incl %ebp
jmp 0x2d74
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x575c
cvtsi2sd %eax, %xmm0
movsd (%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %rbx, %rdi
callq 0x575c
leaq 0x46a6(%rip), %rdi      # 0x74f4
movl %eax, %esi
movsd (%rsp), %xmm0
movb $0x1, %al
callq 0x10c0
leaq 0x46bf(%rip), %r13      # 0x7522
leaq 0x46bd(%rip), %r12      # 0x7527
xorl %ebp, %ebp
leaq 0x50(%rsp), %r14
cmpq $0x14, %rbp
je 0x2eed
leaq 0x469b(%rip), %rdi      # 0x7519
movl %ebp, %esi
xorl %eax, %eax
callq 0x10c0
xorl %ebx, %ebx
cmpq $0x14, %rbx
je 0x2eb3
cmpq %rbp, %rbx
jae 0x2ea0
movq %r13, %rdi
xorl %eax, %eax
callq 0x10c0
jmp 0x2eae
movl (%r14,%rbx,4), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x10c0
incq %rbx
jmp 0x2e89
movq %r13, %rdi
xorl %eax, %eax
callq 0x10c0
xorl %ebx, %ebx
cmpq $0xa, %rbx
je 0x2ed8
movl (%r15,%rbx,4), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x10c0
incq %rbx
jmp 0x2ebf
pushq $0xa
popq %rdi
callq 0x1050
incq %rbp
addq $0x50, %r14
addq $0x50, %r15
jmp 0x2e71
movq 0x8(%rsp), %rdi
callq 0x5334
movq 0x10(%rsp), %rdi
callq 0x5ea8
movq 0x20(%rsp), %rdi
addq $0xcd8, %rsp            # imm = 0xCD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5ea8