// File: /workspace/github_c_makefile_star2_O1/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, (%rsp)
movq 0x8(%rdi), %rbp
testq %rbp, %rbp
je 0x73aa
movq %rsi, %r14
xorl %r12d, %r12d
xorl %eax, %eax
cmpq %r14, 0x18(%rbp)
je 0x739f
movq 0x8(%rsp), %r13
movq 0x10(%r13), %rdi
movq %rax, %rbx
callq 0x1432b
movq %rax, %r15
movq (%rbp), %rax
movq %rax, (%r15)
movq 0x18(%rbp), %rdi
movq (%rsp), %rsi
movq %r13, %rdx
callq 0x732c
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
callq 0x840c
movq %rbx, %rax
movq %r12, 0x18(%r13)
incl %eax
movq %r13, %r12
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
jne 0x7354
jmp 0x73af
xorl %eax, %eax
xorl %r12d, %r12d
testl %eax, %eax
je 0x74ba
movslq %eax, %rbx
leaq (,%rbx,8), %rdi
movq %rax, %rbp
callq 0x1190
testq %rax, %rax
je 0x74ea
movq %rax, %r15
movq %rbx, 0x10(%rsp)
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x14aae
movq %rax, %r14
movq 0x10(%rbx), %rdi
callq 0x1432b
movq (%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x88b9
movl %ebp, %r13d
movq %rbp, (%rsp)
testl %ebp, %ebp
jle 0x7427
xorl %eax, %eax
movq %r12, (%r15,%rax,8)
movq 0x18(%r12), %r12
incq %rax
cmpq %rax, %r13
jne 0x7416
leaq -0x19c0(%rip), %rcx     # 0x5a6e
movl $0x8, %edx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x1080
cmpl $0x0, (%rsp)
movq 0x8(%rsp), %rbx
jle 0x74b0
xorl %r12d, %r12d
movq 0x10(%rbx), %rdi
callq 0x7060
movq %r14, %rdi
movq %rax, %rsi
callq 0x88b9
movq (%r15,%r12,8), %rax
movq (%rax), %rax
movq 0x8(%r14), %rcx
movq %rax, 0x8(%rcx)
movq (%r15,%r12,8), %rax
movq 0x8(%rax), %rax
movq %rax, 0x8(%r14)
movq (%r15,%r12,8), %rax
movq $0x0, (%rax)
movq (%r15,%r12,8), %rsi
movq %rbx, %rdi
callq 0x14b19
movq 0x10(%rbx), %rdi
callq 0x704e
movq %r14, %rdi
movq %rax, %rsi
callq 0x88b9
incq %r12
cmpq %r12, %r13
jne 0x744e
movq %r15, %rdi
callq 0x1030
jmp 0x74fb
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x14aae
movq %rax, %r14
movq 0x10(%rbx), %rdi
callq 0x1432b
movq (%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x88b9
jmp 0x74fb
leaq 0x18c0e(%rip), %rdi     # 0x200ff
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