// File: /workspace/github_c_makefile_star2_O3/pwelke[P]GraphMiningTools/cpk.c
// Function: CyclicPatternKernel
int CyclicPatternKernel(struct Graph *g, struct ShallowGraphPool *sgp, struct GraphPool *gp,
		char outputOptions, struct Vertex* globalPatternSet, struct compInfo** results, int* resSize) {
	struct Graph* tmp;
	struct Graph* idx;
	int numCycles;
	int pos = 0;

	/* find biconnected Components */
	struct ShallowGraph* h = listBiconnectedComponents(g, sgp);
	struct Graph* forest = partitionIntoForestAndCycles(h, g, gp, sgp);
	/* TODO refactor */
	struct Graph* biconnectedComponents = forest->next;

	/* list tree patterns */
	struct ShallowGraph* treePatterns = getTreePatterns(forest, sgp);

	/* create search tree structure */
	struct Vertex* treePatternSearchTree = buildSearchTree(treePatterns, gp, sgp);
	int numTrees = treePatternSearchTree->number;

	/* list all cycles */
	struct ShallowGraph* simpleCycles = NULL;
	struct ShallowGraph* cyclePatterns = NULL;
	struct Vertex* cyclePatternSearchTree = NULL;

	for (idx=biconnectedComponents; idx; idx=idx->next) {
		simpleCycles = addComponent(simpleCycles, listCycles(idx, sgp));
	}

	/* if cycles were found, compute canonical strings */
	if (simpleCycles) {
		/* transform cycle of shallow graphs to a list */
		simpleCycles->prev->next = NULL;
		simpleCycles->prev = NULL;

		cyclePatterns = getCyclePatterns(simpleCycles, sgp);
		cyclePatternSearchTree = buildSearchTree(cyclePatterns, gp, sgp);

		numCycles = cyclePatternSearchTree->number;
	} else {
		numCycles = 0;
	}



	if (numTrees + numCycles > *resSize) {
		if (*results) {
			free(*results);
		}

		*results = getResultVector(numTrees + numCycles);
		*resSize = numTrees + numCycles;
	}


	

	/* add elements to global search trees to obtain mapping from strings to integers */
	mergeSearchTrees(globalPatternSet, treePatternSearchTree, 1, *results, &pos, globalPatternSet, 0, gp);
	if (cyclePatternSearchTree) {
		mergeSearchTrees(globalPatternSet, cyclePatternSearchTree, 2, *results, &pos, globalPatternSet, 0, gp);
	}

	/* sort the output elements by increasing id */
	qsort(*results, pos, sizeof(struct compInfo), &compInfoComparison);

	/* output options */
	switch (outputOptions) {
	int i;

	case 'a':
		/* "all" output the feature vector for the current graph in the format specified
		 * by SVMlight g->activity is assumed to be either 0, 1 or -1 to be compliant with
		 * the specs of SVMlight.
		 */

		/* print the id and label of the graph */
		printf("%i", g->activity);

		/* print key:value pairs */
		for (i=0; i<pos; ++i) {
			printf(" %i:%i", (*results)[i].id, (*results)[i].count);
		}
		printf("\n");
		break;
	case 'c':
		/* returns the number of cycles found in the graph */
		if (cyclePatternSearchTree)
			printf("%i %i\n", g->number, cyclePatternSearchTree->number / 2);
		else
			printf("%i 0\n", g->number);
		break;
	case 't':
		/* returns the number of trees found in the graph */
		printf("%i %i\n", g->number, treePatternSearchTree->number);
		break;
	case 'v':
		/* returns the number of vertices in the graph */
		printf("%i %i\n", g->number, g->n);
		break;
	case 'e':
		/* returns the number of edges in the graph */
		printf("%i %i\n", g->number, g->m);
		break;
	}


	/* garbage collection */

	/* dump cycles, if any
	 * TODO may be moved upwards directly after finding simple cycles */
	if (cyclePatternSearchTree) {
			dumpSearchTree(gp, cyclePatternSearchTree);
	}

	/* dump biconnected components list */
	for (idx=biconnectedComponents; idx; idx=tmp) {
		tmp = idx->next;
		dumpGraph(gp, idx);
	}

	dumpSearchTree(gp, treePatternSearchTree);
	dumpGraph(gp, forest);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r13
movq %r8, 0x38(%rsp)
movl %ecx, 0x44(%rsp)
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r14
movl $0x0, 0xc(%rsp)
callq 0xb23b
movq %rax, %rdi
movq %r14, 0x10(%rsp)
movq %r14, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0xb57c
movq %rax, %r15
movq 0x18(%rax), %r12
movq %rax, %rdi
movq %rbp, %rsi
callq 0x77e2
movq %rax, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1b614
movq %rax, 0x30(%rsp)
movl 0x18(%rax), %eax
testq %r12, %r12
movq %r15, 0x20(%rsp)
je 0x54f3
movl %eax, 0x1c(%rsp)
xorl %r14d, %r14d
movq %r12, %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0xbb0d
movq %r14, %rdi
movq %rax, %rsi
callq 0x883e
movq %rax, %r14
movq 0x18(%r15), %r15
testq %r15, %r15
jne 0x549a
testq %r14, %r14
je 0x550b
movq 0x20(%r14), %rax
xorl %ecx, %ecx
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%r14)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x5de1
movq %rax, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1b614
movq %rax, 0x28(%rsp)
movl 0x18(%rax), %r14d
jmp 0x5517
xorl %r14d, %r14d
movq $0x0, 0x28(%rsp)
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %rdi
jmp 0x5525
xorl %r14d, %r14d
movq $0x0, 0x28(%rsp)
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %rdi
movl 0x1c(%rsp), %eax
addl %eax, %r14d
movq (%r13), %rcx
movq 0x80(%rsp), %rax
cmpl (%rax), %r14d
jle 0x5565
testq %rcx, %rcx
je 0x5546
movq %rcx, %rdi
callq 0x1030
movl %r14d, %edi
callq 0x1ada1
movq %rax, %rcx
movq %rax, (%r13)
movq 0x80(%rsp), %rax
movl %r14d, (%rax)
movq 0x38(%rsp), %rdi
leaq 0xc(%rsp), %r8
movq %rbp, %r15
movq %rbp, %rsi
movl $0x1, %edx
movq %rdi, %rbp
movq %rdi, %r9
pushq %rbx
pushq $0x0
callq 0x1aefc
addq $0x10, %rsp
movq 0x28(%rsp), %r14
testq %r14, %r14
je 0x55b4
movq (%r13), %rcx
leaq 0xc(%rsp), %r8
movq %rbp, %rdi
movq %r14, %rsi
movl $0x2, %edx
movq %rbp, %r9
pushq %rbx
pushq $0x0
callq 0x1aefc
addq $0x10, %rsp
movq (%r13), %rdi
movslq 0xc(%rsp), %rsi
leaq 0x157ed(%rip), %rcx     # 0x1adb1
movl $0xc, %edx
callq 0x1080
movsbl 0x44(%rsp), %eax
cmpl $0x64, %eax
jle 0x5603
cmpl $0x65, %eax
movq %r15, %rbp
je 0x564e
cmpl $0x74, %eax
movq 0x20(%rsp), %r15
je 0x5671
cmpl $0x76, %eax
jne 0x56f9
movq 0x10(%rsp), %rax
movl (%rax), %edx
movl 0x8(%rax), %esi
jmp 0x567c
cmpl $0x61, %eax
je 0x568c
movq %r15, %rbp
cmpl $0x63, %eax
movq 0x20(%rsp), %r15
jne 0x56f9
movq 0x10(%rsp), %rax
movl 0x8(%rax), %esi
testq %r14, %r14
je 0x574d
movl 0x18(%r14), %eax
movl %eax, %edx
shrl $0x1f, %edx
addl %eax, %edx
sarl %edx
leaq 0x1b9a9(%rip), %rdi     # 0x20feb
xorl %eax, %eax
callq 0x10b0
jmp 0x56fe
movq 0x10(%rsp), %rax
movl 0x4(%rax), %edx
movl 0x8(%rax), %esi
leaq 0x1b98b(%rip), %rdi     # 0x20feb
xorl %eax, %eax
callq 0x10b0
movq 0x20(%rsp), %r15
jmp 0x56f9
movq 0x10(%rsp), %rax
movl 0x8(%rax), %esi
movl 0x18(%rbp), %edx
leaq 0x1b968(%rip), %rdi     # 0x20feb
xorl %eax, %eax
callq 0x10b0
jmp 0x56f9
movq 0x10(%rsp), %rax
movl 0xc(%rax), %esi
leaq 0x1bf0f(%rip), %rdi     # 0x215aa
xorl %eax, %eax
callq 0x10b0
cmpl $0x0, 0xc(%rsp)
jle 0x56e0
movl $0x4, %r15d
leaq 0x1aa22(%rip), %r14     # 0x200d8
xorl %ebp, %ebp
movq (%r13), %rax
movl -0x4(%rax,%r15), %esi
movl (%rax,%r15), %edx
movq %r14, %rdi
xorl %eax, %eax
callq 0x10b0
incq %rbp
movslq 0xc(%rsp), %rax
addq $0xc, %r15
cmpq %rax, %rbp
jl 0x56b8
movl $0xa, %edi
callq 0x1040
movq 0x20(%rsp), %r15
movq 0x30(%rsp), %rbp
movq 0x28(%rsp), %r14
testq %r14, %r14
je 0x5709
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1b546
testq %r12, %r12
je 0x5726
movq 0x18(%r12), %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x14700
movq %r14, %r12
testq %r14, %r14
jne 0x570e
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1b546
movq %rbx, %rdi
movq %r15, %rsi
callq 0x14700
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a98b(%rip), %rdi     # 0x200df
xorl %eax, %eax
callq 0x10b0
jmp 0x5709