// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movl %edx, %ebx
movq %rsi, -0x68(%rbp)
movq %rdi, -0x60(%rbp)
leal (%rbx,%rbx), %ecx
leal -0x2(%rbx,%rbx), %eax
movl %eax, -0x4c(%rbp)
movq %rcx, -0x38(%rbp)
movslq %ecx, %rax
movq %rax, -0x48(%rbp)
leaq -0x10(,%rax,8), %r14
movq %r14, %rdi
callq 0x1140
movq %r14, %rdi
movq %rax, %r14
callq 0x1140
movq %rax, %r15
movq %rbx, -0x40(%rbp)
cmpl $0x2, %ebx
jl 0x139a
movl -0x38(%rbp), %r12d
shlq $0x2, %r12
movl -0x4c(%rbp), %eax
movq %rax, -0x30(%rbp)
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x1140
movq %rax, %r13
movq %rax, (%r14,%rbx,8)
movq %r12, %rdi
callq 0x1140
movq %rax, (%r15,%rbx,8)
testq %r13, %r13
je 0x1676
testq %rax, %rax
je 0x1676
incq %rbx
cmpq %rbx, -0x30(%rbp)
jne 0x1364
movq -0x48(%rbp), %rbx
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x1140
movq %rax, -0x48(%rbp)
movq %rbx, %rdi
callq 0x1140
movq %rax, -0x58(%rbp)
xorpd %xmm4, %xmm4
movq -0x40(%rbp), %rbx
cmpl $0x2, %ebx
jge 0x13d0
xorpd %xmm5, %xmm5
jmp 0x162f
movq -0x38(%rbp), %rsi
addl $-0x3, %esi
leal -0x1(%rbx), %ecx
leal 0x1(%rbx), %eax
cmpl $0x2, %esi
movl $0x1, %edx
movq %rsi, -0x38(%rbp)
cmovgel %esi, %edx
incl %edx
movl $0x1, %esi
movq -0x68(%rbp), %r12
movq -0x60(%rbp), %r13
movq (%r13,%rsi,8), %r8
movq (%r12,%rsi,8), %r9
xorl %edi, %edi
movl $0x1, %r11d
xorl %r10d, %r10d
addl (%r8,%r11,4), %r10d
addl (%r9,%r11,4), %edi
incq %r11
cmpq %r11, %rax
jne 0x140f
cmpl $0x1, %r10d
sete %r8b
cmpl %ecx, %r10d
sete %r9b
orb %r8b, %r9b
movzbl %r9b, %r8d
movq -0x48(%rbp), %r9
movl %r8d, (%r9,%rsi,4)
cmpl $0x1, %edi
sete %r8b
cmpl %ecx, %edi
sete %dil
orb %r8b, %dil
movzbl %dil, %edi
movq -0x58(%rbp), %r8
movl %edi, (%r8,%rsi,4)
incq %rsi
cmpq %rdx, %rsi
jne 0x13fb
xorpd %xmm5, %xmm5
cmpl $0x2, %ebx
jl 0x162f
movq -0x38(%rbp), %r11
cmpl $0x2, %r11d
movl $0x1, %ecx
cmovll %ecx, %r11d
incl %r11d
movl $0x1, %edx
movl $0x1, %esi
movq -0x48(%rbp), %rdi
cmpl $0x0, (%rdi,%rdx,4)
jne 0x14ba
movq (%r13,%rdx,8), %rdi
movslq %ecx, %r8
movq (%r14,%r8,8), %r8
movl $0x1, %r9d
movl (%rdi,%r9,4), %r10d
movl %r10d, (%r8,%r9,4)
incq %r9
cmpq %r9, %rax
jne 0x14a8
incl %ecx
movq -0x58(%rbp), %rdi
cmpl $0x0, (%rdi,%rdx,4)
jne 0x14e7
movq (%r12,%rdx,8), %rdi
movslq %esi, %r8
movq (%r15,%r8,8), %r8
movl $0x1, %r9d
movl (%rdi,%r9,4), %r10d
movl %r10d, (%r8,%r9,4)
incq %r9
cmpq %r9, %rax
jne 0x14d5
incl %esi
incq %rdx
cmpq %r11, %rdx
jne 0x148c
xorpd %xmm5, %xmm5
xorpd %xmm4, %xmm4
cmpl $0x4, %ebx
jl 0x15ef
movq -0x40(%rbp), %rcx
addl $-0x2, %ecx
movq %rcx, -0x30(%rbp)
movl $0x1, %ecx
movq %r14, -0x78(%rbp)
movq %r15, -0x70(%rbp)
movq (%r14,%rcx,8), %rsi
movq %rcx, -0x38(%rbp)
movq (%r15,%rcx,8), %rdi
movl $0x1, %r8d
movapd %xmm1, %xmm2
movapd %xmm3, %xmm0
movq (%r15,%r8,8), %r11
movq (%r14,%r8,8), %r12
movl $0x1, %r13d
movq -0x40(%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 0x154e
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %ebx, %xmm3
minsd %xmm3, %xmm1
cmpq $0x1, %r8
je 0x15a7
minsd %xmm2, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
xorps %xmm2, %xmm2
cvtsi2sd %r10d, %xmm2
minsd %xmm2, %xmm3
je 0x15c1
minsd %xmm0, %xmm3
incq %r8
cmpq -0x30(%rbp), %r8
movq -0x78(%rbp), %r14
movq -0x70(%rbp), %r15
jne 0x152a
addsd %xmm1, %xmm5
addsd %xmm3, %xmm4
movq -0x38(%rbp), %rcx
incq %rcx
cmpq -0x30(%rbp), %rcx
jne 0x1518
cmpl $0x2, -0x40(%rbp)
jl 0x162f
movsd %xmm5, -0x40(%rbp)
movsd %xmm4, -0x30(%rbp)
movl -0x4c(%rbp), %ebx
movq %r15, %r12
xorl %r15d, %r15d
movq (%r14,%r15,8), %rdi
callq 0x1100
movq (%r12,%r15,8), %rdi
callq 0x1100
incq %r15
cmpq %r15, %rbx
jne 0x1608
movq %r12, %r15
movsd -0x30(%rbp), %xmm4
movsd -0x40(%rbp), %xmm5
addsd %xmm5, %xmm4
movsd %xmm4, -0x30(%rbp)
movq %r14, %rdi
callq 0x1100
movq %r15, %rdi
callq 0x1100
movq -0x48(%rbp), %rdi
callq 0x1100
movq -0x58(%rbp), %rdi
callq 0x1100
movsd -0x30(%rbp), %xmm0
mulsd 0x139a1(%rip), %xmm0    # 0x15008
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13d65(%rip), %rdi     # 0x153e2
xorl %eax, %eax
callq 0x1030
movl $0x2, %edi
callq 0x1110