// File: /workspace/github_cpp_makefile_star2_O3/dcarrey[P]hgt-detection/./utils_tree.cpp
// Function: NJ(double**, double**, int)
void NJ(double **D1,double **DA,int n)
{
	double **D,*T1,*S,*LP,Som,Smin,Sij,L,Lii,Ljj,l1,l2,l3;
	int *T,i,j,ii,jj,n1;

	D=(double **) malloc((n+1)*sizeof(double*));
	T1=(double *) malloc((n+1)*sizeof(double));
	S=(double *) malloc((n+1)*sizeof(double));
	LP=(double *) malloc((n+1)*sizeof(double));
	T=(int *) malloc((n+1)*sizeof(int));

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

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


	L=0;
	Som=0;
	for (i=1;i<=n;i++)
	{
		S[i]=0; LP[i]=0;
		for (j=1;j<=n;j++)
		{
			D[i][j]=D1[i][j];
			S[i]=S[i]+D[i][j];
		}
		Som=Som+S[i]/2;
		T[i]=i;
		T1[i]=0;
	}

	/* Procedure principale */
	for (n1=n;n1>3;n1--)
	{

		/* Recherche des plus proches voisins */
		Smin=2*Som;
		for (i=1;i<=n1-1;i++)
		{
			for (j=i+1;j<=n1;j++)
			{
				Sij=2*Som-S[i]-S[j]+D[i][j]*(n1-2);
				if (Sij<Smin)
				{
					Smin=Sij;
					ii=i;
					jj=j;
				}
			}
		}
		/* Nouveau groupement */

		Lii=(D[ii][jj]+(S[ii]-S[jj])/(n1-2))/2-LP[ii];
		Ljj=(D[ii][jj]+(S[jj]-S[ii])/(n1-2))/2-LP[jj];

		/* Mise a jour de D */

		if (Lii<2*epsilon) Lii=2*epsilon;
		if (Ljj<2*epsilon) Ljj=2*epsilon;
		L=L+Lii+Ljj;
		LP[ii]=0.5*D[ii][jj];

		Som=Som-(S[ii]+S[jj])/2;
		for (i=1;i<=n1;i++)
		{
			if ((i!=ii)&&(i!=jj))
			{
				S[i]=S[i]-0.5*(D[i][ii]+D[i][jj]);
				D[i][ii]=(D[i][ii]+D[i][jj])/2;
				D[ii][i]=D[i][ii];
			}
		}
		D[ii][ii]=0;
		S[ii]=0.5*(S[ii]+S[jj])-D[ii][jj];

		if (jj!=n1)
		{
			for (i=1;i<=n1-1;i++)
			{
				D[i][jj]=D[i][n1];
				D[jj][i]=D[n1][i];
			}
			D[jj][jj]=0;
			S[jj]=S[n1];
			LP[jj]=LP[n1];
		}
		/* Mise a jour de DA */
		for (i=1;i<=n;i++)
		{
			if (T[i]==ii) T1[i]=T1[i]+Lii;
			if (T[i]==jj) T1[i]=T1[i]+Ljj;
		}


		for (j=1;j<=n;j++)
		{
			if (T[j]==jj)
			{
				for (i=1;i<=n;i++)
				{
					if (T[i]==ii)
					{
						DA[i][j]=T1[i]+T1[j];
						DA[j][i]=DA[i][j];
					}
				}
			}
		}

		for (j=1;j<=n;j++)
			if (T[j]==jj)  T[j]=ii;

		if (jj!=n1)
		{
			for (j=1;j<=n;j++)
				if (T[j]==n1) T[j]=jj;
		}
	}

	/*Il reste 3 sommets */

	l1=(D[1][2]+D[1][3]-D[2][3])/2-LP[1];
	l2=(D[1][2]+D[2][3]-D[1][3])/2-LP[2];
	l3=(D[1][3]+D[2][3]-D[1][2])/2-LP[3];
	if (l1<2*epsilon) l1=2*epsilon;
	if (l2<2*epsilon) l2=2*epsilon;
	if (l3<2*epsilon) l3=2*epsilon;
	L=L+l1+l2+l3;

	for (j=1;j<=n;j++)
	{
		for (i=1;i<=n;i++)
		{
			if ((T[j]==1)&&(T[i]==2))
			{
				DA[i][j]=T1[i]+T1[j]+l1+l2;
				DA[j][i]=DA[i][j];
			}
			if ((T[j]==1)&&(T[i]==3))
			{
				DA[i][j]=T1[i]+T1[j]+l1+l3;
				DA[j][i]=DA[i][j];
			}
			if ((T[j]==2)&&(T[i]==3))
			{
				DA[i][j]=T1[i]+T1[j]+l2+l3;
				DA[j][i]=DA[i][j];
			}
		}
		DA[j][j]=0;
	}

	free(T);
	free(T1);
	free(S);
	free(LP);
	for (i=0;i<=n;i++)
	{
		free(D[i]);
	}
	free(D);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, -0x58(%rbp)
movq %rsi, %r15
movq %rdi, -0x60(%rbp)
leal 0x1(%rdx), %eax
movslq %eax, %rbx
leaq (,%rbx,8), %r14
movq %r14, %rdi
callq 0x1140
movq %rax, -0x38(%rbp)
movq %r14, %rdi
callq 0x1140
movq %rax, %r12
movq %r14, %rdi
callq 0x1140
movq %rax, %r13
movq %r14, -0x48(%rbp)
movq %r14, %rdi
callq 0x1140
movq %rax, -0x40(%rbp)
leaq (,%rbx,4), %rdi
callq 0x1140
movq %rax, %r14
movb $0x1, %dl
movl %ebx, %ebx
cmpl $0x0, -0x58(%rbp)
js 0x1a9f
xorl %ecx, %ecx
movq %rcx, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x1140
movq -0x38(%rbp), %rcx
movq -0x50(%rbp), %rdx
movq %rax, (%rcx,%rdx,8)
movq -0x50(%rbp), %rcx
testq %rax, %rax
je 0x1c07
incq %rcx
cmpq %rcx, %rbx
jne 0x170a
movq -0x58(%rbp), %rdi
testl %edi, %edi
sete %al
movb $0x1, %dl
je 0x1a9f
movb %al, -0x29(%rbp)
xorpd %xmm0, %xmm0
movl $0x1, %ecx
movsd 0x138ab(%rip), %xmm1    # 0x15008
movq -0x60(%rbp), %r8
movq -0x40(%rbp), %rax
movq $0x0, (%rax,%rcx,8)
movq (%r8,%rcx,8), %rax
movq -0x38(%rbp), %rdx
movq (%rdx,%rcx,8), %rdx
movl $0x1, %esi
xorpd %xmm2, %xmm2
movsd (%rax,%rsi,8), %xmm3
movsd %xmm3, (%rdx,%rsi,8)
addsd %xmm3, %xmm2
incq %rsi
cmpq %rsi, %rbx
jne 0x1782
movsd %xmm2, (%r13,%rcx,8)
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movl %ecx, (%r14,%rcx,4)
movq $0x0, (%r12,%rcx,8)
incq %rcx
cmpq %rbx, %rcx
jne 0x1761
cmpl $0x4, %edi
jl 0x1a9c
movl %edi, %ecx
movsd 0x13842(%rip), %xmm2    # 0x15010
movsd 0x13842(%rip), %xmm3    # 0x15018
movq %rbx, -0x50(%rbp)
movapd %xmm0, %xmm5
leal -0x2(%rcx), %eax
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
addsd %xmm0, %xmm5
movl $0x2, %r8d
movl $0x1, %r9d
movapd %xmm5, %xmm6
movq -0x38(%rbp), %rdx
cmpq %rcx, %r9
jge 0x1855
movapd %xmm5, %xmm8
subsd (%r13,%r9,8), %xmm8
movq (%rdx,%r9,8), %r10
movq %r8, %r11
movapd %xmm8, %xmm9
subsd (%r13,%r11,8), %xmm9
movsd (%r10,%r11,8), %xmm7
mulsd %xmm4, %xmm7
addsd %xmm9, %xmm7
ucomisd %xmm7, %xmm6
cmoval %r9d, %edi
minsd %xmm6, %xmm7
cmoval %r11d, %esi
leaq 0x1(%r11), %rax
movapd %xmm7, %xmm6
cmpq %rcx, %r11
movq %rax, %r11
jl 0x1818
jmp 0x1859
movapd %xmm6, %xmm7
incq %r9
incq %r8
movapd %xmm7, %xmm6
cmpq %rcx, %r9
jne 0x1800
movslq %edi, %r10
movq (%rdx,%r10,8), %r11
movslq %esi, %r9
movsd (%r11,%r9,8), %xmm7
movsd (%r13,%r10,8), %xmm6
movsd (%r13,%r9,8), %xmm8
movapd %xmm6, %xmm5
subsd %xmm8, %xmm5
divsd %xmm4, %xmm5
addsd %xmm7, %xmm5
mulsd %xmm1, %xmm5
movq -0x40(%rbp), %rax
subsd (%rax,%r10,8), %xmm5
movapd %xmm8, %xmm9
subsd %xmm6, %xmm9
divsd %xmm4, %xmm9
addsd %xmm7, %xmm9
mulsd %xmm1, %xmm9
subsd (%rax,%r9,8), %xmm9
movapd %xmm2, %xmm4
maxsd %xmm5, %xmm4
movapd %xmm2, %xmm5
maxsd %xmm9, %xmm5
mulsd %xmm1, %xmm7
movsd %xmm7, (%rax,%r10,8)
addsd %xmm8, %xmm6
mulsd %xmm1, %xmm6
subsd %xmm6, %xmm0
movl %r9d, %eax
movq %rax, -0x48(%rbp)
movl %r10d, %eax
movl $0x1, %edx
cmpq %rdx, %rax
je 0x1940
cmpq %rdx, -0x48(%rbp)
je 0x1940
movq -0x38(%rbp), %r8
movq (%r8,%rdx,8), %r8
movsd (%r8,%r10,8), %xmm6
addsd (%r8,%r9,8), %xmm6
movapd %xmm6, %xmm7
mulsd %xmm3, %xmm7
addsd (%r13,%rdx,8), %xmm7
movsd %xmm7, (%r13,%rdx,8)
mulsd %xmm1, %xmm6
movsd %xmm6, (%r8,%r10,8)
movsd %xmm6, (%r11,%rdx,8)
incq %rdx
cmpq %rdx, -0x50(%rbp)
jne 0x18fb
movsd (%r13,%r10,8), %xmm6
addsd (%r13,%r9,8), %xmm6
movq $0x0, (%r11,%r10,8)
mulsd %xmm1, %xmm6
subsd (%r11,%r9,8), %xmm6
movsd %xmm6, (%r13,%r10,8)
cmpq -0x48(%rbp), %rcx
je 0x19d1
movq -0x38(%rbp), %r11
movq (%r11,%rcx,8), %rdx
movq (%r11,%r9,8), %rax
movl $0x1, %r10d
movq (%r11,%r10,8), %r8
movsd (%r8,%rcx,8), %xmm6
movsd %xmm6, (%r8,%r9,8)
movsd (%rdx,%r10,8), %xmm6
movsd %xmm6, (%rax,%r10,8)
incq %r10
cmpq %r10, %rcx
jne 0x1988
movq $0x0, (%rax,%r9,8)
movsd (%r13,%rcx,8), %xmm6
movsd %xmm6, (%r13,%r9,8)
movq -0x40(%rbp), %rax
movsd (%rax,%rcx,8), %xmm6
movsd %xmm6, (%rax,%r9,8)
movl $0x1, %eax
movl (%r14,%rax,4), %edx
cmpl %edi, %edx
jne 0x19ee
movsd (%r12,%rax,8), %xmm6
addsd %xmm4, %xmm6
movsd %xmm6, (%r12,%rax,8)
cmpl %esi, %edx
jne 0x1a02
movsd (%r12,%rax,8), %xmm6
addsd %xmm5, %xmm6
movsd %xmm6, (%r12,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x19d6
movl $0x1, %r9d
cmpl %esi, (%r14,%r9,4)
jne 0x1a48
movl $0x1, %eax
cmpl %edi, (%r14,%rax,4)
jne 0x1a40
movsd (%r12,%rax,8), %xmm4
addsd (%r12,%r9,8), %xmm4
movq (%r15,%rax,8), %rdx
movsd %xmm4, (%rdx,%r9,8)
movq (%r15,%r9,8), %rdx
movsd %xmm4, (%rdx,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x1a1b
incq %r9
cmpq %rbx, %r9
jne 0x1a10
movl $0x1, %eax
cmpl %esi, (%r14,%rax,4)
jne 0x1a5f
movl %edi, (%r14,%rax,4)
incq %rax
cmpq %rax, %rbx
jne 0x1a55
cmpq -0x48(%rbp), %rcx
je 0x1a87
movl $0x1, %eax
movl (%r14,%rax,4), %edx
cmpq %rdx, %rcx
jne 0x1a7f
movl %esi, (%r14,%rax,4)
incq %rax
cmpq %rax, %rbx
jne 0x1a72
leaq -0x1(%rcx), %rax
decq -0x50(%rbp)
cmpq $0x4, %rcx
movq %rax, %rcx
jg 0x17da
movb -0x29(%rbp), %dl
testb %dl, %dl
jne 0x1bb2
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rax
movq 0x10(%rcx), %rcx
movsd 0x10(%rax), %xmm0
movsd 0x18(%rax), %xmm3
movapd %xmm0, %xmm2
addsd %xmm3, %xmm2
movsd 0x18(%rcx), %xmm1
subsd %xmm1, %xmm2
movsd 0x13532(%rip), %xmm4    # 0x15008
mulsd %xmm4, %xmm2
movq -0x40(%rbp), %rax
subsd 0x8(%rax), %xmm2
movapd %xmm0, %xmm5
addsd %xmm1, %xmm5
subsd %xmm3, %xmm5
mulsd %xmm4, %xmm5
subsd 0x10(%rax), %xmm5
addsd %xmm1, %xmm3
subsd %xmm0, %xmm3
mulsd %xmm4, %xmm3
subsd 0x18(%rax), %xmm3
movsd 0x134ff(%rip), %xmm0    # 0x15010
movapd %xmm0, %xmm1
maxsd %xmm2, %xmm1
movapd %xmm0, %xmm2
maxsd %xmm5, %xmm2
maxsd %xmm3, %xmm0
movl $0x1, %eax
movl (%r14,%rax,4), %ecx
movl $0x1, %edx
cmpl $0x2, %ecx
je 0x1b5d
cmpl $0x1, %ecx
jne 0x1b92
movl (%r14,%rdx,4), %esi
movapd %xmm1, %xmm3
movapd %xmm2, %xmm4
cmpl $0x2, %esi
je 0x1b6c
movapd %xmm1, %xmm3
movapd %xmm0, %xmm4
cmpl $0x3, %esi
je 0x1b6c
jmp 0x1b92
cmpl $0x3, (%r14,%rdx,4)
movapd %xmm2, %xmm3
movapd %xmm0, %xmm4
jne 0x1b92
movsd (%r12,%rdx,8), %xmm5
addsd (%r12,%rax,8), %xmm5
addsd %xmm3, %xmm5
addsd %xmm4, %xmm5
movq (%r15,%rdx,8), %rsi
movsd %xmm5, (%rsi,%rax,8)
movq (%r15,%rax,8), %rsi
movsd %xmm5, (%rsi,%rdx,8)
incq %rdx
cmpq %rdx, %rbx
jne 0x1b33
movq (%r15,%rax,8), %rcx
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq %rbx, %rax
jne 0x1b2a
movq %r14, %rdi
callq 0x1100
movq %r12, %rdi
callq 0x1100
movq %r13, %rdi
callq 0x1100
movq -0x40(%rbp), %rdi
callq 0x1100
cmpl $0x0, -0x58(%rbp)
movq -0x38(%rbp), %r15
js 0x1bf1
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x1100
incq %r14
cmpq %r14, %rbx
jne 0x1be0
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1100
leaq 0x137f0(%rip), %rdi     # 0x153fe
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030