// File: /workspace/github_c_makefile_star2_O3/pwelke[P]GraphMiningTools/outerplanar.c
// Function: createBlockAndBridgeTree
struct BBTree* createBlockAndBridgeTree(struct ShallowGraph* list, struct Graph *original, struct GraphPool* gp, struct ShallowGraphPool *sgp) {
	struct ShallowGraph* idx;
	struct ShallowGraph* tmp;
	struct Graph* bbTree = getGraph(gp);
	struct Graph* blocks = getGraph(gp);
	int blockNo = 0;
	int i;

	/* copy V(original) */
	setVertexNumber(bbTree, original->n);
	for (i=0; i<original->n; ++i) {
		bbTree->vertices[i] = shallowCopyVertex(original->vertices[i], gp->vertexPool);
	}

	/* copy all bridges to the new graph and delete them from list;
	 * count the number of blocks */
	for (idx=list; idx; idx=tmp) {

		tmp = idx->next;
		if (idx->m == 1) {
			struct VertexList* e = getVertexList(gp->listPool);

			/* add the edge to the bbTree */
			e->startPoint = bbTree->vertices[idx->edges->startPoint->number];
			e->endPoint = bbTree->vertices[idx->edges->endPoint->number];
			e->label = idx->edges->label;

			addEdge(bbTree->vertices[idx->edges->startPoint->number], e);
			addEdge(bbTree->vertices[idx->edges->endPoint->number], inverseEdge(e, gp->listPool));

			++bbTree->m;

			/* cut idx out of list */
			if (idx->prev) {
				idx->prev->next = tmp;
				if (tmp) {
					tmp->prev = idx->prev;
				}
			} else {
				list = tmp;
				if (tmp) {
					tmp->prev = NULL;
				}
			}

			/* dump idx */
			idx->next = NULL;
			dumpShallowGraph(sgp, idx);
		} else {
			/* we have found a block */
			++blockNo;
		}
	}

	/* add blocks */
	setVertexNumber(blocks, blockNo);
	for (i=0; i<blockNo; ++i) {
		blocks->vertices[i] = getVertex(gp->vertexPool);
		blocks->vertices[i]->number = - (i + 1);
	}

	/* check for outerplanarity of all blocks */
	for (idx=list, i=0; idx; idx=idx->next, ++i) {
		struct ShallowGraph* cString = canonicalStringOfOuterplanarGraph(idx, sgp, gp);
		/* if the current component is not outerplanar,
		 * dump stuff and return NULL
		 * TODO */
		if (cString) {
			blocks->vertices[i]->label = canonicalStringToChar(cString);
			blocks->vertices[i]->isStringMaster = 1;
			dumpShallowGraph(sgp, cString);
		} else {

			/* garbage collection */
			dumpGraph(gp, blocks);
			dumpGraph(gp, bbTree);
			dumpShallowGraphCycle(sgp, list);

			return NULL;
		}
	}

	/* add edges connecting blocks and contained vertices */
	for (idx=list, i=0; idx; idx=idx->next, ++i) {
		struct VertexList* e;
		for (e=idx->edges; e; e=e->next) {

			/* edge  (startPoint, block) if it isn't already there.
			 *
			 * Reason: Each Vertex in a block occurs at least twice below (as
			 * it is on at least one cycle), but we want just one edge connecting
			 * it to the block vertex
			 *
			 * The edge isnt already there, if there is no edge at all OR the endpoint
			 * of the previous edge is the block vertex of the current block */
			if ((bbTree->vertices[e->startPoint->number]->neighborhood == NULL)
					|| (bbTree->vertices[e->startPoint->number]->neighborhood->endPoint != blocks->vertices[i])) {

				struct VertexList* f = getContainmentEdge(gp->listPool);
				f->startPoint = bbTree->vertices[e->startPoint->number];
				f->endPoint = blocks->vertices[i];
				addEdge(bbTree->vertices[e->startPoint->number], f);
				/* addEdge(blocks->vertices[i], inverseEdge(f, gp->listPool));
				 * ... is not done as there is the possibility that we have to remove
				 * the vertex startPoint if it is contained in only one block and no bridge */
			}


			/* edge (endPoint, block) if ...*/
			if ((bbTree->vertices[e->endPoint->number]->neighborhood == NULL)
					|| (bbTree->vertices[e->endPoint->number]->neighborhood->endPoint != blocks->vertices[i])) {
				struct VertexList* f = getContainmentEdge(gp->listPool);
				f->startPoint = bbTree->vertices[e->endPoint->number];
				f->endPoint = blocks->vertices[i];
				addEdge(bbTree->vertices[e->endPoint->number], f);
				/* addEdge(blocks->vertices[i], inverseEdge(f, gp->listPool));
				 * as above	*/
			}

		}
	}

	/* remove vertices that do not belong to more than one biconnected component
	 * runtime O(n+m) */
	for (i=0; i<bbTree->n; ++i) {
		struct VertexList* e;
		char removeVertex = 1;

		if (bbTree->vertices[i]->neighborhood) {
			int firstNo = bbTree->vertices[i]->neighborhood->endPoint->number;

			/* if vertex belongs to just one comp but this is a bridge, keep the vertex */
			if (firstNo >= 0) {
				continue;
			}

			/* if vertex belongs to more than one comp keep it */
			for (e=bbTree->vertices[i]->neighborhood; e; e=e->next) {
				if (e->endPoint->number != firstNo) {
					removeVertex = 0;
					break;
				}
			}
		} else {
			/* this part only occurs, if there are isolated vertices in original.
			 * However, by definition an outerplanar graph does not have to be connected
			 * so we keep the vertex and obtain a block and bridge forest
			 */
			removeVertex = 0;
		}


		/* check if to remove the vertex. if so, dump vertex and all edges dangling at it.
		 * The corresponding entry in bbTree->vertices will be NULL */
		if (removeVertex) {
			struct VertexList* f;
			struct VertexList* tmp2;
			for (f=bbTree->vertices[i]->neighborhood; f; f=tmp2) {
				tmp2 = f->next;
				dumpVertexList(gp->listPool, f);
			}
			dumpVertex(gp->vertexPool, bbTree->vertices[i]);
			bbTree->vertices[i] = NULL;
		}
	}

	/* add reverse edges of the edges connecting a vertex in tree to a block */
	for (i=0; i<bbTree->n; ++i) {
		if (bbTree->vertices[i]) {
			struct VertexList* e;
			for (e=bbTree->vertices[i]->neighborhood; e; e=e->next) {
				if (e->endPoint->number < 0) {
					addEdge(e->endPoint, inverseEdge(e, gp->listPool));
				}
			}
		}
	}

