// File: /workspace/github_c_makefile_star2_O1/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 0x14698(%rip), %rsi     # 0x19cc2
callq 0x4eec
movq %rax, %r15
movl $0x38, %edi
callq 0xf07d
movq %rax, %rbx
movq %r15, 0x18(%rax)
movq 0x10(%r14), %r12
testq %r12, %r12
je 0x5720
movl 0x30(%r14), %ebp
xorl %r13d, %r13d
xorl %eax, %eax
callq 0xab28
movq %rax, (%rsp)
movq 0x10(%r12), %r14
testq %r14, %r14
je 0x56b7
xorl %r13d, %r13d
movq 0x8(%r14), %r15
movq (%rsp), %rdi
cmpl %ebp, %r13d
jge 0x568e
movq %r15, %rsi
callq 0xacc7
incl %r13d
cmpl %ebp, %r13d
je 0x56a3
jmp 0x56af
callq 0xad42
movq %r15, 0x10(%rax)
movq (%rsp), %rdi
movq %rax, %rsi
callq 0xac19
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x3ba4
movq (%r14), %r14
testq %r14, %r14
jne 0x566f
cmpl %ebp, %r13d
jge 0x56c8
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x3ba4
movq (%rsp), %rdi
callq 0xad42
testq %rax, %rax
je 0x570b
movq %rax, %r15
movq (%rsp), %rdi
callq 0xae87
testl %eax, %eax
jne 0x56f2
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x3ba4
movq %r15, %rdi
callq 0xf129
movq (%rsp), %rdi
callq 0xad42
movq %rax, %r15
testq %rax, %rax
jne 0x56d9
movq %rsp, %rdi
callq 0xab7d
movq (%r12), %r12
testq %r12, %r12
jne 0x5654
movq %rbx, %rdi
callq 0x573a
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq