// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq 0x38(%rsp), %rax
cmpl $0x1, (%rax)
jg 0xcf92
movb $0x1, 0x47(%rsp)
jmp 0xd1be
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x10700
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
movq 0x38(%rsp), %rax
movl (%rax), %esi
callq 0x10310
movq %rax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl (%rcx), %eax
jge 0xd013
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rcx
cmpl $0x2, (%rax,%rcx,4)
jle 0xd004
movq 0x20(%rsp), %rdi
callq 0x1030
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x20000
movb $0x0, 0x47(%rsp)
jmp 0xd1be
jmp 0xd006
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xcfc4
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0xd1b9
movl $0x0, 0x4(%rsp)
movq 0x10(%rsp), %rax
cmpl $0x1, 0x10(%rax)
jne 0xd041
jmp 0xd1a6
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0xd086
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movl $0x0, 0x1c(%rax)
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl $0x0, 0x1c(%rax)
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0xd04e
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0xd0e1
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rsp), %rsi
callq 0xd1d0
addl 0x4(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x20(%rsp), %rsi
callq 0xd1d0
addl 0x4(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0xd093
cmpl $0x2, 0x4(%rsp)
jle 0xd10b
movq 0x20(%rsp), %rdi
callq 0x1030
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x20000
movb $0x0, 0x47(%rsp)
jmp 0xd1be
cmpl $0x2, 0x4(%rsp)
jne 0xd1a2
movb $0x1, 0x3(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0xd179
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movslq 0x18(%rcx), %rcx
cmpl $0x1, (%rax,%rcx,4)
jle 0xd167
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq 0x18(%rcx), %rcx
cmpl $0x1, (%rax,%rcx,4)
jle 0xd167
movb $0x0, 0x3(%rsp)
jmp 0xd179
jmp 0xd169
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0xd128
cmpb $0x0, 0x3(%rsp)
je 0xd1a0
movq 0x20(%rsp), %rdi
callq 0x1030
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x20000
movb $0x0, 0x47(%rsp)
jmp 0xd1be
jmp 0xd1a2
jmp 0xd1a4
jmp 0xd1a6
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0xd01d
movb $0x1, 0x47(%rsp)
movb 0x47(%rsp), %al
addq $0x48, %rsp
retq
nopw (%rax,%rax)