// File: /workspace/github_c_makefile_star2_O3/JimKent[P]linearSat/linearSat/linearSat.c
// Function: makeMarkovChains
struct wordTree *makeMarkovChains(struct alphaStore *store)
/* Return wordTree containing all words, and also all chains-of-words of length 
 * chainSize seen in file.  */
{
/* We'll build up the tree starting with an empty root node. */
struct wordTree *wt = wordTreeNew(alphaStoreAddMonomer(store, ""));	
int chainSize = store->maxChainSize;

/* Loop through each read. There's special cases at the beginning and end of read, and for 
 * short reads.  In the main case we'll be maintaining a chain (doubly linked list) of maxChainSize 
 * words, * popping off one word from the start, and adding one word to the end for each
 * new word we encounter. This list is added to the tree each iteration. */
struct alphaRead *read;
for (read = store->readList; read != NULL; read = read->next)
    {
    /* We'll keep a chain of three or so words in a doubly linked list. */
    struct dlNode *node;
    struct dlList *chain = dlListNew();
    int curSize = 0;
    int wordCount = 0;

    struct monomerRef *ref;
    for (ref = read->list; ref != NULL; ref = ref->next)
        {
	struct monomer *monomer = ref->val;
	 /* For the first few words in the file after ID, we'll just build up the chain,
	 * only adding it to the tree when we finally do get to the desired
	 * chain size.  Once past the initial section of the file we'll be
	 * getting rid of the first link in the chain as well as adding a new
	 * last link in the chain with each new word we see. */
	if (curSize < chainSize)
	    {
	    dlAddValTail(chain, monomer);
	    ++curSize;
	    if (curSize == chainSize)
		addChainToTree(wt, chain);
	    }
	else
	    {
	    /* Reuse doubly-linked-list node, but give it a new value, as we move
	     * it from head to tail of list. */
	    node = dlPopHead(chain);
	    node->val = monomer;
	    dlAddTail(chain, node);
	    addChainToTree(wt, chain);
	    }
	++wordCount;
	}
    /* Handle last few words in line, where can't make a chain of full size.  Also handles       
    * lines that have fewer than chain size words. */
    if (curSize < chainSize)
 	addChainToTree(wt, chain);
    while ((node = dlPopHead(chain)) != NULL)
	{
	if (!dlEmpty(chain))
	    addChainToTree(wt, chain);
	freeMem(node);
	}
    dlListFree(&chain);
    }
wordTreeSort(wt);  // Make output of chain file prettier
return wt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
leaq 0x14720(%rip), %rsi     # 0x19cd2
callq 0x4e43
movq %rax, %r15
movl $0x38, %edi
callq 0xee10
movq %rax, %rbx
movq %r15, 0x18(%rax)
movq 0x10(%r14), %r12
testq %r12, %r12
je 0x56b7
movl 0x30(%r14), %ebp
xorl %r13d, %r13d
xorl %eax, %eax
callq 0xa904
movq %rax, (%rsp)
movq 0x10(%r12), %r14
testq %r14, %r14
je 0x564c
xorl %r13d, %r13d
movq 0x8(%r14), %r15
movq (%rsp), %rdi
cmpl %ebp, %r13d
jge 0x5619
movq %r15, %rsi
callq 0xaaa3
incl %r13d
movl %ebp, %r15d
cmpl %ebp, %r13d
je 0x5631
jmp 0x5640
callq 0xab1e
movq %r15, 0x10(%rax)
movq (%rsp), %rdi
movq %rax, %rsi
callq 0xa9f5
movl %r13d, %r15d
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x3b61
movl %r15d, %r13d
movq (%r14), %r14
testq %r14, %r14
jne 0x55f7
movq (%rsp), %rax
cmpl %ebp, %r13d
jge 0x5660
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3b61
movq (%rsp), %rax
movq %rax, %rdi
callq 0xab1e
testq %rax, %rax
je 0x56a2
movq %rax, %r15
movq (%rsp), %rdi
callq 0xac63
testl %eax, %eax
jne 0x5689
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x3b61
movq %r15, %rdi
callq 0xeebc
movq (%rsp), %rdi
callq 0xab1e
movq %rax, %r15
testq %rax, %rax
jne 0x5670
movq %rsp, %rdi
callq 0xa959
movq (%r12), %r12
testq %r12, %r12
jne 0x55dc
movq %rbx, %rdi
callq 0x56d1
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq