// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movl %edx, 0x6c(%rsp)
movq %rcx, 0x60(%rsp)
movq $0x0, 0x58(%rsp)
movl $0x0, 0x4c(%rsp)
movq 0x78(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, 0x50(%rsp)
je 0x98fb
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rax
cmpq 0x70(%rsp), %rax
je 0x98e6
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rax
cmpl $0x0, 0x1c(%rax)
je 0x98e4
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x1c(%rax), %eax
cmpl 0x6c(%rsp), %eax
jg 0x98e4
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1f310
movq %rax, 0x38(%rsp)
movq 0x50(%rsp), %rax
movq (%rax), %rcx
movq 0x38(%rsp), %rax
movq %rcx, (%rax)
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x78(%rsp), %rsi
movl 0x6c(%rsp), %edx
movq 0x60(%rsp), %rcx
callq 0x97f0
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0xb610
movq 0x58(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x40(%rsp), %rax
movq %rax, 0x58(%rsp)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x98e6
jmp 0x98e8
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x50(%rsp)
jmp 0x9829
cmpl $0x0, 0x4c(%rsp)
jne 0x9955
movq 0x60(%rsp), %rdi
callq 0x1fe70
movq %rax, 0x30(%rsp)
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1f310
movq %rax, 0x28(%rsp)
movq 0x78(%rsp), %rax
movq (%rax), %rcx
movq 0x28(%rsp), %rax
movq %rcx, (%rax)
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0xb610
movq 0x30(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x9b3a
movslq 0x4c(%rsp), %rdi
shlq $0x3, %rdi
callq 0x1170
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x9b16
movq 0x60(%rsp), %rdi
callq 0x1fe70
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1f310
movq %rax, (%rsp)
movq 0x78(%rsp), %rax
movq (%rax), %rcx
movq (%rsp), %rax
movq %rcx, (%rax)
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
callq 0xb610
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0x99fa
movq 0x10(%rsp), %rdx
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x99c2
movq 0x20(%rsp), %rdi
movslq 0x4c(%rsp), %rsi
movl $0x8, %edx
leaq -0x36f0(%rip), %rcx     # 0x6320
callq 0x1060
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x50(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0x9afd
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x9000
movq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq $0x0, (%rax)
movq 0x60(%rsp), %rdi
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1ff70
movq 0x50(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x9ac5
jmp 0x9ab5
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x50(%rsp)
jmp 0x9aa7
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x8fc0
movq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x50(%rsp)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x9a2a
movq 0x20(%rsp), %rdi
callq 0x1030
movq 0x8(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x9b3a
movq 0x344db(%rip), %rax     # 0x3dff8
movq (%rax), %rdi
leaq 0x2b633(%rip), %rsi     # 0x3515a
movb $0x0, %al
callq 0x1140
movq $0x0, 0x80(%rsp)
movq 0x80(%rsp), %rax
addq $0x88, %rsp
retq
nopw (%rax,%rax)