// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rdx, -0x30(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, -0x50(%rbp)
leal 0x1(%rdx), %eax
movl %eax, -0x48(%rbp)
movslq %eax, %r13
leaq (,%r13,8), %r14
movq %r14, %rdi
callq 0x1130
movq %rax, %rbx
movq %r14, %rdi
callq 0x1130
movq %rax, %r15
movq %r14, %rdi
callq 0x1130
movq %rax, %r12
movq %r14, -0x40(%rbp)
movq %r14, %rdi
callq 0x1130
movq %rax, -0x38(%rbp)
shlq $0x2, %r13
movq %r13, %rdi
callq 0x1130
movq %rax, %r13
movslq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
xorl %r14d, %r14d
cmpq -0x58(%rbp), %r14
jg 0x16b7
movq -0x40(%rbp), %rdi
callq 0x1130
movq %rax, (%rbx,%r14,8)
incq %r14
testq %rax, %rax
jne 0x1680
leaq 0x12d5c(%rip), %rdi     # 0x143fe
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030
xorl %eax, %eax
movq -0x30(%rbp), %rcx
testl %ecx, %ecx
cmovgl %ecx, %eax
leal 0x1(%rax), %r14d
movl -0x48(%rbp), %ecx
xorpd %xmm2, %xmm2
pushq $0x1
popq %rdx
movsd 0x12930(%rip), %xmm1    # 0x14008
movq %rdx, %rsi
movq -0x60(%rbp), %r11
movq -0x50(%rbp), %r9
cmpq %r14, %rsi
je 0x173c
andq $0x0, (%r12,%rsi,8)
movq -0x38(%rbp), %rdi
andq $0x0, (%rdi,%rsi,8)
xorpd %xmm0, %xmm0
movq %rdx, %rdi
cmpq %rdi, %rcx
je 0x1725
movq (%r9,%rsi,8), %r8
movsd (%r8,%rdi,8), %xmm3
movq (%rbx,%rsi,8), %r8
movsd %xmm3, (%r8,%rdi,8)
addsd %xmm3, %xmm0
movsd %xmm0, (%r12,%rsi,8)
incq %rdi
jmp 0x16fd
mulsd %xmm1, %xmm0
addsd %xmm0, %xmm2
movl %esi, (%r13,%rsi,4)
andq $0x0, (%r15,%rsi,8)
incq %rsi
jmp 0x16e3
movabsq $0x100000000, %rdx      # imm = 0x100000000
movl %eax, %eax
movq %rax, -0x40(%rbp)
movsd 0x128bc(%rip), %xmm0    # 0x14010
movsd 0x128bc(%rip), %xmm3    # 0x14018
movq -0x58(%rbp), %r10
movq %rcx, -0x50(%rbp)
movq %r14, -0x70(%rbp)
cmpq $0x4, %r10
jl 0x1a44
movapd %xmm2, %xmm6
addsd %xmm2, %xmm6
leal -0x2(%r10), %eax
cvtsi2sd %eax, %xmm4
movapd %xmm2, %xmm5
addsd %xmm2, %xmm5
movabsq $0x200000000, %rsi      # imm = 0x200000000
pushq $0x1
popq %r8
pushq $0x2
popq %r14
cmpq %r10, %r8
jge 0x1806
movq %rsi, -0x30(%rbp)
movq %r14, %r11
movq %rsi, %rax
sarq $0x20, %rax
cmpq %r10, %rax
jg 0x17f7
movapd %xmm5, %xmm7
subsd (%r12,%r8,8), %xmm7
subsd (%r12,%r11,8), %xmm7
movq (%rbx,%r8,8), %rax
movsd (%rax,%r11,8), %xmm8
mulsd %xmm4, %xmm8
addsd %xmm7, %xmm8
ucomisd %xmm8, %xmm6
cmoval %r8d, %edi
cmoval %r11d, %r9d
minsd %xmm6, %xmm8
incq %r11
addq %rdx, %rsi
movapd %xmm8, %xmm6
jmp 0x17a8
incq %r8
incq %r14
movq -0x30(%rbp), %rsi
addq %rdx, %rsi
jmp 0x179c
movslq %edi, %rsi
movq (%rbx,%rsi,8), %rax
movslq %r9d, %r8
movq %rax, -0x68(%rbp)
movsd (%rax,%r8,8), %xmm7
movsd (%r12,%rsi,8), %xmm5
movsd (%r12,%r8,8), %xmm8
movapd %xmm5, %xmm9
subsd %xmm8, %xmm9
divsd %xmm4, %xmm9
addsd %xmm7, %xmm9
mulsd %xmm1, %xmm9
movq -0x38(%rbp), %rax
subsd (%rax,%rsi,8), %xmm9
movapd %xmm8, %xmm6
subsd %xmm5, %xmm6
divsd %xmm4, %xmm6
addsd %xmm7, %xmm6
mulsd %xmm1, %xmm6
subsd (%rax,%r8,8), %xmm6
movapd %xmm0, %xmm4
maxsd %xmm9, %xmm4
mulsd %xmm1, %xmm7
movsd %xmm7, (%rax,%rsi,8)
addsd %xmm8, %xmm5
mulsd %xmm1, %xmm5
movq %rsi, -0x48(%rbp)
movl %esi, %esi
movl %r8d, %eax
movq %rax, -0x30(%rbp)
pushq $0x1
popq %r11
cmpq %r11, -0x50(%rbp)
je 0x18e4
cmpq %r11, %rsi
je 0x18df
cmpq %r11, -0x30(%rbp)
je 0x18df
movq (%rbx,%r11,8), %rax
movq -0x48(%rbp), %r14
movsd (%rax,%r14,8), %xmm7
addsd (%rax,%r8,8), %xmm7
movapd %xmm7, %xmm8
mulsd %xmm3, %xmm8
addsd (%r12,%r11,8), %xmm8
movsd %xmm8, (%r12,%r11,8)
mulsd %xmm1, %xmm7
movsd %xmm7, (%rax,%r14,8)
movq -0x68(%rbp), %rax
movsd %xmm7, (%rax,%r11,8)
incq %r11
jmp 0x1890
movq -0x48(%rbp), %rax
movq -0x68(%rbp), %rsi
andq $0x0, (%rsi,%rax,8)
movsd (%r12,%rax,8), %xmm7
addsd (%r12,%r8,8), %xmm7
mulsd %xmm1, %xmm7
subsd (%rsi,%r8,8), %xmm7
movsd %xmm7, (%r12,%rax,8)
cmpl %r10d, %r8d
movq -0x60(%rbp), %r11
movq -0x70(%rbp), %r14
je 0x196e
pushq $0x1
popq %rax
cmpq %r10, %rax
jge 0x1949
movq (%rbx,%rax,8), %rsi
movsd (%rsi,%r10,8), %xmm7
movsd %xmm7, (%rsi,%r8,8)
movq (%rbx,%r10,8), %rsi
movsd (%rsi,%rax,8), %xmm7
movq (%rbx,%r8,8), %rsi
movsd %xmm7, (%rsi,%rax,8)
incq %rax
jmp 0x191d
movq (%rbx,%r8,8), %rax
andq $0x0, (%rax,%r8,8)
movsd (%r12,%r10,8), %xmm7
movsd %xmm7, (%r12,%r8,8)
movq -0x38(%rbp), %rax
movsd (%rax,%r10,8), %xmm7
movsd %xmm7, (%rax,%r8,8)
movapd %xmm0, %xmm7
maxsd %xmm6, %xmm7
subsd %xmm5, %xmm2
xorl %eax, %eax
cmpq %rax, -0x40(%rbp)
je 0x19b9
movl 0x4(%r13,%rax,4), %esi
cmpl %edi, %esi
jne 0x199d
movsd 0x8(%r15,%rax,8), %xmm5
addsd %xmm4, %xmm5
movsd %xmm5, 0x8(%r15,%rax,8)
cmpl %r9d, %esi
jne 0x19b4
movsd 0x8(%r15,%rax,8), %xmm5
addsd %xmm7, %xmm5
movsd %xmm5, 0x8(%r15,%rax,8)
incq %rax
jmp 0x197c
pushq $0x1
popq %rax
cmpq %r14, %rax
je 0x1a01
cmpl %r9d, (%r13,%rax,4)
jne 0x19fc
pushq $0x1
popq %rsi
cmpq %rsi, %rcx
je 0x19fc
cmpl %edi, (%r13,%rsi,4)
jne 0x19f7
movsd (%r15,%rsi,8), %xmm4
addsd (%r15,%rax,8), %xmm4
movq (%r11,%rsi,8), %r8
movsd %xmm4, (%r8,%rax,8)
movq (%r11,%rax,8), %r8
movsd %xmm4, (%r8,%rsi,8)
incq %rsi
jmp 0x19cb
incq %rax
jmp 0x19bc
xorl %eax, %eax
cmpq %rax, -0x40(%rbp)
je 0x1a1a
cmpl %r9d, 0x4(%r13,%rax,4)
jne 0x1a15
movl %edi, 0x4(%r13,%rax,4)
incq %rax
jmp 0x1a03
cmpl %r10d, %r9d
jne 0x1a2b
decq %r10
decq -0x50(%rbp)
jmp 0x1768
xorl %eax, %eax
cmpq %rax, -0x40(%rbp)
je 0x1a1f
cmpl %r10d, 0x4(%r13,%rax,4)
jne 0x1a3f
movl %r9d, 0x4(%r13,%rax,4)
incq %rax
jmp 0x1a2d
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rdx
movsd 0x10(%rax), %xmm2
movsd 0x18(%rax), %xmm3
movapd %xmm2, %xmm4
addsd %xmm3, %xmm4
movsd 0x18(%rdx), %xmm5
subsd %xmm5, %xmm4
mulsd %xmm1, %xmm4
movq -0x38(%rbp), %rax
subsd 0x8(%rax), %xmm4
movapd %xmm2, %xmm6
addsd %xmm5, %xmm6
subsd %xmm3, %xmm6
mulsd %xmm1, %xmm6
subsd 0x10(%rax), %xmm6
addsd %xmm5, %xmm3
subsd %xmm2, %xmm3
mulsd %xmm1, %xmm3
subsd 0x18(%rax), %xmm3
movapd %xmm0, %xmm1
maxsd %xmm4, %xmm1
movapd %xmm0, %xmm2
maxsd %xmm6, %xmm2
maxsd %xmm3, %xmm0
pushq $0x1
popq %rax
movq %rax, %rdx
cmpq %r14, %rdx
je 0x1b41
movq %rax, %rsi
cmpq %rsi, %rcx
je 0x1b30
movl (%r13,%rdx,4), %edi
cmpl $0x2, %edi
je 0x1af5
cmpl $0x1, %edi
jne 0x1b2b
movl (%r13,%rsi,4), %edi
movapd %xmm1, %xmm3
movapd %xmm2, %xmm4
cmpl $0x2, %edi
je 0x1b05
movapd %xmm1, %xmm3
movapd %xmm0, %xmm4
cmpl $0x3, %edi
je 0x1b05
jmp 0x1b2b
cmpl $0x3, (%r13,%rsi,4)
movapd %xmm2, %xmm3
movapd %xmm0, %xmm4
jne 0x1b2b
movsd (%r15,%rsi,8), %xmm5
addsd (%r15,%rdx,8), %xmm5
addsd %xmm3, %xmm5
addsd %xmm4, %xmm5
movq (%r11,%rsi,8), %rdi
movsd %xmm5, (%rdi,%rdx,8)
movq (%r11,%rdx,8), %rdi
movsd %xmm5, (%rdi,%rsi,8)
incq %rsi
jmp 0x1ac0
movq (%r11,%rdx,8), %rsi
andq $0x0, (%rsi,%rdx,8)
incq %rdx
jmp 0x1ab4
movq %r13, %rdi
callq 0x10f0
movq %r15, %rdi
callq 0x10f0
movq %r12, %rdi
callq 0x10f0
movq -0x38(%rbp), %rdi
callq 0x10f0
xorl %r14d, %r14d
cmpq -0x58(%rbp), %r14
jg 0x1b79
movq (%rbx,%r14,8), %rdi
callq 0x10f0
incq %r14
jmp 0x1b65
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f0