// File: /workspace/github_c_makefile_star2_O0/pwelke[P]GraphMiningTools/treeCenter.c
// Function: _moveCenter
void __moveCenter(struct Vertex* root, struct Vertex* parent, int value, int depth, int* centers) {
	struct VertexList* e;
	int max = INT_MIN;
	struct VertexList* max1 = NULL;
	struct VertexList* max2 = NULL;

	/* update depths to fit to the new root */
	for (e=root->neighborhood; e!=NULL; e=e->next) {
		if (e->endPoint != parent) {
			e->used -= depth;
		} else {
			e->used = value;
		}
	}

	/* find maximum depth branch */
	for (e=root->neighborhood; e!=NULL; e=e->next) {
		if (e->used > max) {
			max = e->used;
			max1 = e;
		}
	}

	/* find second maximum depth branch */
	max = INT_MIN;
	for (e=root->neighborhood; e!=NULL; e=e->next) {
		if (e != max1) {
			if (e->used > max) {
				max = e->used;
				max2 = e;
			}
		}
	}

	/* if the current node is no leaf */
	if (max2 != NULL) {

		/* if the two deepest branches have same depth, we are at the 
		center */
		if (max1->used == max2->used) {
			centers[0] = 2;
			centers[1] = root->number;
			return;
		}

		/* this is the case where the longest path has odd length
		and we get two center nodes */
		if (max1->used == max2->used + 1) {
			centers[0] = 3;
			centers[1] = root->number;
			centers[2] = max1->endPoint->number;
			return;
		}
		/* ...and the symmetric case */
		if (max2->used == max1->used + 1) {
			centers[0] = 3;
			centers[1] = root->number;
			centers[2] = max2->endPoint->number;
			return;
		}

		/* move the center to the root of the deepest branch */
		__moveCenter(max1->endPoint, root, max2->used + 1, depth + 1, centers);
	} else {
		/* if we start at a leaf */
		__moveCenter(max1->endPoint, root, 1, 1, centers);
	}	
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movl %edx, 0x34(%rsp)
movl %ecx, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movl $0x80000000, 0x1c(%rsp) # imm = 0x80000000
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x3057c
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
cmpq 0x38(%rsp), %rax
je 0x3055e
movl 0x30(%rsp), %edx
movq 0x20(%rsp), %rax
movl 0x20(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x20(%rax)
jmp 0x3056a
movl 0x34(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x20(%rax)
jmp 0x3056c
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x30533
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x305c8
movq 0x20(%rsp), %rax
movl 0x20(%rax), %eax
cmpl 0x1c(%rsp), %eax
jle 0x305b6
movq 0x20(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x305b8
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x3058a
movl $0x80000000, 0x1c(%rsp) # imm = 0x80000000
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x3062a
movq 0x20(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x30618
movq 0x20(%rsp), %rax
movl 0x20(%rax), %eax
cmpl 0x1c(%rsp), %eax
jle 0x30616
movq 0x20(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x30618
jmp 0x3061a
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x305de
cmpq $0x0, 0x8(%rsp)
je 0x30727
movq 0x10(%rsp), %rax
movl 0x20(%rax), %eax
movq 0x8(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jne 0x30668
movq 0x28(%rsp), %rax
movl $0x2, (%rax)
movq 0x40(%rsp), %rax
movl 0x18(%rax), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x4(%rax)
jmp 0x30746
movq 0x10(%rsp), %rax
movl 0x20(%rax), %eax
movq 0x8(%rsp), %rcx
movl 0x20(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x306b3
movq 0x28(%rsp), %rax
movl $0x3, (%rax)
movq 0x40(%rsp), %rax
movl 0x18(%rax), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x8(%rax)
jmp 0x30746
movq 0x8(%rsp), %rax
movl 0x20(%rax), %eax
movq 0x10(%rsp), %rcx
movl 0x20(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x306fb
movq 0x28(%rsp), %rax
movl $0x3, (%rax)
movq 0x40(%rsp), %rax
movl 0x18(%rax), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x8(%rax)
jmp 0x30746
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40(%rsp), %rsi
movq 0x8(%rsp), %rax
movl 0x20(%rax), %edx
addl $0x1, %edx
movl 0x30(%rsp), %ecx
addl $0x1, %ecx
movq 0x28(%rsp), %r8
callq 0x304f0
jmp 0x30746
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x40(%rsp), %rsi
movq 0x28(%rsp), %r8
movl $0x1, %ecx
movl %ecx, %edx
callq 0x304f0
addq $0x48, %rsp
retq
nopl (%rax,%rax)