// File: /workspace/github_c_makefile_star2_O2/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, 0x50(%rsp)
movq 0xa882(%rip), %rax      # 0x10398
movq 0x8(%rax), %r15
movslq 0x14(%rax), %rbx
movq %rdi, 0x20(%rsp)
movslq %edi, %r13
movslq 0x18(%rax), %rdi
pushq $0x4
popq %r14
movq %r14, %rsi
callq 0x8b24
movq %rax, 0x30(%rsp)
movq 0xa856(%rip), %rax      # 0x10398
movslq 0x18(%rax), %rdi
movq %r14, %rsi
callq 0x8b24
movq %rax, 0x60(%rsp)
pushq $0x40
popq %rdi
pushq $0x4
popq %rsi
callq 0x82c8
movq %rax, 0x38(%rsp)
movq 0xa8be(%rip), %rax      # 0x10428
movslq (%rax,%r13,4), %rdi
testq %rdi, %rdi
je 0x5e5c
movq %rbx, %rcx
imulq %r13, %rcx
movq %rcx, 0x10(%rsp)
movl %edi, %ebx
shlq $0x2, %rdi
movq %rdi, 0x48(%rsp)
callq 0x8afc
movq 0x10(%rsp), %r8
movq %rax, %r12
movq 0xa87e(%rip), %rax      # 0x10420
movq (%rax,%r13,8), %r9
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %esi
cmovgl %ebx, %esi
movq %r15, %rdi
cmpq %rax, %rsi
je 0x5bce
movq 0x10(%rdi,%r8), %rcx
movzwl (%r9,%rax,2), %edx
movb $0x0, 0x6(%rcx,%rdx,8)
incq %rax
jmp 0x5bb5
movq %rsi, 0x58(%rsp)
movq %r13, 0x70(%rsp)
andl $0x0, 0x8(%rdi,%r8)
andq $0x0, 0x18(%rdi,%r8)
xorl %ebx, %ebx
movl $0x0, 0x18(%rsp)
movq %rdi, 0x28(%rsp)
movq %r9, 0x68(%rsp)
cmpq 0x58(%rsp), %rbx
je 0x5d9b
movq %rbx, %rbp
movq 0x10(%rdi,%r8), %rax
movzwl (%r9,%rbx,2), %ecx
movl (%rax,%rcx,8), %r15d
movq 0xa775(%rip), %rax      # 0x10390
movq 0x8(%rax), %rcx
movl 0x14(%rax), %eax
imull %r15d, %eax
movl (%rcx,%rax), %r13d
movq %r12, %rdi
xorl %esi, %esi
movq 0x48(%rsp), %rdx
callq 0x10f0
movq 0x60(%rsp), %r8
movq 0xa783(%rip), %rax      # 0x103c8
incq %rbx
xorl %ecx, %ecx
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %r9
cmpq %rcx, %r13
je 0x5c8f
movq 0x8(%rax), %rdx
movl 0x14(%rax), %esi
imull %r15d, %esi
movq (%rdx,%rsi), %rdx
movslq (%rdx,%rcx,4), %rdx
cmpl %r9d, %edx
je 0x5c8a
movl (%r8,%rdx,4), %esi
testl %esi, %esi
jne 0x5c7f
movl %ebx, (%r8,%rdx,4)
movl %ebx, %esi
movslq %esi, %rsi
incl -0x4(%r12,%rsi,4)
incl (%rdi,%rdx,4)
incq %rcx
jmp 0x5c54
xorl %eax, %eax
xorl %r15d, %r15d
xorl %ecx, %ecx
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %r8
movq 0x68(%rsp), %r9
cmpq %rax, %rbp
je 0x5cbf
movl (%r12,%rax,4), %edx
cmpl %ecx, %edx
cmovgl %eax, %r14d
cmovgl %edx, %ecx
addl %edx, %r15d
incq %rax
jmp 0x5ca5
cmpl 0x50(%rsp), %ecx
jl 0x5bf8
movq 0x10(%rdi,%r8), %r10
movslq %r14d, %rax
movzwl (%r9,%rax,2), %r11d
movb 0x6(%r10,%r11,8), %al
testb %al, %al
je 0x5cee
movq %r10, %rcx
movq 0x38(%rsp), %rsi
movl 0x18(%rsp), %r13d
jmp 0x5d52
movl 0x8(%rdi,%r8), %esi
incl %esi
movl %esi, 0x8(%rdi,%r8)
cmpl $0x100, %esi            # imm = 0x100
jae 0x618d
movb %sil, 0x6(%r10,%r11,8)
movl 0x18(%rsp), %r13d
incl %r13d
movq 0x38(%rsp), %rdi
movq %r10, 0x18(%rsp)
movq %r11, 0x40(%rsp)
callq 0x8560
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %r11
movq 0x18(%rsp), %r10
movq 0x68(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x28(%rsp), %rdi
movl %r14d, (%rax)
movb 0x6(%r10,%r11,8), %al
movq 0x10(%rdi,%r8), %rcx
movzwl (%r9,%rbp,2), %edx
movb %al, 0x6(%rcx,%rdx,8)
incl %r13d
movl %r13d, 0x18(%rsp)
movq 0x8(%rsi), %rax
movzbl 0x6(%r10,%r11,8), %ecx
movslq 0x14(%rsi), %rdx
imulq %rcx, %rdx
movslq (%rax,%rdx), %rax
cvtsi2sdl (%r12,%rax,4), %xmm0
cvtsi2sd %r15d, %xmm1
divsd %xmm1, %xmm0
addsd 0x18(%rdi,%r8), %xmm0
movsd %xmm0, 0x18(%rdi,%r8)
jmp 0x5bf8
movq 0x70(%rsp), %rbx
movl 0x18(%rsp), %ebp
movl 0x4(%rdi,%r8), %ecx
movl 0x8(%rdi,%r8), %eax
movq 0xa5eb(%rip), %rdi      # 0x103a0
movq 0xa66c(%rip), %rdx      # 0x10428
movl (%rdx,%rbx,4), %r8d
testl %eax, %eax
je 0x5ddf
movl %eax, (%rsp)
leaq 0x475a(%rip), %rsi      # 0xa528
movq 0x20(%rsp), %rdx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1150
jmp 0x5df2
leaq 0x478c(%rip), %rsi      # 0xa572
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x1150
movq 0x28(%rsp), %rbx
movq 0x10(%rsp), %rax
cmpl $0x0, 0x8(%rbx,%rax)
je 0x5e5c
movl (%rbx,%rax), %edi
pushq $0x4
popq %r14
movq %r14, %rsi
callq 0x8b24
movq %rax, %r15
movq 0x10(%rsp), %rax
movl 0x8(%rbx,%rax), %edi
incl %edi
shlq $0x2, %rdi
callq 0x8afc
movq %rax, %rbp
movq 0x10(%rsp), %rax
movl 0x8(%rbx,%rax), %edi
incl %edi
movq %r14, %rsi
callq 0x8b24
movq 0x10(%rsp), %r10
xorl %edx, %edx
movq %rbx, %r9
movl 0x8(%r9,%r10), %ecx
cmpq %rcx, %rdx
ja 0x5e6b
movl %edx, (%rbp,%rdx,4)
incq %rdx
jmp 0x5e49
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r11d, %r11d
pushq $0x1
popq %rcx
movl 0x4(%r9,%r10), %edx
cmpq %rdx, %r11
jae 0x5edf
movq 0x10(%r9,%r10), %rdx
movzbl 0x6(%rdx,%r11,8), %esi
movl (%rbp,%rsi,4), %edi
testl %edi, %edi
je 0x5eda
movzwl 0x4(%rdx,%r11,8), %esi
movslq (%r15,%rsi,4), %rdx
movl (%rbp,%rdx,4), %edx
cmpl %edx, %edi
je 0x5eda
testl %edx, %edx
je 0x5ed6
cmpl %edx, %edi
movl %edx, %esi
cmovll %edi, %esi
cmovgl %edi, %edx
movq %rcx, %rdi
movl 0x8(%r9,%r10), %r8d
cmpq %r8, %rdi
ja 0x5eca
cmpl %edx, (%rbp,%rdi,4)
jne 0x5ec5
movl %esi, (%rbp,%rdi,4)
incq %rdi
jmp 0x5eb1
movslq %edx, %rdx
movl $0x1, (%rax,%rdx,4)
jmp 0x5eda
movl %edi, (%r15,%rsi,4)
incq %r11
jmp 0x5e71
pushq $0x1
popq %rdx
movl 0x8(%r9,%r10), %ecx
cmpq %rcx, %rdx
ja 0x5efd
movl -0x4(%rax,%rdx,4), %ecx
subl (%rax,%rdx,4), %ecx
incl %ecx
movl %ecx, (%rax,%rdx,4)
incq %rdx
jmp 0x5ee2
pushq $0x1
popq %rdx
movl %ecx, %ecx
cmpq %rcx, %rdx
ja 0x5f1d
movslq (%rbp,%rdx,4), %rcx
movl (%rax,%rcx,4), %ecx
movl %ecx, (%rbp,%rdx,4)
incq %rdx
movl 0x8(%r9,%r10), %ecx
jmp 0x5f00
movl (%rax,%rcx,4), %edx
movl %edx, 0x8(%r9,%r10)
movl 0x4(%r9,%r10), %r8d
xorl %ecx, %ecx
cmpq %rcx, %r8
je 0x5f4a
movq 0x10(%r9,%r10), %rsi
movzbl 0x6(%rsi,%rcx,8), %edi
movb (%rbp,%rdi,4), %dil
movb %dil, 0x6(%rsi,%rcx,8)
incq %rcx
jmp 0x5f2c
movq %rax, 0x48(%rsp)
movq %r15, 0x40(%rsp)
movq 0xa445(%rip), %rdi      # 0x103a0
leaq 0x39bc(%rip), %rsi      # 0x991e
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1150
movq 0x9056(%rip), %rax      # 0xefc8
movq (%rax), %rdi
callq 0x11a0
pushq $0x4
popq %rsi
movl $0x100, %edi            # imm = 0x100
callq 0x82c8
movq %rax, %r14
pushq $0x8
popq %rsi
movl $0x400, %edi            # imm = 0x400
callq 0x82c8
movq %rax, %r13
movq 0x30(%rsp), %rdx
movq 0xa3f2(%rip), %rax      # 0x10398
movslq 0x18(%rax), %rax
cmpq %rax, %rbx
jge 0x5fe5
movl (%rdx,%rbx,4), %esi
movq %r14, %rdi
callq 0x8560
movq 0x30(%rsp), %rdx
incl (%rax)
cmpl $0x0, (%rdx,%rbx,4)
jle 0x5fe0
movl 0x18(%r13), %esi
movq %r13, %rdi
callq 0x8560
movq 0x30(%rsp), %rdx
movl %ebx, (%rax)
movl (%rdx,%rbx,4), %ecx
movl %ecx, 0x4(%rax)
incq %rbx
jmp 0x5f9f
leaq 0x45bc(%rip), %rdi      # 0xa5a8
movq %r14, %rsi
callq 0x20fe
movq %r14, %rdi
callq 0x8408
movq 0x20(%rsp), %rdi
leal 0x1(%rdi), %esi
callq 0x3f30
movq 0x8(%r13), %rdi
movslq 0x18(%r13), %rsi
movslq 0x14(%r13), %rdx
leaq 0x255(%rip), %rcx       # 0x6271
callq 0x1090
movl 0x18(%r13), %ecx
movq %r13, 0x50(%rsp)
subl $0x1, %ecx
jb 0x613d
movq 0x8(%r13), %r14
movslq 0x14(%r13), %rax
movl %ecx, 0x20(%rsp)
movslq %ecx, %r13
imulq %rax, %r13
movq 0xa353(%rip), %rdi      # 0x103a0
movl (%r14,%r13), %edx
movl 0x4(%r14,%r13), %ecx
xorl %ebx, %ebx
leaq 0x4555(%rip), %rsi      # 0xa5b4
xorl %eax, %eax
callq 0x1150
movq 0xa32b(%rip), %rdx      # 0x10398
movq 0x8(%rdx), %rcx
movq %r14, 0x58(%rsp)
movq %r13, %r15
movslq (%r14,%r13), %rax
movslq 0x14(%rdx), %rdx
imulq %rax, %rdx
movl 0x4(%rcx,%rdx), %esi
movq 0xa348(%rip), %rdi      # 0x103d8
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %r10
cmpq %rbx, %rsi
je 0x60cc
movq 0x10(%rcx,%rdx), %r8
movl (%r8,%rbx,8), %r8d
movzbl (%rdi,%r8), %r8d
cmpl $0x2, %r8d
je 0x60c4
cmpl $0x1, %r8d
jne 0x60c7
incl %r13d
jmp 0x60c7
incl %r14d
incq %rbx
jmp 0x60a0
movq 0xa2cd(%rip), %rcx      # 0x103a0
movq %rcx, 0x18(%rsp)
movq 0x60(%rsp), %rcx
movslq (%rcx,%rax,4), %rbx
movq 0x10(%r9,%r10), %rax
movq 0x68(%rsp), %rcx
movzwl -0x2(%rcx,%rbx,2), %ecx
decq %rbx
movl (%rax,%rcx,8), %edi
callq 0x2a97
movq 0x18(%rsp), %rdi
leaq 0x44d5(%rip), %rsi      # 0xa5dc
movl %r13d, %edx
movl %r14d, %ecx
movl %ebx, %r8d
leaq 0xa2d9(%rip), %r9       # 0x103f0
xorl %eax, %eax
callq 0x1150
movq 0x58(%rsp), %rax
movl 0x4(%rax,%r15), %eax
cmpl 0xa32a(%rip), %eax      # 0x10458
movq 0x50(%rsp), %r13
movl 0x20(%rsp), %ecx
jge 0x602a
movq 0x40(%rsp), %rdi
callq 0x1040
movq %rbp, %rdi
callq 0x1040
movq 0x48(%rsp), %rdi
callq 0x1040
movq 0x60(%rsp), %rdi
callq 0x1040
movq 0x30(%rsp), %rdi
callq 0x1040
movq %r12, %rdi
callq 0x1040
movq 0x38(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8408
andl $0x0, 0x8(%rdi,%r8)
xorl %eax, %eax
cmpq %rax, %rbp
je 0x61aa
movzwl (%r9,%rax,2), %ecx
movb $0x0, 0x6(%r10,%rcx,8)
incq %rax
jmp 0x6195
movq 0x8e3f(%rip), %rax      # 0xeff0
movq (%rax), %rdi
movq 0xa26d(%rip), %rax      # 0x10428
movq 0x70(%rsp), %rbx
movl (%rax,%rbx,4), %ecx
leaq 0x368b(%rip), %rsi      # 0x9855
xorl %ebp, %ebp
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x1150
movq 0x10(%rsp), %r8
movq 0x28(%rsp), %rdi
jmp 0x5da4