// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movl %r8d, -0x98(%rbp)
movl %ecx, %r13d
movq %rdx, -0x58(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdi, -0x30(%rbp)
leal 0x1(%r13), %eax
movslq %eax, %r15
leaq (,%r15,4), %rdi
callq 0x1140
movq %rax, -0x68(%rbp)
leaq (,%r15,8), %r14
movq %r14, %rdi
callq 0x1140
movq %rax, -0x60(%rbp)
leal -0x2(,%r13,2), %eax
movl %eax, -0x94(%rbp)
movslq %eax, %rdi
shlq $0x4, %rdi
callq 0x1140
movq %rax, %rbx
movslq %r13d, %rdi
movq %rdi, -0xa0(%rbp)
shlq $0x4, %rdi
addq $0x20, %rdi
callq 0x1140
movq %rax, -0x78(%rbp)
movq %r14, %rdi
callq 0x1140
movl %r15d, %r12d
movq %r13, -0x48(%rbp)
testl %r13d, %r13d
movq %rax, %r13
js 0x24bf
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x1140
movq %rax, (%r13,%r15,8)
testq %rax, %rax
je 0x2afa
incq %r15
cmpq %r15, %r12
jne 0x24a1
leaq -0xb0(%rbp), %rdx
movl $0x1, (%rdx)
leaq -0xac(%rbp), %rcx
movq -0x48(%rbp), %r14
movl %r14d, (%rcx)
movq -0x30(%rbp), %r15
movq %r15, %rdi
movq -0x68(%rbp), %rsi
movl %r14d, %r8d
callq 0x22c1
movq %r15, %r9
testl %r14d, %r14d
movq -0x60(%rbp), %r15
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %r8
jle 0x252e
movl $0x1, %eax
movq (%r9,%rax,8), %rcx
movq (%r13,%rax,8), %rdx
movl $0x1, %esi
movsd (%rcx,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x2514
incq %rax
cmpq %r12, %rax
jne 0x2506
movq -0x68(%rbp), %rcx
movslq 0x4(%rcx), %rax
movq (%r13,%rax,8), %rdx
movslq 0x8(%rcx), %rcx
movq %rdx, -0x38(%rbp)
movsd (%rdx,%rcx,8), %xmm5
movsd %xmm5, 0x8(%r15)
movq -0x78(%rbp), %rsi
movl %eax, 0x10(%rsi)
movl %ecx, 0x14(%rsi)
movl $0x1, %r10d
xorl %edx, %edx
cmpl $0x3, %r14d
jl 0x2797
leal -0x1(%r14), %eax
movq %rax, -0x90(%rbp)
movl %r14d, %eax
movq %rax, -0x88(%rbp)
leaq 0x14(%rsi), %rax
movq %rax, -0x70(%rbp)
leaq 0x8(%r15), %rax
movq %rax, -0xa8(%rbp)
movl $0x1, %r14d
movl $0x2, %r11d
movsd 0x12a62(%rip), %xmm0    # 0x15008
movsd 0x12a62(%rip), %xmm1    # 0x15010
movsd 0x12a6a(%rip), %xmm2    # 0x15020
movsd 0x12a6a(%rip), %xmm3    # 0x15028
movsd 0x12a4a(%rip), %xmm4    # 0x15010
movq %r13, -0x50(%rbp)
movslq %ecx, %rax
movq -0x68(%rbp), %rcx
movslq 0x4(%rcx,%r11,4), %rdi
movq -0x38(%rbp), %rcx
movsd (%rcx,%rdi,8), %xmm6
movapd %xmm5, %xmm10
addsd %xmm6, %xmm10
movq (%r13,%rax,8), %rax
movsd (%rax,%rdi,8), %xmm7
subsd %xmm7, %xmm10
mulsd %xmm0, %xmm10
xorpd %xmm8, %xmm8
ucomisd %xmm1, %xmm10
movl $0x1, %eax
xorpd %xmm9, %xmm9
jbe 0x263e
movapd %xmm10, %xmm9
addsd %xmm2, %xmm9
xorl %eax, %eax
ucomisd %xmm8, %xmm9
jbe 0x2639
xorl %eax, %eax
addsd 0x8(%r15,%rax,8), %xmm8
incq %rax
ucomisd %xmm8, %xmm9
ja 0x2628
movapd %xmm10, %xmm9
movq %rdi, -0x30(%rbp)
movq -0x90(%rbp), %rcx
leal (%rcx,%r11), %edi
movslq %edx, %rcx
shlq $0x4, %rcx
movl %edi, 0x10(%rbx,%rcx)
movslq %eax, %r10
movq %r10, %r8
shlq $0x4, %r8
movl 0x4(%rsi,%r8), %r9d
movl %r9d, 0x14(%rbx,%rcx)
movapd %xmm8, %xmm10
subsd %xmm9, %xmm10
movapd %xmm10, %xmm11
cmpltsd %xmm3, %xmm11
movapd %xmm11, %xmm12
andnpd %xmm10, %xmm12
andpd %xmm4, %xmm11
orpd %xmm12, %xmm11
movlpd %xmm11, 0x18(%rbx,%rcx)
cmpl %r14d, %r10d
jge 0x2725
movl %eax, %r13d
leal 0x1(%r14), %ecx
movl %ecx, -0x80(%rbp)
leal 0x1(%rdx), %r9d
movq -0xa8(%rbp), %rcx
leaq (%rcx,%r13,8), %r15
shlq $0x4, %r13
addq -0x70(%rbp), %r13
subl %eax, %r14d
xorl %eax, %eax
movl -0x4(%r13), %esi
leal (%r9,%rax), %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
movl %esi, 0x10(%rbx,%rcx)
movl (%r13), %esi
movl %esi, 0x14(%rbx,%rcx)
movsd (%r15,%rax,8), %xmm10
movsd %xmm10, 0x18(%rbx,%rcx)
ucomisd %xmm10, %xmm1
jbe 0x2708
movabsq $0x3ef4f8b588e368f1, %rcx # imm = 0x3EF4F8B588E368F1
movq %rcx, (%r15,%rax,8)
incq %rax
addq $0x10, %r13
cmpl %eax, %r14d
jne 0x26cb
movq -0x50(%rbp), %r13
movq -0x60(%rbp), %r15
movq -0x78(%rbp), %rsi
movl -0x80(%rbp), %ecx
jmp 0x272b
incl %r14d
movl %r14d, %ecx
incq %r11
addsd %xmm6, %xmm7
subsd %xmm5, %xmm7
mulsd %xmm0, %xmm7
leaq (%rsi,%r8), %rax
addq $0x4, %rax
addl %ecx, %edx
subl %r10d, %edx
leal 0x1(%r10), %r14d
movl %edi, (%rax)
movl %edi, 0x10(%rsi,%r8)
movq -0x30(%rbp), %rcx
movl %ecx, 0x14(%rsi,%r8)
addsd (%r15,%r10,8), %xmm9
subsd %xmm8, %xmm9
movsd %xmm9, (%r15,%r10,8)
movsd %xmm7, 0x8(%r15,%r10,8)
movapd %xmm6, %xmm5
cmpq -0x88(%rbp), %r11
jne 0x25ca
movslq %edx, %rdx
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %r8
movl $0x1, %r10d
jmp 0x279d
movl $0x1, %r14d
movq -0x48(%rbp), %rax
leal (%rax,%rax), %r9d
cmpl $0x2, %r14d
cmovgel %r14d, %r10d
shlq $0x4, %rdx
leaq (%rdx,%rbx), %rax
addq $0x18, %rax
shlq $0x3, %r10
xorl %ecx, %ecx
movl 0x10(%rsi,%rcx,2), %edx
movl %edx, -0x8(%rax,%rcx,2)
movl 0x14(%rsi,%rcx,2), %edx
movl %edx, -0x4(%rax,%rcx,2)
movsd 0x8(%r15,%rcx), %xmm0
movsd %xmm0, (%rax,%rcx,2)
addq $0x8, %rcx
cmpq %rcx, %r10
jne 0x27bf
addl $-0x3, %r9d
movq -0x48(%rbp), %r14
cmpl $0x2, %r14d
jl 0x2869
cmpl $0x2, %r9d
movl $0x1, %eax
cmovgel %r9d, %eax
shlq $0x3, %rax
xorl %ecx, %ecx
movsd 0x12813(%rip), %xmm0    # 0x15020
movapd 0x1286b(%rip), %xmm1    # 0x15080
movsd 0x127f3(%rip), %xmm2    # 0x15010
movsd 0x18(%rbx,%rcx,2), %xmm3
movapd %xmm3, %xmm4
addsd %xmm0, %xmm4
andpd %xmm1, %xmm4
ucomisd %xmm4, %xmm2
jb 0x2842
movq $0x0, 0x18(%rbx,%rcx,2)
xorpd %xmm3, %xmm3
movl 0x10(%rbx,%rcx,2), %edx
movq %rdx, (%r8,%rcx,2)
movl 0x14(%rbx,%rcx,2), %edx
movq %rdx, 0x8(%r8,%rcx,2)
movapd %xmm2, %xmm4
maxsd %xmm3, %xmm4
movsd %xmm4, (%rdi,%rcx)
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0x281d
xorl %ecx, %ecx
cmpl $0x0, -0x98(%rbp)
jne 0x2a9d
testl %r9d, %r9d
jle 0x2a97
movabsq $0x200000000, %rsi      # imm = 0x200000000
leaq 0x8(%r8), %rdi
xorl %ecx, %ecx
movsd 0x12777(%rip), %xmm0    # 0x15010
movl %r9d, %edx
movq %r13, -0x50(%rbp)
movl %r9d, -0x88(%rbp)
movq %rdi, -0x80(%rbp)
movl %edx, %eax
movq %rax, -0x30(%rbp)
movl -0x94(%rbp), %eax
movl %eax, %r14d
movq %rcx, -0x38(%rbp)
subl %ecx, %r14d
movb $0x1, %r8b
movl $0x1, %edx
movl $0x1, %eax
movq %rdi, %r13
movq %rsi, %r9
movl $0x2, %r10d
xorl %r11d, %r11d
movq %rdx, %rsi
movq -0x58(%rbp), %rcx
movsd -0x8(%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x290c
jp 0x290c
movq -0x8(%r13), %rdx
cmpq -0xa0(%rbp), %rdx
jle 0x290c
movq (%r13), %r15
cmpq -0xa0(%rbp), %r15
jg 0x293c
leaq 0x1(%rsi), %rdx
cmpq -0x30(%rbp), %rsi
setb %r8b
addq $0x10, %r13
incl %eax
incq %r11
addl $0x2, %r10d
movabsq $0x200000000, %rcx      # imm = 0x200000000
addq %rcx, %r9
cmpq %rdx, %r14
jne 0x28dd
jmp 0x2a7c
cmpq %r15, %rdx
movq %r15, %rcx
cmovgq %rdx, %rcx
cmovlq %rdx, %r15
movq %rcx, -0x70(%rbp)
movslq %ecx, %r13
movslq %r15d, %rcx
movq %rcx, -0x90(%rbp)
movl %eax, %edi
movl $0x1, %r15d
movq -0x40(%rbp), %rdx
cmpq %r15, %rdi
je 0x2999
movq %rdx, %rcx
cmpq %r13, (%rdx)
je 0x2989
cmpq %r13, 0x8(%rdx)
jne 0x2999
movq %r15, %rcx
shlq $0x4, %rcx
addq -0x40(%rbp), %rcx
addq $-0x8, %rcx
movq %r13, %rax
movq -0x90(%rbp), %r13
movq %r13, (%rcx)
movq %rax, %r13
incq %r15
addq $0x10, %rdx
cmpq %r15, %r14
jne 0x2967
movq -0x30(%rbp), %r14
cmpl %esi, %r14d
movq -0x60(%rbp), %r15
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %rdx
movabsq $0x200000000, %rsi      # imm = 0x200000000
jl 0x29fd
movsd 0x8(%rcx,%r11,8), %xmm1
movsd %xmm1, (%rcx,%r11,8)
movq %r9, %rax
sarq $0x1d, %rax
movq 0x8(%rdx,%rax), %rax
movslq %r10d, %r10
movq %rax, -0x8(%rdx,%r10,8)
movq (%rdx,%r10,8), %rax
movq %rax, -0x10(%rdx,%r10,8)
incq %r11
addl $0x2, %r10d
addq %rsi, %r9
cmpq %r14, %r11
jb 0x29c4
cmpl $0x1, %r14d
movq -0x48(%rbp), %r14
movq -0x30(%rbp), %r10
movq -0x50(%rbp), %r13
movl -0x88(%rbp), %r9d
movq -0x80(%rbp), %rdi
je 0x2a60
cmpl $0x3, %r10d
movl $0x2, %eax
cmovll %eax, %r10d
movslq -0x70(%rbp), %rax
shlq $0x4, %r10
addq $-0x10, %r10
xorl %edx, %edx
movq -0x8(%rdi,%rdx), %rcx
cmpq %rax, %rcx
jle 0x2a47
decq %rcx
movq %rcx, -0x8(%rdi,%rdx)
movq (%rdi,%rdx), %rcx
cmpq %rax, %rcx
jle 0x2a57
decq %rcx
movq %rcx, (%rdi,%rdx)
addq $0x10, %rdx
cmpq %rdx, %r10
jne 0x2a35
movzbl %r8b, %eax
andl $0x1, %eax
movq -0x38(%rbp), %rcx
addl %eax, %ecx
xorl %eax, %eax
movl %r9d, %edx
subl %ecx, %edx
jg 0x28ab
jmp 0x2a9b
xorl %eax, %eax
cmpq -0x30(%rbp), %rsi
setb %al
movq -0x48(%rbp), %r14
movq -0x50(%rbp), %r13
movq -0x60(%rbp), %r15
movq -0x38(%rbp), %rcx
jmp 0x2a9b
xorl %ecx, %ecx
xorl %eax, %eax
addl %eax, %ecx
movq %rcx, -0x38(%rbp)
movq -0x68(%rbp), %rdi
callq 0x1100
movq %rbx, %rdi
callq 0x1100
movq %r15, %rdi
callq 0x1100
movq -0x78(%rbp), %rdi
callq 0x1100
testl %r14d, %r14d
js 0x2adc
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
callq 0x1100
incq %rbx
cmpq %rbx, %r12
jne 0x2aca
movq %r13, %rdi
callq 0x1100
movq -0x38(%rbp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x128fd(%rip), %rdi     # 0x153fe
xorl %eax, %eax
callq 0x1030
movl $0x1, %edi
callq 0x1110