// File: /workspace/github_c_makefile_star2_O2/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, %rbx
leaq 0x12b7a(%rip), %rsi     # 0x17cd2
callq 0x4a68
movq %rax, %rdi
callq 0x38e3
movq %rax, %r14
movl 0x30(%rbx), %ebp
addq $0x10, %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x5233
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x9a0d
movq %rax, (%rsp)
leaq 0x10(%rbx), %r13
movq (%r13), %r13
testq %r13, %r13
je 0x51e1
movq 0x8(%r13), %r12
movq (%rsp), %rdi
cmpl %ebp, %r15d
jge 0x51b8
movq %r12, %rsi
callq 0x9b9f
incl %r15d
movl %ebp, %r12d
cmpl %ebp, %r15d
jne 0x518d
jmp 0x51d0
callq 0x9c18
movq %r12, 0x10(%rax)
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x9af7
movl %r15d, %r12d
movq (%rsp), %rsi
movq %r14, %rdi
callq 0x397e
movl %r12d, %r15d
jmp 0x518d
cmpl %ebp, %r15d
jge 0x5218
movq (%rsp), %rsi
movq %r14, %rdi
callq 0x397e
jmp 0x5218
movq %rax, %r12
movq (%rsp), %rdi
callq 0x9d4c
testl %eax, %eax
jne 0x5210
movq (%rsp), %rsi
movq %r14, %rdi
callq 0x397e
movq %r12, %rdi
callq 0xd8f9
movq (%rsp), %rdi
callq 0x9c18
testq %rax, %rax
jne 0x51f4
movq %rsp, %rdi
callq 0x9a57
jmp 0x516f
movq %r14, %rdi
callq 0x524d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq