// File: /workspace/github_c_makefile_star2_O2/pwelke[P]GraphMiningTools/localEasySubtreeIsomorphism.c
// Function: getNumberOfSpanningTreesObtainedByLocalEasySampling
int getNumberOfSpanningTreesObtainedByLocalEasySampling(struct Graph* g, int k, struct GraphPool* gp, struct ShallowGraphPool* sgp) {

	struct BlockTree blockTree = getBlockTreeT(g, sgp);
	int numberOfConsideredSpanningTrees = 1;

	struct ShallowGraph** localSpanningTrees = malloc(blockTree.nRoots * sizeof(struct Graph*));

	for (int v=0; v<blockTree.nRoots; ++v) {
		struct ShallowGraph* mergedEdges = mergeShallowGraphs(blockTree.vRootedBlocks[v], sgp);
		struct Graph* mergedGraph = blockConverterKeepIDs(mergedEdges, gp);

		struct ShallowGraph* shallowSpanningtrees = NULL;
		if (mergedGraph->m != mergedGraph->n-1) {
			// sample spanning trees according to parameter
			for (int i=0; i<k; ++i) {
				struct ShallowGraph* spt = randomSpanningTreeAsShallowGraph(mergedGraph, sgp);
				spt->next = shallowSpanningtrees;
				shallowSpanningtrees = spt;
			}

			/* Duplicate spanning trees are NOT filtered here.
			 * In contrast to normal spanning tree sampling, here we can only filter identical trees (seen as edge sets)
			 * and not trees up to isomorphism, as two isomorphic but different local spanning trees might result in different
			 * (and hence possibly nonisomorphic) global spanning trees, when combined. */
//			shallowSpanningtrees = filterDuplicateSpanningTrees(shallowSpanningtrees, sgp);

		} else {
			// if the mergedGraph is a tree, we use it directly
			shallowSpanningtrees = getGraphEdges(mergedGraph, sgp);
		}

		// make the shallowgraphs be subgraphs of g, not of mergedGraph
		rebaseShallowGraphsOnLowPoints(shallowSpanningtrees, g);

		localSpanningTrees[v] = shallowSpanningtrees;

		// garbage collection
		dumpShallowGraph(sgp, mergedEdges);
		dumpGraph(gp, mergedGraph);

	}

	for (int i=0; i<blockTree.nRoots; ++i) {
		int j=0;
		for (struct ShallowGraph* localTree=localSpanningTrees[i]; localTree!=NULL; localTree=localTree->next) {
			++j;
		}

		int tmp = numberOfConsideredSpanningTrees * j;
		if (j != 0 && tmp / j != numberOfConsideredSpanningTrees) {
			numberOfConsideredSpanningTrees = RAND_MAX;
			break;
		} else {
			numberOfConsideredSpanningTrees = tmp;
		}
	}

	//garbage collection
	for (int v=0; v<blockTree.nRoots; ++v) {
		dumpShallowGraphCycle(sgp, localSpanningTrees[v]);
	}
	free(localSpanningTrees);
	free(blockTree.parents);
	free(blockTree.roots);
	free(blockTree.vRootedBlocks);

	return numberOfConsideredSpanningTrees;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %rbx
movq %rdx, 0x10(%rsp)
movl %esi, %ebp
movq %rdi, %rsi
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rsi, 0x20(%rsp)
movq %rcx, %rdx
callq 0xbe98
movslq 0x20(%r14), %r12
movq %r12, %rdi
shlq $0x3, %rdi
callq 0x1180
movq %rax, %rcx
movq 0x18(%r14), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
testl %ebp, %ebp
cmovlel %r15d, %ebp
movl %ebp, 0x4(%rsp)
testl %r12d, %r12d
movl $0x0, %r13d
cmovgl %r12d, %r13d
movq %rcx, %r12
movq %rcx, 0x18(%rsp)
movq %r13, 0x28(%rsp)
cmpq %r13, %r15
je 0xd931
movq 0x8(%rsp), %rax
movq (%rax,%r15,8), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc7c2
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0xd6ca
movq %rax, %r14
movl (%rax), %eax
decl %eax
cmpl %eax, 0x4(%r14)
jne 0xd8d4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6172
movq %rax, %r13
jmp 0xd8fb
movl 0x4(%rsp), %r12d
xorl %r13d, %r13d
subl $0x1, %r12d
jb 0xd8f6
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1bdd7
movq %r13, 0x18(%rax)
movq %rax, %r13
jmp 0xd8dc
movq 0x18(%rsp), %r12
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x6529
movq %r13, (%r12,%r15,8)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1184e
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1166e
incq %r15
movq 0x28(%rsp), %r13
jmp 0xd88d
pushq $0x1
popq %rbp
xorl %eax, %eax
cmpq %r13, %rax
je 0xd958
leaq (%r12,%rax,8), %rdx
xorl %ecx, %ecx
movq (%rdx), %rdx
testq %rdx, %rdx
je 0xd951
addq $0x18, %rdx
addl %ebp, %ecx
jmp 0xd941
incq %rax
movl %ecx, %ebp
jmp 0xd936
xorl %r14d, %r14d
cmpq %r14, %r13
je 0xd971
movq (%r12,%r14,8), %rsi
movq %rbx, %rdi
callq 0x11888
incq %r14
jmp 0xd95b
movq %r12, %rdi
callq 0x1030
movq 0x40(%rsp), %rdi
callq 0x1030
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x8(%rsp), %rdi
callq 0x1030
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq