// File: /workspace/github_c_makefile_star2_O3/JimKent[P]linearSat/linearSat/linearSat.c
// Function: fillInTypeFromReads
struct monomerType *fillInTypeFromReads(struct monomer *monomer, struct alphaStore *store)
/* Look through read list trying to find best supported type for this monomer. */
{
/* The algorithm here could be better.  It picks the closest nearby monomer that is typed
 * to fill in from.  When have information from both before and after the program arbitrarily 
 * uses before preferentially.  When have information that is equally close from multiple reads,
 * it just uses info from first read.  Fortunately the data, at least on Y, shows a lot of
 * consistency, so it's probably not actually worth a more sophisticated algorithm that would
 * instead take the most popular choice rather than the first one. */
verbose(3, "fillInTypeFromReads on %s from %d reads\n", monomer->word, slCount(monomer->readList));
struct monomerType *bestType = NULL;
int bestPos = 0;
boolean bestIsAfter = FALSE;
struct slRef *readRef;
for (readRef = monomer->readList; readRef != NULL; readRef = readRef->next)
    {
    struct alphaRead *read = readRef->val;
    struct monomerRef *ref;
	{
	verbose(3, "  read %s (%d els):", read->name, slCount(read->list));
	for (ref = read->list; ref != NULL; ref = ref->next)
	    {
	    struct monomer *m = ref->val;
	    verbose(3, " %s", m->word);
	    }
	verbose(3, "\n");
	}
    struct monomerType *beforeType = NULL;
    int beforePos = 0;

    /* Look before. */
    for (ref = read->list; ref != NULL; ref = ref->next)
        {
	struct monomer *m = ref->val;
	if (m == monomer)
	     break;
	if (m->type != NULL)
	    {
	    beforeType = m->type;
	    beforePos = 0;
	    }
	++beforePos;
	}

    /* Look after. */
    struct monomerType *afterType = NULL;
    int afterPos = 0;
    assert(ref != NULL && ref->val == monomer);   
    for (ref = ref->next; ref != NULL; ref = ref->next)
        {
	struct monomer *m = ref->val;
	++afterPos;
	if (m->type != NULL)
	    {
	    afterType = m->type;
	    break;
	    }
	}

    if (beforeType != NULL)
        {
	if (bestType == NULL || beforePos < bestPos)
	    {
	    bestType = beforeType;
	    bestPos = beforePos;
	    bestIsAfter = FALSE;
	    }
	}

    if (afterType != NULL)
        {
	if (bestType == NULL || afterPos < bestPos)
	    {
	    bestType = afterType;
	    bestPos = afterPos;
	    bestIsAfter = TRUE;
	    }
	}
    }

/* Now have found a type that is at a known position relative to ourselves.  From this
 * infer our own type. */
struct monomerType *chosenType = NULL;
if (bestType != NULL)
    {
    if (bestIsAfter)
	chosenType = typeBefore(store, bestType, bestPos);
    else
	chosenType = typeAfter(store, bestType, bestPos);
    }
verbose(3, "chosenType for %s is %s\n", monomer->word, 
    (bestType == NULL ? "(null)" : chosenType->name));
return chosenType;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rdi), %r14
movq 0x28(%rdi), %rdi
callq 0x65f4
leaq 0x1331a(%rip), %rsi     # 0x18e3c
movl $0x3, %edi
movq %r14, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x12a31
movq 0x28(%rbx), %r14
leaq 0x133c6(%rip), %rcx     # 0x18f04
testq %r14, %r14
je 0x5c98
movq %r15, 0x8(%rsp)
movl $0x1, %r15d
leaq 0x14302(%rip), %r12     # 0x19e5b
xorl %r13d, %r13d
xorl %edx, %edx
movl $0x0, (%rsp)
movl %edx, 0x4(%rsp)
movq %r13, 0x10(%rsp)
movq 0x8(%r14), %rbp
movq 0x8(%rbp), %r13
movq 0x10(%rbp), %rdi
callq 0x65f4
movl $0x3, %edi
leaq 0x132da(%rip), %rsi     # 0x18e65
movq %r13, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x12a31
movq 0x10(%rbp), %r13
testq %r13, %r13
je 0x5bbd
movq 0x8(%r13), %rax
movq 0x8(%rax), %rdx
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x12a31
movq (%r13), %r13
jmp 0x5b9b
movl $0x3, %edi
leaq 0x168f7(%rip), %rsi     # 0x1c4c0
xorl %eax, %eax
callq 0x12a31
movq 0x10(%rbp), %rdi
testq %rdi, %rdi
movq 0x10(%rsp), %r13
movl 0x4(%rsp), %edx
je 0x5cf0
xorl %eax, %eax
xorl %r8d, %r8d
movq 0x8(%rdi), %rcx
cmpq %rbx, %rcx
je 0x5c12
movq 0x20(%rcx), %rcx
incl %eax
testq %rcx, %rcx
cmovneq %rcx, %r8
cmovnel %r15d, %eax
movq (%rdi), %rdi
testq %rdi, %rdi
jne 0x5beb
jmp 0x5cf0
xorl %ecx, %ecx
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x5c2d
movq 0x8(%rdi), %rsi
incl %ecx
movq 0x20(%rsi), %rsi
testq %rsi, %rsi
je 0x5c14
jmp 0x5c2f
xorl %esi, %esi
testq %r8, %r8
je 0x5c49
testq %r13, %r13
je 0x5c3d
cmpl %edx, %eax
jge 0x5c49
movl $0x0, (%rsp)
movl %eax, %edx
movq %r8, %r13
testq %rsi, %rsi
je 0x5c6f
testq %r13, %r13
sete %al
cmpl %edx, %ecx
setl %dil
orb %al, %dil
cmpb $0x1, %dil
jne 0x5c6f
movl $0x1, (%rsp)
movl %ecx, %edx
movq %rsi, %r13
movq (%r14), %r14
testq %r14, %r14
jne 0x5b65
testq %r13, %r13
je 0x5c9d
cmpl $0x0, (%rsp)
je 0x5ca9
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x437f
movq %rax, %r13
jmp 0x5cc3
xorl %r13d, %r13d
jmp 0x5cc7
xorl %r13d, %r13d
leaq 0x1325d(%rip), %rcx     # 0x18f04
jmp 0x5cc7
testl %edx, %edx
movq 0x8(%rsp), %rax
jle 0x5cc3
movq (%r13), %r13
testq %r13, %r13
jne 0x5cbf
movq 0x38(%rax), %r13
decl %edx
jne 0x5cb2
movq 0x8(%r13), %rcx
movq 0x8(%rbx), %rdx
leaq 0x13219(%rip), %rsi     # 0x18eeb
movl $0x3, %edi
xorl %eax, %eax
callq 0x12a31
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13182(%rip), %rdi     # 0x18e79
leaq 0x129b9(%rip), %rsi     # 0x186b7
leaq 0x13197(%rip), %rcx     # 0x18e9c
movl $0x65e, %edx            # imm = 0x65E
callq 0x32e0