// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
pushq %rax
movb $0x1, %bl
cmpl $0x2, (%rdi)
jl 0x944b
movq %rsi, %r14
movq %rdi, %r13
callq 0xb2bf
movq %rax, %r15
movl (%r13), %esi
movq %rax, %rdi
callq 0xb0ec
movq %rax, %r12
movslq (%r13), %rax
testq %rax, %rax
jle 0x9335
xorl %ecx, %ecx
cmpl $0x3, (%r12,%rcx,4)
jge 0x9436
incq %rcx
cmpq %rcx, %rax
jne 0x9322
testq %r15, %r15
je 0x944b
xorl %ebp, %ebp
movq %r15, %r13
movl $0x7, %ecx
cmpl $0x1, 0x10(%r13)
je 0x941e
movq (%r13), %rax
testq %rax, %rax
je 0x9370
movq 0x10(%rax), %rcx
movl %ebp, 0x1c(%rcx)
movq 0x18(%rax), %rcx
movl %ebp, 0x1c(%rcx)
movq 0x8(%rax), %rax
jmp 0x9357
movq (%r13), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x93cb
movq 0x10(%rcx), %rdx
cmpl $0x0, 0x1c(%rdx)
jne 0x939c
movl $0x1, 0x1c(%rdx)
movslq 0x18(%rdx), %rsi
movl $0x1, %edx
cmpl $0x1, (%r12,%rsi,4)
jg 0x939e
xorl %edx, %edx
movq 0x18(%rcx), %rsi
cmpl $0x0, 0x1c(%rsi)
jne 0x93bf
movl $0x1, 0x1c(%rsi)
movslq 0x18(%rsi), %rdi
movl $0x1, %esi
cmpl $0x1, (%r12,%rdi,4)
jg 0x93c1
xorl %esi, %esi
addl %edx, %eax
addl %esi, %eax
movq 0x8(%rcx), %rcx
jmp 0x9376
cmpl $0x2, %eax
jle 0x93ea
movq %r12, %rdi
callq 0x1030
movq %r14, %rdi
movq %r15, %rsi
callq 0x14b75
movl $0x1, %ecx
jmp 0x941e
xorl %ecx, %ecx
cmpl $0x2, %eax
jne 0x941e
movq (%r13), %rax
testq %rax, %rax
je 0x93d0
movq 0x10(%rax), %rdx
movslq 0x18(%rdx), %rdx
cmpl $0x2, (%r12,%rdx,4)
jl 0x9418
movq 0x18(%rax), %rdx
movslq 0x18(%rdx), %rdx
cmpl $0x1, (%r12,%rdx,4)
jg 0x941e
movq 0x8(%rax), %rax
jmp 0x93f5
cmpl $0x7, %ecx
je 0x9427
testl %ecx, %ecx
jne 0x9449
movq 0x18(%r13), %r13
testq %r13, %r13
jne 0x9343
jmp 0x944b
movq %r12, %rdi
callq 0x1030
movq %r14, %rdi
movq %r15, %rsi
callq 0x14b75
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq