// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdx, 0x78(%rsp)
movq $0x0, 0x70(%rsp)
movl $0x0, 0x64(%rsp)
movq 0x88(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x68(%rsp)
cmpq $0x0, 0x68(%rsp)
je 0x95a7
movq 0x68(%rsp), %rax
movq 0x18(%rax), %rax
cmpq 0x80(%rsp), %rax
je 0x9592
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1f310
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %rax
movq (%rax), %rcx
movq 0x50(%rsp), %rax
movq %rcx, (%rax)
movq 0x68(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x88(%rsp), %rsi
movq 0x78(%rsp), %rdx
callq 0x94c0
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0xb610
movq 0x70(%rsp), %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x58(%rsp), %rax
movq %rax, 0x70(%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x9594
movq 0x68(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x68(%rsp)
jmp 0x94fe
cmpl $0x0, 0x64(%rsp)
jne 0x9604
movq 0x78(%rsp), %rdi
callq 0x1fe70
movq %rax, 0x48(%rsp)
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1f310
movq %rax, 0x40(%rsp)
movq 0x88(%rsp), %rax
movq (%rax), %rcx
movq 0x40(%rsp), %rax
movq %rcx, (%rax)
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0xbf70
movq 0x48(%rsp), %rax
movq %rax, 0x90(%rsp)
jmp 0x97d3
movslq 0x64(%rsp), %rdi
shlq $0x3, %rdi
callq 0x1170
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x97b9
movq 0x78(%rsp), %rdi
callq 0x1fe70
movq %rax, 0x20(%rsp)
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1f310
movq %rax, 0x18(%rsp)
movq 0x88(%rsp), %rax
movq (%rax), %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0xbf70
movq 0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x64(%rsp), %eax
jge 0x96af
movq 0x28(%rsp), %rdx
movq 0x38(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x9677
movq 0x38(%rsp), %rdi
movslq 0x64(%rsp), %rsi
movl $0x8, %edx
leaq -0x33a5(%rip), %rcx     # 0x6320
callq 0x1060
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x64(%rsp), %eax
jge 0x97a0
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x9000
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0xbf70
movq 0x38(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rcx
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x38(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x38(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq $0x0, (%rax)
movq 0x78(%rsp), %rdi
movq 0x38(%rsp), %rax
movslq 0x34(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1ff70
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x8fc0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0xbf70
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x96d2
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x20(%rsp), %rax
movq %rax, 0x90(%rsp)
jmp 0x97d3
leaq 0x2b99a(%rip), %rdi     # 0x3515a
movb $0x0, %al
callq 0x1090
movq $0x0, 0x90(%rsp)
movq 0x90(%rsp), %rax
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)