// File: 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
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x14(%rbp), %eax
shll %eax
subl $0x2, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x1110
movq %rax, -0x88(%rbp)
movl -0x14(%rbp), %eax
shll %eax
subl $0x2, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x1110
movq %rax, -0x90(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl -0x14(%rbp), %ecx
shll %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jge 0x13a8
movl -0x14(%rbp), %eax
shll %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x1110
movq %rax, %rdx
movq -0x88(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x14(%rbp), %eax
shll %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x1110
movq %rax, %rdx
movq -0x90(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x90(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x1380
movq -0x88(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x1398
leaq 0x2403b(%rip), %rdi     # 0x253c2
movb $0x0, %al
callq 0x1030
movl $0x2, %edi
callq 0x10f0
jmp 0x139a
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1303
movl -0x14(%rbp), %eax
shll %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x1110
movq %rax, -0x78(%rbp)
movl -0x14(%rbp), %eax
shll %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x1110
movq %rax, -0x80(%rbp)
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl -0x14(%rbp), %ecx
shll %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x150c
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x78(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl $0x1, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x1476
movq -0x78(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x1c(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movq -0x78(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x1c(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1411
movq -0x78(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x1, (%rax,%rcx,4)
je 0x1499
movq -0x78(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x14aa
movq -0x78(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0x14b9
movq -0x78(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x1, (%rax,%rcx,4)
je 0x14dc
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x14ed
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0x14fc
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x14fe
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x13d9
movl $0x1, -0x24(%rbp)
movl $0x1, -0x20(%rbp)
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl -0x14(%rbp), %ecx
shll %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x15f8
movq -0x78(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x158e
movl $0x1, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x1585
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x88(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1549
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movq -0x80(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x15e8
movl $0x1, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x15df
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x90(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x15a3
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x15ea
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1521
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x17da
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x1, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x17ae
movl -0x14(%rbp), %eax
movl %eax, -0x34(%rbp)
movl %eax, -0x30(%rbp)
movl %eax, -0x2c(%rbp)
movl %eax, -0x28(%rbp)
movl $0x1, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x16ec
movq -0x88(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x90(%rbp), %rcx
movslq -0x1c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x20(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x1691
movl -0x28(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x169a
movl -0x2c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x90(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x88(%rbp), %rcx
movslq -0x1c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x20(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x16d3
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x16dc
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x16de
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x164c
cvtsi2sdl -0x28(%rbp), %xmm1
cvtsi2sdl -0x2c(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x170b
cvtsi2sdl -0x28(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
jmp 0x1718
cvtsi2sdl -0x2c(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
cmpl $0x1, -0x1c(%rbp)
je 0x173b
movsd -0x48(%rbp), %xmm1
movsd -0x58(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1745
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
cvtsi2sdl -0x30(%rbp), %xmm1
cvtsi2sdl -0x34(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1764
cvtsi2sdl -0x30(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
jmp 0x1771
cvtsi2sdl -0x34(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
cmpl $0x1, -0x1c(%rbp)
je 0x1794
movsd -0x50(%rbp), %xmm1
movsd -0x60(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x179e
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
jmp 0x17a0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1625
movsd -0x58(%rbp), %xmm0
addsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x60(%rbp), %xmm0
addsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x15ff
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl -0x14(%rbp), %ecx
shll %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jge 0x1823
movq -0x88(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10e0
movq -0x90(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10e0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x17e1
movq -0x88(%rbp), %rdi
callq 0x10e0
movq -0x90(%rbp), %rdi
callq 0x10e0
movq -0x78(%rbp), %rdi
callq 0x10e0
movq -0x80(%rbp), %rdi
callq 0x10e0
movsd -0x68(%rbp), %xmm0
addsd -0x70(%rbp), %xmm0
movsd 0x237a9(%rip), %xmm1    # 0x25008
divsd %xmm1, %xmm0
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)