// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xb8, %rsp
movb %cl, %al
movq 0xc0(%rsp), %rcx
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movb %al, 0x9f(%rsp)
movq %r8, 0x90(%rsp)
movq %r9, 0x88(%rsp)
movl $0x0, 0x70(%rsp)
movq 0xb0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x10700
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movq 0xa8(%rsp), %rcx
callq 0x10d30
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x9c40
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
callq 0x2d520
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x44(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
je 0x5964
movq 0x38(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x78(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x11750
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0xbd60
movq %rax, 0x38(%rsp)
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x78(%rsp)
jmp 0x591e
cmpq $0x0, 0x38(%rsp)
je 0x59ce
movq 0x38(%rsp), %rax
movq 0x20(%rax), %rax
movq $0x0, 0x18(%rax)
movq 0x38(%rsp), %rax
movq $0x0, 0x20(%rax)
movq 0x38(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x6a00
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
callq 0x2d520
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x74(%rsp)
jmp 0x59d6
movl $0x0, 0x74(%rsp)
movl 0x44(%rsp), %eax
addl 0x74(%rsp), %eax
movq 0xc0(%rsp), %rcx
cmpl (%rcx), %eax
jle 0x5a35
movq 0x88(%rsp), %rax
cmpq $0x0, (%rax)
je 0x5a08
movq 0x88(%rsp), %rax
movq (%rax), %rdi
callq 0x1030
movl 0x44(%rsp), %edi
addl 0x74(%rsp), %edi
callq 0x2c430
movq %rax, %rcx
movq 0x88(%rsp), %rax
movq %rcx, (%rax)
movl 0x44(%rsp), %ecx
addl 0x74(%rsp), %ecx
movq 0xc0(%rsp), %rax
movl %ecx, (%rax)
movq 0x90(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x88(%rsp), %rax
movq (%rax), %rcx
movq 0x90(%rsp), %r9
movq 0xa0(%rsp), %rax
movl $0x1, %edx
leaq 0x70(%rsp), %r8
xorl %r10d, %r10d
movl $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x2c650
cmpq $0x0, 0x28(%rsp)
je 0x5ac9
movq 0x90(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x88(%rsp), %rax
movq (%rax), %rcx
movq 0x90(%rsp), %r9
movq 0xa0(%rsp), %rax
movl $0x2, %edx
leaq 0x70(%rsp), %r8
xorl %r10d, %r10d
movl $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x2c650
movq 0x88(%rsp), %rax
movq (%rax), %rdi
movslq 0x70(%rsp), %rsi
leaq 0x26970(%rip), %rcx     # 0x2c450
movl $0xc, %edx
callq 0x1060
movsbl 0x9f(%rsp), %eax
addl $-0x61, %eax
movl %eax, %ecx
movq %rcx, 0x10(%rsp)
subl $0x15, %eax
ja 0x5c62
movq 0x10(%rsp), %rax
leaq 0x2f5c7(%rip), %rcx     # 0x350d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0xb0(%rsp), %rax
movl 0xc(%rax), %esi
leaq 0x30ac8(%rip), %rdi     # 0x365f4
movb $0x0, %al
callq 0x1090
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x70(%rsp), %eax
jge 0x5b93
movq 0x88(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %esi
movq 0x88(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edx
leaq 0x2f5b1(%rip), %rdi     # 0x35130
movb $0x0, %al
callq 0x1090
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x5b3b
leaq 0x2f51b(%rip), %rdi     # 0x350b5
movb $0x0, %al
callq 0x1090
jmp 0x5c62
cmpq $0x0, 0x28(%rsp)
je 0x5bdb
movq 0xb0(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x28(%rsp), %rax
movl 0x18(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %edx
leaq 0x30471(%rip), %rdi     # 0x36043
movb $0x0, %al
callq 0x1090
jmp 0x5bf4
movq 0xb0(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0x2f54a(%rip), %rdi     # 0x35137
movb $0x0, %al
callq 0x1090
jmp 0x5c62
movq 0xb0(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x48(%rsp), %rax
movl 0x18(%rax), %edx
leaq 0x30433(%rip), %rdi     # 0x36043
movb $0x0, %al
callq 0x1090
jmp 0x5c62
movq 0xb0(%rsp), %rax
movl 0x8(%rax), %esi
movq 0xb0(%rsp), %rax
movl (%rax), %edx
leaq 0x3040e(%rip), %rdi     # 0x36043
movb $0x0, %al
callq 0x1090
jmp 0x5c62
movq 0xb0(%rsp), %rax
movl 0x8(%rax), %esi
movq 0xb0(%rsp), %rax
movl 0x4(%rax), %edx
leaq 0x303e8(%rip), %rdi     # 0x36043
movb $0x0, %al
callq 0x1090
cmpq $0x0, 0x28(%rsp)
je 0x5c7c
movq 0xa0(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x2d3c0
movq 0x58(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
je 0x5cc0
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x80(%rsp)
movq 0xa0(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1fb30
movq 0x80(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x5c86
movq 0xa0(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x2d3c0
movq 0xa0(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x1fb30
xorl %eax, %eax
addq $0xb8, %rsp
retq
nop