// File: /workspace/github_c_makefile_star2_O1/richarddurbin[P]hash10x/hash10x.c
// Function: codeExplore
void codeExplore (int code, int clusterThreshold) /* clusters this barcode's good hashes */
{
  ClusterBlock *b = arrp(clusterBlocks, code, ClusterBlock), *c ;
  ClusterHash *cb = b->clusHash, *cc ;
  int i, j ;
  U32 x, nc ;
  int *countShare = new0 (arrayMax(clusterBlocks), int) ; /* number of hashes linking to code */
  int *minShare = new0 (arrayMax(clusterBlocks), int) ; /* lowest hash shared with code */
  Array clusterMin = arrayCreate (64, int) ;

  int n = nGoodHashes[code] ; if (!n) return ;
  int *minShareCount = new (n, int) ;
  U16 *g = goodHashes[code] ;
  
  for (i = 0 ; i < n ; ++i) b->clusHash[goodHashes[code][i]].subCluster = 0 ; /* wipe existing */

  b->nSubCluster = 0 ;
  //  if (!b->cluster) b->cluster = arrayCreate (32, ClusterInfo) ;
  b->pointToMin = 0.0 ;
  int nSubClustered = 0 ;
  for (i = 0 ; i < n ; ++i)	// shouldn't this be from i = 0 ?
    { x = b->clusHash[g[i]].hash ;
      nc = arr(hashDepth, x, U32) ;
      memset (minShareCount, 0, n*sizeof(int)) ;
					       
      for (j = 0 ; j < nc ; ++j)
	{ int cj = arr(hashCodes, x, U32*)[j] ;
	  if (cj == code) continue ;
	  if (!minShare[cj]) minShare[cj] = i + 1 ;
	  ++minShareCount[minShare[cj]-1] ;
	  ++countShare[cj] ;
	}

      int msMax = 0, msTot = 0, msBest ;
      for (j = 0 ; j < i ; ++j)
	{ if (minShareCount[j] > msMax)  { msBest = j ; msMax = minShareCount[j] ; }
	  msTot += minShareCount[j] ;
	}
      if (msMax >= clusterThreshold)
	{ ClusterHash *ch = &b->clusHash[g[msBest]] ;
	  if (!ch->subCluster)	/* create a new cluster */
	    { if (++b->nSubCluster > U8MAX) /* abandon this clustering */
		{ b->nSubCluster = 0 ; nSubClustered = 0 ;
		  for (j = 0 ; j < i ; ++j) b->clusHash[g[j]].subCluster = 0 ;
		  fprintf (stderr, "    code %d with %d good hashes has too many clusters\n",
			   code, nGoodHashes[code]) ;
		  break ;
		}
	      ch->subCluster = b->nSubCluster ; ++nSubClustered ;
	      array(clusterMin,b->nSubCluster,int) = msBest ;
	    }
	  b->clusHash[g[i]].subCluster = ch->subCluster ; ++nSubClustered ;
	  b->pointToMin += minShareCount[arr(clusterMin,ch->subCluster,int)] / (double)msTot ;
	}
    }

  if (b->nSubCluster)
    fprintf (outFile, "  code %d with %d hashes, %d good hashes, of which %d cluster into %d raw",
	     code, b->nHash, nGoodHashes[code], nSubClustered, b->nSubCluster) ;
  else
    fprintf (outFile, "  code %d with %d hashes, %d good hashes, 0 clusters\n",
	     code, b->nHash, nGoodHashes[code]) ;

  if (!b->nSubCluster) return ;	/* nothing to do */
  int *readMap = new0 (b->nRead, int), *trueCluster = new (b->nSubCluster+1, int) ;
  int *deadCluster = new0 (b->nSubCluster+1, int) ;
  for (i = 0 ; i <= b->nSubCluster ; ++i) trueCluster[i] = i ; /* initialisation */
  for (i = 0 ; i < b->nHash ; ++i)
    { int hashCluster = trueCluster[b->clusHash[i].subCluster] ; if (!hashCluster) continue ;
      int readCluster = trueCluster[readMap[b->clusHash[i].read]] ;
      if (hashCluster == readCluster) continue ;
      else if (!readCluster) readMap[b->clusHash[i].read] = hashCluster ;
      else			/* merge clusters hashCluster and readCluster */
	{ if (hashCluster > readCluster)
	    { int t = hashCluster ; hashCluster = readCluster ; readCluster = t ; }
	  for (j = 1 ; j <= b->nSubCluster ; ++j)
	    if (trueCluster[j] == readCluster) trueCluster[j] = hashCluster ;
	  deadCluster[readCluster] = 1 ;
	}
    }

  /* now compactify trueCluster, reusing deadCluster[] for the map removing dead indices */
  for (j = 1 ; j <= b->nSubCluster ; ++j) deadCluster[j] = deadCluster[j-1] + 1 - deadCluster[j] ;
  for (j = 1 ; j <= b->nSubCluster ; ++j) trueCluster[j] = deadCluster[trueCluster[j]] ;
  b->nSubCluster = deadCluster[b->nSubCluster] ;
  for (i = 0 ; i < b->nHash ; ++i) b->clusHash[i].subCluster = trueCluster[b->clusHash[i].subCluster] ;

  fprintf (outFile, " then %d merged clusters\n", b->nSubCluster) ; fflush (stdout) ;

  /* now go through countShare and identify clusters that share at least 3 hashes */

  Array h = arrayCreate (256, int) ;
  Array z = arrayCreate (1024, CountStruct) ;
  for (i = 0 ; i < arrayMax(clusterBlocks) ; ++i)
    { ++array(h, countShare[i], int) ;
      if (countShare[i] > 0)
	{ CountStruct *c = arrayp(z,arrayMax(z), CountStruct) ;
	  c->i = i ; c->count = countShare[i] ;
	}
    }
  histogramReport ("COUNT_SHARE", h) ; arrayDestroy (h) ;
  codeClusterReport (code, code+1) ;
  arraySort (z, compareCount) ;
  for (j = arrayMax(z) ; j-- ; ) /* run backwards through z */
    { CountStruct *cz = arrp(z, j, CountStruct) ;
      fprintf (outFile, "  SHARE code %d shared_hashes %d counts", cz->i, cz->count) ;
      int nA = 0, nB = 0 ;
      ClusterBlock *czb = arrp(clusterBlocks, cz->i, ClusterBlock) ;
      for (i = 0 ; i < czb->nHash ; ++i)
	if (cribType[czb->clusHash[i].hash] == CRIB_HTA) ++nA ;
	else if (cribType[czb->clusHash[i].hash] == CRIB_HTB) ++nB ;
      fprintf (outFile, " %d htA %d htB minShare %d %s\n", nA, nB, minShare[cz->i]-1,
	       cribText (b->clusHash[g[minShare[cz->i]-1]].hash)) ;
      if (cz->count < params.clusterThreshold) break ;
    }
  
  free (readMap) ; free(trueCluster) ; free(deadCluster) ;
  free (minShare) ; free (countShare) ; free (minShareCount) ;
  arrayDestroy (clusterMin) ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %esi, 0x60(%rsp)
movq 0xa2a8(%rip), %rax      # 0x10388
movq 0x8(%rax), %rbx
movslq 0x14(%rax), %r14
movq %rdi, 0x18(%rsp)
movslq %edi, %r13
movslq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x94f8
movq %rax, 0x30(%rsp)
movq 0xa27e(%rip), %rax      # 0x10388
movslq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x94f8
movq %rax, 0x68(%rsp)
movl $0x40, %edi
movl $0x4, %esi
callq 0x8c70
movq %r14, %rcx
movq %rax, %r14
movq 0xa2df(%rip), %rax      # 0x10418
movslq (%rax,%r13,4), %rbp
testq %rbp, %rbp
je 0x67f4
imulq %r13, %rcx
leaq (,%rbp,4), %rdi
movq %rdi, 0x28(%rsp)
movq %rcx, %r15
callq 0x94d0
movq %r15, %r10
movq %rax, %r12
movq 0xa2a4(%rip), %rax      # 0x10410
movq (%rax,%r13,8), %r8
testl %ebp, %ebp
movq %rbx, %rdi
jle 0x6190
movq 0x10(%rdi,%r10), %rax
xorl %ecx, %ecx
movzwl (%r8,%rcx,2), %edx
movb $0x0, 0x6(%rax,%rdx,8)
incq %rcx
cmpq %rcx, %rbp
jne 0x617e
movq %r13, 0x70(%rsp)
movq %r14, 0x50(%rsp)
movl $0x0, 0x8(%rdi,%r10)
movq $0x0, 0x18(%rdi,%r10)
testl %ebp, %ebp
movq %rdi, 0x20(%rsp)
movq %r8, 0x38(%rsp)
movq %r10, 0x10(%rsp)
jle 0x63da
movq %rbp, 0x58(%rsp)
xorl %r14d, %r14d
xorl %r9d, %r9d
movl %r9d, %r15d
movq 0x10(%rdi,%r10), %rax
movzwl (%r8,%r14,2), %ecx
movl (%rax,%rcx,8), %ebx
movq 0xa19b(%rip), %rax      # 0x10380
movq 0x8(%rax), %rcx
movl 0x14(%rax), %eax
imull %ebx, %eax
movl (%rcx,%rax), %r13d
movq %r12, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
callq 0x10f0
testq %r13, %r13
movq 0x30(%rsp), %r9
movq 0x68(%rsp), %r10
movq 0x18(%rsp), %rdx
je 0x6259
movq 0xa19b(%rip), %rax      # 0x103b8
movq 0x8(%rax), %rcx
leal 0x1(%r14), %r11d
xorl %esi, %esi
movl 0x14(%rax), %edi
imull %ebx, %edi
movq (%rcx,%rdi), %rdi
movslq (%rdi,%rsi,4), %rdi
cmpl %edx, %edi
je 0x6251
cmpl $0x0, (%r10,%rdi,4)
jne 0x6244
movl %r11d, (%r10,%rdi,4)
movslq (%r10,%rdi,4), %r8
incl -0x4(%r12,%r8,4)
incl (%r9,%rdi,4)
incq %rsi
cmpq %rsi, %r13
jne 0x6227
testq %r14, %r14
movq 0x38(%rsp), %r8
movq 0x10(%rsp), %r10
je 0x6295
xorl %ecx, %ecx
xorl %eax, %eax
xorl %r9d, %r9d
movq 0x20(%rsp), %rdi
movl (%r12,%rcx,4), %esi
cmpl %eax, %esi
cmovgl %ecx, %ebp
cmovgl %esi, %eax
addl %esi, %r9d
incq %rcx
cmpq %rcx, %r14
jne 0x6274
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
jmp 0x62a0
xorl %eax, %eax
xorpd %xmm1, %xmm1
movq 0x20(%rsp), %rdi
cmpl 0x60(%rsp), %eax
movl %r15d, %r9d
jl 0x6365
movq 0x10(%rdi,%r10), %rbx
movslq %ebp, %rax
movzwl (%r8,%rax,2), %r13d
cmpb $0x0, 0x6(%rbx,%r13,8)
je 0x62c9
movq 0x50(%rsp), %r15
jmp 0x6318
movl 0x8(%rdi,%r10), %esi
incl %esi
movl %esi, 0x8(%rdi,%r10)
cmpl $0x100, %esi            # imm = 0x100
jae 0x637b
movb %sil, 0x6(%rbx,%r13,8)
incl %r9d
movl %r9d, 0x48(%rsp)
movq 0x50(%rsp), %r15
movq %r15, %rdi
movsd %xmm1, 0x40(%rsp)
callq 0x8f2b
movsd 0x40(%rsp), %xmm1
movl 0x48(%rsp), %r9d
movq 0x38(%rsp), %r8
movq 0x20(%rsp), %rdi
movl %ebp, (%rax)
movb 0x6(%rbx,%r13,8), %al
movq 0x10(%rsp), %rsi
movq 0x10(%rdi,%rsi), %rcx
movzwl (%r8,%r14,2), %edx
movb %al, 0x6(%rcx,%rdx,8)
incl %r9d
movq 0x8(%r15), %rax
movzbl 0x6(%rbx,%r13,8), %ecx
movslq 0x14(%r15), %rdx
movq %rsi, %r10
imulq %rcx, %rdx
movslq (%rax,%rdx), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%r12,%rax,4), %xmm0
divsd %xmm1, %xmm0
addsd 0x18(%rdi,%rsi), %xmm0
movsd %xmm0, 0x18(%rdi,%rsi)
xorl %eax, %eax
testb %al, %al
jne 0x63dd
incq %r14
cmpq 0x58(%rsp), %r14
jne 0x61ce
jmp 0x63dd
movl $0x0, 0x8(%rdi,%r10)
testq %r14, %r14
je 0x639d
xorl %eax, %eax
movzwl (%r8,%rax,2), %ecx
movb $0x0, 0x6(%rbx,%rcx,8)
incq %rax
cmpq %rax, %r14
jne 0x638b
movq 0x8c4c(%rip), %rax      # 0xeff0
movq (%rax), %rdi
movq 0xa06a(%rip), %rax      # 0x10418
movq 0x70(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
leaq 0x4498(%rip), %rsi      # 0xa855
xorl %eax, %eax
callq 0x1150
xorl %r9d, %r9d
movq 0x38(%rsp), %r8
movb $0x1, %al
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r10
jmp 0x6367
xorl %r9d, %r9d
movl 0x4(%rdi,%r10), %ecx
movl 0x8(%rdi,%r10), %eax
movq 0x9fa2(%rip), %rdi      # 0x10390
movq 0xa023(%rip), %rdx      # 0x10418
movq 0x70(%rsp), %rsi
movl (%rdx,%rsi,4), %r8d
testl %eax, %eax
je 0x641a
movl %eax, (%rsp)
leaq 0x5136(%rip), %rsi      # 0xb542
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1150
jmp 0x642d
leaq 0x516b(%rip), %rsi      # 0xb58c
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1150
movq 0x20(%rsp), %rbx
movq 0x10(%rsp), %rax
cmpl $0x0, 0x8(%rbx,%rax)
je 0x67f4
movl (%rbx,%rax), %edi
movl $0x4, %esi
callq 0x94f8
movq %rax, %r14
movq 0x10(%rsp), %rax
movl 0x8(%rbx,%rax), %edi
incl %edi
shlq $0x2, %rdi
callq 0x94d0
movq %rax, %r13
movq 0x10(%rsp), %rax
movl 0x8(%rbx,%rax), %edi
incl %edi
movl $0x4, %esi
callq 0x94f8
movq 0x10(%rsp), %r11
xorl %esi, %esi
movq %rbx, %r9
movl %esi, (%r13,%rsi,4)
leaq 0x1(%rsi), %rcx
movl 0x8(%r9,%r11), %edx
cmpq %rdx, %rsi
movq %rcx, %rsi
jb 0x6488
cmpl $0x0, 0x4(%r9,%r11)
je 0x651d
movq 0x10(%r9,%r11), %r10
xorl %ecx, %ecx
movzbl 0x6(%r10,%rcx,8), %edx
movl (%r13,%rdx,4), %edi
testl %edi, %edi
je 0x6510
movzwl 0x4(%r10,%rcx,8), %esi
movslq (%r14,%rsi,4), %rdx
movl (%r13,%rdx,4), %edx
cmpl %edx, %edi
je 0x6510
testl %edx, %edx
je 0x650c
cmpl %edx, %edi
movl %edx, %esi
cmovll %edi, %esi
cmovgl %edi, %edx
cmpl $0x0, 0x8(%r9,%r11)
je 0x6500
xorl %edi, %edi
cmpl %edx, 0x4(%r13,%rdi,4)
jne 0x64f3
movl %esi, 0x4(%r13,%rdi,4)
movl 0x8(%r9,%r11), %r8d
incq %rdi
cmpq %r8, %rdi
jb 0x64e7
movslq %edx, %rdx
movl $0x1, (%rax,%rdx,4)
jmp 0x6510
movl %edi, (%r14,%rsi,4)
incq %rcx
movl 0x4(%r9,%r11), %edx
cmpq %rdx, %rcx
jb 0x64ad
cmpl $0x0, 0x8(%r9,%r11)
je 0x6540
movl (%rax), %esi
xorl %ecx, %ecx
subl 0x4(%rax,%rcx,4), %esi
incl %esi
movl %esi, 0x4(%rax,%rcx,4)
movl 0x8(%r9,%r11), %edx
incq %rcx
cmpq %rdx, %rcx
jb 0x6529
movl 0x8(%r9,%r11), %edx
testl %edx, %edx
je 0x6565
xorl %ecx, %ecx
movslq 0x4(%r13,%rcx,4), %rdx
movl (%rax,%rdx,4), %edx
movl %edx, 0x4(%r13,%rcx,4)
movl 0x8(%r9,%r11), %edx
incq %rcx
cmpq %rdx, %rcx
jb 0x654b
movq %r14, 0x58(%rsp)
movl %edx, %ecx
movq %rax, 0x40(%rsp)
movl (%rax,%rcx,4), %edx
movl %edx, 0x8(%r9,%r11)
movl 0x4(%r9,%r11), %eax
testq %rax, %rax
je 0x65a1
movq 0x10(%r9,%r11), %rcx
xorl %esi, %esi
movzbl 0x6(%rcx,%rsi,8), %edi
movb (%r13,%rdi,4), %dil
movb %dil, 0x6(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x658a
movq %r13, 0x48(%rsp)
movq 0x9de3(%rip), %rdi      # 0x10390
leaq 0x436a(%rip), %rsi      # 0xa91e
xorl %eax, %eax
callq 0x1150
movq 0x8a06(%rip), %rax      # 0xefc8
movq (%rax), %rdi
callq 0x11a0
movl $0x100, %edi            # imm = 0x100
movl $0x4, %esi
callq 0x8c70
movq %rax, %r14
movl $0x400, %edi            # imm = 0x400
movl $0x8, %esi
callq 0x8c70
movq %rax, 0x28(%rsp)
movq 0x9d91(%rip), %rax      # 0x10388
cmpl $0x0, 0x18(%rax)
movq 0x30(%rsp), %rdx
jle 0x6649
xorl %ebx, %ebx
movl (%rdx,%rbx,4), %esi
movq %r14, %rdi
callq 0x8f2b
movq 0x30(%rsp), %rdx
incl (%rax)
cmpl $0x0, (%rdx,%rbx,4)
jle 0x6636
movq 0x28(%rsp), %rdi
movl 0x18(%rdi), %esi
callq 0x8f2b
movq 0x30(%rsp), %rdx
movl %ebx, (%rax)
movl (%rdx,%rbx,4), %ecx
movl %ecx, 0x4(%rax)
incq %rbx
movq 0x9d48(%rip), %rax      # 0x10388
movslq 0x18(%rax), %rax
cmpq %rax, %rbx
jl 0x6604
leaq 0x4f72(%rip), %rdi      # 0xb5c2
movq %r14, %rsi
callq 0x2aaa
movq %r14, %rdi
callq 0x8dc9
movq 0x18(%rsp), %rdi
leal 0x1(%rdi), %esi
callq 0x4a44
movq 0x28(%rsp), %rbx
movq 0x8(%rbx), %rdi
movslq 0x18(%rbx), %rsi
movslq 0x14(%rbx), %rdx
leaq 0x20a(%rip), %rcx       # 0x688f
callq 0x1090
movl 0x18(%rbx), %ecx
subl $0x1, %ecx
jb 0x67a2
movq 0x28(%rsp), %rax
movq 0x8(%rax), %r14
movslq 0x14(%rax), %rax
movl %ecx, 0x18(%rsp)
movslq %ecx, %rbx
imulq %rax, %rbx
movq 0x9cdb(%rip), %rdi      # 0x10390
movl (%r14,%rbx), %edx
movl 0x4(%r14,%rbx), %ecx
xorl %r13d, %r13d
leaq 0x4f06(%rip), %rsi      # 0xb5ce
xorl %eax, %eax
callq 0x1150
movq 0x9cb2(%rip), %rcx      # 0x10388
movq 0x8(%rcx), %rsi
movq %r14, %r15
movslq (%r14,%rbx), %rax
movslq 0x14(%rcx), %rdi
imulq %rax, %rdi
movl 0x4(%rsi,%rdi), %ecx
movl $0x0, %r14d
testq %rcx, %rcx
movq 0x20(%rsp), %r9
movq 0x68(%rsp), %r10
movq 0x10(%rsp), %r11
je 0x6740
movq 0x9cba(%rip), %rdx      # 0x103c8
movq 0x10(%rsi,%rdi), %rsi
xorl %edi, %edi
xorl %r14d, %r14d
xorl %r13d, %r13d
movl (%rsi,%rdi,8), %r8d
movzbl (%rdx,%r8), %r8d
cmpl $0x2, %r8d
je 0x6735
cmpl $0x1, %r8d
jne 0x6738
incl %r13d
jmp 0x6738
incl %r14d
incq %rdi
cmpq %rdi, %rcx
jne 0x671b
movq 0x9c49(%rip), %rcx      # 0x10390
movq %rcx, 0x60(%rsp)
movslq (%r10,%rax,4), %rbp
movq 0x10(%r9,%r11), %rax
movq 0x38(%rsp), %rcx
movzwl -0x2(%rcx,%rbp,2), %ecx
decq %rbp
movl (%rax,%rcx,8), %edi
callq 0x353c
movq 0x60(%rsp), %rdi
leaq 0x4e80(%rip), %rsi      # 0xb5f6
movl %r13d, %edx
movl %r14d, %ecx
movl %ebp, %r8d
leaq 0x9c5a(%rip), %r9       # 0x103e0
xorl %eax, %eax
callq 0x1150
movl 0x4(%r15,%rbx), %eax
cmpl 0x9ca8(%rip), %eax      # 0x10440
movl 0x18(%rsp), %ecx
jge 0x668d
movq 0x58(%rsp), %rdi
callq 0x1040
movq 0x48(%rsp), %rdi
callq 0x1040
movq 0x40(%rsp), %rdi
callq 0x1040
movq 0x68(%rsp), %rdi
callq 0x1040
movq 0x30(%rsp), %rdi
callq 0x1040
movq %r12, %rdi
callq 0x1040
movq 0x50(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8dc9
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq