// File: /workspace/github_c_makefile_star2_O1/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, %r15
movl %esi, 0xc(%rsp)
movq %rdi, %rsi
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rsi, 0x20(%rsp)
movq %rcx, %rdx
callq 0xe85c
movslq 0x20(%r14), %r12
leaq (,%r12,8), %rdi
callq 0x1190
movq %rax, %r13
testq %r12, %r12
jle 0x104eb
xorl %r14d, %r14d
movq %r13, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq 0x48(%rsp), %rax
movq (%rax,%r14,8), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0xf237
movq %r12, %rdi
movq %r15, %rsi
callq 0x10297
movq %rax, %rbp
movl (%rax), %eax
decl %eax
cmpl %eax, 0x4(%rbp)
movq %r12, 0x28(%rsp)
jne 0x1047a
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x8376
movq %rax, %r12
jmp 0x104a6
movl 0xc(%rsp), %r15d
testl %r15d, %r15d
jle 0x104a3
xorl %r13d, %r13d
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x1ec39
movq %rax, %r12
movq %r13, 0x18(%rax)
movq %rax, %r13
decl %r15d
jne 0x10487
jmp 0x104a6
xorl %r12d, %r12d
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x877e
movq 0x18(%rsp), %r13
movq %r12, (%r13,%r14,8)
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x14b19
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x14832
incq %r14
movslq 0x50(%rsp), %r12
cmpq %r12, %r14
jl 0x1043a
movl %r12d, %r14d
movl $0x1, %ebp
testl %r12d, %r12d
jle 0x10521
xorl %eax, %eax
movq (%r13,%rax,8), %rdx
movl $0x0, %ecx
testq %rdx, %rdx
je 0x10516
xorl %ecx, %ecx
incl %ecx
movq 0x18(%rdx), %rdx
testq %rdx, %rdx
jne 0x1050b
imull %ecx, %ebp
incq %rax
cmpq %r14, %rax
jne 0x104fa
testl %r12d, %r12d
jle 0x1053e
xorl %r15d, %r15d
movq (%r13,%r15,8), %rsi
movq %rbx, %rdi
callq 0x14b75
incq %r15
cmpq %r15, %r14
jne 0x10529
movq %r13, %rdi
callq 0x1030
movq 0x40(%rsp), %rdi
callq 0x1030
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x48(%rsp), %rdi
callq 0x1030
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq