// File: /workspace/github_c_makefile_star2_O3/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 0x1498d
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x1e6f0
movl $0xffffffff, 0x20(%r14) # imm = 0xFFFFFFFF
movl $0x1, 0x28(%r14)
movq %r12, %rdi
movq %r15, %rsi
callq 0x1e719
movq %r14, %r13
testq %rax, %rax
je 0x184b1
movq %rax, %r13
decl 0x28(%rax)
je 0x18479
movq %r12, %rdi
movq %r15, %rsi
callq 0x1e719
jmp 0x1845f
movq 0x8(%r13), %rbp
testq %rbp, %rbp
je 0x1846c
cmpl $0x0, 0x24(%rbp)
jle 0x184ab
movq 0x18(%rbp), %rdi
cmpl $0x0, 0x28(%rdi)
jne 0x184ab
movl 0x18(%r13), %eax
movl %eax, 0x20(%rdi)
movl $0x1, 0x28(%rdi)
movq %r12, %rsi
movq %r15, %rdx
callq 0x1e6f0
movq 0x8(%rbp), %rbp
jmp 0x1847d
movl 0x20(%r13), %eax
cmpl $-0x1, %eax
je 0x1851d
movq 0x10(%rbx), %rcx
movl %eax, %edi
movq %r13, %rdx
movq %rdx, %rsi
movslq %edi, %rdx
movq (%rcx,%rdx,8), %rdx
movq %rdx, %rdi
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x184e2
cmpq %rsi, 0x18(%rdi)
jne 0x184d0
decl 0x24(%rdi)
movl 0x20(%rdx), %edi
cmpl $-0x1, %edi
jne 0x184c3
decl 0x1c(%r14)
incl 0x1c(%r13)
cmpl $-0x1, %eax
je 0x18525
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 0x18503
jmp 0x1852a
decl 0x1c(%r14)
incl 0x1c(%r13)
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 0x18578
decl %ebp
movq 0x10(%rbx), %rcx
movl 0x18(%r13), %edx
testb $0x1, %dl
jne 0x1856b
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 0x18559
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq