// File: /workspace/github_c_makefile_star2_O1/pwelke[P]GraphMiningTools/poset_pathCover.c
// Function: getLongestPathInFlowInstance
static int* getLongestPathInFlowInstance(struct Vertex* v, struct Graph* flowInstance, struct ShallowGraphPool* sgp) {

	struct ShallowGraph* border = getShallowGraph(sgp);
	addToVertexQueue(v, border, sgp);
	v->lowPoint = -1;
	v->d = 1;

	// use ->lowPoint to store the a parent of w
	// for sake of simplicity we count the number of times a vertex is in the border and
	// at the end of this loop highestVertex stores the last visited pattern that,
	// by construction, is on the highest level of the poset.
	struct Vertex* highestVertex = v;
	for (struct Vertex* w=popFromVertexQueue(border, sgp); w!=NULL; w=popFromVertexQueue(border, sgp)) {
		highestVertex = w;
		w->d -= 1;
		if (w->d == 0) {
			for (struct VertexList* e=w->neighborhood; e!=NULL; e=e->next) {
				if ((e->flag > 0) && (e->endPoint->d == 0)) {
					e->endPoint->lowPoint = w->number;
					e->endPoint->d += 1;
					addToVertexQueue(e->endPoint, border, sgp);
				}
			}
		}
	}

	// remove path from graph by deleting flow on edges and fixing excess at start and endpoint of path
	for (struct Vertex* x=highestVertex; x->lowPoint!=-1; x=flowInstance->vertices[x->lowPoint]) {
		for (struct VertexList* e=flowInstance->vertices[x->lowPoint]->neighborhood; e!=NULL; e=e->next) {
			if (e->endPoint == x) {
				--e->flag;
				break; // ...looping over neighbors
			}
		}
	}
	--v->visited;
	++highestVertex->visited;

	// we want to obtain paths in the original poset we obtained flowInstance from.
	// Therefore we count only those vertices on the current path in flowinstance that have even ->number
	// and will store their ->numbers divided by two to obtain the vertex ids in the original poset.
	// This should work. TODO pathLength->arrayLength
	int arrayLength = 2;
	for (struct Vertex* x=highestVertex; x->lowPoint!=-1; x=flowInstance->vertices[x->lowPoint]) {
		if (x->number % 2 == 0) {
			++arrayLength;
		}
	}

	// alloc space for path and store vertex ids
	int* path = malloc(arrayLength * sizeof(int));
	path[0] = arrayLength;
	path[1] = v->number / 2;

	// to obtain a path in the original flowInstance, we keep only those vertices that have even ->number and convert them back
	// to original numbers by dividing by two.
	arrayLength -= 1;
	for (struct Vertex* x=highestVertex; arrayLength > 1; x=flowInstance->vertices[x->lowPoint]) {
		if (x->number % 2 == 0) {
			path[arrayLength] = x->number / 2;
			--arrayLength;
		}
	}

	return path;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
movq %rdx, %rdi
callq 0x14aae
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x1e8ac
movl $0xffffffff, 0x20(%r14) # imm = 0xFFFFFFFF
movl $0x1, 0x28(%r14)
movq %r12, %rdi
movq %r15, %rsi
callq 0x1e8d5
movq %r14, %r13
testq %rax, %rax
je 0x185b5
movq %rax, %r13
decl 0x28(%rax)
je 0x1857d
movq %r12, %rdi
movq %r15, %rsi
callq 0x1e8d5
jmp 0x18563
movq 0x8(%r13), %rbp
testq %rbp, %rbp
je 0x18570
cmpl $0x0, 0x24(%rbp)
jle 0x185af
movq 0x18(%rbp), %rdi
cmpl $0x0, 0x28(%rdi)
jne 0x185af
movl 0x18(%r13), %eax
movl %eax, 0x20(%rdi)
movl $0x1, 0x28(%rdi)
movq %r12, %rsi
movq %r15, %rdx
callq 0x1e8ac
movq 0x8(%rbp), %rbp
jmp 0x18581
movl 0x20(%r13), %edx
cmpl $-0x1, %edx
je 0x185ec
movq 0x10(%rbx), %rax
movq %r13, %rcx
movq %rcx, %rsi
movslq %edx, %rcx
movq (%rax,%rcx,8), %rcx
movq %rcx, %rdx
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
je 0x185e4
cmpq %rsi, 0x18(%rdx)
jne 0x185d2
decl 0x24(%rdx)
movl 0x20(%rcx), %edx
cmpl $-0x1, %edx
jne 0x185c5
decl 0x1c(%r14)
incl 0x1c(%r13)
movl 0x20(%r13), %eax
cmpl $-0x1, %eax
je 0x18623
movq 0x10(%rbx), %rcx
movl $0x2, %ebp
movq %r13, %rdx
movl 0x18(%rdx), %edx
notl %edx
andl $0x1, %edx
addl %edx, %ebp
cltq
movq (%rcx,%rax,8), %rdx
movl 0x20(%rdx), %eax
cmpl $-0x1, %eax
jne 0x18609
jmp 0x18628
movl $0x2, %ebp
movslq %ebp, %r15
leaq (,%r15,4), %rdi
callq 0x1190
movl %r15d, (%rax)
movl 0x18(%r14), %ecx
movl %ecx, %edx
shrl $0x1f, %edx
addl %ecx, %edx
sarl %edx
movl %edx, 0x4(%rax)
cmpl $0x3, %r15d
jl 0x18676
decl %ebp
movq 0x10(%rbx), %rcx
movl 0x18(%r13), %edx
testb $0x1, %dl
jne 0x18669
sarl %edx
movl %ebp, %esi
movl %edx, (%rax,%rsi,4)
decl %ebp
movslq 0x20(%r13), %rdx
movq (%rcx,%rdx,8), %r13
cmpl $0x1, %ebp
jg 0x18657
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq