// File: /workspace/github_c_makefile_star2_O3/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, %r14
movl (%rdi), %eax
testl %eax, %eax
jle 0x7831
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x10(%r14), %rcx
movq (%rcx,%rbx,8), %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x781f
incl %r15d
movl %r15d, %esi
callq 0xb883
movl (%r14), %eax
incq %rbx
movslq %eax, %rcx
cmpq %rcx, %rbx
jl 0x7803
testl %eax, %eax
setg %bl
jmp 0x7836
xorl %ebx, %ebx
xorl %r15d, %r15d
movslq %r15d, %rdi
shlq $0x3, %rdi
callq 0x1190
testq %rax, %rax
je 0x7913
movq %rax, %r12
testl %r15d, %r15d
jle 0x7864
movl %r15d, %edx
shlq $0x3, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r15, 0x10(%rsp)
testb %bl, %bl
je 0x78cf
xorl %ebx, %ebx
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rdi
movslq 0x1c(%rdi), %r15
xorl %esi, %esi
movq 0x8(%rsp), %rdx
callq 0x736c
movq %rax, %r13
movq -0x8(%r12,%r15,8), %rbp
testq %rbp, %rbp
je 0x78b0
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5b1f
testl %eax, %eax
jle 0x78b7
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x149f4
movq %r13, -0x8(%r12,%r15,8)
jmp 0x78c4
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x149f4
incq %rbx
movslq (%r14), %rax
cmpq %rax, %rbx
jl 0x786f
movq 0x10(%rsp), %rcx
leal -0x1(%rcx), %eax
movq (%r12), %rbx
cmpl $0x2, %ecx
jl 0x78fb
movl %eax, %ecx
xorl %edx, %edx
movq %rbx, %rsi
movq 0x8(%r12,%rdx,8), %rdi
incq %rdx
movq %rdi, 0x18(%rsi)
movq %rdi, %rsi
cmpq %rdx, %rcx
jne 0x78e7
cltq
movq (%r12,%rax,8), %rax
movq $0x0, 0x18(%rax)
movq %r12, %rdi
callq 0x1030
jmp 0x7926
leaq 0x18831(%rip), %rdi     # 0x2014b
xorl %ebx, %ebx
movl %r15d, %esi
xorl %eax, %eax
callq 0x10b0
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq