// File: /workspace/github_c_makefile_star2_O3/pwelke[P]GraphMiningTools/localEasySubtreeIsomorphism.c
// Function: getBlockTreeT
struct BlockTree getBlockTreeT(struct Graph* g, struct ShallowGraphPool* sgp) {

	struct ShallowGraph* biconnectedComponents = listBiconnectedComponents(g, sgp);

	for (int v=0; v<g->n; ++v) {
		g->vertices[v]->visited = -1; // isRoot
		g->vertices[v]->d = -1; // parent root
	}

	/* we mark for each component the vertex with the lowest lowPoint.
	 * that is the root of the component.
	 *
	 * Due to the order in which listBiconnectedComponents() returns the bics,
	 * we obtain the right parent roots for the roots in this way. */

	for (struct ShallowGraph* bic=biconnectedComponents; bic!=NULL; bic=bic->next) {

		// listBiconnectedComponents returns bics where the first vertex in the first edge is the root of each bic
		struct Vertex* rootOfComponent = bic->edges->startPoint;

		// mark the root of this component as root.
		rootOfComponent->visited = 1;

		// store the root as information with each vertex and the component
		int rootId = rootOfComponent->number;
		bic->data = rootId;
		for (struct VertexList* e=bic->edges; e!=NULL; e=e->next) {
			e->startPoint->d = rootId;
			e->endPoint->d = rootId;
		}
	}

	// create output struct
	struct BlockTree blockTree = {0,0,0,0,0};
	blockTree.g = g;

	// count number of roots in g, init storage
	for (int v=0; v<g->n; ++v) {
		if (g->vertices[v]->visited == 1) {
			++blockTree.nRoots;
		}
	}
	blockTree.roots = malloc(blockTree.nRoots * sizeof(struct Vertex*));
	blockTree.parents = malloc(blockTree.nRoots * sizeof(struct Vertex*));
	blockTree.vRootedBlocks = malloc(blockTree.nRoots * sizeof(struct ShallowGraph*));

	// select and sort roots by lowpoint. this ensures bottom up traversal of the
	// blocktree if iterating through the array
	for (int v=0, r=0; v<g->n; ++v) {
		if (g->vertices[v]->visited == 1) {
			// set ->visited of each root to the lowpoint of its parent root
			// this way, we can order the roots in one block correctly using lowPointComparator
			g->vertices[v]->visited = g->vertices[g->vertices[v]->d]->lowPoint;
			// if v is its own parent, mark this with a special visited value
			if (g->vertices[g->vertices[v]->d]->number == g->vertices[v]->number) {
				g->vertices[v]->visited = 0;
			}
			blockTree.roots[r] = g->vertices[v];
			++r;
		}
	}
	qsort(blockTree.roots, blockTree.nRoots, sizeof(struct Vertex*), &lowPointComparator);

	// add parents of roots to array (after sorting the above, of course)
	// init vRootedBlocks to NULL
	// set root->visited to its position in roots (so that we can map the bics efficiently)
	for (int v=0; v<blockTree.nRoots; ++v) {
		blockTree.parents[v] = g->vertices[blockTree.roots[v]->d];
		blockTree.vRootedBlocks[v] = NULL;
		blockTree.roots[v]->visited = v;
	}

	// add blocks to respective roots
	for (struct ShallowGraph* bic=popShallowGraph(&biconnectedComponents); bic!=NULL; bic=popShallowGraph(&biconnectedComponents)) {
		int rootPositionInBlockTreeArrays = g->vertices[bic->data]->visited;
		bic->next = blockTree.vRootedBlocks[rootPositionInBlockTreeArrays];
		blockTree.vRootedBlocks[rootPositionInBlockTreeArrays] = bic;
	}

	return blockTree;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq %rsi, %rdi
movq %rdx, %rsi
callq 0xb23b
movslq (%r14), %rbx
testq %rbx, %rbx
jle 0xe7fc
movq 0x10(%r14), %rdi
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq (%rdi,%rcx,8), %rsi
movl %edx, 0x1c(%rsi)
movl %edx, 0x28(%rsi)
incq %rcx
cmpq %rcx, %rbx
jne 0xe7ea
testq %rax, %rax
je 0xe838
movq %rax, %rdi
movq (%rdi), %rcx
movq 0x10(%rcx), %rdx
movl $0x1, 0x1c(%rdx)
movl 0x18(%rdx), %edx
movl %edx, 0x28(%rdi)
movq 0x10(%rcx), %rsi
movl %edx, 0x28(%rsi)
movq 0x18(%rcx), %rsi
movl %edx, 0x28(%rsi)
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
jne 0xe818
movq 0x18(%rdi), %rdi
testq %rdi, %rdi
jne 0xe804
movq %rax, (%rsp)
movq $0x0, 0x20(%r15)
movq %r14, (%r15)
testl %ebx, %ebx
movq %r14, 0x8(%rsp)
jle 0xe876
movq 0x10(%r14), %rcx
xorl %edx, %edx
xorl %eax, %eax
movq (%rcx,%rdx,8), %rsi
xorl %edi, %edi
cmpl $0x1, 0x1c(%rsi)
sete %dil
addl %edi, %eax
incq %rdx
cmpq %rdx, %rbx
jne 0xe858
movl %eax, 0x20(%r15)
jmp 0xe878
xorl %eax, %eax
movslq %eax, %r13
leaq (,%r13,8), %r12
movq %r12, %rdi
callq 0x1190
movq %rax, %rbp
movq %rax, 0x8(%r15)
movq %r12, %rdi
callq 0x1190
movq %r15, %r14
movq %rax, %r15
movq %rax, 0x10(%r14)
movq %r12, %rdi
callq 0x1190
movq %rax, %r12
movq %r14, 0x10(%rsp)
movq %rax, 0x18(%r14)
testl %ebx, %ebx
movq 0x8(%rsp), %r14
jle 0xe8ff
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x10(%r14), %rsi
movq (%rsi,%rax,8), %rdx
cmpl $0x1, 0x1c(%rdx)
jne 0xe8f7
movslq 0x28(%rdx), %rdi
movq (%rsi,%rdi,8), %rdi
movl 0x18(%rdi), %r8d
xorl %esi, %esi
cmpl 0x18(%rdx), %r8d
je 0xe8ea
movl 0x20(%rdi), %esi
movl %esi, 0x1c(%rdx)
movslq %ecx, %rcx
movq %rdx, (%rbp,%rcx,8)
incl %ecx
incq %rax
cmpq %rax, %rbx
jne 0xe8c5
leaq 0xb5(%rip), %rcx        # 0xe9bb
movl $0x8, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1080
testl %r13d, %r13d
jle 0xe945
movq 0x10(%r14), %rax
xorl %ecx, %ecx
movq (%rbp,%rcx,8), %rdx
movslq 0x28(%rdx), %rsi
movq (%rax,%rsi,8), %rsi
movq %rsi, (%r15,%rcx,8)
movq $0x0, (%r12,%rcx,8)
movl %ecx, 0x1c(%rdx)
incq %rcx
cmpq %rcx, %r13
jne 0xe921
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xe9a7
movq 0x18(%rdi), %rcx
movq $0x0, 0x18(%rdi)
movq 0x10(%r14), %rax
movslq 0x28(%rdi), %rdx
movq (%rax,%rdx,8), %rdx
movslq 0x1c(%rdx), %rdx
movq (%r12,%rdx,8), %rsi
movq %rsi, 0x18(%rdi)
movq %rdi, (%r12,%rdx,8)
testq %rcx, %rcx
je 0xe9a7
movq 0x18(%rcx), %rdx
movq $0x0, 0x18(%rcx)
movslq 0x28(%rcx), %rsi
movq (%rax,%rsi,8), %rsi
movslq 0x1c(%rsi), %rsi
movq (%r12,%rsi,8), %rdi
movq %rdi, 0x18(%rcx)
movq %rcx, (%r12,%rsi,8)
movq %rdx, %rcx
testq %rdx, %rdx
jne 0xe97b
movq 0x10(%rsp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq