// File: /workspace/github_c_makefile_star2_O2/pwelke[P]GraphMiningTools/cs_Tree.c
// Function: canonicalStringOfRootedTree
struct ShallowGraph* canonicalStringOfRootedTree(struct Vertex* vertex, struct Vertex* parent, struct ShallowGraphPool *p) {
	struct ShallowGraph* stringList = NULL;
	struct VertexList* idx;
	int numChildren = 0;

	/* for any neighbor...*/
	for (idx = vertex->neighborhood; idx; idx = idx->next) {
		/* ... that is not the parent (pointer comparison!) */
		if (idx->endPoint != parent) {
			struct ShallowGraph* tmp;

			/* copy edge that connects vertex and its child */
			struct VertexList* e = getVertexList(p->listPool);
			e->label = idx->label;

			/* compute canonical string recursively */
			tmp = canonicalStringOfRootedTree(idx->endPoint, vertex, p);

			/* add the edge connecting the child with vertex */
			pushEdge(tmp, e);

			/* add it to the list of strings for the children */
			tmp->next = stringList;
			stringList = tmp;
			++numChildren;
		}
	}

	/* if the list is empty, vertex is a leaf. this stops recursion */
	if (numChildren == 0) {
		struct ShallowGraph* result = getShallowGraph(p);
		struct VertexList* e = getVertexList(p->listPool);

		/* the canonical string of a single vertex consists of the label of
		that vertex */
		e->label = vertex->label;
		appendEdge(result, e);

		/* base case of recursion */
		return result;
	} else {
		/* otherwise we have to sort the canonical strings in ascending order 
		therefore create array containing pointers to the strings */
		struct ShallowGraph** tempArray;
		if ((tempArray = malloc(numChildren * sizeof(struct ShallowGraph*)))) {
			int i;
			struct ShallowGraph* tmp;

			/* this will be the resulting canonical string */
			struct ShallowGraph* result = getShallowGraph(p);
			/* add an edge representing the label of the current vertex */
			struct VertexList* v = getVertexList(p->listPool);
			v->label = vertex->label;
			appendEdge(result, v);

			/* stdlib qsort requires an array of elements as input, so we will give it to it */
			tmp = stringList;

			for (i=0; i<numChildren; ++i) {
				tempArray[i] = tmp;
				tmp = tmp->next;
			}

			/*stdlib qsort using my lexicographic comparison of ShallowGraphs */
			qsort(tempArray, numChildren, sizeof(struct ShallowGraph*), &lexCompCS);

			/* append the edges of the substrings to the result */
			for (i=0; i<numChildren; ++i) {

				/* add open bracket as a substring begins */
				appendEdge(result, getInitialisatorEdge(p->listPool));

				/* append canonical string of the current child, this is a list of struct VertexList's 
				  that starts at tempArray[i]->edges and ends at tempArray[i]->lastEdge */ 
				result->lastEdge->next = tempArray[i]->edges;

				/* Use skip pointers to avoid going through all of tempArray[i] */
				result->lastEdge = tempArray[i]->lastEdge;

				/* Remove the edges from their old shallowgraph.
				This is important as dumping otherwise would dump the edges, too */
				tempArray[i]->edges = NULL;
				dumpShallowGraph(p, tempArray[i]);

				/* add close bracket as substring ends */
				appendEdge(result, getTerminatorEdge(p->listPool));
			}
			free(tempArray);
			return result;
		} else {
			printf("canonicalStringOfRootedTree: Error allocating memory for array of neighbors");
			return NULL;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
xorl %ebp, %ebp
movq %rdi, %r15
xorl %r13d, %r13d
movq 0x8(%r15), %r15
testq %r15, %r15
je 0x52d0
cmpq %r14, 0x18(%r15)
je 0x5277
movq 0x10(%rbx), %rdi
callq 0x11307
movq %rax, 0x10(%rsp)
movq (%r15), %rcx
movq %rcx, (%rax)
movq 0x18(%r15), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x5258
movq %r12, 0x8(%rsp)
movq %rax, %r12
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x61e7
movq %r13, 0x18(%r12)
incq %rbp
movq %r12, %r13
movq 0x8(%rsp), %r12
jmp 0x5277
testl %ebp, %ebp
je 0x53b6
movl %ebp, %r14d
leaq (,%r14,8), %rdi
callq 0x1180
testq %rax, %rax
je 0x53de
movq %rax, %r15
movq %r14, 0x8(%rsp)
movq %rbx, %rdi
callq 0x117e9
movq %rax, %r14
movq 0x10(%rbx), %rdi
callq 0x11307
movq (%r12), %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x6620
xorl %eax, %eax
cmpq %rax, %rbp
je 0x5333
movq %r13, (%r15,%rax,8)
movq 0x18(%r13), %r13
incq %rax
jmp 0x5321
leaq -0x1813(%rip), %rcx     # 0x3b27
pushq $0x8
popq %rdx
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x1080
movq %r15, %r12
subq $0x1, %rbp
jb 0x53ac
movq 0x10(%rbx), %rdi
callq 0x4fac
movq %r14, %rdi
movq %rax, %rsi
callq 0x6620
movq (%r12), %rax
movq (%rax), %rax
movq 0x8(%r14), %rcx
movq %rax, 0x8(%rcx)
movq (%r12), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%r14)
andq $0x0, (%rax)
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x1184e
movq 0x10(%rbx), %rdi
callq 0x4f9a
movq %r14, %rdi
movq %rax, %rsi
callq 0x6620
addq $0x8, %r12
jmp 0x534d
movq %r15, %rdi
callq 0x1030
jmp 0x53ef
movq %rbx, %rdi
callq 0x117e9
movq %rax, %r14
movq 0x10(%rbx), %rdi
callq 0x11307
movq (%r12), %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x6620
jmp 0x53ef
leaq 0x17d1a(%rip), %rdi     # 0x1d0ff
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x10b0
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq