// File: /workspace/github_c_makefile_star2_O3/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 0x481c
movq %rax, %r15
movq 0x8(%rbx), %rdx
leaq 0x13fc4(%rip), %rsi     # 0x18964
xorl %ebp, %ebp
movl $0x4, %edi
xorl %eax, %eax
callq 0x12a31
xorl %eax, %eax
callq 0x12caa
cmpl $0x4, %eax
jl 0x49cc
movq 0x22637(%rip), %rax     # 0x26ff8
movq (%rax), %rsi
movq %r14, %rdi
callq 0x47d6
movq %r15, %rdi
callq 0x65f4
leaq 0x13fa2(%rip), %rsi     # 0x1897d
movl $0x4, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x12a31
testq %r15, %r15
je 0x4b3a
movq %r14, (%rsp)
leaq 0xc(%rsp), %r13
movl $0x0, (%r13)
leaq 0x10(%rsp), %rbp
movq $0x0, (%rbp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x48ad
movq 0x8(%r12), %rdi
movq (%rbp), %r12
movq %r12, %rsi
callq 0xc0b1
movq %rax, %r14
movq 0x8(%rbx), %rdx
movl (%r13), %ebp
movl 0x30(%rax), %r9d
leaq 0x13f51(%rip), %rsi     # 0x18996
xorl %r13d, %r13d
movl $0x4, %edi
movq %r12, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x12a31
movl 0x30(%r14), %r9d
cmpl $0x1, %r9d
jg 0x4a88
movq 0x8(%rbx), %rdx
leaq 0x13f81(%rip), %rsi     # 0x189f0
xorl %ebp, %ebp
movl $0x3, %edi
movq %r12, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x12a31
jmp 0x4b3a
callq 0x3780
cltd
idivl %ebp
movl %edx, %ebp
movq 0x8(%r15), %r14
movq 0x10(%r14), %rax
movq 0x8(%rax), %rdi
movq %r12, %rsi
callq 0x3440
testl %eax, %eax
jne 0x4ab2
cmpl %ebp, %r13d
je 0x4ad8
incl %r13d
movq (%r15), %r15
testq %r15, %r15
jne 0x4a92
leaq 0x13fb2(%rip), %rdi     # 0x18a73
leaq 0x13bef(%rip), %rsi     # 0x186b7
xorl %ebp, %ebp
movl $0x3ce, %edx            # imm = 0x3CE
xorl %eax, %eax
callq 0xb6c8
jmp 0x4b3a
movq 0x8(%rbx), %r15
movq (%rsp), %rdi
callq 0x65f4
leaq 0x13f3f(%rip), %rsi     # 0x18a2b
movl $0x3, %edi
movq %r15, %rdx
movq %r12, %rcx
movl %eax, %r8d
xorl %eax, %eax
callq 0x12a31
movq 0x10(%r14), %r15
movq 0x20(%r15), %rax
cmpq 0x20(%rbx), %rax
je 0x4b2a
movq 0x8(%r15), %rdx
movq 0x8(%rbx), %rcx
leaq 0x13f36(%rip), %rsi     # 0x18a54
movl $0x3, %edi
xorl %eax, %eax
callq 0x12a31
movq %rbx, 0x10(%r14)
decl 0x30(%r15)
incl 0x30(%rbx)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq