// File: /workspace/github_cpp_makefile_star2_O1/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 %rsi, %r15
movq %rdi, -0x50(%rbp)
movq %rdx, -0x30(%rbp)
movslq %edx, %r14
leaq 0x8(,%r14,8), %rbx
movq %rbx, %rdi
callq 0x1140
movq %rax, -0x40(%rbp)
movq %rbx, %rdi
callq 0x1140
movq %rax, %r12
movq %rbx, %rdi
callq 0x1140
movq %rax, %r13
movq %rbx, -0x58(%rbp)
movq %rbx, %rdi
callq 0x1140
movq %rax, -0x38(%rbp)
leaq 0x4(,%r14,4), %rdi
callq 0x1140
movq %rax, %rbx
testl %r14d, %r14d
js 0x173f
movq -0x30(%rbp), %rax
incl %eax
movq %rax, -0x48(%rbp)
xorl %r14d, %r14d
movq -0x58(%rbp), %rdi
callq 0x1140
movq -0x40(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
testq %rax, %rax
je 0x17c6
incq %r14
cmpq %r14, -0x48(%rbp)
jne 0x171c
movq -0x30(%rbp), %rax
testl %eax, %eax
jle 0x17e2
incl %eax
xorpd %xmm0, %xmm0
movl $0x1, %ecx
xorl %edx, %edx
movsd 0x138a8(%rip), %xmm1    # 0x15008
movq -0x40(%rbp), %r14
movq -0x50(%rbp), %r9
movq %rdx, (%r13,%rcx,8)
movq -0x38(%rbp), %rsi
movq %rdx, (%rsi,%rcx,8)
movq (%r9,%rcx,8), %rsi
movq (%r14,%rcx,8), %rdi
movsd (%r13,%rcx,8), %xmm2
movl $0x1, %r8d
movsd (%rsi,%r8,8), %xmm3
movsd %xmm3, (%rdi,%r8,8)
addsd %xmm3, %xmm2
incq %r8
cmpq %r8, %rax
jne 0x178a
movsd %xmm2, (%r13,%rcx,8)
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movl %ecx, (%rbx,%rcx,4)
movq $0x0, (%r12,%rcx,8)
incq %rcx
cmpq %rax, %rcx
jne 0x1768
jmp 0x17ea
leaq 0x13c31(%rip), %rdi     # 0x153fe
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030
xorpd %xmm0, %xmm0
movq -0x40(%rbp), %r14
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq %rcx, -0x50(%rbp)
cmpl $0x4, %eax
jl 0x1aea
movq -0x30(%rbp), %rax
leal 0x1(%rax), %ecx
movsd 0x137fc(%rip), %xmm1    # 0x15008
movsd 0x137fc(%rip), %xmm2    # 0x15010
movsd 0x137fc(%rip), %xmm3    # 0x15018
movq -0x50(%rbp), %rsi
movq %rcx, -0x58(%rbp)
movapd %xmm0, %xmm4
leal -0x2(%rsi), %eax
xorps %xmm5, %xmm5
cvtsi2sd %eax, %xmm5
addsd %xmm0, %xmm4
movl $0x2, %eax
movl $0x1, %r9d
movapd %xmm4, %xmm6
cmpq %rsi, %r9
jge 0x189a
movapd %xmm4, %xmm8
subsd (%r13,%r9,8), %xmm8
movq (%r14,%r9,8), %r10
movq %rax, %r11
movapd %xmm8, %xmm9
subsd (%r13,%r11,8), %xmm9
movsd (%r10,%r11,8), %xmm7
mulsd %xmm5, %xmm7
addsd %xmm9, %xmm7
ucomisd %xmm7, %xmm6
cmoval %r9d, %r8d
minsd %xmm6, %xmm7
cmoval %r11d, %edi
leaq 0x1(%r11), %rdx
movapd %xmm7, %xmm6
cmpq %rsi, %r11
movq %rdx, %r11
jl 0x185d
jmp 0x189e
movapd %xmm6, %xmm7
incq %r9
incq %rax
movapd %xmm7, %xmm6
cmpq %rsi, %r9
jne 0x1845
movslq %r8d, %r11
movq (%r14,%r11,8), %rax
movslq %edi, %r10
movsd (%rax,%r10,8), %xmm8
movsd (%r13,%r11,8), %xmm6
movsd (%r13,%r10,8), %xmm7
movapd %xmm6, %xmm5
subsd %xmm7, %xmm5
leal -0x2(%rsi), %edx
xorps %xmm4, %xmm4
cvtsi2sd %edx, %xmm4
divsd %xmm4, %xmm5
addsd %xmm8, %xmm5
mulsd %xmm1, %xmm5
movq -0x38(%rbp), %rdx
subsd (%rdx,%r11,8), %xmm5
movapd %xmm7, %xmm9
subsd %xmm6, %xmm9
divsd %xmm4, %xmm9
addsd %xmm8, %xmm9
mulsd %xmm1, %xmm9
subsd (%rdx,%r10,8), %xmm9
movapd %xmm2, %xmm4
maxsd %xmm5, %xmm4
movapd %xmm2, %xmm5
maxsd %xmm9, %xmm5
mulsd %xmm1, %xmm8
movsd %xmm8, (%rdx,%r11,8)
addsd %xmm7, %xmm6
mulsd %xmm1, %xmm6
subsd %xmm6, %xmm0
movl %r10d, %edx
movq %rdx, -0x48(%rbp)
movl %r11d, %r14d
movl $0x1, %edx
cmpq %rdx, %r14
je 0x198e
cmpq %rdx, -0x48(%rbp)
je 0x198e
movq -0x40(%rbp), %r9
movq (%r9,%rdx,8), %r9
movsd (%r9,%r11,8), %xmm6
addsd (%r9,%r10,8), %xmm6
movapd %xmm6, %xmm7
mulsd %xmm3, %xmm7
addsd (%r13,%rdx,8), %xmm7
movsd %xmm7, (%r13,%rdx,8)
mulsd %xmm1, %xmm6
movsd %xmm6, (%r9,%r11,8)
movsd %xmm6, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, -0x58(%rbp)
jne 0x194a
movq $0x0, (%rax,%r11,8)
movsd (%r13,%r11,8), %xmm6
addsd (%r13,%r10,8), %xmm6
mulsd %xmm1, %xmm6
subsd (%rax,%r10,8), %xmm6
movsd %xmm6, (%r13,%r11,8)
cmpq -0x48(%rbp), %rsi
movq -0x40(%rbp), %r14
je 0x1a23
movq (%r14,%rsi,8), %rax
movq (%r14,%r10,8), %rdx
movl $0x1, %r11d
movq (%r14,%r11,8), %r9
movsd (%r9,%rsi,8), %xmm6
movsd %xmm6, (%r9,%r10,8)
movsd (%rax,%r11,8), %xmm6
movsd %xmm6, (%rdx,%r11,8)
incq %r11
cmpq %r11, %rsi
jne 0x19d6
movq (%r14,%r10,8), %rax
movq $0x0, (%rax,%r10,8)
movsd (%r13,%rsi,8), %xmm6
movsd %xmm6, (%r13,%r10,8)
movq -0x38(%rbp), %rax
movsd (%rax,%rsi,8), %xmm6
movsd %xmm6, (%rax,%r10,8)
movl $0x1, %eax
movl (%rbx,%rax,4), %edx
cmpl %r8d, %edx
jne 0x1a40
movsd (%r12,%rax,8), %xmm6
addsd %xmm4, %xmm6
movsd %xmm6, (%r12,%rax,8)
cmpl %edi, %edx
jne 0x1a54
movsd (%r12,%rax,8), %xmm6
addsd %xmm5, %xmm6
movsd %xmm6, (%r12,%rax,8)
incq %rax
cmpq %rax, %rcx
jne 0x1a28
movl $0x1, %eax
cmpl %edi, (%rbx,%rax,4)
jne 0x1a99
movl $0x1, %edx
cmpl %r8d, (%rbx,%rdx,4)
jne 0x1a91
movsd (%r12,%rdx,8), %xmm4
addsd (%r12,%rax,8), %xmm4
movq (%r15,%rdx,8), %r9
movsd %xmm4, (%r9,%rax,8)
movq (%r15,%rax,8), %r9
movsd %xmm4, (%r9,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x1a6b
incq %rax
cmpq %rcx, %rax
jne 0x1a61
movl $0x1, %eax
cmpl %edi, (%rbx,%rax,4)
jne 0x1aaf
movl %r8d, (%rbx,%rax,4)
incq %rax
cmpq %rax, %rcx
jne 0x1aa6
cmpq -0x48(%rbp), %rsi
je 0x1ad5
movl $0x1, %eax
movl (%rbx,%rax,4), %edx
cmpq %rdx, %rsi
jne 0x1acd
movl %edi, (%rbx,%rax,4)
incq %rax
cmpq %rax, %rcx
jne 0x1ac2
leaq -0x1(%rsi), %rax
decq -0x58(%rbp)
cmpq $0x4, %rsi
movq %rax, %rsi
jg 0x1824
cmpl $0x0, -0x30(%rbp)
movq -0x50(%rbp), %r8
jle 0x1c4f
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
movsd 0x10(%rax), %xmm0
movsd 0x18(%rax), %xmm3
movapd %xmm0, %xmm2
addsd %xmm3, %xmm2
movsd 0x18(%rcx), %xmm1
subsd %xmm1, %xmm2
movsd 0x134e5(%rip), %xmm4    # 0x15008
mulsd %xmm4, %xmm2
movq -0x38(%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 0x134b2(%rip), %xmm0    # 0x15010
movapd %xmm0, %xmm1
maxsd %xmm2, %xmm1
movapd %xmm0, %xmm2
maxsd %xmm5, %xmm2
maxsd %xmm3, %xmm0
leaq 0x4(,%r8,4), %rcx
incq %r8
addq $-0x4, %rcx
movl $0x1, %eax
movl (%rbx,%rax,4), %edx
xorl %esi, %esi
cmpl $0x1, %edx
jne 0x1bf5
cmpl $0x2, 0x4(%rbx,%rsi)
jne 0x1bc0
movsd 0x8(%r12,%rsi,2), %xmm3
addsd (%r12,%rax,8), %xmm3
addsd %xmm1, %xmm3
addsd %xmm2, %xmm3
movq 0x8(%r15,%rsi,2), %rdi
movsd %xmm3, (%rdi,%rax,8)
movq (%r15,%rax,8), %rdi
movsd %xmm3, 0x8(%rdi,%rsi,2)
cmpl $0x1, %edx
jne 0x1bf5
cmpl $0x3, 0x4(%rbx,%rsi)
jne 0x1bf5
movsd 0x8(%r12,%rsi,2), %xmm3
addsd (%r12,%rax,8), %xmm3
addsd %xmm1, %xmm3
addsd %xmm0, %xmm3
movq 0x8(%r15,%rsi,2), %rdi
movsd %xmm3, (%rdi,%rax,8)
movq (%r15,%rax,8), %rdi
movsd %xmm3, 0x8(%rdi,%rsi,2)
cmpl $0x2, %edx
jne 0x1c2a
cmpl $0x3, 0x4(%rbx,%rsi)
jne 0x1c2a
movsd 0x8(%r12,%rsi,2), %xmm3
addsd (%r12,%rax,8), %xmm3
addsd %xmm2, %xmm3
addsd %xmm0, %xmm3
movq 0x8(%r15,%rsi,2), %rdi
movsd %xmm3, (%rdi,%rax,8)
movq (%r15,%rax,8), %rdi
movsd %xmm3, 0x8(%rdi,%rsi,2)
addq $0x4, %rsi
cmpq %rsi, %rcx
jne 0x1b8b
movq (%r15,%rax,8), %rdx
movq $0x0, (%rdx,%rax,8)
incq %rax
cmpq %r8, %rax
jne 0x1b86
movq %rbx, %rdi
callq 0x1100
movq %r12, %rdi
callq 0x1100
movq %r13, %rdi
callq 0x1100
movq -0x38(%rbp), %rdi
callq 0x1100
movq %r14, %r15
movq -0x30(%rbp), %r14
testl %r14d, %r14d
js 0x1c92
incl %r14d
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
callq 0x1100
incq %rbx
cmpq %rbx, %r14
jne 0x1c81
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1100