// File: /workspace/github_c_makefile_star2_O1/pwelke[P]GraphMiningTools/cs_Tree.c
// Function: getTreePatterns
struct ShallowGraph* getTreePatterns(struct Graph* forest, struct ShallowGraphPool *sgp) {
	/* components contains the number of trees in the forest after the first for loop */
	int components = 0;
	int i;
	struct ShallowGraph **canonicalStrings;
	struct ShallowGraph* result = NULL;

	/* mark every tree with a different number */
	for (i=0; i<forest->n; ++i) {
		if (forest->vertices[i]->visited == 0) {
			++components;
			markConnectedComponent(forest->vertices[i], components);
		}
	}

	/* for each component, we will have a list of possible canonical strings */
	if (!(canonicalStrings = malloc(components * sizeof(struct ShallowGraph*)))) {
		printf("getTreePatterns: Error allocating memory for canonical string array for %i components.\n", components);
		return NULL;
	} else {

		/* initialize the thing to NULL */
		for (i=0; i<components; ++i) {
			canonicalStrings[i] = NULL;
		}

		/* compute the canonical string of the unique tree rooted at vertex i and save it at the correct position of the 
		canonicalStrings array */
		for (i=0; i<forest->n; ++i) {
			/* readability: the label of the current tree minus 1 for indexing purposes */
			int currentComponent = forest->vertices[i]->visited - 1;
			/* compute the canonical string */
			struct ShallowGraph *cs = canonicalStringOfRootedTree(forest->vertices[i], NULL, sgp);

			/* keep the string if its the first one, computed for this component or lexicograhically smaller 
			than the saved string */
			if (canonicalStrings[currentComponent]) {
				if (compareCanonicalStrings(canonicalStrings[currentComponent], cs) > 0) {
					dumpShallowGraph(sgp, canonicalStrings[currentComponent]);
					canonicalStrings[currentComponent] = cs;
				} else {
					dumpShallowGraph(sgp, cs);
				}
			} else {
				canonicalStrings[currentComponent] = cs;
			}
		}


		/* concatenate the elements referred to in the array to a list, free the array */
		for (i=0; i<components-1; ++i) {
			canonicalStrings[i]->next = canonicalStrings[i+1];
		}
		canonicalStrings[components-1]->next = NULL;
		result = canonicalStrings[0];
		free(canonicalStrings);
	}
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %r15
cmpl $0x0, (%rdi)
jle 0x77f5
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x10(%r15), %rax
movq (%rax,%r14,8), %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x77e8
incl %ebx
movl %ebx, %esi
callq 0xb8fe
incq %r14
movslq (%r15), %rax
cmpq %rax, %r14
jl 0x77d1
jmp 0x77f7
xorl %ebx, %ebx
movslq %ebx, %rdi
shlq $0x3, %rdi
callq 0x1190
testq %rax, %rax
je 0x78d7
movq %rax, %r12
testl %ebx, %ebx
jle 0x7823
movl %ebx, %edx
shlq $0x3, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x10d0
movq %rbx, 0x10(%rsp)
cmpl $0x0, (%r15)
jle 0x7891
xorl %r14d, %r14d
movq 0x10(%r15), %rax
movq (%rax,%r14,8), %rdi
movslq 0x1c(%rdi), %rbx
xorl %esi, %esi
movq 0x8(%rsp), %rdx
callq 0x732c
movq %rax, %r13
movq -0x8(%r12,%rbx,8), %rbp
testq %rbp, %rbp
je 0x7872
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5a7f
testl %eax, %eax
jle 0x7879
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x14b19
movq %r13, -0x8(%r12,%rbx,8)
jmp 0x7886
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x14b19
incq %r14
movslq (%r15), %rax
cmpq %rax, %r14
jl 0x7831
movq 0x10(%rsp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x2, %ecx
jl 0x78bb
movl %eax, %ecx
xorl %edx, %edx
movq (%r12,%rdx,8), %rsi
movq 0x8(%r12,%rdx,8), %rdi
leaq 0x1(%rdx), %r8
movq %rdi, 0x18(%rsi)
movq %r8, %rdx
cmpq %r8, %rcx
jne 0x78a2
cltq
movq (%r12,%rax,8), %rax
movq $0x0, 0x18(%rax)
movq (%r12), %r14
movq %r12, %rdi
callq 0x1030
jmp 0x78ea
leaq 0x1886d(%rip), %rdi     # 0x2014b
xorl %r14d, %r14d
movl %ebx, %esi
xorl %eax, %eax
callq 0x10b0
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq