// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movb $0x1, %bl
cmpl $0x2, (%rdi)
jl 0x9496
movq %rsi, %r14
movq %rdi, %r12
callq 0xb23b
movq %rax, %r15
movl (%r12), %esi
movq %rax, %rdi
callq 0xb068
movslq (%r12), %rcx
testq %rcx, %rcx
jle 0x9396
xorl %edx, %edx
cmpl $0x3, (%rax,%rdx,4)
jge 0x9481
incq %rdx
cmpq %rdx, %rcx
jne 0x9384
testq %r15, %r15
je 0x9496
xorl %ecx, %ecx
movq %r15, %rdx
cmpl $0x1, 0x10(%rdx)
je 0x9472
movq (%rdx), %rsi
movq %rsi, %rdi
testq %rsi, %rsi
je 0x9472
movq 0x10(%rdi), %r8
movl %ecx, 0x1c(%r8)
movq 0x18(%rdi), %r8
movl %ecx, 0x1c(%r8)
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
jne 0x93bd
testq %rsi, %rsi
je 0x9472
xorl %edi, %edi
movq %rsi, %r8
movq 0x10(%r8), %r9
cmpl $0x0, 0x1c(%r9)
jne 0x9408
movl $0x1, 0x1c(%r9)
movslq 0x18(%r9), %r10
movl $0x1, %r9d
cmpl $0x1, (%rax,%r10,4)
jg 0x940b
xorl %r9d, %r9d
movq 0x18(%r8), %r10
cmpl $0x0, 0x1c(%r10)
jne 0x942f
movl $0x1, 0x1c(%r10)
movslq 0x18(%r10), %r11
movl $0x1, %r10d
cmpl $0x1, (%rax,%r11,4)
jg 0x9432
xorl %r10d, %r10d
addl %r9d, %edi
addl %r10d, %edi
movq 0x8(%r8), %r8
testq %r8, %r8
jne 0x93e4
cmpl $0x3, %edi
jge 0x9481
cmpl $0x2, %edi
jne 0x9472
testq %rsi, %rsi
je 0x9481
movq 0x10(%rsi), %rdi
movslq 0x18(%rdi), %rdi
cmpl $0x2, (%rax,%rdi,4)
jl 0x946c
movq 0x18(%rsi), %rdi
movslq 0x18(%rdi), %rdi
cmpl $0x1, (%rax,%rdi,4)
jg 0x9472
movq 0x8(%rsi), %rsi
jmp 0x944b
movq 0x18(%rdx), %rdx
testq %rdx, %rdx
jne 0x93a4
jmp 0x9496
movq %rax, %rdi
callq 0x1030
movq %r14, %rdi
movq %r15, %rsi
callq 0x14a56
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq