// File: /workspace/github_c_makefile_star2_O3/pwelke[P]GraphMiningTools/cs_Tree.c
// Function: canonicalStringOfRootedLevelTree
struct ShallowGraph* canonicalStringOfRootedLevelTree(struct Vertex* vertex, struct Vertex* parent, int maxDepth, 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) {
			/* ... and has distance smaller than maxDepth */
			if (idx->endPoint->visited && (idx->endPoint->visited <= maxDepth)) {
				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 = canonicalStringOfRootedLevelTree(idx->endPoint, vertex, maxDepth, 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;
		pushEdge(result, e);

		/* stop 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;
			pushEdge(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 */
			idx = result->edges;
			for (i=0; i<numChildren; ++i) {

				/* add open bracket as a substring begins */
				idx->next = getInitialisatorEdge(p->listPool);
				idx = idx->next;

				/* append canonical string of the current child */
				idx->next = tempArray[i]->edges;

				/* the edges do not belong to this element any longer.
				This is important as dumping otherwise would dump the edges, too */
				tempArray[i]->edges = NULL;
				dumpShallowGraph(p, tempArray[i]);

				/* go as long as we are not at the end of the list */

				/*********************************************************
				 ** TODO  Use skip pointers to avoid quadratic efford  **
				 *********************************************************/
				for (; idx->next; idx = idx->next);

				/* add close bracket as substring ends */
				idx->next = getTerminatorEdge(p->listPool);
				idx = idx->next;
			}

			free(tempArray);
			return result;

		} else {
			fprintf(stderr, "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 %rcx, %r13
movq %rdi, 0x10(%rsp)
movq 0x8(%rdi), %rbx
testq %rbx, %rbx
je 0x76f7
movl %edx, %r14d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r13, (%rsp)
movq 0x18(%rbx), %rax
cmpq %rsi, %rax
je 0x75ee
movl 0x1c(%rax), %eax
testl %eax, %eax
sete %cl
cmpl %r14d, %eax
setg %al
orb %cl, %al
jne 0x75ee
movq 0x10(%r13), %rdi
movl %ebp, 0x8(%rsp)
movq %rsi, %rbp
callq 0x141f1
movq %rax, %r15
movq (%rbx), %rax
movq %rax, (%r15)
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movl %r14d, %edx
movq %r13, %rcx
callq 0x7555
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
callq 0x848a
movq %rbp, %rsi
movl 0x8(%rsp), %ebp
movq %r12, 0x18(%r13)
incl %ebp
movq %r13, %r12
movq (%rsp), %r13
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x7584
testl %ebp, %ebp
je 0x76f7
movslq %ebp, %r13
leaq (,%r13,8), %rdi
callq 0x1190
testq %rax, %rax
je 0x7759
movq %rax, %r15
movq (%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 0x848a
testl %ebp, %ebp
movq %r14, 0x8(%rsp)
jle 0x7723
movl %ebp, %ebx
movl %ebp, %ebp
xorl %eax, %eax
movq %r12, (%r15,%rax,8)
movq 0x18(%r12), %r12
incq %rax
cmpq %rax, %rbp
jne 0x765c
leaq -0x1b66(%rip), %rcx     # 0x5b0e
movl $0x8, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1080
testl %ebx, %ebx
movq (%rsp), %r13
jle 0x773a
movq 0x8(%rsp), %rax
movq (%rax), %r12
xorl %ebx, %ebx
movq 0x10(%r13), %rdi
callq 0x709c
movq %r13, %rdi
movq %rax, %r13
movq %rax, 0x8(%r12)
movq (%r15,%rbx,8), %rax
movq (%rax), %rax
movq %rax, 0x8(%r13)
movq (%r15,%rbx,8), %rax
movq $0x0, (%rax)
movq (%r15,%rbx,8), %rsi
callq 0x149f4
movq %r13, %r14
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x76cd
movq (%rsp), %r13
movq 0x10(%r13), %rdi
callq 0x708a
movq %rax, %r12
movq %rax, 0x8(%r14)
incq %rbx
cmpq %rbp, %rbx
jne 0x769a
jmp 0x773a
movq %r13, %rdi
callq 0x1498d
movq %rax, %rbx
movq 0x10(%r13), %rdi
callq 0x141f1
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x848a
jmp 0x7747
leaq -0x1c1c(%rip), %rcx     # 0x5b0e
movl $0x8, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1080
movq %r15, %rdi
callq 0x1030
movq 0x8(%rsp), %rbx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x129c
xorl %ebx, %ebx
jmp 0x7747