// File: /workspace/github_c_makefile_star2_O2/JimKent[P]linearSat/linearSat/linearSat.c
// Function: subCommonCenter
boolean subCommonCenter(struct alphaStore *store,
    struct monomer *center, struct monomerRef *neighborhood, struct dlList *ll)
/* Scan list for places where have all items in neighborhood (except for center) matching. 
 * Substitute in center at one of these places chosen at random and return TRUE if possible. */
{
struct slRef *centerRefList = refsToPossibleCenters(center, neighborhood, ll);
verbose(4, "sub %s in neighborhood: ", center->word);
if (verboseLevel() >= 4)
    printMonomerRefList(neighborhood, stderr);
verbose(4, "Got %d possible centers\n", slCount(centerRefList));

if (centerRefList == NULL)
    return FALSE;
int commonCount = 0;
char *commonWord = NULL;
mostCommonMonomerWord(centerRefList, &commonWord, &commonCount);
struct monomer *commonMonomer = hashFindVal(store->monomerHash, commonWord);
verbose(4, "Commonest word to displace with %s is %s which occurs %d times in context and %d overall\n", center->word, commonWord, commonCount, commonMonomer->subbedOutCount);
if (commonMonomer->subbedOutCount < 2)
    {
    verbose(3, "Want to substitute %s for %s, but %s only occurs %d time.\n", 
	center->word, commonWord, commonWord, commonMonomer->subbedOutCount);
    return FALSE;
    }

/* Select a random one of the most commonly occuring possible centers. */
int targetIx = rand() % commonCount;
struct slRef *ref;
int currentIx = 0;
for (ref = centerRefList; ref != NULL; ref = ref->next)
    {
    struct dlNode *node = ref->val;
    struct monomer *monomer = node->val;
    if (sameString(monomer->word, commonWord))
         {
	 if (currentIx == targetIx)
	     {
	     verbose(3, "Substituting %s for %s in context of %d\n", center->word, commonWord, slCount(neighborhood));
	     struct monomer *oldCenter = node->val;
	     if (oldCenter->type != center->type)
		 verbose(3, "Type mismatch subbig %s vs %s\n", oldCenter->word, center->word);
	     node->val = center;
	     oldCenter->subbedOutCount -= 1;
	     center->subbedOutCount += 1;
	     return TRUE;
	     }
	 ++currentIx;
	 }
    }
internalErr();	// Should not get here.
return FALSE;	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
movq %rsi, %rdi
movq %rdx, %rsi
movq %rcx, %rdx
callq 0x4503
movq %rax, %r15
movq 0x8(%rbx), %rdx
leaq 0x12339(%rip), %rsi     # 0x16964
xorl %ebp, %ebp
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x110f9
xorl %eax, %eax
callq 0x1135c
cmpl $0x4, %eax
jl 0x4655
movq 0x219ae(%rip), %rax     # 0x25ff8
movq (%rax), %rsi
movq %r14, %rdi
callq 0x44c2
movq %r15, %rdi
callq 0x6082
leaq 0x12319(%rip), %rsi     # 0x1697d
pushq $0x4
popq %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x110f9
testq %r15, %r15
je 0x47b3
movq %r14, (%rsp)
leaq 0xc(%rsp), %r13
andl $0x0, (%r13)
leaq 0x10(%rsp), %rbp
andq $0x0, (%rbp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x4555
movq 0x8(%r12), %rdi
movq (%rbp), %r12
movq %r12, %rsi
callq 0xaf0d
movq %rax, %r14
movq 0x8(%rbx), %rdx
movl (%r13), %ebp
movl 0x30(%rax), %r9d
leaq 0x122d0(%rip), %rsi     # 0x16996
xorl %r13d, %r13d
pushq $0x4
popq %rdi
movq %r12, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x110f9
movl 0x30(%r14), %r9d
cmpl $0x1, %r9d
jg 0x4705
movq 0x8(%rbx), %rdx
leaq 0x12302(%rip), %rsi     # 0x169f0
xorl %ebp, %ebp
pushq $0x3
popq %rdi
movq %r12, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x110f9
jmp 0x47b3
callq 0x3780
cltd
idivl %ebp
movl %edx, %ebp
testq %r15, %r15
je 0x4739
movq 0x8(%r15), %r14
movq 0x10(%r14), %rax
movq 0x8(%rax), %rdi
movq %r12, %rsi
callq 0x3440
testl %eax, %eax
jne 0x4734
cmpl %ebp, %r13d
je 0x4757
incl %r13d
movq (%r15), %r15
jmp 0x470f
leaq 0x12333(%rip), %rdi     # 0x16a73
leaq 0x11f70(%rip), %rsi     # 0x166b7
xorl %ebp, %ebp
movl $0x3ce, %edx            # imm = 0x3CE
xorl %eax, %eax
callq 0xa61f
jmp 0x47b3
movq 0x8(%rbx), %r15
movq (%rsp), %rdi
callq 0x6082
leaq 0x122c0(%rip), %rsi     # 0x16a2b
pushq $0x3
popq %rdi
movq %r15, %rdx
movq %r12, %rcx
movl %eax, %r8d
xorl %eax, %eax
callq 0x110f9
movq 0x10(%r14), %r15
movq 0x20(%r15), %rax
cmpq 0x20(%rbx), %rax
je 0x47a5
movq 0x8(%r15), %rdx
movq 0x8(%rbx), %rcx
leaq 0x122b9(%rip), %rsi     # 0x16a54
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x110f9
movq %rbx, 0x10(%r14)
decl 0x30(%r15)
incl 0x30(%rbx)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq