// File: /workspace/github_cpp_makefile_star2_O1/dcarrey[P]hgt-detection/./utils_tree.cpp
// Function: BipartitionDistance(int**, int**, int)
double BipartitionDistance (int **B, int ** B1,int n)
{
	int i,j,k,k1,cpt1,cpt2,cpt3,cpt4,t1,t2;
	double c1,c2,min1,min2,cptB=0.0,cptB1=0.0;
	int *flag,*flag1,**Bi,**Bi1;
	Bi=(int **) malloc((2*n-2)*sizeof(int*));
	Bi1=(int **) malloc((2*n-2)*sizeof(int*));
	for (i=0;i<2*n-2;i++)
	{
		Bi[i]=(int *) malloc((2*n)*sizeof(int));
		Bi1[i]=(int *) malloc((2*n)*sizeof(int)); 
		if ((Bi1[i]==NULL)||(Bi[i]==NULL))
		{
			printf(" Data matrix is too large\n "); 
			exit(2);
		}        
	}
	flag = (int*) malloc(2*n*(sizeof(int)));
	flag1 = (int*) malloc(2*n*(sizeof(int)));
	for(i=1;i<=2*n-3;i++){
		flag[i] = flag1[i] = 0;
		for(j=1;j<=n;j++){
			flag[i] = flag[i] + B[i][j];
			flag1[i] = flag1[i] + B1[i][j];
		}
		if(flag[i] == 1 || flag[i] == n-1)
			flag[i] = 1;
		else
			flag[i] = 0;
		if(flag1[i] == 1 || flag1[i] == n-1)
			flag1[i] = 1;
		else
			flag1[i] = 0;
	}
    k=k1=1;
	for(i=1;i<=2*n-3;i++){
		if(flag[i] == 0){
			for(j=1;j<=n;j++)
				Bi[k][j] = B[i][j];
			k++;
		}
		if(flag1[i] == 0){
			for(j=1;j<=n;j++)
				Bi1[k1][j] = B1[i][j];
			k1++;
		}
	
	}
	
	/*for(i=1;i<=2*n-3;i++)
		printf("%d ",flag[i]);
	printf("\n");
	for(i=1;i<=2*n-3;i++)
		printf("%d ",flag1[i]);
	printf("\n");*/
	for(i=1;i<=n-3;i++){
		t1=t2=0;
		for(j=1;j<=n-3;j++){
			cpt1=cpt2=cpt3=cpt4=n;
			//p=p1=0;
			for(k=1;k<=n;k++){
				/*if(flag[i] == 0 && flag1[j] == 0){
					p=1;*/
					if(Bi[i][k]==Bi1[j][k])
						cpt1--;
					else
						cpt2--;
				/*}
				if(flag1[i] == 0 && flag[j] == 0){
					p1=1;*/
					if(Bi1[i][k]==Bi[j][k])
							cpt3--;
						else
							cpt4--;
				//}
			}
			/*if(p==1){*/
			c1 = ((double)cpt1<(double)cpt2)?(double)cpt1:(double)cpt2;
				if(j==1 || c1 < min1)
					min1 = c1;
			/*}
			else
				min1=0;
			if(p1==1){*/
				c2 = ((double)cpt3<(double)cpt4)?(double)cpt3:(double)cpt4;
				if(j==1 || c2 < min2)
					min2 = c2;
			/*}
			else
				min2=0;*/
		}
		cptB  += min1;
		cptB1 += min2;
	}

	for (i=0;i<2*n-2;i++)
	{
		free(Bi[i]); free(Bi1[i]);
	}

	free(Bi);
	free(Bi1);
	free(flag); free(flag1);

	return (cptB + cptB1) / 2.0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebx
movq %rsi, -0x60(%rbp)
movq %rdi, -0x40(%rbp)
leal (%rbx,%rbx), %ecx
leal -0x2(%rbx,%rbx), %eax
movl %eax, -0x4c(%rbp)
movq %rcx, -0x38(%rbp)
movslq %ecx, %rax
movq %rax, -0x58(%rbp)
leaq -0x10(,%rax,8), %r14
movq %r14, %rdi
callq 0x1140
movq %r14, %rdi
movq %rax, %r14
callq 0x1140
movq %rax, -0x48(%rbp)
movq %rbx, -0x30(%rbp)
cmpl $0x2, %ebx
jl 0x13a7
movl -0x38(%rbp), %r12d
shlq $0x2, %r12
movl -0x4c(%rbp), %eax
cmpl $0x2, %eax
movl $0x1, %r15d
cmovgel %eax, %r15d
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x1140
movq %rax, %r13
movq %rax, (%r14,%rbx,8)
movq %r12, %rdi
callq 0x1140
movq -0x48(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
testq %r13, %r13
je 0x1689
testq %rax, %rax
je 0x1689
incq %rbx
cmpq %rbx, %r15
jne 0x136e
movq -0x58(%rbp), %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x1140
movq %rax, %rbx
movq %r15, %rdi
callq 0x1140
movq %rax, %r15
movq -0x38(%rbp), %rax
addl $-0x3, %eax
movq %rax, -0x38(%rbp)
cmpl $0x2, -0x30(%rbp)
movq -0x48(%rbp), %r12
movq -0x60(%rbp), %r13
jl 0x1475
movq -0x30(%rbp), %rcx
leal -0x1(%rcx), %eax
incl %ecx
movq -0x38(%rbp), %rsi
cmpl $0x2, %esi
movl $0x1, %edx
cmovgel %esi, %edx
incl %edx
movl $0x1, %esi
xorl %r8d, %r8d
movl %r8d, (%r15,%rsi,4)
movl %r8d, (%rbx,%rsi,4)
movq -0x40(%rbp), %rdi
movq (%rdi,%rsi,8), %r9
movq (%r13,%rsi,8), %r10
movl (%r15,%rsi,4), %edi
movl $0x1, %r11d
addl (%r9,%r11,4), %r8d
addl (%r10,%r11,4), %edi
incq %r11
cmpq %r11, %rcx
jne 0x1423
movl %r8d, (%rbx,%rsi,4)
movl %edi, (%r15,%rsi,4)
cmpl $0x1, %r8d
sete %r9b
cmpl %eax, %r8d
sete %r8b
orb %r9b, %r8b
movzbl %r8b, %r8d
movl %r8d, (%rbx,%rsi,4)
cmpl $0x1, %edi
sete %r8b
cmpl %eax, %edi
sete %dil
orb %r8b, %dil
movzbl %dil, %edi
movl %edi, (%r15,%rsi,4)
incq %rsi
cmpq %rdx, %rsi
jne 0x1401
cmpl $0x2, -0x30(%rbp)
movq -0x38(%rbp), %r11
jl 0x1503
cmpl $0x2, %r11d
movl $0x1, %eax
cmovll %eax, %r11d
movq -0x30(%rbp), %rcx
incl %ecx
incl %r11d
movl $0x1, %edx
movl $0x1, %esi
cmpl $0x0, (%rbx,%rdx,4)
jne 0x14d0
movq -0x40(%rbp), %rdi
movq (%rdi,%rdx,8), %rdi
movslq %eax, %r8
movq (%r14,%r8,8), %r8
movl $0x1, %r9d
movl (%rdi,%r9,4), %r10d
movl %r10d, (%r8,%r9,4)
incq %r9
cmpq %r9, %rcx
jne 0x14be
incl %eax
cmpl $0x0, (%r15,%rdx,4)
jne 0x14fb
movq (%r13,%rdx,8), %rdi
movslq %esi, %r8
movq (%r12,%r8,8), %r8
movl $0x1, %r9d
movl (%rdi,%r9,4), %r10d
movl %r10d, (%r8,%r9,4)
incq %r9
cmpq %r9, %rcx
jne 0x14e9
incl %esi
incq %rdx
cmpq %r11, %rdx
jne 0x14a3
movq %r15, -0x58(%rbp)
movq %rbx, -0x60(%rbp)
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
cmpl $0x4, -0x30(%rbp)
jl 0x160b
movq -0x30(%rbp), %rcx
leal 0x1(%rcx), %eax
addl $-0x2, %ecx
movq %rcx, -0x40(%rbp)
movl $0x1, %ecx
movq %r14, -0x68(%rbp)
movq (%r14,%rcx,8), %rsi
movq %rcx, -0x38(%rbp)
movq (%r12,%rcx,8), %rdi
movl $0x1, %r8d
movapd %xmm1, %xmm2
movapd %xmm3, %xmm0
movq (%r12,%r8,8), %r11
movq (%r14,%r8,8), %r12
movl $0x1, %r13d
movq -0x30(%rbp), %rcx
movl %ecx, %edx
movl %ecx, %ebx
movl %ecx, %r9d
movl %ecx, %r10d
movl (%rsi,%r13,4), %r14d
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpl (%r11,%r13,4), %r14d
sete %cl
setne %r15b
subl %r15d, %ebx
subl %ecx, %edx
movl (%rdi,%r13,4), %ecx
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpl (%r12,%r13,4), %ecx
sete %r14b
setne %r15b
subl %r15d, %r10d
subl %r14d, %r9d
incq %r13
cmpq %r13, %rax
jne 0x156a
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %ebx, %xmm3
minsd %xmm3, %xmm1
cmpq $0x1, %r8
je 0x15c3
minsd %xmm2, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
xorps %xmm2, %xmm2
cvtsi2sd %r10d, %xmm2
minsd %xmm2, %xmm3
movq -0x48(%rbp), %r12
je 0x15e1
minsd %xmm0, %xmm3
incq %r8
cmpq -0x40(%rbp), %r8
movq -0x68(%rbp), %r14
jne 0x1546
addsd %xmm1, %xmm4
addsd %xmm3, %xmm5
movq -0x38(%rbp), %rcx
incq %rcx
cmpq -0x40(%rbp), %rcx
jne 0x1534
movsd %xmm5, -0x40(%rbp)
movsd %xmm4, -0x48(%rbp)
cmpl $0x2, -0x30(%rbp)
jl 0x1646
movl -0x4c(%rbp), %eax
cmpl $0x2, %eax
movl $0x1, %ebx
cmovgel %eax, %ebx
xorl %r15d, %r15d
movq (%r14,%r15,8), %rdi
callq 0x1100
movq (%r12,%r15,8), %rdi
callq 0x1100
incq %r15
cmpq %r15, %rbx
jne 0x162c
movq %r14, %rdi
callq 0x1100
movq %r12, %rdi
callq 0x1100
movq -0x60(%rbp), %rdi
callq 0x1100
movq -0x58(%rbp), %rdi
callq 0x1100
movsd -0x48(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
mulsd 0x1398e(%rip), %xmm0    # 0x15008
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13d52(%rip), %rdi     # 0x153e2
xorl %eax, %eax
callq 0x1030
movl $0x2, %edi
callq 0x1110