// File: /workspace/github_cpp_makefile_star2_O1/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, -0x34(%rbp)
movq %rdx, -0x90(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdi, -0x40(%rbp)
movq %rcx, -0x50(%rbp)
movslq %ecx, %rbx
leaq 0x4(,%rbx,4), %rdi
callq 0x1140
movq %rax, -0x80(%rbp)
leaq 0x8(,%rbx,8), %r13
movq %r13, %rdi
callq 0x1140
movq %rax, -0x48(%rbp)
leal (%rbx,%rbx), %eax
movq %rax, -0x78(%rbp)
movslq %eax, %rdi
shlq $0x4, %rdi
addq $-0x20, %rdi
callq 0x1140
movq %rax, %r12
movq %rbx, %rdi
shlq $0x4, %rdi
addq $0x20, %rdi
callq 0x1140
movq %rax, %r14
movq %r13, %r15
movq %r13, %rdi
callq 0x1140
movq %rax, -0x68(%rbp)
movq %rbx, -0xa8(%rbp)
testl %ebx, %ebx
js 0x252e
movq -0x50(%rbp), %rax
leal 0x1(%rax), %ebx
xorl %r13d, %r13d
movq %r15, %rdi
callq 0x1140
movq -0x68(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
testq %rax, %rax
je 0x2bb3
incq %r13
cmpq %r13, %rbx
jne 0x250d
leaq -0x2c(%rbp), %rdx
movl $0x1, (%rdx)
leaq -0x30(%rbp), %rcx
movq -0x50(%rbp), %rbx
movl %ebx, (%rcx)
movq -0x40(%rbp), %r13
movq %r13, %rdi
movq -0x80(%rbp), %rsi
movl %ebx, %r8d
callq 0x234d
movl $0x1, %r9d
movl %ebx, %ecx
movl $0x1, %eax
testl %ebx, %ebx
movq -0x70(%rbp), %r8
movq -0x68(%rbp), %r15
jle 0x25a2
leal 0x1(%rbx), %edx
movl $0x1, %eax
movq (%r13,%rax,8), %rsi
movq (%r15,%rax,8), %rdi
movl $0x1, %ecx
movsd (%rsi,%rcx,8), %xmm0
movsd %xmm0, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %rdx
jne 0x2584
incq %rax
cmpq %rdx, %rax
jne 0x2576
movq -0x50(%rbp), %rbx
movl %eax, -0x2c(%rbp)
movl %ecx, -0x30(%rbp)
movq -0x80(%rbp), %rcx
movslq 0x4(%rcx), %rax
movq (%r15,%rax,8), %rdx
movslq 0x8(%rcx), %rcx
movq %rdx, -0x58(%rbp)
movsd (%rdx,%rcx,8), %xmm0
movq -0x48(%rbp), %rdx
movsd %xmm0, 0x8(%rdx)
movl %eax, 0x10(%r14)
movl %ecx, 0x14(%r14)
xorl %eax, %eax
cmpl $0x3, %ebx
jl 0x2801
leal -0x1(%rbx), %ecx
movq %rcx, -0xa0(%rbp)
movl %ebx, %ecx
movq %rcx, -0x98(%rbp)
movl $0x1, %r10d
movl $0x2, %r8d
movsd 0x12a04(%rip), %xmm0    # 0x15008
movsd 0x12a04(%rip), %xmm1    # 0x15010
movsd 0x12a0c(%rip), %xmm2    # 0x15020
movsd 0x12a0c(%rip), %xmm3    # 0x15028
movsd 0x129ec(%rip), %xmm4    # 0x15010
movq -0x80(%rbp), %rdx
movslq (%rdx,%r8,4), %rcx
movq -0x58(%rbp), %rsi
movsd (%rsi,%rcx,8), %xmm7
movslq 0x4(%rdx,%r8,4), %rdx
movsd (%rsi,%rdx,8), %xmm5
movapd %xmm7, %xmm10
addsd %xmm5, %xmm10
movq (%r15,%rcx,8), %rcx
movq %rdx, -0x88(%rbp)
movsd (%rcx,%rdx,8), %xmm8
subsd %xmm8, %xmm10
mulsd %xmm0, %xmm10
xorpd %xmm6, %xmm6
ucomisd %xmm1, %xmm10
movl $0x1, %r11d
xorpd %xmm9, %xmm9
jbe 0x26ab
movapd %xmm10, %xmm9
addsd %xmm2, %xmm9
xorl %r11d, %r11d
ucomisd %xmm6, %xmm9
jbe 0x26a6
xorl %r11d, %r11d
movq -0x48(%rbp), %rcx
addsd 0x8(%rcx,%r11,8), %xmm6
incq %r11
ucomisd %xmm6, %xmm9
ja 0x2695
movapd %xmm10, %xmm9
movq -0xa0(%rbp), %rcx
leal (%rcx,%r8), %edx
movslq %eax, %rcx
shlq $0x4, %rcx
movl %edx, -0x60(%rbp)
movl %edx, 0x10(%r12,%rcx)
movslq %r11d, %rdi
movq %rdi, %r13
shlq $0x4, %r13
movl 0x4(%r14,%r13), %edx
movl %edx, 0x14(%r12,%rcx)
movapd %xmm6, %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(%r12,%rcx)
leal 0x1(%rdi), %ecx
movq %rcx, -0x40(%rbp)
movl %ecx, %edx
cmpl %r10d, %edi
jge 0x2786
movl %r11d, %r9d
leal 0x1(%rax), %ebx
shlq $0x3, %r9
leal 0x1(%r11), %esi
movl %r10d, %ecx
negl %ecx
movl 0x10(%r14,%r9,2), %edx
movslq %ebx, %r15
shlq $0x4, %r15
movl %edx, 0x10(%r12,%r15)
movl 0x14(%r14,%r9,2), %edx
movl %edx, 0x14(%r12,%r15)
movq -0x48(%rbp), %rdx
movsd 0x8(%rdx,%r9), %xmm10
movsd %xmm10, 0x18(%r12,%r15)
ucomisd %xmm10, %xmm1
jbe 0x276d
movabsq $0x3ef4f8b588e368f1, %r15 # imm = 0x3EF4F8B588E368F1
movq %r15, 0x8(%rdx,%r9)
incl %ebx
addq $0x8, %r9
movl %esi, %edx
incl %edx
leal (%rcx,%rsi), %r15d
incl %r15d
movl %edx, %esi
cmpl $0x1, %r15d
jne 0x272a
incq %r8
addsd %xmm8, %xmm5
subsd %xmm7, %xmm5
mulsd %xmm0, %xmm5
leaq (%r14,%r13), %rcx
addq $0x4, %rcx
addl %eax, %r10d
subl %r11d, %r10d
movl %r10d, %eax
incl %eax
movl -0x60(%rbp), %esi
movl %esi, (%rcx)
movl %esi, 0x10(%r14,%r13)
movq -0x88(%rbp), %rcx
movl %ecx, 0x14(%r14,%r13)
movq -0x48(%rbp), %rcx
addsd (%rcx,%rdi,8), %xmm9
subsd %xmm6, %xmm9
movsd %xmm9, (%rcx,%rdi,8)
movsd %xmm5, 0x8(%rcx,%rdi,8)
movq -0x40(%rbp), %r9
movl %r9d, %r10d
cmpq -0x98(%rbp), %r8
movq -0x68(%rbp), %r15
jne 0x2624
cltq
movq -0x90(%rbp), %rbx
movq -0x70(%rbp), %r8
jmp 0x280f
movl -0x2c(%rbp), %r11d
movl -0x30(%rbp), %edx
movq -0x90(%rbp), %rbx
movl %r11d, -0x2c(%rbp)
movl %edx, -0x30(%rbp)
cmpl $0x2, %r9d
movl $0x1, %ecx
cmovll %ecx, %r9d
shlq $0x4, %rax
addq %r12, %rax
addq $0x18, %rax
shlq $0x3, %r9
xorl %edx, %edx
movq -0x48(%rbp), %rdi
movl 0x10(%r14,%rdx,2), %esi
movl %esi, -0x8(%rax,%rdx,2)
movl 0x14(%r14,%rdx,2), %esi
movl %esi, -0x4(%rax,%rdx,2)
movsd 0x8(%rdi,%rdx), %xmm0
movsd %xmm0, (%rax,%rdx,2)
addq $0x8, %rdx
incl %ecx
cmpq %rdx, %r9
jne 0x2838
movl %ecx, -0x2c(%rbp)
movq -0x78(%rbp), %rax
addl $-0x3, %eax
movl %eax, -0x88(%rbp)
movl $0x1, %r11d
cmpl $0x2, -0x50(%rbp)
jl 0x2902
movl -0x88(%rbp), %eax
cmpl $0x2, %eax
movl $0x1, %r11d
cmovgel %eax, %r11d
leaq (,%r11,8), %rax
incl %r11d
xorl %ecx, %ecx
movsd 0x12778(%rip), %xmm0    # 0x15020
movapd 0x127d0(%rip), %xmm1    # 0x15080
movsd 0x12758(%rip), %xmm2    # 0x15010
movsd 0x18(%r12,%rcx,2), %xmm3
addsd %xmm0, %xmm3
andpd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jb 0x28d6
movq $0x0, 0x18(%r12,%rcx,2)
movl 0x10(%r12,%rcx,2), %edx
movq %rdx, (%r8,%rcx,2)
movl 0x14(%r12,%rcx,2), %edx
movq %rdx, 0x8(%r8,%rcx,2)
movapd %xmm2, %xmm3
maxsd 0x18(%r12,%rcx,2), %xmm3
movsd %xmm3, (%rbx,%rcx)
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0x28b8
movl %r11d, -0x2c(%rbp)
movl -0x30(%rbp), %r10d
movl -0x34(%rbp), %edx
testl %edx, %edx
je 0x2985
xorl %esi, %esi
movq %rsi, %r13
movl %r11d, -0x2c(%rbp)
movl %r10d, -0x30(%rbp)
movq -0x80(%rbp), %rdi
callq 0x1100
movq %r12, %rdi
callq 0x1100
movq -0x48(%rbp), %rdi
callq 0x1100
movq %r14, %rdi
callq 0x1100
movq -0x50(%rbp), %rbx
testl %ebx, %ebx
movl $0x0, %r14d
js 0x2964
incl %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x1100
incq %r14
cmpq %r14, %rbx
jne 0x2953
movl %r14d, -0x2c(%rbp)
movq %r15, %rdi
callq 0x1100
movl %r13d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x78(%rbp), %rax
leal -0x4(%rax), %ecx
movl %ecx, -0x98(%rbp)
addl $-0x2, %eax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
xorl %esi, %esi
movsd 0x12661(%rip), %xmm0    # 0x15010
movl -0x88(%rbp), %ecx
movl %ecx, %eax
subl %esi, %eax
testl %eax, %eax
setg %r8b
movl %ecx, %eax
subl %esi, %eax
jle 0x2a7c
movq %rax, -0xa0(%rbp)
movl %eax, %edx
movl -0x98(%rbp), %eax
subl %esi, %eax
addl $0x2, %eax
movq %rax, -0x60(%rbp)
movq -0x78(%rbp), %r13
movq %rsi, -0x40(%rbp)
subl %esi, %r13d
movl $0x1, %r11d
movl $0x1, %r9d
movl $0x2, %esi
movabsq $0x200000000, %rcx      # imm = 0x200000000
xorl %edi, %edi
movq -0x58(%rbp), %rax
movsd -0x8(%rbx,%r11,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x2a35
jp 0x2a35
movq -0x8(%rax), %rbx
cmpq -0xa8(%rbp), %rbx
jle 0x2a35
movq (%rax), %r15
cmpq -0xa8(%rbp), %r15
jg 0x2aa2
cmpq %rdx, %r11
leaq 0x1(%r11), %rbx
setb %r8b
addq $0x10, %rax
incl %r9d
incq %rdi
addl $0x2, %esi
movabsq $0x200000000, %r11      # imm = 0x200000000
addq %r11, %rcx
movq %rbx, %r11
cmpq %rbx, %r13
movq -0x90(%rbp), %rbx
jne 0x2a0d
xorl %eax, %eax
movq -0x68(%rbp), %r15
movl -0x34(%rbp), %edx
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %r11
jmp 0x2a84
xorl %eax, %eax
movl $0x1, %r11d
movzbl %r8b, %ecx
andl $0x1, %ecx
addl %ecx, %esi
testl %edx, %edx
jne 0x2913
testb %al, %al
jne 0x29af
jmp 0x2913
cmpq %r15, %rbx
movq %r15, %rax
cmovgq %rbx, %rax
cmovlq %rbx, %r15
movq %rax, -0x60(%rbp)
cltq
movslq %r15d, %r15
movl %r9d, %r9d
movl $0x1, %r10d
movq -0x58(%rbp), %rbx
cmpq %r10, %r9
je 0x2adf
cmpq %rax, -0x8(%rbx)
jne 0x2ad7
movq %r15, -0x8(%rbx)
jmp 0x2adf
cmpq %rax, (%rbx)
jne 0x2adf
movq %r15, (%rbx)
incq %r10
addq $0x10, %rbx
cmpq %r10, %r13
jne 0x2ac6
movq -0xa0(%rbp), %r13
cmpl %r11d, %r13d
movq -0x90(%rbp), %rbx
movq -0x70(%rbp), %r9
movabsq $0x200000000, %r10      # imm = 0x200000000
jl 0x2b42
movsd 0x8(%rbx,%rdi,8), %xmm1
movsd %xmm1, (%rbx,%rdi,8)
movq %rcx, %rax
sarq $0x1d, %rax
movq 0x8(%r9,%rax), %rax
movslq %esi, %rsi
movq %rax, -0x8(%r9,%rsi,8)
movq (%r9,%rsi,8), %rax
movq %rax, -0x10(%r9,%rsi,8)
incq %rdi
addl $0x2, %esi
addq %r10, %rcx
cmpq %rdx, %rdi
jb 0x2b0c
cmpl $0x1, %r13d
movq -0x68(%rbp), %r15
jne 0x2b60
movb $0x1, %al
movl $0x1, %r10d
movl -0x34(%rbp), %edx
movq -0x40(%rbp), %rsi
jmp 0x2a84
movslq -0x60(%rbp), %rax
cmpl $0x3, %r13d
movl $0x2, %ecx
cmovll %ecx, %r13d
movl $0x1, %r10d
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %rsi
movq -0x8(%rcx), %rdx
cmpq %rax, %rdx
jle 0x2b8f
decq %rdx
movq %rdx, -0x8(%rcx)
movq (%rcx), %rdx
cmpq %rax, %rdx
jle 0x2b9d
decq %rdx
movq %rdx, (%rcx)
incq %r10
addq $0x10, %rcx
cmpq %r10, %r13
jne 0x2b7f
movb $0x1, %al
movl -0x34(%rbp), %edx
jmp 0x2a84
movl %r13d, -0x2c(%rbp)
leaq 0x12840(%rip), %rdi     # 0x153fe
xorl %eax, %eax
callq 0x1030
movl $0x1, %edi
callq 0x1110