// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %rbp
movq %rdx, %rdi
callq 0x11619
movq %rax, %r14
movq %rbx, %rdi
callq 0x11619
movq %rax, %r15
movl (%r12), %esi
movq %r14, %rdi
callq 0x5c80
xorl %r13d, %r13d
movslq (%r12), %rax
cmpq %rax, %r13
jge 0x14527
movq 0x10(%r12), %rax
movq (%rax,%r13,8), %rdi
movq 0x10(%rbx), %rsi
callq 0x5a70
movq 0x10(%r14), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
jmp 0x144ff
movq $0x0, 0x20(%rsp)
movq %rbp, %r13
movq %rbp, %r12
testq %rbp, %rbp
movq %r13, 0x8(%rsp)
je 0x14636
movq 0x18(%r12), %rbp
cmpl $0x1, 0x10(%r12)
jne 0x1462c
movq %rbp, 0x10(%rsp)
movq 0x18(%rbx), %rdi
callq 0x11307
movq %rax, %rbp
movq 0x10(%r14), %rax
movq (%r12), %rcx
movq 0x10(%rcx), %rdx
movslq 0x18(%rdx), %rdx
movq (%rax,%rdx,8), %rdx
movq %rdx, 0x10(%rbp)
movq 0x18(%rcx), %rdx
movslq 0x18(%rdx), %rdx
movq (%rax,%rdx,8), %rdx
movq %rdx, 0x18(%rbp)
movq (%rcx), %rdx
movq %rdx, (%rbp)
movq 0x10(%rcx), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq %rbp, %rsi
callq 0x5b2c
movq 0x10(%r14), %rax
movq (%r12), %rcx
movq 0x18(%rcx), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %r13
movq 0x18(%rbx), %rsi
movq %rbp, %rdi
callq 0x5a44
movq %r13, %rdi
movq %rax, %rsi
callq 0x5b2c
incl 0x4(%r14)
movq 0x20(%r12), %rax
testq %rax, %rax
je 0x145f5
movq 0x10(%rsp), %rbp
movq %rbp, 0x18(%rax)
movq 0x8(%rsp), %rcx
movq %rcx, %r13
jmp 0x14608
movl $0x0, %eax
movq 0x10(%rsp), %rbp
movq %rbp, %rcx
movl $0x0, %r13d
testq %rbp, %rbp
movq 0x18(%rsp), %rdi
je 0x14619
movq %rax, 0x20(%rbp)
movq %rcx, %r13
andq $0x0, 0x18(%r12)
movq %r12, %rsi
callq 0x1184e
jmp 0x14533
incq 0x20(%rsp)
jmp 0x14533
movq %r15, %rdi
movq 0x20(%rsp), %r13
movl %r13d, %esi
callq 0x5c80
pushq $-0x1
popq %rbp
xorl %r12d, %r12d
subq $0x1, %r13
jb 0x14676
movq 0x10(%rbx), %rdi
callq 0x114a5
movq 0x10(%r15), %rcx
movq %rax, (%rcx,%r12)
movq 0x10(%r15), %rax
movq (%rax,%r12), %rax
movl %ebp, 0x18(%rax)
decl %ebp
addq $0x8, %r12
jmp 0x1464c
xorl %r12d, %r12d
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rbp
testq %rdi, %rdi
je 0x146dc
movq %rdi, 0x10(%rsp)
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x4f3c
testq %rax, %rax
je 0x148bd
movq %rax, %r13
movq %rax, %rdi
callq 0x4fbe
movq 0x10(%r15), %rcx
movq (%rcx,%r12), %rdx
movq %rax, (%rdx)
movq (%rcx,%r12), %rax
movl $0x1, 0x24(%rax)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1184e
movq 0x10(%rsp), %rdi
movq 0x18(%rdi), %rdi
addq $0x8, %r12
jmp 0x14683
xorl %r12d, %r12d
movq 0x8(%rsp), %r13
movq %r13, %rbp
testq %r13, %r13
jne 0x147c1
jmp 0x147da
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 0x1471c
movq 0x18(%rcx), %rcx
movq 0x10(%r15), %rdx
cmpq (%rdx,%r12,8), %rcx
je 0x1475d
movq 0x18(%rbx), %rdi
callq 0x14499
movq 0x10(%r14), %rcx
movq 0x10(%rbp), %rdx
movslq 0x18(%rdx), %rdx
movq (%rcx,%rdx,8), %rdx
movq %rdx, 0x10(%rax)
movq 0x10(%r15), %rdx
movq (%rdx,%r12,8), %rdx
movq %rdx, 0x18(%rax)
movq 0x10(%rbp), %rdx
movslq 0x18(%rdx), %rdx
movq (%rcx,%rdx,8), %rdi
movq %rax, %rsi
callq 0x5b2c
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 0x14780
movq 0x18(%rax), %rax
movq 0x10(%r15), %rcx
cmpq (%rcx,%r12,8), %rax
je 0x147bd
movq 0x18(%rbx), %rdi
callq 0x14499
movq 0x10(%r14), %rcx
movq 0x18(%rbp), %rdx
movslq 0x18(%rdx), %rdx
movq (%rcx,%rdx,8), %rdx
movq %rdx, 0x10(%rax)
movq 0x10(%r15), %rdx
movq (%rdx,%r12,8), %rdx
movq %rdx, 0x18(%rax)
movq 0x18(%rbp), %rdx
movslq 0x18(%rdx), %rdx
movq (%rcx,%rdx,8), %rdi
movq %rax, %rsi
callq 0x5b2c
addq $0x8, %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x146f5
movq 0x18(%r13), %r13
incq %r12
jmp 0x146e4
xorl %r12d, %r12d
movslq (%r14), %rax
cmpq %rax, %r12
jge 0x1484c
movq 0x10(%r14), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rsi
testq %rsi, %rsi
je 0x14847
movq 0x18(%rsi), %rax
movl 0x18(%rax), %eax
testl %eax, %eax
jns 0x14847
movq %rsi, %rcx
testq %rcx, %rcx
je 0x14828
movq 0x18(%rcx), %rdx
cmpl %eax, 0x18(%rdx)
jne 0x14847
movq 0x8(%rcx), %rcx
jmp 0x14804
movq 0x8(%rsi), %r13
movq 0x18(%rbx), %rdi
callq 0x11364
movq %r13, %rsi
testq %rsi, %rsi
jne 0x14818
movq 0x10(%rbx), %rdi
movq 0x10(%r14), %rax
movq (%rax,%r12,8), %rsi
callq 0x11512
movq 0x10(%r14), %rax
andq $0x0, (%rax,%r12,8)
incq %r12
jmp 0x147dd
xorl %ebp, %ebp
movslq %eax, %rcx
cmpq %rcx, %rbp
jge 0x1489a
movq 0x10(%r14), %rcx
movq (%rcx,%rbp,8), %r12
testq %r12, %r12
je 0x14895
movq 0x8(%r12), %r12
testq %r12, %r12
je 0x14892
movq 0x18(%r12), %r13
cmpl $0x0, 0x18(%r13)
jns 0x14863
movq 0x18(%rbx), %rsi
movq %r12, %rdi
callq 0x5a44
movq %r13, %rdi
movq %rax, %rsi
callq 0x5b2c
jmp 0x14863
movl (%r14), %eax
incq %rbp
jmp 0x1484e
movq %r14, %rdi
movq %r15, %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 0x1434f
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1166e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1166e
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0x11888
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq