// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movsd %xmm0, 0x40(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, %r15
movq %rcx, %r14
movq %rdx, 0x30(%rsp)
movq %rsi, %rbx
movq %rdi, 0x20(%rsp)
movq 0xb0(%rsp), %rbp
leaq (,%rsi,8), %rdi
callq 0x1190
movq %rax, 0x18(%rsp)
testq %rbx, %rbx
je 0x13e70
movq 0x20(%rsp), %rax
leaq -0x1(%rax), %r12
xorl %r13d, %r13d
movq %r14, %rax
cmpq %r13, %r12
je 0x13e5f
movq 0x10(%rbp), %rdi
callq 0x1447e
movq 0x18(%rsp), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
cmpq %r13, %rbx
jne 0x13e4e
movq %r15, 0x8(%rsp)
testq %r15, %r15
je 0x14018
movl 0x20(%r14), %eax
movl %eax, 0x14(%rsp)
leaq 0x8(%rsp), %r15
movq %rbx, 0x28(%rsp)
movq %r15, %rdi
callq 0x1d1b7
movq %rax, %r12
movq (%rax), %rax
movq 0x10(%rax), %r13
movslq (%r13), %r14
cmpq %rbx, %r14
jae 0x13fe8
movl 0x8(%r13), %edx
movq 0xa8(%rsp), %r15
movq %r15, %rdi
leaq 0xcb71(%rip), %rsi      # 0x20a36
movq %r14, %rcx
xorl %eax, %eax
callq 0x1160
movq %r15, %rdi
callq 0x11a0
movq 0x10(%rbp), %rdi
callq 0x1447e
movq %rbp, %rbx
movq %rax, %rbp
movq %rax, 0x50(%rsp)
movq %rax, %rdi
movl 0x14(%rsp), %esi
callq 0x1b0bb
movq 0x18(%rsp), %rax
movq -0x8(%rax,%r14,8), %rsi
movq (%rax,%r14,8), %r15
subq $0x8, %rsp
movq %r12, %rdi
movq %r15, %rdx
movq 0x38(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x98(%rsp), %r9
movsd 0x48(%rsp), %xmm0
movq 0xc0(%rsp), %rax
pushq %rax
pushq %rbx
pushq 0xc0(%rsp)
leaq 0x70(%rsp), %rax
pushq %rax
xorl %eax, %eax
pushq %rax
callq 0x138a5
addq $0x30, %rsp
movq %r13, 0x48(%rsp)
movq %rax, %r13
movl 0x20(%rbp), %eax
movl %eax, 0x14(%rsp)
movq %rbp, %rdi
movq 0xa0(%rsp), %r12
movq %r12, %rsi
movq 0xb8(%rsp), %rdx
callq 0x1b764
movq %r12, %rdi
callq 0x11a0
movq %r13, %rdi
movq 0x98(%rsp), %r12
movq %r12, %rsi
callq 0x1cdf7
movq %r12, %rdi
callq 0x11a0
movq %r15, %rdi
movq %rbp, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r15, %r9
leaq 0x8(%rsp), %r15
pushq %rbx
xorl %eax, %eax
pushq %rax
callq 0x1aefc
addq $0x10, %rsp
movq %rbx, %rdi
movq %rbp, %rsi
movq %rbx, %rbp
movq 0x28(%rsp), %rbx
callq 0x1b546
movq 0x8(%rsp), %rsi
movq %r13, %rdi
movq 0x48(%rsp), %r13
callq 0x1d197
movq %rax, 0x8(%rsp)
jmp 0x13ffb
movq %r12, %rdi
callq 0x1ce4c
movq %rbp, %rdi
movq %r13, %rsi
callq 0x14700
cmpl $0x1, %r14d
jne 0x1400c
movq %rbp, %rdi
movq %r13, %rsi
callq 0x14700
cmpq $0x0, 0x8(%rsp)
jne 0x13e90
testq %rbx, %rbx
movq 0x20(%rsp), %r15
je 0x14046
decq %r15
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x1403e
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq %rbp, %rdi
callq 0x1b546
incq %r14
cmpq %r14, %rbx
jne 0x14028
movq 0x18(%rsp), %rdi
callq 0x1030
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop