// File: /workspace/github_c_makefile_star2_O2/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 0x117e9
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x1ba70
orl $-0x1, 0x20(%r14)
movl $0x1, 0x28(%r14)
movq %r14, %rbp
movq %r12, %rdi
movq %r15, %rsi
callq 0x1ba99
testq %rax, %rax
je 0x15498
movq %rax, %r13
decl 0x28(%rax)
movq %rax, %rbp
jne 0x15441
movq %r13, %rbp
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
je 0x15493
cmpl $0x0, 0x24(%rbp)
jle 0x1545f
movq 0x18(%rbp), %rdi
cmpl $0x0, 0x28(%rdi)
jne 0x1545f
movl 0x18(%r13), %eax
movl %eax, 0x20(%rdi)
movl $0x1, 0x28(%rdi)
movq %r12, %rsi
movq %r15, %rdx
callq 0x1ba70
jmp 0x1545f
movq %r13, %rbp
jmp 0x15441
movq %rbp, %rax
movslq 0x20(%rax), %rcx
cmpq $-0x1, %rcx
je 0x154c7
movq 0x10(%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, %rdx
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
je 0x154c2
cmpq %rax, 0x18(%rdx)
jne 0x154b0
decl 0x24(%rdx)
movq %rcx, %rax
jmp 0x1549b
decl 0x1c(%r14)
incl 0x1c(%rbp)
pushq $0x2
popq %rax
movq %rbp, %rcx
movslq 0x20(%rcx), %rdx
cmpq $-0x1, %rdx
je 0x154f2
movl 0x18(%rcx), %ecx
notl %ecx
andl $0x1, %ecx
addl %ecx, %eax
movq 0x10(%rbx), %rcx
movq (%rcx,%rdx,8), %rcx
jmp 0x154d4
movslq %eax, %r15
leaq (,%r15,4), %rdi
callq 0x1180
movq %rax, %rcx
movl %r15d, (%rax)
movl 0x18(%r14), %eax
pushq $0x2
popq %rsi
cltd
idivl %esi
movl %eax, 0x4(%rcx)
decl %r15d
cmpl $0x1, %r15d
jle 0x1553e
movl 0x18(%rbp), %eax
testb $0x1, %al
jne 0x15530
sarl %eax
movl %r15d, %edx
movl %eax, (%rcx,%rdx,4)
decl %r15d
movq 0x10(%rbx), %rax
movslq 0x20(%rbp), %rdx
movq (%rax,%rdx,8), %rbp
jmp 0x15518
movq %rcx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq