// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
leaq 0x28(%rdi), %r15
movq 0x8(%rdi), %r14
movq 0x28(%rdi), %rdi
callq 0x6082
leaq 0x1180b(%rip), %rsi     # 0x16e3c
pushq $0x3
popq %rbp
movl %ebp, %edi
movq %r14, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x110f9
pushq $0x1
popq %r14
leaq 0x1280e(%rip), %r12     # 0x17e5b
movl $0x0, 0x14(%rsp)
movl $0x0, 0x4(%rsp)
movq $0x0, 0x8(%rsp)
movq (%r15), %r15
testq %r15, %r15
je 0x5794
movq %r15, 0x20(%rsp)
movq 0x8(%r15), %rax
leaq 0x10(%rax), %r15
movq 0x8(%rax), %r13
movq 0x10(%rax), %rdi
callq 0x6082
movl %ebp, %edi
leaq 0x117d0(%rip), %rsi     # 0x16e65
movq %r13, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x110f9
movq %r15, %r13
movq (%r13), %r13
testq %r13, %r13
je 0x56c3
movq 0x8(%r13), %rax
movq 0x8(%rax), %rdx
movl %ebp, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x110f9
jmp 0x56a4
xorl %r13d, %r13d
movl %ebp, %edi
leaq 0x14df1(%rip), %rsi     # 0x1a4c0
xorl %eax, %eax
callq 0x110f9
xorl %ecx, %ecx
movq (%r15), %r15
testq %r15, %r15
je 0x5805
movq 0x8(%r15), %rax
cmpq %rbx, %rax
je 0x5700
movq 0x20(%rax), %rax
incl %ecx
testq %rax, %rax
cmovneq %rax, %r13
cmovnel %r14d, %ecx
jmp 0x56d8
xorl %eax, %eax
movq (%r15), %r15
testq %r15, %r15
je 0x571b
movq 0x8(%r15), %rdx
incl %eax
movq 0x20(%rdx), %rdx
testq %rdx, %rdx
je 0x5702
jmp 0x571d
xorl %edx, %edx
testq %r13, %r13
je 0x5755
cmpq $0x0, 0x8(%rsp)
sete %sil
cmpl 0x4(%rsp), %ecx
setl %dil
orb %sil, %dil
cmpb $0x1, %dil
movq 0x20(%rsp), %r15
jne 0x575a
movl $0x0, 0x14(%rsp)
movl %ecx, 0x4(%rsp)
movq %r13, 0x8(%rsp)
jmp 0x575a
movq 0x20(%rsp), %r15
testq %rdx, %rdx
je 0x5666
cmpq $0x0, 0x8(%rsp)
sete %cl
cmpl 0x4(%rsp), %eax
setl %sil
orb %cl, %sil
cmpb $0x1, %sil
jne 0x5666
movl %r14d, 0x14(%rsp)
movl %eax, 0x4(%rsp)
movq %rdx, 0x8(%rsp)
jmp 0x5666
cmpq $0x0, 0x8(%rsp)
je 0x57b8
cmpl $0x0, 0x14(%rsp)
je 0x57c4
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
callq 0x40b6
jmp 0x57d7
leaq 0x11745(%rip), %rcx     # 0x16f04
xorl %r14d, %r14d
jmp 0x57de
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %edx
callq 0x4098
movq %rax, %r14
movq 0x8(%rax), %rcx
movq 0x8(%rbx), %rdx
leaq 0x11702(%rip), %rsi     # 0x16eeb
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x110f9
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1166d(%rip), %rdi     # 0x16e79
leaq 0x10ea4(%rip), %rsi     # 0x166b7
leaq 0x11682(%rip), %rcx     # 0x16e9c
movl $0x65e, %edx            # imm = 0x65E
callq 0x32e0