// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r12
movq %rsi, %rdi
movq %rdx, 0x10(%rsp)
movq %rdx, %rsi
movq %rcx, %rdx
callq 0x48c2
movq %rax, %r15
movq 0x8(%rbx), %rdx
leaq 0x13f19(%rip), %rsi     # 0x18954
xorl %ebp, %ebp
movl $0x4, %edi
xorl %eax, %eax
callq 0x12c8e
xorl %eax, %eax
callq 0x12f02
cmpl $0x4, %eax
jl 0x4a69
movq 0x2259c(%rip), %rax     # 0x26ff8
movq (%rax), %rsi
movq 0x10(%rsp), %rdi
callq 0x487c
movq %r15, %rdi
callq 0x6674
leaq 0x13ef5(%rip), %rsi     # 0x1896d
movl $0x4, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x12c8e
testq %r15, %r15
je 0x4bec
leaq 0x1c(%rsp), %r13
movl $0x0, (%r13)
leaq 0x20(%rsp), %rbp
movq $0x0, (%rbp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x4945
movq 0x8(%r12), %rdi
movq (%rbp), %r12
movq %r12, %rsi
callq 0xc2ba
movq %rax, %r14
movq 0x8(%rbx), %rdx
movl (%r13), %ebp
movl 0x30(%rax), %r9d
leaq 0x13ea8(%rip), %rsi     # 0x18986
movl $0x4, %edi
movq %r12, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x12c8e
movl 0x30(%r14), %r9d
cmpl $0x1, %r9d
jg 0x4b1e
movq 0x8(%rbx), %rdx
leaq 0x13edb(%rip), %rsi     # 0x189e0
xorl %ebp, %ebp
movl $0x3, %edi
movq %r12, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x12c8e
jmp 0x4bec
callq 0x3780
cltd
idivl %ebp
movl %edx, %r14d
xorl %r13d, %r13d
movl %edx, 0xc(%rsp)
movq 0x8(%r15), %rbp
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq %r12, %rsi
callq 0x3440
movb $0x1, %cl
testl %eax, %eax
jne 0x4bb9
cmpl %r14d, %r13d
jne 0x4bb6
movq 0x8(%rbx), %r14
movq 0x10(%rsp), %rdi
callq 0x6674
movl $0x3, %edi
leaq 0x13eb2(%rip), %rsi     # 0x18a1b
movq %r14, %rdx
movq %r12, %rcx
movl %eax, %r8d
xorl %eax, %eax
callq 0x12c8e
movq 0x10(%rbp), %r14
movq 0x20(%r14), %rax
cmpq 0x20(%rbx), %rax
je 0x4ba2
movq 0x8(%r14), %rdx
movq 0x8(%rbx), %rcx
movl $0x3, %edi
leaq 0x13ea9(%rip), %rsi     # 0x18a44
xorl %eax, %eax
callq 0x12c8e
movq %rbx, 0x10(%rbp)
decl 0x30(%r14)
incl 0x30(%rbx)
xorl %ecx, %ecx
movl 0xc(%rsp), %r14d
jmp 0x4bb9
incl %r13d
testb %cl, %cl
je 0x4be7
movq (%r15), %r15
testq %r15, %r15
jne 0x4b30
leaq 0x13e93(%rip), %rdi     # 0x18a63
leaq 0x13ad0(%rip), %rsi     # 0x186a7
xorl %ebp, %ebp
movl $0x3ce, %edx            # imm = 0x3CE
xorl %eax, %eax
callq 0xb8d7
jmp 0x4bec
movl $0x1, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq