// File: /workspace/github_c_makefile_star2_O1/pwelke[P]GraphMiningTools/subtreeIsomorphismSampling.c
// Function: recursiveSubtreeIsomorphismSampler
static char recursiveSubtreeIsomorphismSampler(struct Vertex* parent, struct Graph* g) {

	struct Vertex* currentImage = g->vertices[parent->visited - 1];

	int nNeighbors = degree(parent);
	struct VertexList** shuffledNeighbors = shuffleNeighbors(parent, nNeighbors);

	// TODO need to shuffle the neighbors of the image, as well
	int unassignedNeighborsOfRoot = 0;
	int processedNeighborsOfRoot = 0;

	for (int i=0; i<nNeighbors; ++i) {
		struct VertexList* child = shuffledNeighbors[i];
		if (child->endPoint->visited == 0) {
			++unassignedNeighborsOfRoot;
			++processedNeighborsOfRoot;
			for (struct VertexList* embeddingEdge=currentImage->neighborhood; embeddingEdge!=NULL; embeddingEdge=embeddingEdge->next) {
				if ((embeddingEdge->endPoint->visited == 0)
						&& (labelCmp(child->label, embeddingEdge->label) == 0)
						&& (labelCmp(child->endPoint->label, embeddingEdge->endPoint->label) == 0)) {

					// if the child vertex and edge labels fit, map the vertices to each other, mark it as a novel assignment
					child->endPoint->visited = embeddingEdge->endPoint->number + 1;
					child->flag = 1;
					embeddingEdge->endPoint->visited = 1;

					// and remember that the child vertex was assigned
					--unassignedNeighborsOfRoot;
					break; //...looping over the neighbors of the image of parent in g
				}
			}
		}
	}

	// check, if we could assign all neighbors of the currentRoot to some neighbors of its image.
	if (unassignedNeighborsOfRoot > 0) {
		free(shuffledNeighbors);
		return 0;
	}

	// if none of the neighbors needs to be newly asigned, we have embedded this branch.
	if (processedNeighborsOfRoot == 0) {
		free(shuffledNeighbors);
		return 1;
	}

	// if new neighbors were assigned, recurse to the newly assigned neighbors
	char embeddingWorked = 1;
	for (int i=0; i<nNeighbors; ++i) {
		struct VertexList* child = shuffledNeighbors[i];
		if (child->flag) {
			child->flag = 0;
			embeddingWorked = recursiveSubtreeIsomorphismSampler(child->endPoint, g);
		}
		if (!embeddingWorked) {
			break;
		}
	}
	free(shuffledNeighbors);
	return embeddingWorked;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movq 0x10(%rsi), %rax
movslq 0x1c(%rdi), %rcx
movq -0x8(%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
callq 0x7c22
movl %eax, %r14d
movq %rbx, %rdi
movl %eax, %esi
callq 0x1be64
movq %rax, %rbp
xorl %r15d, %r15d
testl %r14d, %r14d
jle 0x1c0ee
movq %r12, 0x18(%rsp)
movl %r14d, 0x14(%rsp)
movl %r14d, %ecx
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %rcx, 0x8(%rsp)
movq %rbp, (%rsp)
movq (%rbp,%r14,8), %r12
movq 0x18(%r12), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x1c0ab
leal 0x1(%r15), %ebp
incl %r13d
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rbx
testq %rbx, %rbx
je 0x1c09f
movq 0x18(%rbx), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x1c099
movq (%r12), %rdi
movq (%rbx), %rsi
callq 0x1bc18
testl %eax, %eax
jne 0x1c099
movq 0x18(%r12), %rax
movq (%rax), %rdi
movq 0x18(%rbx), %rax
movq (%rax), %rsi
callq 0x1bc18
testl %eax, %eax
je 0x1c0b5
movq 0x8(%rbx), %rbx
jmp 0x1c062
movl %ebp, %r15d
movq (%rsp), %rbp
movq 0x8(%rsp), %rcx
incq %r14
cmpq %rcx, %r14
jne 0x1c042
jmp 0x1c0d5
movq 0x18(%rbx), %rax
movl 0x18(%rax), %ecx
incl %ecx
movq 0x18(%r12), %rdx
movl %ecx, 0x1c(%rdx)
movl $0x1, %ecx
movl %ecx, 0x24(%r12)
movl %ecx, 0x1c(%rax)
jmp 0x1c0a2
testl %r15d, %r15d
setg %r15b
testl %r13d, %r13d
sete %al
movq 0x18(%rsp), %r12
movl 0x14(%rsp), %r14d
jmp 0x1c0f0
movb $0x1, %al
orb %r15b, %al
testb $0x1, %al
je 0x1c0fd
xorb $0x1, %r15b
jmp 0x1c145
testl %r14d, %r14d
jle 0x1c142
movl %r14d, %ebx
decq %rbx
movb $0x1, %r15b
xorl %r14d, %r14d
movq (%rbp,%r14,8), %rax
cmpl $0x0, 0x24(%rax)
je 0x1c12f
movl $0x0, 0x24(%rax)
movq 0x18(%rax), %rdi
movq %r12, %rsi
callq 0x1bfdf
movl %eax, %r15d
testb %r15b, %r15b
je 0x1c145
leaq 0x1(%r14), %rax
cmpq %r14, %rbx
movq %rax, %r14
jne 0x1c10e
jmp 0x1c145
movb $0x1, %r15b
movq %rbp, %rdi
callq 0x1030
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq