// File: /workspace/github_c_makefile_star2_O1/JimKent[P]linearSat/linearSat/linearSat.c
// Function: pickWeightedRandomFromList
struct wordTree *pickWeightedRandomFromList(struct wordTree *list)
/* Pick word from list randomly, but so that words with higher outTargets
 * are picked more often. */
{
struct wordTree *picked = NULL;

/* Debug output. */
    {
    verbose(4, "   pickWeightedRandomFromList(");
    struct wordTree *wt;
    for (wt = list; wt != NULL; wt = wt->next)
        {
	if (wt != list)
	    verbose(4, " ");
	verbose(4, "%s:%d", wt->monomer->word, wt->outTarget);
	}
    verbose(4, ") total %d\n", wordTreeSumOutTargets(list));
    }

/* Figure out total number of outputs left, and a random number between 0 and that total. */
int total = wordTreeSumOutTargets(list);
if (total > 0)
    {
    int threshold = rand() % total; 
    verbose(4, "      threshold %d\n", threshold);

    /* Loop through list returning selection corresponding to random threshold. */
    int binStart = 0;
    struct wordTree *wt;
    for (wt = list; wt != NULL; wt = wt->next)
	{
	int size = wt->outTarget;
	int binEnd = binStart + size;
	verbose(4, "      %s size %d, binEnd %d\n", wt->monomer->word, size, binEnd);
	if (threshold < binEnd)
	    {
	    picked = wt;
	    verbose(4, "      picked %s\n", wt->monomer->word);
	    break;
	    }
	binStart = binEnd;
	}
    }
return picked;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x146d1(%rip), %rsi     # 0x186fa
movl $0x4, %edi
xorl %eax, %eax
callq 0x12c8e
testq %rbx, %rbx
je 0x4085
leaq 0x146d8(%rip), %r14     # 0x18719
leaq 0x1788c(%rip), %r15     # 0x1b8d4
movq %rbx, %r12
cmpq %rbx, %r12
je 0x405f
movl $0x4, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x12c8e
movq 0x18(%r12), %rax
movq 0x8(%rax), %rdx
movl 0x24(%r12), %ecx
movl $0x4, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x12c8e
movq (%r12), %r12
testq %r12, %r12
jne 0x404b
testq %rbx, %rbx
je 0x40c6
xorl %edx, %edx
movq %rbx, %rax
addl 0x24(%rax), %edx
movq (%rax), %rax
testq %rax, %rax
jne 0x408f
leaq 0x1467e(%rip), %rsi     # 0x1871f
xorl %ebp, %ebp
movl $0x4, %edi
xorl %eax, %eax
callq 0x12c8e
testq %rbx, %rbx
je 0x40dd
xorl %ebp, %ebp
movq %rbx, %rax
addl 0x24(%rax), %ebp
movq (%rax), %rax
testq %rax, %rax
jne 0x40b9
jmp 0x40dd
leaq 0x14652(%rip), %rsi     # 0x1871f
xorl %ebp, %ebp
movl $0x4, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x12c8e
testl %ebp, %ebp
jle 0x4170
callq 0x3780
cltd
idivl %ebp
movl %edx, %ebp
leaq 0x14635(%rip), %rsi     # 0x1872b
movl $0x4, %edi
xorl %eax, %eax
callq 0x12c8e
testq %rbx, %rbx
je 0x4170
leaq 0x14631(%rip), %r14     # 0x1873f
xorl %r12d, %r12d
xorl %r15d, %r15d
movl 0x24(%rbx), %ecx
leal (%rcx,%r15), %r13d
movq 0x18(%rbx), %rax
movq 0x8(%rax), %rdx
movl $0x4, %edi
movq %r14, %rsi
movl %r13d, %r8d
xorl %eax, %eax
callq 0x12c8e
movl %r13d, %eax
cmpl %r13d, %ebp
jge 0x415e
movq 0x18(%rbx), %rax
movq 0x8(%rax), %rdx
movl $0x4, %edi
leaq 0x1460b(%rip), %rsi     # 0x1875c
xorl %eax, %eax
callq 0x12c8e
movl %r15d, %eax
movq %rbx, %r12
cmpl %r13d, %ebp
jl 0x4173
movq (%rbx), %rbx
movl %eax, %r15d
testq %rbx, %rbx
jne 0x4114
jmp 0x4173
xorl %r12d, %r12d
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq