// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x9cc1
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x9cb2
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl 0x2c(%rsp), %esi
callq 0x112d0
jmp 0x9cb4
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x9c67
movslq 0x2c(%rsp), %rdi
shlq $0x3, %rdi
callq 0x1170
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
jne 0x9cfa
movl 0x2c(%rsp), %esi
leaq 0x2b4c1(%rip), %rdi     # 0x351a6
movb $0x0, %al
callq 0x1090
movq $0x0, 0x40(%rsp)
jmp 0x9e9b
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x9d2b
movq 0x20(%rsp), %rax
movslq 0x28(%rsp), %rcx
movq $0x0, (%rax,%rcx,8)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x9d02
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x9e16
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x1c(%rax), %eax
subl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x30(%rsp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x94c0
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x9df1
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x8(%rsp), %rsi
callq 0x6360
cmpl $0x0, %eax
jle 0x9de0
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1ff70
movq 0x8(%rsp), %rdx
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x9def
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x1ff70
jmp 0x9e04
movq 0x8(%rsp), %rdx
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x9e06
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x9d33
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x9e5f
movq 0x20(%rsp), %rax
movl 0x28(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq 0x20(%rsp), %rax
movslq 0x28(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x18(%rax)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x9e1e
movq 0x20(%rsp), %rax
movl 0x2c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rdi
callq 0x1030
movq 0x18(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)