// File: /workspace/github_c_makefile_star2_O2/pwelke[P]GraphMiningTools/hp_cactus.c
// Function: isThisCactusTraceable
char isThisCactusTraceable(struct Graph* g, struct ShallowGraphPool* sgp) {
	struct ShallowGraph* biconnectedComponents;
	int* criticality;
	int v; 
	struct ShallowGraph* c;

	/* the empty graph and singleton vertices are traceable cactus graphs */
	if (g->n <= 1) {
		return 1;
	}

	biconnectedComponents = listBiconnectedComponents(g, sgp);
	criticality = computeCriticality(biconnectedComponents, g->n);

	// test if every vertex has criticality at most 2
	for (v=0; v<g->n; ++v) {
		if (criticality[v] > 2) {
			free(criticality);
			dumpShallowGraphCycle(sgp, biconnectedComponents);
			return 0;
		}
	}

	// test if every biconnected component contains at most two critical vertices.
	for (c=biconnectedComponents; c!=NULL; c=c->next) {
		struct VertexList* e;
		int criticalVertexCount = 0;

		// TODO measure if the alg is faster with or without this line
		if (c->m == 1) continue;

		// init ->visited = 0
		for (e=c->edges; e!=NULL; e=e->next) {
			e->startPoint->visited = 0;
			e->endPoint->visited = 0;
		}
		// count critical vertices
		for (e=c->edges; e!=NULL; e=e->next) {
			criticalVertexCount += visit(e->startPoint, criticality);
			criticalVertexCount += visit(e->endPoint, criticality);
		}
		if (criticalVertexCount > 2) {
			free(criticality);
			dumpShallowGraphCycle(sgp, biconnectedComponents);
			return 0;
		 } else {
			if (criticalVertexCount == 2) {
				char notFound = 1;
				// search for the edge that contains both critical vertices
				for (e=c->edges; e!=NULL; e=e->next) {
					if ((criticality[e->startPoint->number] > 1) && (criticality[e->endPoint->number] > 1)) {
						notFound = 0;
						break;
					} 
				}
				if (notFound) {
					free(criticality);
					dumpShallowGraphCycle(sgp, biconnectedComponents);
					return 0;
				}
			}
		}
	}

	// // TODO is there a bug below? if there is only one critical vertex, then the method cannot find the edge its looking for

	// // test if the critical vertices of each block are adjacent
	// for (c=biconnectedComponents; c!=NULL; c=c->next) {
	// 	struct VertexList* e;
	// 	char notFound = 1;

	// 	// TODO measure if the alg is faster with or without this line
	// 	if (c->m == 1) continue;

	// 	// search for the edge that contains both critical vertices
	// 	for (e=c->edges; e!=NULL; e=e->next) {
	// 		if ((criticality[e->startPoint->number] > 1) && (criticality[e->endPoint->number] > 1)) {
	// 			notFound = 0;
	// 			break;
	// 		} 
	// 	}
	// 	if (notFound) {
	// 		free(criticality);
	// 		dumpShallowGraphCycle(sgp, biconnectedComponents);
	// 		return 0;
	// 	}
	// }

	// otherwise, there exists a Hamiltonian path
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movb $0x1, %bpl
cmpl $0x2, (%rdi)
jl 0x7022
movq %rsi, %rbx
movq %rdi, %r15
callq 0x8ccd
movq %rax, %r14
movl (%r15), %esi
movq %rax, %rdi
callq 0x8b10
movl (%r15), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
cmpq %rdx, %rcx
je 0x702f
cmpl $0x3, (%rax,%rdx,4)
leaq 0x1(%rdx), %rdx
jl 0x6ffe
movq %rax, %rdi
callq 0x1030
movq %rbx, %rdi
movq %r14, %rsi
callq 0x11888
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rcx
movq %r14, %rdx
testq %rdx, %rdx
je 0x7022
cmpl $0x1, 0x10(%rdx)
je 0x7108
movq %rdx, %rsi
movq (%rsi), %rsi
testq %rsi, %rsi
je 0x7065
movq 0x10(%rsi), %rdi
andl $0x0, 0x1c(%rdi)
movq 0x18(%rsi), %rdi
andl $0x0, 0x1c(%rdi)
addq $0x8, %rsi
jmp 0x7047
xorl %esi, %esi
movq %rdx, %rdi
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x70c9
movq 0x10(%rdi), %r8
cmpl $0x0, 0x1c(%r8)
jne 0x7093
movl $0x1, 0x1c(%r8)
movslq 0x18(%r8), %r8
cmpl $0x1, (%rax,%r8,4)
movl %ecx, %r8d
jg 0x7096
xorl %r8d, %r8d
movq 0x18(%rdi), %r9
cmpl $0x0, 0x1c(%r9)
jne 0x70b7
movl $0x1, 0x1c(%r9)
movslq 0x18(%r9), %r9
cmpl $0x1, (%rax,%r9,4)
movl %ecx, %r9d
jg 0x70ba
xorl %r9d, %r9d
addl %esi, %r8d
addl %r9d, %r8d
addq $0x8, %rdi
movl %r8d, %esi
jmp 0x706a
cmpl $0x3, %esi
jge 0x700d
cmpl $0x2, %esi
jne 0x7108
movq %rdx, %rsi
movq (%rsi), %rsi
testq %rsi, %rsi
je 0x700d
movq 0x10(%rsi), %rdi
movslq 0x18(%rdi), %rdi
cmpl $0x2, (%rax,%rdi,4)
jl 0x7102
movq 0x18(%rsi), %rdi
movslq 0x18(%rdi), %rdi
cmpl $0x1, (%rax,%rdi,4)
jg 0x7108
addq $0x8, %rsi
jmp 0x70da
movq 0x18(%rdx), %rdx
jmp 0x7035