	return createBBTree(bbTree, blocks, list, NULL, gp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %rbp
movq %rdx, %rdi
callq 0x146a9
movq %rax, %r14
movq %rbx, %rdi
callq 0x146a9
movq %rax, %r12
movl (%r15), %esi
movq %r14, %rdi
callq 0x7e6c
cmpl $0x0, (%r15)
jle 0x17429
xorl %r13d, %r13d
movq 0x10(%r15), %rax
movq (%rax,%r13,8), %rdi
movq 0x10(%rbx), %rsi
callq 0x7c34
movq 0x10(%r14), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
movslq (%r15), %rax
cmpq %rax, %r13
jl 0x17405
testq %rbp, %rbp
je 0x17721
movq %rbp, %r13
xorl %eax, %eax
movq %rbp, %r15
movq 0x18(%r15), %rbp
cmpl $0x1, 0x10(%r15)
jne 0x174ed
movq %rax, 0x20(%rsp)
movq %r13, 0x8(%rsp)
movq 0x18(%rbx), %rdi
callq 0x141f1
movq %rax, %r13
movq 0x10(%r14), %rax
movq (%r15), %rcx
movq 0x10(%rcx), %rdx
movslq 0x18(%rdx), %rdx
movq (%rax,%rdx,8), %rdx
movq %rdx, 0x10(%r13)
movq 0x18(%rcx), %rdx
movslq 0x18(%rdx), %rdx
movq (%rax,%rdx,8), %rdx
movq %rdx, 0x18(%r13)
movq (%rcx), %rdx
movq %rdx, (%r13)
movq 0x10(%rcx), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq %r13, %rsi
callq 0x7cfe
movq 0x10(%r14), %rax
movq (%r15), %rcx
movq 0x18(%rcx), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rbx), %rsi
movq %r13, %rdi
callq 0x7c08
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x7cfe
incl 0x4(%r14)
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x174f1
movq %rbp, 0x18(%rax)
movq 0x8(%rsp), %rcx
movq %rcx, %r13
jmp 0x174ff
incl %eax
jmp 0x17525
movl $0x0, %eax
movq %rbp, %rcx
movl $0x0, %r13d
testq %rbp, %rbp
movq 0x10(%rsp), %rdi
je 0x17510
movq %rax, 0x20(%rbp)
movq %rcx, %r13
movq $0x0, 0x18(%r15)
movq %r15, %rsi
callq 0x149f4
movq 0x20(%rsp), %rax
movq %rbp, %r15
testq %rbp, %rbp
jne 0x1743a
movq %r13, 0x8(%rsp)
movq %r12, %rdi
movq %rax, %r15
movl %r15d, %esi
callq 0x7e6c
testl %r15d, %r15d
jle 0x1757c
movl %r15d, %r15d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x10(%rbx), %rdi
callq 0x1447e
movq 0x10(%r12), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x10(%r12), %rax
movq (%rax,%r13,8), %rax
incq %r13
movl %ebp, 0x18(%rax)
decl %ebp
cmpq %r13, %r15
jne 0x17554
movq 0x8(%rsp), %r15
testq %r15, %r15
je 0x17736
xorl %r13d, %r13d
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x7028
testq %rax, %rax
je 0x17836
movq %rax, %rbp
movq %rax, %rdi
callq 0x70ae
movq 0x10(%r12), %rcx
movq (%rcx,%r13), %rdx
movq %rax, (%rdx)
movq (%rcx,%r13), %rax
movl $0x1, 0x24(%rax)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x149f4
movq 0x18(%r15), %r15
addq $0x8, %r13
testq %r15, %r15
jne 0x1758d
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq (%rax), %rbp
testq %rbp, %rbp
je 0x1770a
movq 0x10(%r14), %rax
movq 0x10(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rcx
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
je 0x17623
movq 0x18(%rcx), %rcx
movq 0x10(%r12), %rdx
cmpq (%rdx,%r13,8), %rcx
je 0x17682
movq 0x18(%rbx), %rdi
callq 0x141f1
movq %rax, %r15
movl $0x2, %edi
callq 0x1190
movq %rax, (%r15)
movw $0x23, (%rax)
movl $0x1, 0x28(%r15)
movq 0x10(%r14), %rax
movq 0x10(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rcx
movq %rcx, 0x10(%r15)
movq 0x10(%r12), %rcx
movq (%rcx,%r13,8), %rcx
movq %rcx, 0x18(%r15)
movq 0x10(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x7cfe
movq 0x10(%r14), %rax
movq 0x18(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x176a6
movq 0x18(%rax), %rax
movq 0x10(%r12), %rcx
cmpq (%rcx,%r13,8), %rax
je 0x17701
movq 0x18(%rbx), %rdi
callq 0x141f1
movq %rax, %r15
movl $0x2, %edi
callq 0x1190
movq %rax, (%r15)
movw $0x23, (%rax)
movl $0x1, 0x28(%r15)
movq 0x10(%r14), %rax
movq 0x18(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rcx
movq %rcx, 0x10(%r15)
movq 0x10(%r12), %rcx
movq (%rcx,%r13,8), %rcx
movq %rcx, 0x18(%r15)
movq 0x18(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x7cfe
movq 0x8(%rbp), %rbp
jmp 0x175f2
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
incq %r13
testq %rax, %rax
jne 0x175ea
jmp 0x1773f
movq $0x0, 0x8(%rsp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x7e6c
jmp 0x1773f
movq $0x0, 0x8(%rsp)
movl (%r14), %eax
testl %eax, %eax
jle 0x17813
xorl %r15d, %r15d
movq 0x10(%r14), %rcx
movq (%rcx,%r15,8), %rcx
movq 0x8(%rcx), %rsi
testq %rsi, %rsi
je 0x177b3
movq 0x18(%rsi), %rcx
movl 0x18(%rcx), %ecx
testl %ecx, %ecx
jns 0x177b3
movq %rsi, %rdx
movq 0x18(%rdx), %rdi
cmpl %ecx, 0x18(%rdi)
jne 0x177b3
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
jne 0x1776c
movq 0x8(%rsi), %r13
movq 0x18(%rbx), %rdi
callq 0x14250
movq %r13, %rsi
testq %r13, %r13
jne 0x1777e
movq 0x10(%rbx), %rdi
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rsi
callq 0x144f0
movq 0x10(%r14), %rax
movq $0x0, (%rax,%r15,8)
movl (%r14), %eax
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x1774d
testl %eax, %eax
jle 0x17813
xorl %ebp, %ebp
movq 0x10(%r14), %rcx
movq (%rcx,%rbp,8), %rcx
testq %rcx, %rcx
je 0x17808
movq 0x8(%rcx), %r15
testq %r15, %r15
je 0x17808
movq 0x18(%r15), %r13
cmpl $0x0, 0x18(%r13)
jns 0x177fc
movq 0x18(%rbx), %rsi
movq %r15, %rdi
callq 0x7c08
movq %r13, %rdi
movq %rax, %rsi
callq 0x7cfe
movq 0x8(%r15), %r15
testq %r15, %r15
jne 0x177da
movl (%r14), %eax
incq %rbp
movslq %eax, %rcx
cmpq %rcx, %rbp
jl 0x177c4
movq %r14, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
movq %rbx, %r8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x17244
movq %rbx, %rdi
movq %r12, %rsi
callq 0x14700
movq %rbx, %rdi
movq %r14, %rsi
callq 0x14700
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x14a56
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq