// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r12
xorl %r13d, %r13d
movq %rdi, (%rsp)
xorl %r15d, %r15d
movq 0x8(%r12), %r12
testq %r12, %r12
je 0x5494
movq 0x18(%r12), %rax
cmpq %r14, %rax
je 0x5424
movl 0x1c(%rax), %eax
testl %eax, %eax
sete %cl
cmpl %ebp, %eax
setg %al
orb %cl, %al
jne 0x5424
movq 0x10(%rbx), %rdi
callq 0x11307
movq %rax, 0x8(%rsp)
movq (%r12), %rcx
movq %rcx, (%rax)
movq 0x18(%r12), %rdi
movq (%rsp), %rsi
movl %ebp, %edx
movq %rbx, %rcx
callq 0x5401
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x61e7
movq 0x10(%rsp), %rax
movq %r15, 0x18(%rax)
incq %r13
movq %rax, %r15
jmp 0x5424
testl %r13d, %r13d
je 0x5587
movl %r13d, %eax
movq %rax, 0x8(%rsp)
leaq (,%rax,8), %rdi
callq 0x1180
testq %rax, %rax
je 0x55c2
movq %rax, %r12
movq %rbx, %rdi
callq 0x117e9
movq %rax, %r14
movq 0x10(%rbx), %rdi
callq 0x11307
movq (%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %r14, (%rsp)
movq %r14, %rdi
movq %rax, %rsi
callq 0x61e7
movq %r12, %rax
subq $0x1, %r13
jb 0x5501
movq %r15, (%rax)
movq 0x18(%r15), %r15
addq $0x8, %rax
jmp 0x54ee
leaq -0x19e1(%rip), %rcx     # 0x3b27
pushq $0x8
popq %rdx
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0x1080
movq (%rsp), %rax
movq (%rax), %r13
xorl %r15d, %r15d
cmpq 0x8(%rsp), %r15
je 0x5579
movq 0x10(%rbx), %rdi
callq 0x4fac
movq %rax, %rbp
movq %rax, 0x8(%r13)
movq (%r12,%r15,8), %rax
movq (%rax), %rax
movq %rax, 0x8(%rbp)
movq (%r12,%r15,8), %rax
andq $0x0, (%rax)
movq (%r12,%r15,8), %rsi
movq %rbx, %rdi
callq 0x1184e
movq %rbp, %r14
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
jne 0x5558
movq 0x10(%rbx), %rdi
callq 0x4f9a
movq %rax, %r13
movq %rax, 0x8(%r14)
incq %r15
jmp 0x5522
movq %r12, %rdi
callq 0x1030
movq (%rsp), %r14
jmp 0x55b0
movq %rbx, %rdi
callq 0x117e9
movq %rax, %r14
movq 0x10(%rbx), %rdi
callq 0x11307
movq (%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x61e7
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x24a2f(%rip), %rax     # 0x29ff8
movq (%rax), %rcx
leaq 0x17b2c(%rip), %rdi     # 0x1d0ff
pushq $0x4b
popq %rsi
pushq $0x1
popq %rdx
callq 0x1210
xorl %r14d, %r14d
jmp 0x55b0