// File: /workspace/github_cpp_makefile_star2_O2/dcarrey[P]hgt-detection/./utils_tree.cpp
// Function: Tree_edges(double**, long*, double*, int, int)
int Tree_edges (double **DI, long int *ARETE, double *LONGUEUR, int n,int binaire)
{

	struct EDGE { unsigned int U; unsigned int V; double LN;};
	struct EDGE *Path,*Tree;
	int i,j,k,p,P,*X;
	double S,DIS,DIS1,*L,**D;
	int pasfini = 1;
	int kt=0;
	int SomToDel,OtherSom;

	X=(int *)malloc((n+1)*sizeof(int));  
	L=(double *)malloc((n+1)*sizeof(double));
	Tree=(struct EDGE *)malloc((2*n-2)*sizeof(struct EDGE));
	Path=(struct EDGE *)malloc((n+2)*sizeof(struct EDGE));


	D=(double **) malloc((n+1)*sizeof(double*));

	for (i=0;i<=n;i++)
	{
		D[i]=(double*)malloc((n+1)*sizeof(double)); 

		if (D[i]==NULL)
		{
			printf("Data matrix is too large"); exit(1); 
		}
	}

	i=1; j=n;
	odp1(DI,X,&i,&j,n);

	for (i=1;i<=n;i++)
	{ 
		for (j=1;j<=n;j++) 
			D[i][j]=DI[i][j];
	}  

	/* Verification de l'equivalence des topologies */
	L[1]=D[X[1]][X[2]];
	Path[1].U=X[1];
	Path[1].V=X[2];

	p=0;
	P=1;

	for(k=2;k<=n-1;k++)
	{

		DIS=(D[X[1]][X[k]]+D[X[1]][X[k+1]]-D[X[k]][X[k+1]])/2;
		DIS1=(D[X[1]][X[k+1]]+D[X[k]][X[k+1]]-D[X[1]][X[k]])/2;

		S=0.0;
		i=0;

		if (DIS>2*epsilon)
		{
			while (S<DIS-(epsilon))
			{
				i=i+1;
				S=S+L[i];
			}
		}
		else { DIS=0; i=1; }

		Tree[p+1].U=n+k-1;
		Tree[p+1].V=Path[i].V;
		Tree[p+1].LN=S-DIS;
		if (Tree[p+1].LN<epsilon) Tree[p+1].LN=2*epsilon; 

		for (j=i+1;j<=P;j++)
		{
			Tree[p+j-i+1].U=Path[j].U;
			Tree[p+j-i+1].V=Path[j].V;
			Tree[p+j-i+1].LN=L[j];
			if (L[j]<2*epsilon) L[j]=2*epsilon; 
		}
		p=p+P-i+1;

		Path[i].V=n+k-1;
		Path[i+1].U=n+k-1;
		Path[i+1].V=X[k+1];
		L[i]=L[i]+DIS-S;
		L[i+1]=DIS1;
		P=i+1;
	}

	for (i=1;i<=P;i++) 
	{
		Tree[p+i].U=Path[i].U;
		Tree[p+i].V=Path[i].V;
		Tree[p+i].LN=L[i];
	}

	for (i=1;i<=2*n-3;i++)
	{
		if (fabs(Tree[i].LN-epsilon)<=2*epsilon)
			Tree[i].LN=0.0;
		ARETE[2*i-2]=Tree[i].U;
		ARETE[2*i-1]=Tree[i].V;
		LONGUEUR[i-1]=Tree[i].LN;   
		if (LONGUEUR[i-1]<2*epsilon) LONGUEUR[i-1] = 2*epsilon;
	} 
	/*/== rajoute le 22 avril 2005*/
	while((pasfini==1)&&(binaire==0)){
		//printf("non-binaire");
		//printf("\n");
		//for(i=1;i<=2*n-3-kt;i++){
		//	printf("\n%d : %d--%d -> %lf",i,ARETE[2*i-2],ARETE[2*i-1],LONGUEUR[i-1]);
		//}
		pasfini = 0;
		for (i=1;i<=2*n-3-kt;i++){
			if((LONGUEUR[i-1] == 2*epsilon)&&(ARETE[2*i-2]>n)&&(ARETE[2*i-1]>n)){	//= branche interne de taille=2*epsilon
				if(ARETE[2*i-2] > ARETE[2*i-1]){
					SomToDel = ARETE[2*i-2];
					OtherSom = ARETE[2*i-1];
				}
				else{
					SomToDel = ARETE[2*i-1];
					OtherSom = ARETE[2*i-2];
				}
				pasfini=1;

				/*/printf("\n\n edge to delete = %d \n\n",SomToDel);
				//== trouver les branches connexes*/
				for (j=1;j<=2*n-3-kt;j++){

					if(j!=i){
						if(ARETE[2*j-2] == SomToDel){/*&&(ARETE[2*j-1] != ARETE[2*i-1])){*/
							ARETE[2*j-2] = OtherSom;
							/*/break;*/
						}
						else if(ARETE[2*j-1] == SomToDel){/*/&&(ARETE[2*j-2] != ARETE[2*i-1])){*/
							ARETE[2*j-1] = OtherSom;
							/*/break;*/
						}
					}
				}
				for(j=i;j<=2*n-3-kt;j++){
					LONGUEUR[j-1] = LONGUEUR[j];
					ARETE[2*j-1] = ARETE[2*(j+1)-1];
					ARETE[2*j-2] = ARETE[2*(j+1)-2];
				}
				for(j=1;j<=2*n-3-kt-1;j++){
					if(ARETE[2*j-2] > SomToDel)
						ARETE[2*j-2]--;
					if(ARETE[2*j-1] > SomToDel)
						ARETE[2*j-1]--;
				}
				break;
			}
		} 
		if(pasfini)
			kt++;
	}

	/*printf("\n\n");
	for(i=1;i<=2*n-3-kt;i++){
	printf("\n%d : %d--%d -> %lf",i,ARETE[2*i-2],ARETE[2*i-1],LONGUEUR[i-1]);
	}
	*/


	/*printf("\n\n");*/

	free(X);
	free(Tree);
	free(L);
	free(Path);


	for (i=0;i<=n;i++)    
		free(D[i]);

	free(D);

	return kt;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r8d, -0x84(%rbp)
movl %ecx, %r15d
movq %rdx, -0x80(%rbp)
movq %rsi, -0x58(%rbp)
movq %rdi, %rbx
leal 0x1(%r15), %eax
movl %eax, -0x38(%rbp)
movslq %eax, %r14
leaq (,%r14,4), %rdi
callq 0x1130
movq %rax, -0x48(%rbp)
shlq $0x3, %r14
movq %r14, %rdi
callq 0x1130
movq %rax, %r13
leal (%r15,%r15), %eax
movl %eax, -0x2c(%rbp)
movslq %eax, %rdi
shlq $0x4, %rdi
addq $-0x20, %rdi
callq 0x1130
movq %rax, %r12
movq %r15, -0x50(%rbp)
movslq %r15d, %rdi
movq %rdi, -0x78(%rbp)
shlq $0x4, %rdi
addq $0x20, %rdi
callq 0x1130
movq %rax, %r15
movq %r14, -0x40(%rbp)
movq %r14, %rdi
callq 0x1130
xorl %r14d, %r14d
movq %rax, -0x70(%rbp)
cmpq -0x78(%rbp), %r14
jg 0x2338
movq -0x40(%rbp), %rdi
callq 0x1130
movq -0x70(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
testq %rax, %rax
jne 0x2303
leaq 0x120d5(%rip), %rdi     # 0x143fe
xorl %eax, %eax
callq 0x1030
pushq $0x1
popq %rdi
callq 0x1100
leaq -0xa0(%rbp), %rdx
movl $0x1, (%rdx)
leaq -0x9c(%rbp), %rcx
movq -0x50(%rbp), %r14
movl %r14d, (%rcx)
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movl %r14d, %r8d
callq 0x214f
xorl %eax, %eax
testl %r14d, %r14d
cmovgl %r14d, %eax
incl %eax
movl -0x38(%rbp), %ecx
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq -0x70(%rbp), %r9
cmpq %rax, %rsi
je 0x23a5
movq %rdx, %rdi
cmpq %rdi, %rcx
je 0x23a0
movq (%rbx,%rsi,8), %r8
movsd (%r8,%rdi,8), %xmm0
movq (%r9,%rsi,8), %r8
movsd %xmm0, (%r8,%rdi,8)
incq %rdi
jmp 0x2382
incq %rsi
jmp 0x237a
movq -0x48(%rbp), %rcx
movslq 0x4(%rcx), %rax
movq (%r9,%rax,8), %rdx
movslq 0x8(%rcx), %rcx
movq %rdx, -0x90(%rbp)
movsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, 0x8(%r13)
movl %eax, 0x10(%r15)
movl %ecx, 0x14(%r15)
movq -0x50(%rbp), %rax
decl %eax
movq %rax, -0x50(%rbp)
xorl %r11d, %r11d
pushq $0x2
popq %rdi
movsd 0x11c21(%rip), %xmm2    # 0x14008
movsd 0x11c21(%rip), %xmm0    # 0x14010
movsd 0x11c29(%rip), %xmm1    # 0x14020
movsd 0x11c29(%rip), %xmm3    # 0x14028
movsd 0x11c09(%rip), %xmm4    # 0x14010
pushq $0x1
popq %rax
movl %eax, %ecx
movslq %r11d, %r8
cmpq -0x78(%rbp), %rdi
jge 0x25b2
movq -0x48(%rbp), %rdx
movslq (%rdx,%rdi,4), %rax
movq -0x90(%rbp), %rsi
movsd (%rsi,%rax,8), %xmm8
movslq 0x4(%rdx,%rdi,4), %rdx
movsd (%rsi,%rdx,8), %xmm5
movapd %xmm8, %xmm10
addsd %xmm5, %xmm10
movq (%r9,%rax,8), %rax
movq %rdx, -0x38(%rbp)
movsd (%rax,%rdx,8), %xmm9
subsd %xmm9, %xmm10
mulsd %xmm2, %xmm10
xorpd %xmm6, %xmm6
ucomisd %xmm0, %xmm10
pushq $0x1
popq %rax
movl %eax, %r10d
xorpd %xmm7, %xmm7
jbe 0x2492
movapd %xmm10, %xmm7
addsd %xmm1, %xmm7
xorl %r10d, %r10d
ucomisd %xmm6, %xmm7
jbe 0x248d
addsd 0x8(%r13,%r10,8), %xmm6
incq %r10
jmp 0x247b
movapd %xmm10, %xmm7
movq %r11, -0x40(%rbp)
leaq 0x1(%rdi), %rax
movq %rax, -0x68(%rbp)
addsd %xmm9, %xmm5
subsd %xmm8, %xmm5
mulsd %xmm2, %xmm5
addl -0x50(%rbp), %edi
movq %r8, %r11
shlq $0x4, %r11
movl %edi, 0x10(%r12,%r11)
movl %r10d, %eax
movq %rax, %rbx
shlq $0x4, %rbx
leaq (%r15,%rbx), %rdx
addq $0x4, %rdx
movq %rdx, -0x60(%rbp)
movl 0x4(%r15,%rbx), %r14d
movl %r14d, 0x14(%r12,%r11)
movapd %xmm6, %xmm8
subsd %xmm7, %xmm8
movapd %xmm8, %xmm9
cmpltsd %xmm3, %xmm9
movapd %xmm9, %xmm10
andnpd %xmm8, %xmm10
andpd %xmm4, %xmm9
orpd %xmm10, %xmm9
movlpd %xmm9, 0x18(%r12,%r11)
incl %r8d
movl %r10d, %r14d
cmpl %ecx, %r14d
jge 0x2566
incl %r14d
movslq %r14d, %r9
movq %r9, %r11
shlq $0x4, %r11
movl (%r15,%r11), %edx
movslq %r8d, %rsi
shlq $0x4, %rsi
movl %edx, 0x10(%r12,%rsi)
movl 0x4(%r15,%r11), %edx
movl %edx, 0x14(%r12,%rsi)
movsd (%r13,%r9,8), %xmm8
movsd %xmm8, 0x18(%r12,%rsi)
ucomisd %xmm8, %xmm0
jbe 0x2561
movabsq $0x3ef4f8b588e368f1, %rdx # imm = 0x3EF4F8B588E368F1
movq %rdx, (%r13,%r9,8)
incl %r8d
jmp 0x2511
addl -0x40(%rbp), %ecx
subl %r10d, %ecx
movl %ecx, %r11d
incl %r11d
movq -0x60(%rbp), %rcx
movl %edi, (%rcx)
movl %edi, 0x10(%r15,%rbx)
movq -0x38(%rbp), %rcx
movl %ecx, 0x14(%r15,%rbx)
addsd (%r13,%rax,8), %xmm7
subsd %xmm6, %xmm7
movsd %xmm7, (%r13,%rax,8)
movsd %xmm5, 0x8(%r13,%rax,8)
incl %r10d
movq -0x68(%rbp), %rdi
movl %r10d, %ecx
movq -0x70(%rbp), %r9
jmp 0x240c
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
shlq $0x3, %rcx
shlq $0x4, %r8
leaq (%r12,%r8), %rdx
addq $0x18, %rdx
movq -0x58(%rbp), %rdi
cmpq %rax, %rcx
je 0x25f6
movl 0x10(%r15,%rax,2), %esi
movl %esi, -0x8(%rdx,%rax,2)
movl 0x14(%r15,%rax,2), %esi
movl %esi, -0x4(%rdx,%rax,2)
movsd 0x8(%r13,%rax), %xmm2
movsd %xmm2, (%rdx,%rax,2)
addq $0x8, %rax
jmp 0x25cd
movl -0x2c(%rbp), %edx
addl $-0x3, %edx
xorl %eax, %eax
testl %edx, %edx
movl $0x0, %ecx
movl %edx, -0x2c(%rbp)
cmovgl %edx, %ecx
shlq $0x3, %rcx
movapd 0x11a69(%rip), %xmm2    # 0x14080
cmpq %rax, %rcx
je 0x2669
movsd 0x18(%r12,%rax,2), %xmm3
movapd %xmm3, %xmm4
addsd %xmm1, %xmm4
andpd %xmm2, %xmm4
ucomisd %xmm4, %xmm0
jb 0x263f
andq $0x0, 0x18(%r12,%rax,2)
xorpd %xmm3, %xmm3
movl 0x10(%r12,%rax,2), %edx
movq %rdx, (%rdi,%rax,2)
movl 0x14(%r12,%rax,2), %edx
movq %rdx, 0x8(%rdi,%rax,2)
movapd %xmm0, %xmm4
maxsd %xmm3, %xmm4
movq -0x80(%rbp), %rdx
movsd %xmm4, (%rdx,%rax)
addq $0x8, %rax
jmp 0x2617
leaq 0x8(%rdi), %rax
movq %rax, -0x60(%rbp)
xorl %r14d, %r14d
pushq $0x1
popq %rax
movl %eax, %edx
movq %r12, -0x98(%rbp)
cmpl $0x0, -0x84(%rbp)
movq -0x78(%rbp), %rax
jne 0x283d
cmpl $0x1, %edx
jne 0x283d
movl -0x2c(%rbp), %ecx
movl %ecx, %r10d
movq %r14, -0x38(%rbp)
subl %r14d, %r10d
movslq %r10d, %rcx
xorl %edx, %edx
testl %ecx, %ecx
movl $0x0, %edi
movq %rcx, -0x68(%rbp)
cmovgl %ecx, %edi
xorl %esi, %esi
movabsq $0x200000000, %r11      # imm = 0x200000000
pushq $0x2
popq %rbx
movq -0x60(%rbp), %rcx
xorl %r9d, %r9d
cmpq %r9, %rdi
je 0x2714
movq -0x80(%rbp), %r8
movsd (%r8,%r9,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x26f8
jp 0x26f8
movq -0x8(%rcx), %r8
cmpq %rax, %r8
jle 0x26f8
movq (%rcx), %r14
cmpq %rax, %r14
jg 0x271d
incq %r9
addq $0x10, %rcx
addq $0x2, %rbx
movabsq $0x200000000, %r8       # imm = 0x200000000
addq %r8, %r11
incl %esi
jmp 0x26d0
movq -0x68(%rbp), %rdi
jmp 0x2826
cmpq %r14, %r8
movq %r14, %rax
cmovgq %r8, %rax
cmovlq %r8, %r14
movslq %eax, %rdx
movslq %r14d, %rax
movq %rax, -0x40(%rbp)
movq -0x58(%rbp), %rcx
xorl %r8d, %r8d
pushq $0x1
popq %rax
cmpq %r8, %rdi
je 0x2798
cmpq %r8, %r9
je 0x278c
movq %rcx, %r14
cmpq %rdx, (%rcx)
je 0x2766
cmpq %rdx, 0x8(%rcx)
jne 0x278c
movq %rax, %r14
shlq $0x4, %r14
addq -0x58(%rbp), %r14
addq $-0x8, %r14
movq %r10, %r12
movq %rdx, %r10
movq %r15, %rdx
movq %r13, %r15
movq -0x40(%rbp), %r13
movq %r13, (%r14)
movq %r15, %r13
movq %rdx, %r15
movq %r10, %rdx
movq %r12, %r10
movq -0x98(%rbp), %r12
incq %rax
incq %r8
addq $0x10, %rcx
jmp 0x273f
cmpl %r10d, %esi
cmovgl %esi, %r10d
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdi
cmpq %r10, %r9
je 0x27ee
movq -0x80(%rbp), %rax
movsd (%rax,%rbx,4), %xmm1
movsd %xmm1, -0x8(%rax,%rbx,4)
movq %r11, %rax
sarq $0x1d, %rax
movq 0x8(%rsi,%rax), %rax
movslq %ebx, %rcx
movq %rax, -0x8(%rsi,%rcx,8)
movq (%rsi,%rcx,8), %rax
movq %rax, -0x10(%rsi,%rcx,8)
addq $0x2, %rbx
movabsq $0x200000000, %rax      # imm = 0x200000000
addq %rax, %r11
decq %r10
jmp 0x27a7
movq -0x60(%rbp), %rcx
pushq $0x1
popq %rsi
cmpq %rdi, %rsi
jge 0x2821
movq -0x8(%rcx), %rax
cmpq %rdx, %rax
jle 0x280a
decq %rax
movq %rax, -0x8(%rcx)
movq (%rcx), %rax
cmpq %rdx, %rax
jle 0x2818
decq %rax
movq %rax, (%rcx)
incq %rsi
addq $0x10, %rcx
jmp 0x27f5
pushq $0x1
popq %rax
movl %eax, %edx
incq %r9
xorl %eax, %eax
cmpq %rdi, %r9
setle %al
movq -0x38(%rbp), %r14
addl %eax, %r14d
jmp 0x2680
movq -0x48(%rbp), %rdi
callq 0x10f0
movq %r12, %rdi
callq 0x10f0
movq %r13, %rdi
callq 0x10f0
movq %r15, %rdi
callq 0x10f0
xorl %ebx, %ebx
cmpq -0x78(%rbp), %rbx
movq -0x70(%rbp), %rdi
jg 0x2878
movq (%rdi,%rbx,8), %rdi
callq 0x10f0
incq %rbx
jmp 0x2860
callq 0x10f0
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq