// File: /workspace/github_c_makefile_star2_O3/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, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x8(%rdi), %rbx
testq %rbx, %rbx
je 0x74e0
movq %rsi, %r14
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq %r14, 0x18(%rbx)
je 0x73de
movq 0x8(%rsp), %r13
movq 0x10(%r13), %rdi
callq 0x141f1
movq %rax, %r15
movq (%rbx), %rax
movq %rax, (%r15)
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x736c
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
callq 0x848a
movq %r12, 0x18(%r13)
incl %ebp
movq %r13, %r12
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x7399
testl %ebp, %ebp
je 0x74e0
movslq %ebp, %r13
leaq (,%r13,8), %rdi
callq 0x1190
testq %rax, %rax
je 0x7511
movq %rax, %r15
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x1498d
movq %rax, %r14
movq 0x10(%rbx), %rdi
callq 0x141f1
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x8916
testl %ebp, %ebp
jle 0x7524
movq %r13, %rsi
movl %ebp, %r13d
xorl %eax, %eax
movq %r12, (%r15,%rax,8)
movq 0x18(%r12), %r12
incq %rax
cmpq %rax, %r13
jne 0x744a
leaq -0x1954(%rip), %rcx     # 0x5b0e
movl $0x8, %edx
movq %r15, %rdi
callq 0x1080
testl %ebp, %ebp
movq 0x8(%rsp), %r12
jle 0x753b
xorl %ebx, %ebx
movq 0x10(%r12), %rdi
callq 0x709c
movq %r14, %rdi
movq %rax, %rsi
callq 0x8916
movq (%r15,%rbx,8), %rax
movq (%rax), %rax
movq 0x8(%r14), %rcx
movq %rax, 0x8(%rcx)
movq (%r15,%rbx,8), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%r14)
movq $0x0, (%rax)
movq (%r15,%rbx,8), %rsi
movq %r12, %rdi
callq 0x149f4
movq 0x10(%r12), %rdi
callq 0x708a
movq %r14, %rdi
movq %rax, %rsi
callq 0x8916
incq %rbx
cmpq %rbx, %r13
jne 0x747e
jmp 0x753b
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x1498d
movq %rax, %r14
movq 0x10(%rbx), %rdi
callq 0x141f1
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x8916
jmp 0x7543
leaq 0x18be7(%rip), %rdi     # 0x200ff
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x10b0
jmp 0x7543
leaq -0x1a1d(%rip), %rcx     # 0x5b0e
movl $0x8, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1080
movq %r15, %rdi
callq 0x1030
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq