// File: /workspace/github_c_makefile_star2_O2/pwelke[P]GraphMiningTools/lwmr_miningAndExtension.c
// Function: DFSStrategyRooted
void DFSStrategyRooted(size_t startPatternSize,
					  size_t maxPatternSize,
		              size_t threshold,
					  struct Vertex* initialFrequentPatterns,
					  struct SupportSet* supportSets,
					  struct ShallowGraph* extensionEdges,
					  // embedding operator function pointer,
					  struct SubtreeIsoDataStore (*embeddingOperator)(struct SubtreeIsoDataStore, struct Graph*, double, struct GraphPool*, struct ShallowGraphPool*),
					  double importance,
					  FILE* featureStream,
					  FILE* patternStream,
					  FILE* logStream,
					  struct GraphPool* gp,
					  struct ShallowGraphPool* sgp) {

	const char dontUseApriori = 0;

	// init an array of searchtrees, one for each level
	// this should speed up the alg but will increase memory consumption in the long run
	struct Vertex** levelSearchTrees = malloc(maxPatternSize * sizeof(struct Vertex*));
	for (size_t i=0; i<maxPatternSize; ++i) {
		if (i != startPatternSize - 1) {
			levelSearchTrees[i] = getVertex(gp->vertexPool);
		} else {
			levelSearchTrees[i] = initialFrequentPatterns;
		}
	}

	// the frontier of candidate patterns
	struct SupportSet* candidateSupportSetStack = supportSets;
	size_t patternID = initialFrequentPatterns->lowPoint;

	while (candidateSupportSetStack) {

		// support set of current pattern, pattern, and pattern size
		struct SupportSet* currentPatternSupport = popSupportSet(&candidateSupportSetStack);
		struct Graph *currentPattern = currentPatternSupport->first->data.h;
		size_t currentPatternSize = currentPattern->n;

		// we extend all patterns that have at most maxPatternSize - 1 vertices
		if (currentPatternSize < maxPatternSize) {

			// logging
			fprintf(logStream, "Processing pattern %i with %zu vertices:\n", currentPattern->number, currentPatternSize);
			fflush(logStream);

			// extend current pattern and check frequencies
			struct Vertex* newFrequentPatternSearchTree = getVertex(gp->vertexPool);
			offsetSearchTreeIds(newFrequentPatternSearchTree, patternID);
			// TODO add infrequent patterns to some other search tree and output.
			struct SupportSet *newSupportSets = _getNextLevelRooted(currentPatternSupport, levelSearchTrees[currentPatternSize - 1], levelSearchTrees[currentPatternSize], threshold, extensionEdges, embeddingOperator, importance, dontUseApriori, &newFrequentPatternSearchTree, logStream, gp, sgp);
			patternID = newFrequentPatternSearchTree->lowPoint;

			// print frequent patterns
			printStringsInSearchTree(newFrequentPatternSearchTree, patternStream, sgp);
			fflush(patternStream);
			printSupportSetsSparse(newSupportSets, featureStream);
			fflush(featureStream);

			// add patterns to frequent pattern list and support sets to frontier
			mergeSearchTrees(levelSearchTrees[currentPatternSize], 
							 newFrequentPatternSearchTree, 
							 1, 
							 NULL, // &searchTreeHelper, don't need results here 
							 0, // &searchTreePosition, don't need results here
							 levelSearchTrees[currentPatternSize], 
							 0, 
							 gp);
			dumpSearchTree(gp, newFrequentPatternSearchTree);
			candidateSupportSetStack = appendSupportSets(newSupportSets, candidateSupportSetStack);
		} else {
			// dump support of the pattern, as this is not done by the extension
			dumpSupportSetCopy(currentPatternSupport);
			dumpGraph(gp, currentPattern);
		}
		if (currentPatternSize == 1) {
			// dumpSupportSet(currentPatternSupport);
			dumpGraph(gp, currentPattern);
		}
	}

	// garbage collection: TODO
	for (size_t i = 0; i < maxPatternSize; ++i) {
		if (i != startPatternSize - 1) {
			dumpSearchTree(gp, levelSearchTrees[i]);
		}
	}
	free(levelSearchTrees);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq %r8, %r13
movq %rcx, %r15
movq %rdx, 0x20(%rsp)
movq %rdi, %r12
movq %rsi, 0x18(%rsp)
leaq (,%rsi,8), %rdi
callq 0x1180
movq %rax, 0x8(%rsp)
decq %r12
xorl %r14d, %r14d
cmpq %r14, 0x18(%rsp)
je 0x11049
movq %r15, %rax
cmpq %r14, %r12
je 0x1103b
movq 0xa0(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x114a5
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
jmp 0x1101b
leaq 0x10(%rsp), %rbx
movq %r13, (%rbx)
movl 0x20(%r15), %eax
movl %eax, 0x4(%rsp)
movq %r12, 0x38(%rsp)
testq %r13, %r13
je 0x111fa
movq %rbx, %rdi
callq 0x1a6dc
movq %rax, %r12
movq (%rax), %rax
movq 0x10(%rax), %r13
movslq (%r13), %rbp
cmpq 0x18(%rsp), %rbp
jae 0x111be
movl 0x8(%r13), %edx
movq 0x98(%rsp), %rbx
movq %rbx, %rdi
leaq 0xc963(%rip), %rsi      # 0x1da01
movq %rbp, %rcx
xorl %eax, %eax
callq 0x1150
movq %rbx, %rdi
callq 0x1190
movq 0xa0(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x114a5
movq %rax, %r15
movq %rax, 0x40(%rsp)
movq %rax, %rdi
movl 0x4(%rsp), %esi
callq 0x18943
movq 0x8(%rsp), %rax
movq -0x8(%rax,%rbp,8), %rsi
movq (%rax,%rbp,8), %r14
subq $0x8, %rsp
movq %r12, %rdi
movq %r14, %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x88(%rsp), %r9
movsd 0x38(%rsp), %xmm0
movq 0xb0(%rsp), %rbx
pushq %rbx
pushq 0xb0(%rsp)
pushq 0xb0(%rsp)
leaq 0x60(%rsp), %rax
pushq %rax
pushq $0x0
callq 0x10adc
addq $0x30, %rsp
movq %rax, %r12
movl 0x20(%r15), %eax
movl %eax, 0x4(%rsp)
movq %r15, %rdi
movq 0x90(%rsp), %rbx
movq %rbx, %rsi
movq 0xa8(%rsp), %rdx
callq 0x18fd2
movq %rbx, %rdi
callq 0x1190
movq %r12, %rdi
movq 0x88(%rsp), %rbx
movq %rbx, %rsi
callq 0x1a3b4
movq %rbx, %rdi
leaq 0x10(%rsp), %rbx
callq 0x1190
movq %r14, %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r14, %r9
movq 0xa0(%rsp), %r14
pushq %r14
pushq $0x0
callq 0x1877e
popq %rax
popq %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x18dcd
movq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x1a6bc
movq %rax, 0x10(%rsp)
jmp 0x111d6
movq %r12, %rdi
callq 0x1a402
movq 0xa0(%rsp), %rdi
movq %r13, %rsi
callq 0x1166e
cmpl $0x1, %ebp
movq 0x38(%rsp), %r12
jne 0x111f0
movq 0xa0(%rsp), %rdi
movq %r13, %rsi
callq 0x1166e
movq 0x10(%rsp), %r13
jmp 0x1105e
xorl %r14d, %r14d
cmpq %r14, 0x18(%rsp)
je 0x11224
cmpq %r14, %r12
je 0x1121f
movq 0x8(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq 0xa0(%rsp), %rdi
callq 0x18dcd
incq %r14
jmp 0x111fd
movq 0x8(%rsp), %rdi
callq 0x1030
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)