// File: /workspace/github_c_makefile_star2_O2/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
xorl %r14d, %r14d
xorl %ebx, %ebx
movslq (%r15), %r13
cmpq %r13, %r14
jge 0x5696
movq 0x10(%r15), %rax
movq (%rax,%r14,8), %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x5691
incl %ebx
movl %ebx, %esi
callq 0x9307
incq %r14
jmp 0x5672
movslq %ebx, %rdi
shlq $0x3, %rdi
callq 0x1180
testq %rax, %rax
je 0x577a
movq %rax, %r12
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
movl %ebx, 0x14(%rsp)
cmovgl %ebx, %ecx
cmpq %rax, %rcx
je 0x56cd
andq $0x0, (%r12,%rax,8)
incq %rax
jmp 0x56be
xorl %r14d, %r14d
movslq %r13d, %rax
cmpq %rax, %r14
jge 0x5735
movq 0x10(%r15), %rax
movq (%rax,%r14,8), %rdi
movslq 0x1c(%rdi), %rbx
xorl %esi, %esi
movq 0x8(%rsp), %rdx
callq 0x5258
movq %rax, %r13
movq -0x8(%r12,%rbx,8), %rbp
testq %rbp, %rbp
je 0x5719
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3b32
testl %eax, %eax
jle 0x5720
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x1184e
movq %r13, -0x8(%r12,%rbx,8)
jmp 0x572d
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x1184e
incq %r14
movl (%r15), %r13d
jmp 0x56d0
movl 0x14(%rsp), %edx
decl %edx
xorl %ecx, %ecx
testl %edx, %edx
movl $0x0, %eax
movl %edx, %edi
cmovgl %edx, %eax
cmpq %rcx, %rax
je 0x5760
movq (%r12,%rcx,8), %rdx
movq 0x8(%r12,%rcx,8), %rsi
incq %rcx
movq %rsi, 0x18(%rdx)
jmp 0x5749
movslq %edi, %rax
movq (%r12,%rax,8), %rax
andq $0x0, 0x18(%rax)
movq (%r12), %r14
movq %r12, %rdi
callq 0x1030
jmp 0x578d
leaq 0x179ca(%rip), %rdi     # 0x1d14b
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