// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, %r13
movq %rdi, %r12
leal (%rdx,%rdx), %eax
movq %rdx, -0x38(%rbp)
leal -0x2(%rdx,%rdx), %ecx
movq %rcx, -0x48(%rbp)
movq %rax, -0x50(%rbp)
movslq %eax, %r15
leaq -0x10(,%r15,8), %r14
movq %r14, %rdi
callq 0x1130
movq %rax, %rbx
movq %r14, %rdi
callq 0x1130
movq %rax, %r14
shlq $0x2, %r15
movq %r15, -0x30(%rbp)
xorl %eax, %eax
movq -0x48(%rbp), %rcx
testl %ecx, %ecx
movl $0x0, %r15d
cmovlel %eax, %ecx
movq %rcx, -0x48(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1130
cmpq %r15, -0x48(%rbp)
movq %rax, -0x58(%rbp)
je 0x1395
movq %r15, -0x40(%rbp)
movq %rax, (%rbx,%r15,8)
movq -0x30(%rbp), %rdi
callq 0x1130
movq -0x40(%rbp), %rcx
movq %rax, (%r14,%rcx,8)
cmpq $0x0, -0x58(%rbp)
je 0x137f
incq %r15
testq %rax, %rax
jne 0x1344
leaq 0x1305c(%rip), %rdi     # 0x143e2
xorl %eax, %eax
callq 0x1030
pushq $0x2
popq %rdi
callq 0x1100
movq -0x30(%rbp), %rdi
callq 0x1130
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rcx
leal -0x1(%rcx), %edx
xorl %eax, %eax
testl %ecx, %ecx
cmovgl %ecx, %eax
movq -0x58(%rbp), %rcx
movq -0x50(%rbp), %r15
cmpl $0x4, %r15d
pushq $0x3
popq %rsi
cmovll %esi, %r15d
addl $-0x2, %r15d
pushq $0x1
popq %rsi
cmpq %r15, %rsi
je 0x143b
xorl %r8d, %r8d
xorl %edi, %edi
xorl %r9d, %r9d
cmpq %r8, %rax
je 0x1400
movq (%r12,%rsi,8), %r10
addl 0x4(%r10,%r8,4), %r9d
movl %r9d, (%rcx,%rsi,4)
movq (%r13,%rsi,8), %r10
addl 0x4(%r10,%r8,4), %edi
movq -0x30(%rbp), %r10
movl %edi, (%r10,%rsi,4)
incq %r8
jmp 0x13d7
cmpl $0x1, %r9d
sete %r8b
cmpl %edx, %r9d
sete %r9b
orb %r8b, %r9b
movzbl %r9b, %r8d
movl %r8d, (%rcx,%rsi,4)
cmpl $0x1, %edi
sete %r8b
cmpl %edx, %edi
sete %dil
orb %r8b, %dil
movzbl %dil, %edi
movq -0x30(%rbp), %r8
movl %edi, (%r8,%rsi,4)
incq %rsi
jmp 0x13ca
pushq $0x1
popq %rdx
pushq $0x1
popq %rsi
movl %edx, %edi
cmpq %r15, %rsi
je 0x14a7
cmpl $0x0, (%rcx,%rsi,4)
jne 0x1472
movslq %edi, %r8
xorl %r9d, %r9d
cmpq %r9, %rax
je 0x1470
movq (%r12,%rsi,8), %r10
movl 0x4(%r10,%r9,4), %r10d
movq (%rbx,%r8,8), %r11
movl %r10d, 0x4(%r11,%r9,4)
incq %r9
jmp 0x1454
incl %edi
movq -0x30(%rbp), %r8
cmpl $0x0, (%r8,%rsi,4)
jne 0x14a2
movslq %edx, %r8
xorl %r9d, %r9d
cmpq %r9, %rax
je 0x14a0
movq (%r13,%rsi,8), %r10
movl 0x4(%r10,%r9,4), %r10d
movq (%r14,%r8,8), %r11
movl %r10d, 0x4(%r11,%r9,4)
incq %r9
jmp 0x1483
incl %edx
incq %rsi
jmp 0x1443
movq -0x38(%rbp), %rcx
leal -0x2(%rcx), %edx
movq %rdx, -0x40(%rbp)
cmpl $0x4, %ecx
pushq $0x3
popq %r15
cmovgel %ecx, %r15d
addl $-0x2, %r15d
movq %r15, -0x50(%rbp)
xorpd %xmm4, %xmm4
pushq $0x1
popq %rdi
xorpd %xmm5, %xmm5
cmpq -0x50(%rbp), %rdi
je 0x159a
pushq $0x1
popq %r8
cmpq -0x40(%rbp), %r8
je 0x158a
xorl %r11d, %r11d
movq -0x38(%rbp), %rcx
movl %ecx, %r9d
movl %ecx, %r10d
movl %ecx, %r12d
movl %ecx, %r13d
cmpq %r11, %rax
je 0x154e
movq (%rbx,%rdi,8), %rsi
movl 0x4(%rsi,%r11,4), %esi
movq (%r14,%r8,8), %rcx
xorl %edx, %edx
xorl %r15d, %r15d
cmpl 0x4(%rcx,%r11,4), %esi
sete %dl
setne %r15b
subl %r15d, %r12d
subl %edx, %r13d
movq (%r14,%rdi,8), %rcx
movl 0x4(%rcx,%r11,4), %ecx
movq (%rbx,%r8,8), %rdx
xorl %esi, %esi
xorl %r15d, %r15d
cmpl 0x4(%rdx,%r11,4), %ecx
sete %sil
setne %r15b
subl %r15d, %r9d
subl %esi, %r10d
incq %r11
jmp 0x14fb
cvtsi2sd %r13d, %xmm2
cvtsi2sd %r12d, %xmm3
minsd %xmm3, %xmm2
cmpq $0x1, %r8
je 0x1566
minsd %xmm1, %xmm2
cvtsi2sd %r10d, %xmm3
cvtsi2sd %r9d, %xmm1
minsd %xmm1, %xmm3
je 0x157a
minsd %xmm0, %xmm3
incq %r8
movapd %xmm2, %xmm1
movapd %xmm3, %xmm0
jmp 0x14de
addsd %xmm1, %xmm4
addsd %xmm0, %xmm5
incq %rdi
jmp 0x14d0
movsd %xmm5, -0x38(%rbp)
movsd %xmm4, -0x40(%rbp)
xorl %r15d, %r15d
cmpq %r15, -0x48(%rbp)
je 0x15c4
movq (%rbx,%r15,8), %rdi
callq 0x10f0
movq (%r14,%r15,8), %rdi
callq 0x10f0
incq %r15
jmp 0x15a7
movq %rbx, %rdi
callq 0x10f0
movq %r14, %rdi
callq 0x10f0
movq -0x58(%rbp), %rdi
callq 0x10f0
movq -0x30(%rbp), %rdi
callq 0x10f0
movsd -0x40(%rbp), %xmm0
addsd -0x38(%rbp), %xmm0
mulsd 0x12a10(%rip), %xmm0    # 0x14008
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq