// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rcx, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movq 0x8(%rdi), %rbp
testq %rbp, %rbp
je 0x75be
movl %edx, %r13d
movq %rsi, %r14
xorl %r15d, %r15d
movq $0x0, 0x18(%rsp)
movq 0x18(%rbp), %rax
cmpq %r14, %rax
je 0x75b3
movl 0x1c(%rax), %eax
testl %eax, %eax
sete %cl
cmpl %r13d, %eax
setg %al
orb %cl, %al
jne 0x75b3
movq 0x10(%rsp), %rbx
movq 0x10(%rbx), %rdi
callq 0x1432b
movq %rax, %r12
movq (%rbp), %rax
movq %rax, (%r12)
movq 0x18(%rbp), %rdi
movq 0x8(%rsp), %rsi
movl %r13d, %edx
movq %rbx, %rcx
callq 0x750d
movl %r13d, %ebx
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
callq 0x840c
movq %r15, 0x18(%r13)
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movq %r13, %r15
movl %ebx, %r13d
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
jne 0x7544
jmp 0x75ca
movq $0x0, 0x18(%rsp)
xorl %r15d, %r15d
movq 0x18(%rsp), %r14
testl %r14d, %r14d
je 0x76e6
movslq %r14d, %r13
leaq (,%r13,8), %rdi
callq 0x1190
testq %rax, %rax
je 0x7727
movq %rax, %r12
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x14aae
movq %rax, %rbp
movq 0x10(%rbx), %rdi
callq 0x1432b
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %rbp, 0x20(%rsp)
movq %rbp, %rdi
movq %rax, %rsi
callq 0x840c
movl %r14d, %ebx
testl %r14d, %r14d
jle 0x7642
xorl %eax, %eax
movq %r15, (%r12,%rax,8)
movq 0x18(%r15), %r15
incq %rax
cmpq %rax, %rbx
jne 0x7632
leaq -0x1bdb(%rip), %rcx     # 0x5a6e
movl $0x8, %edx
movq %r12, 0x8(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1080
testl %r14d, %r14d
movq 0x10(%rsp), %r12
jle 0x76d5
movq 0x20(%rsp), %rax
movq (%rax), %r15
xorl %r14d, %r14d
movq 0x10(%r12), %rdi
callq 0x7060
movq %rax, %r13
movq %rax, 0x8(%r15)
movq 0x8(%rsp), %rcx
movq (%rcx,%r14,8), %rax
movq (%rax), %rax
movq %rax, 0x8(%r13)
movq (%rcx,%r14,8), %rax
movq $0x0, (%rax)
movq (%rcx,%r14,8), %rsi
movq %r12, %rdi
callq 0x14b19
movq %r13, %rbp
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x76ab
movq 0x10(%rsp), %r12
movq 0x10(%r12), %rdi
callq 0x704e
movq %rax, %r15
movq %rax, 0x8(%rbp)
incq %r14
cmpq %rbx, %r14
jne 0x7673
movq 0x8(%rsp), %rdi
callq 0x1030
movq 0x20(%rsp), %r14
jmp 0x7715
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x14aae
movq %rax, %r14
movq 0x10(%rbx), %rdi
callq 0x1432b
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x840c
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x129c
xorl %r14d, %r14d
jmp 0x7715