// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rdi, %rbp
movq %rdx, %rdi
callq 0x147d7
movq %rax, %r14
movq %rbx, %rdi
callq 0x147d7
movq %rax, %r15
movl (%r12), %esi
movq %r14, %rdi
callq 0x7e30
cmpl $0x0, (%r12)
jle 0x17558
xorl %r13d, %r13d
movq 0x10(%r12), %rax
movq (%rax,%r13,8), %rdi
movq 0x10(%rbx), %rsi
callq 0x7c08
movq 0x10(%r14), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
movslq (%r12), %rax
cmpq %rax, %r13
jl 0x17532
testq %rbp, %rbp
movq %rbp, 0x8(%rsp)
je 0x1766c
xorl %r13d, %r13d
movq %rbp, %r12
movq 0x18(%r12), %rbp
cmpl $0x1, 0x10(%r12)
jne 0x1761f
movq %r13, 0x20(%rsp)
movq 0x18(%rbx), %rdi
callq 0x1432b
movq %rax, %r13
movq 0x10(%r14), %rax
movq (%r12), %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 0x7cd0
movq 0x10(%r14), %rax
movq (%r12), %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 0x7bdc
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x7cd0
incl 0x4(%r14)
movq 0x20(%r12), %rax
testq %rax, %rax
je 0x17624
movq %rbp, 0x18(%rax)
movq 0x8(%rsp), %rcx
movq %rcx, %r13
jmp 0x17632
incl %r13d
jmp 0x1765e
movl $0x0, %eax
movq %rbp, %rcx
movl $0x0, %r13d
testq %rbp, %rbp
movq 0x10(%rsp), %rdi
je 0x17643
movq %rax, 0x20(%rbp)
movq %rcx, %r13
movq $0x0, 0x18(%r12)
movq %r12, %rsi
callq 0x14b19
movq %r13, 0x8(%rsp)
movq 0x20(%rsp), %r13
movq %rbp, %r12
testq %rbp, %rbp
jne 0x1756c
jmp 0x1766f
xorl %r13d, %r13d
movq %r15, %rdi
movl %r13d, %esi
callq 0x7e30
testl %r13d, %r13d
jle 0x176b0
movl %r13d, %r12d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x10(%rbx), %rdi
callq 0x145b2
movq 0x10(%r15), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x10(%r15), %rax
movq (%rax,%r13,8), %rax
incq %r13
movl %ebp, 0x18(%rax)
decl %ebp
cmpq %r13, %r12
jne 0x1768a
cmpq $0x0, 0x8(%rsp)
je 0x1788a
xorl %r13d, %r13d
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x6fee
movq %rax, %rbp
testq %rax, %rax
je 0x17709
movq %rbp, %rdi
callq 0x7072
movq 0x10(%r15), %rcx
movq (%rcx,%r13), %rdx
movq %rax, (%rdx)
movq (%rcx,%r13), %rax
movl $0x1, 0x24(%rax)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x14b19
jmp 0x1772e
movq %rbx, %rdi
movq %r15, %rsi
callq 0x14832
movq %rbx, %rdi
movq %r14, %rsi
callq 0x14832
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x14b75
testq %rbp, %rbp
je 0x17996
movq 0x18(%r12), %r12
addq $0x8, %r13
testq %r12, %r12
jne 0x176c4
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq (%rax), %rbp
testq %rbp, %rbp
je 0x17875
movq 0x10(%r14), %rax
movq 0x10(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x17789
movq 0x18(%rax), %rax
movq 0x10(%r15), %rcx
cmpq (%rcx,%r13,8), %rax
je 0x177e7
movq 0x18(%rbx), %rdi
callq 0x1432b
movq %rax, %r12
movl $0x2, %edi
callq 0x1190
movq %rax, (%r12)
movw $0x23, (%rax)
movl $0x1, 0x28(%r12)
movq 0x10(%r14), %rax
movq 0x10(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rcx
movq %rcx, 0x10(%r12)
movq 0x10(%r15), %rcx
movq (%rcx,%r13,8), %rcx
movq %rcx, 0x18(%r12)
movq 0x10(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq %r12, %rsi
callq 0x7cd0
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 0x1780e
movq 0x18(%rax), %rax
movq 0x10(%r15), %rcx
cmpq (%rcx,%r13,8), %rax
je 0x1786c
movq 0x18(%rbx), %rdi
callq 0x1432b
movq %rax, %r12
movl $0x2, %edi
callq 0x1190
movq %rax, (%r12)
movw $0x23, (%rax)
movl $0x1, 0x28(%r12)
movq 0x10(%r14), %rax
movq 0x18(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rcx
movq %rcx, 0x10(%r12)
movq 0x10(%r15), %rcx
movq (%rcx,%r13,8), %rcx
movq %rcx, 0x18(%r12)
movq 0x18(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq %r12, %rsi
callq 0x7cd0
movq 0x8(%rbp), %rbp
jmp 0x17759
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
incq %r13
testq %rax, %rax
jne 0x17751
cmpl $0x0, (%r14)
jle 0x17920
xorl %r12d, %r12d
movq 0x10(%r14), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rsi
testq %rsi, %rsi
je 0x178cc
movq 0x18(%rsi), %rax
movl 0x18(%rax), %ecx
movb $0x1, %al
testl %ecx, %ecx
jns 0x178d2
movq %rsi, %rdx
movq 0x18(%rdx), %rdi
cmpl %ecx, 0x18(%rdi)
jne 0x178d0
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
jne 0x178b8
jmp 0x178d2
xorl %eax, %eax
jmp 0x178d6
xorl %eax, %eax
testl %ecx, %ecx
jns 0x17911
testb %al, %al
je 0x17911
testq %rsi, %rsi
je 0x178f4
movq 0x8(%rsi), %r13
movq 0x18(%rbx), %rdi
callq 0x1438e
movq %r13, %rsi
testq %r13, %r13
jne 0x178df
movq 0x10(%rbx), %rdi
movq 0x10(%r14), %rax
movq (%rax,%r12,8), %rsi
callq 0x14628
movq 0x10(%r14), %rax
movq $0x0, (%rax,%r12,8)
incq %r12
movslq (%r14), %rax
cmpq %rax, %r12
jl 0x17897
cmpl $0x0, (%r14)
jle 0x17973
xorl %ebp, %ebp
movq 0x10(%r14), %rax
movq (%rax,%rbp,8), %rax
testq %rax, %rax
je 0x17968
movq 0x8(%rax), %r12
testq %r12, %r12
je 0x17968
movq 0x18(%r12), %r13
cmpl $0x0, 0x18(%r13)
jns 0x17961
movq 0x18(%rbx), %rsi
movq %r12, %rdi
callq 0x7bdc
movq %r13, %rdi
movq %rax, %rsi
callq 0x7cd0
movq 0x8(%r12), %r12
jmp 0x17939
incq %rbp
movslq (%r14), %rax
cmpq %rax, %rbp
jl 0x17928
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 0x1736f
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq