// File: /workspace/github_c_makefile_star2_O1/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 0x6674
leaq 0x132b4(%rip), %rsi     # 0x18e2c
movl $0x3, %edi
movq %r14, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x12c8e
movq 0x28(%rbx), %rbp
testq %rbp, %rbp
je 0x5cd9
movq %r15, 0x8(%rsp)
leaq 0x142a9(%rip), %r14     # 0x19e4b
xorl %r12d, %r12d
xorl %edx, %edx
movl $0x0, 0x4(%rsp)
movq %r12, 0x10(%rsp)
movq %rdx, %r12
movq 0x8(%rbp), %r15
movq 0x8(%r15), %r13
movq 0x10(%r15), %rdi
callq 0x6674
movl $0x3, %edi
leaq 0x13281(%rip), %rsi     # 0x18e55
movq %r13, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x12c8e
movq 0x10(%r15), %r13
testq %r13, %r13
je 0x5c06
movq 0x8(%r13), %rax
movq 0x8(%rax), %rdx
movl $0x3, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x12c8e
movq (%r13), %r13
jmp 0x5be4
addq $0x10, %r15
xorl %r13d, %r13d
movl $0x3, %edi
leaq 0x168cb(%rip), %rsi     # 0x1c4e4
xorl %eax, %eax
callq 0x12c8e
xorl %eax, %eax
movq %r12, %rdx
movq 0x10(%rsp), %r12
movl $0x1, %edi
movq (%r15), %r15
testq %r15, %r15
je 0x5c55
movq 0x8(%r15), %rcx
cmpq %rbx, %rcx
je 0x5c50
movq 0x20(%rcx), %rsi
incl %eax
testq %rsi, %rsi
cmovneq %rsi, %r13
cmovnel %edi, %eax
cmpq %rbx, %rcx
jne 0x5c2f
testq %r15, %r15
je 0x5d53
cmpq %rbx, 0x8(%r15)
jne 0x5d53
xorl %ecx, %ecx
xorl %edi, %edi
movq (%r15), %r15
testq %r15, %r15
je 0x5c87
movq 0x8(%r15), %rsi
incl %edi
movq 0x20(%rsi), %rsi
testq %rsi, %rsi
cmovneq %rsi, %rcx
je 0x5c6c
testq %r13, %r13
je 0x5ca2
testq %r12, %r12
je 0x5c95
cmpl %edx, %eax
jge 0x5ca2
movl $0x0, 0x4(%rsp)
movl %eax, %edx
movq %r13, %r12
testq %rcx, %rcx
je 0x5cbd
testq %r12, %r12
je 0x5cb0
cmpl %edx, %edi
jge 0x5cbd
movl $0x1, 0x4(%rsp)
movl %edi, %edx
movq %rcx, %r12
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x5baf
cmpl $0x0, 0x4(%rsp)
sete %al
movq 0x8(%rsp), %r15
jmp 0x5ce0
movb $0x1, %al
xorl %r12d, %r12d
xorl %edx, %edx
testq %r12, %r12
je 0x5d05
testb %al, %al
je 0x5d0a
movq %r12, %r14
testl %edx, %edx
jle 0x5d18
movq %r12, %r14
movq (%r14), %r14
testq %r14, %r14
jne 0x5cff
movq 0x38(%r15), %r14
decl %edx
jne 0x5cf3
jmp 0x5d18
xorl %r14d, %r14d
jmp 0x5d18
movq %r15, %rdi
movq %r12, %rsi
callq 0x43fc
movq %rax, %r14
movq 0x8(%rbx), %rdx
testq %r12, %r12
je 0x5d27
movq 0x8(%r14), %rcx
jmp 0x5d2e
leaq 0x131c6(%rip), %rcx     # 0x18ef4
leaq 0x131a6(%rip), %rsi     # 0x18edb
movl $0x3, %edi
xorl %eax, %eax
callq 0x12c8e
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1310f(%rip), %rdi     # 0x18e69
leaq 0x12946(%rip), %rsi     # 0x186a7
leaq 0x13124(%rip), %rcx     # 0x18e8c
movl $0x65e, %edx            # imm = 0x65E
callq 0x32e0