// File: /workspace/github_c_makefile_star2_O3/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, 0x18(%rsp)
movl %edi, %r13d
movq 0xa125(%rip), %rax      # 0x10388
movslq 0x14(%rax), %rcx
movslq %edi, %r14
imulq %r14, %rcx
addq 0x8(%rax), %rcx
movq %rcx, 0x10(%rsp)
movslq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x96e8
movq %rax, 0x28(%rsp)
movq 0xa0f7(%rip), %rax      # 0x10388
movslq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x96e8
movq %rax, 0x30(%rsp)
movl $0x40, %edi
movl $0x4, %esi
callq 0x8e88
movq %rax, %rbx
movq 0xa15b(%rip), %rax      # 0x10418
movslq (%rax,%r14,4), %r12
testq %r12, %r12
je 0x6694
leaq (,%r12,4), %rdi
movq %rdi, 0x70(%rsp)
callq 0x96c0
movq %rax, %rdi
movq 0xa12a(%rip), %rax      # 0x10410
movq (%rax,%r14,8), %r10
testl %r12d, %r12d
movq %rbx, 0x60(%rsp)
movq %rdi, 0x40(%rsp)
movq %r10, 0x38(%rsp)
jle 0x6538
movq 0x10(%rsp), %r8
movq 0x10(%r8), %rax
xorl %ecx, %ecx
movq 0x30(%rsp), %rbx
movzwl (%r10,%rcx,2), %edx
movb $0x0, 0x6(%rax,%rdx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x6312
leaq 0x8(%r8), %r15
movl $0x0, 0x8(%r8)
movq $0x0, 0x18(%r8)
testl %r12d, %r12d
jle 0x6558
movq %r12, 0x68(%rsp)
movq %r14, 0x48(%rsp)
movq %r15, 0x50(%rsp)
xorl %r14d, %r14d
xorl %r9d, %r9d
movl %r9d, 0x20(%rsp)
movq 0x10(%r8), %rax
movzwl (%r10,%r14,2), %ecx
movl (%rax,%rcx,8), %r12d
movq 0xa011(%rip), %rax      # 0x10380
movq 0x8(%rax), %rcx
movl 0x14(%rax), %eax
imull %r12d, %eax
movl (%rcx,%rax), %r15d
xorl %esi, %esi
movq 0x70(%rsp), %rdx
callq 0x10f0
testq %r15, %r15
movq 0x28(%rsp), %r9
je 0x63e1
movq 0xa01d(%rip), %rax      # 0x103b8
movq 0x8(%rax), %rcx
leal 0x1(%r14), %edx
xorl %esi, %esi
movl 0x14(%rax), %edi
imull %r12d, %edi
movq (%rcx,%rdi), %rdi
movslq (%rdi,%rsi,4), %rdi
cmpl %r13d, %edi
je 0x63d9
movl (%rbx,%rdi,4), %r8d
testl %r8d, %r8d
jne 0x63c8
movl %edx, (%rbx,%rdi,4)
movl %edx, %r8d
movslq %r8d, %r8
movq 0x40(%rsp), %r10
incl -0x4(%r10,%r8,4)
incl (%r9,%rdi,4)
incq %rsi
cmpq %rsi, %r15
jne 0x63a5
testq %r14, %r14
movq 0x10(%rsp), %r8
movq 0x40(%rsp), %rdi
movl 0x20(%rsp), %r9d
je 0x641e
xorl %ecx, %ecx
xorl %eax, %eax
xorl %edx, %edx
movq 0x38(%rsp), %r10
movl (%rdi,%rcx,4), %esi
cmpl %eax, %esi
cmovgl %ecx, %ebp
cmovgl %esi, %eax
addl %esi, %edx
incq %rcx
cmpq %rcx, %r14
jne 0x6400
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
jmp 0x6429
xorl %eax, %eax
xorpd %xmm1, %xmm1
movq 0x38(%rsp), %r10
cmpl 0x18(%rsp), %eax
jl 0x64ed
movq 0x10(%r8), %r12
movslq %ebp, %rax
movzwl (%r10,%rax,2), %r15d
movb 0x6(%r12,%r15,8), %al
testb %al, %al
je 0x6452
movq %r12, %rcx
movq 0x60(%rsp), %rbx
jmp 0x64ae
movq 0x50(%rsp), %rax
movl (%rax), %esi
incl %esi
movl %esi, (%rax)
cmpl $0x100, %esi            # imm = 0x100
jae 0x6920
movb %sil, 0x6(%r12,%r15,8)
incl %r9d
movl %r9d, 0x20(%rsp)
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
movsd %xmm1, 0x58(%rsp)
callq 0x9131
movsd 0x58(%rsp), %xmm1
movl 0x20(%rsp), %r9d
movq 0x38(%rsp), %r10
movq 0x40(%rsp), %rdi
movq 0x10(%rsp), %r8
movl %ebp, (%rax)
movb 0x6(%r12,%r15,8), %al
movq 0x10(%r8), %rcx
movzwl (%r10,%r14,2), %edx
movb %al, 0x6(%rcx,%rdx,8)
incl %r9d
movq 0x8(%rbx), %rax
movzbl 0x6(%r12,%r15,8), %ecx
movslq 0x14(%rbx), %rdx
imulq %rcx, %rdx
movslq (%rax,%rdx), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rdi,%rax,4), %xmm0
divsd %xmm1, %xmm0
addsd 0x18(%r8), %xmm0
movsd %xmm0, 0x18(%r8)
movq 0x30(%rsp), %rbx
incq %r14
cmpq 0x68(%rsp), %r14
jne 0x6356
movq 0x50(%rsp), %r15
movl (%r15), %eax
testl %eax, %eax
je 0x6553
movq 0x9e82(%rip), %rdi      # 0x10390
movl 0x4(%r8), %ecx
movq 0x9eff(%rip), %rdx      # 0x10418
movq 0x48(%rsp), %rsi
movl (%rdx,%rsi,4), %r8d
movl %eax, (%rsp)
leaq 0x503c(%rip), %rsi      # 0xb568
movl %r13d, %edx
xorl %eax, %eax
callq 0x1150
jmp 0x657f
movq 0x10(%rsp), %r8
leaq 0x8(%r8), %r15
movl $0x0, 0x8(%r8)
movq $0x0, 0x18(%r8)
jmp 0x6558
movq 0x48(%rsp), %r14
movq 0x9e31(%rip), %rdi      # 0x10390
movl 0x4(%r8), %ecx
movq 0x9eae(%rip), %rax      # 0x10418
movl (%rax,%r14,4), %r8d
leaq 0x503d(%rip), %rsi      # 0xb5b2
movl %r13d, %edx
xorl %eax, %eax
callq 0x1150
cmpl $0x0, (%r15)
je 0x6694
movq 0x10(%rsp), %r14
movl (%r14), %edi
movl $0x4, %esi
callq 0x96e8
movq %rax, %rbx
movl 0x8(%r14), %edi
incl %edi
shlq $0x2, %rdi
callq 0x96c0
movq %rax, %r12
movl 0x8(%r14), %edi
incl %edi
movl $0x4, %esi
callq 0x96e8
xorl %ecx, %ecx
movl %ecx, (%r12,%rcx,4)
leaq 0x1(%rcx), %rdx
movl (%r15), %esi
cmpq %rsi, %rcx
movq %rdx, %rcx
jb 0x65c2
movq 0x10(%rsp), %r10
cmpl $0x0, 0x4(%r10)
je 0x6657
movq 0x10(%r10), %r11
xorl %ecx, %ecx
movzbl 0x6(%r11,%rcx,8), %edx
movl (%r12,%rdx,4), %edi
testl %edi, %edi
je 0x6648
movzwl 0x4(%r11,%rcx,8), %esi
movslq (%rbx,%rsi,4), %rdx
movl (%r12,%rdx,4), %edx
cmpl %edx, %edi
je 0x6648
testl %edx, %edx
je 0x6645
cmpl %edx, %edi
movl %edx, %esi
cmovll %edi, %esi
cmovgl %edi, %edx
movl (%r15), %edi
testl %edi, %edi
je 0x6639
xorl %r8d, %r8d
cmpl %edx, 0x4(%r12,%r8,4)
jne 0x662e
movl %esi, 0x4(%r12,%r8,4)
movl (%r15), %edi
movl %edi, %r9d
incq %r8
cmpq %r9, %r8
jb 0x661f
movslq %edx, %rdx
movl $0x1, (%rax,%rdx,4)
jmp 0x6648
movl %edi, (%rbx,%rsi,4)
incq %rcx
movl 0x4(%r10), %edx
cmpq %rdx, %rcx
jb 0x65e7
movl (%r15), %esi
testl %esi, %esi
je 0x66a3
movl (%rax), %esi
xorl %ecx, %ecx
subl 0x4(%rax,%rcx,4), %esi
incl %esi
movl %esi, 0x4(%rax,%rcx,4)
movl (%r15), %edx
incq %rcx
cmpq %rdx, %rcx
jb 0x665f
testl %edx, %edx
je 0x66a3
xorl %ecx, %ecx
movslq 0x4(%r12,%rcx,4), %rdx
movl (%rax,%rdx,4), %edx
movl %edx, 0x4(%r12,%rcx,4)
movl (%r15), %edx
incq %rcx
cmpq %rdx, %rcx
jb 0x667a
jmp 0x66a5
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edx, %edx
movq %rbx, 0x70(%rsp)
movq %rax, 0x58(%rsp)
movl (%rax,%rdx,4), %edx
movl %edx, 0x8(%r10)
movl 0x4(%r10), %eax
testq %rax, %rax
je 0x66db
movq 0x10(%r10), %rcx
xorl %esi, %esi
movzbl 0x6(%rcx,%rsi,8), %edi
movb (%r12,%rdi,4), %dil
movb %dil, 0x6(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x66c5
movq %r12, 0x68(%rsp)
movq 0x9ca9(%rip), %rdi      # 0x10390
leaq 0x4270(%rip), %rsi      # 0xa95e
xorl %eax, %eax
callq 0x1150
movq 0x88cc(%rip), %rax      # 0xefc8
movq (%rax), %rdi
callq 0x11a0
movl $0x100, %edi            # imm = 0x100
movl $0x4, %esi
callq 0x8e88
movq %rax, %rbp
movl $0x400, %edi            # imm = 0x400
movl $0x8, %esi
callq 0x8e88
movq %rax, 0x18(%rsp)
movq 0x9c57(%rip), %rax      # 0x10388
cmpl $0x0, 0x18(%rax)
movq 0x28(%rsp), %rdx
jle 0x6783
xorl %ebx, %ebx
movl (%rdx,%rbx,4), %esi
movq %rbp, %rdi
callq 0x9131
movq 0x28(%rsp), %rdx
incl (%rax)
cmpl $0x0, (%rdx,%rbx,4)
jle 0x6770
movq 0x18(%rsp), %rdi
movl 0x18(%rdi), %esi
callq 0x9131
movq 0x28(%rsp), %rdx
movl %ebx, (%rax)
movl (%rdx,%rbx,4), %ecx
movl %ecx, 0x4(%rax)
incq %rbx
movq 0x9c0e(%rip), %rax      # 0x10388
movslq 0x18(%rax), %rax
cmpq %rax, %rbx
jl 0x673e
leaq 0x4e5e(%rip), %rdi      # 0xb5e8
movq %rbp, %rsi
callq 0x2a95
movq %rbp, %rdi
callq 0x8fd6
leal 0x1(%r13), %esi
movl %r13d, %edi
callq 0x4b09
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %rdi
movslq 0x18(%rbx), %rsi
movslq 0x14(%rbx), %rdx
leaq 0x24a(%rip), %rcx       # 0x6a08
callq 0x1090
movl 0x18(%rbx), %r14d
subl $0x1, %r14d
jb 0x68cc
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rbx
movslq 0x14(%rax), %rax
movslq %r14d, %r12
imulq %rax, %r12
movq 0x9ba4(%rip), %rdi      # 0x10390
movl (%rbx,%r12), %edx
movl 0x4(%rbx,%r12), %ecx
xorl %r13d, %r13d
leaq 0x4df5(%rip), %rsi      # 0xb5f4
xorl %eax, %eax
callq 0x1150
movq 0x9b7b(%rip), %rcx      # 0x10388
movq 0x8(%rcx), %rsi
movq %rbx, 0x20(%rsp)
movslq (%rbx,%r12), %rax
movslq 0x14(%rcx), %rdi
imulq %rax, %rdi
movl 0x4(%rsi,%rdi), %ecx
movl $0x0, %ebp
testq %rcx, %rcx
movq 0x10(%rsp), %r9
je 0x686c
movq 0x9b8c(%rip), %rdx      # 0x103c8
movq 0x10(%rsi,%rdi), %rsi
xorl %edi, %edi
xorl %ebp, %ebp
xorl %r13d, %r13d
movl (%rsi,%rdi,8), %r8d
movzbl (%rdx,%r8), %r8d
cmpl $0x2, %r8d
je 0x6862
cmpl $0x1, %r8d
jne 0x6864
incl %r13d
jmp 0x6864
incl %ebp
incq %rdi
cmpq %rdi, %rcx
jne 0x6848
movq 0x9b1d(%rip), %rbx      # 0x10390
movq 0x30(%rsp), %rcx
movslq (%rcx,%rax,4), %r15
movq 0x10(%r9), %rax
movq 0x38(%rsp), %rcx
movzwl -0x2(%rcx,%r15,2), %ecx
decq %r15
movl (%rax,%rcx,8), %edi
callq 0x3513
movq %rbx, %rdi
leaq 0x4d7c(%rip), %rsi      # 0xb61c
movl %r13d, %edx
movl %ebp, %ecx
movl %r15d, %r8d
leaq 0x9b31(%rip), %r9       # 0x103e0
xorl %eax, %eax
callq 0x1150
movq 0x20(%rsp), %rax
movl 0x4(%rax,%r12), %eax
cmpl 0x9b82(%rip), %eax      # 0x10448
jge 0x67c7
movq 0x70(%rsp), %rdi
callq 0x1040
movq 0x68(%rsp), %rdi
callq 0x1040
movq 0x58(%rsp), %rdi
callq 0x1040
movq 0x30(%rsp), %rdi
callq 0x1040
movq 0x28(%rsp), %rdi
callq 0x1040
movq 0x40(%rsp), %rdi
callq 0x1040
movq 0x60(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8fd6
movq 0x50(%rsp), %rax
movl $0x0, (%rax)
testq %r14, %r14
je 0x6945
xorl %eax, %eax
movzwl (%r10,%rax,2), %ecx
movb $0x0, 0x6(%r12,%rcx,8)
incq %rax
cmpq %rax, %r14
jne 0x6932
movq 0x86a4(%rip), %rax      # 0xeff0
movq (%rax), %rdi
movq 0x9ac2(%rip), %rax      # 0x10418
movq 0x48(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
leaq 0x3f30(%rip), %rsi      # 0xa895
movl %r13d, %edx
xorl %eax, %eax
callq 0x1150
xorl %r9d, %r9d
movq 0x10(%rsp), %r8
jmp 0x64fb