// File: /workspace/github_cpp_makefile_star2_O2/dcarrey[P]hgt-detection/./fonctions.cpp
// Function: TestSubTreeConstraint(InputTree, int, int, DescTree*, DescTree*)
int TestSubTreeConstraint(struct InputTree aTree,int source, int dest,struct DescTree *DTSpecies, struct DescTree *DTGene){

	int basSource,basDest;
	int etape1=0,etape2=0;
	int LGTpossible = 0;
	int pv,i,y,z,tk,tl;
	int n = aTree.size;
	int mk1,mk2,nbSom;
	struct DescTree DTemp;
	double ** DISTemp =(double **)malloc((2*n+1)*sizeof(double*));

	int * PLACEk1=(int *) malloc((2*n-2)*sizeof(int));
	int * PLACEk2=(int *) malloc((2*n-2)*sizeof(int));
	
	int ** Bk1=(int **) malloc((2*n-2)*sizeof(int*));
	int ** Bk2=(int **) malloc((2*n-2)*sizeof(int*));

	for (i=0;i<2*n-2;i++)
	{
		Bk1[i]=(int *) malloc((n)*sizeof(int));
		Bk2[i]=(int *) malloc((n)*sizeof(int));     
		DISTemp[i]=(double *)malloc((2*n+1)*sizeof(double));  
	}
	
	//== find the node inf for the source branch
	if(aTree.ADD[aTree.ARETE[2*source-1]][aTree.Root] <  aTree.ADD[aTree.ARETE[2*source-2]][aTree.Root])
		basSource = aTree.ARETE[2*source-2];
	else
		basSource = aTree.ARETE[2*source-1];

	//== find the node inf for the dest branch
	if(aTree.ADD[aTree.ARETE[2*dest-1]][aTree.Root] <  aTree.ADD[aTree.ARETE[2*dest-2]][aTree.Root])
		basDest = aTree.ARETE[2*dest-2];
	else
		basDest = aTree.ARETE[2*dest-1];


	if(basSource<n)
		etape1=1;
	else{
		for(pv=n+1;pv<2*n-2;pv++){
			if(vecteursEgaux(DTSpecies[basSource],DTGene[pv]) == 1){	
				/*if(DTSpecies[basSource].nbSommet == 3)
					etape1= 0;
				else*/{
					mk1=Bipartition_Table(DTGene[pv].Matrice,Bk1,PLACEk1,DTGene[pv].nbSommet+1);
					mk2=Bipartition_Table(DTSpecies[basSource].Matrice,Bk2,PLACEk2,DTSpecies[basSource].nbSommet+1);
					if(Table_Comparaison(Bk1,Bk2,PLACEk1,PLACEk2,mk1,mk2,DTSpecies[basSource].nbSommet+1) == 0)
						etape1 = 1;
				}
				pv = (int)INFINI;
			}
		}
	}

	if(etape1){/*/== comparer les sous arbres au niveau destination*/
		if(basDest<n)
			etape2 = 1;
		else{
			for(pv=n+1;pv<2*n-2;pv++){
				//printf("[pv = %d] ",pv);
				if(vecteursEgaux(DTSpecies[basDest],DTGene[pv]) == 1){
					/*if(DTSpecies[basDest].nbSommet == 3)
						etape2 = 0;
					else*/{
						mk1=Bipartition_Table(DTGene[pv].Matrice,Bk1,PLACEk1,DTGene[pv].nbSommet+1);
						mk2=Bipartition_Table(DTSpecies[basDest].Matrice,Bk2,PLACEk2,DTSpecies[basDest].nbSommet+1);
						if(Table_Comparaison(Bk1,Bk2,PLACEk1,PLACEk2,mk1,mk2,DTGene[pv].nbSommet+1) == 0)
							etape2 = 1;
					}
					pv = (int)INFINI;
				}				
			}	
		}
	}

	if(etape1==1 && etape2==1){
		
		nbSom = DTemp.nbSommet = DTSpecies[basDest].nbSommet + DTSpecies[basSource].nbSommet;

		DTemp.Tableau = (int*)malloc((DTemp.nbSommet+1)*sizeof(int));

		for(y=1;y<=DTSpecies[basDest].nbSommet;y++)
			DTemp.Tableau[y] = DTSpecies[basDest].Tableau[y];
		y--;
		for(z=1;z<=DTSpecies[basSource].nbSommet;z++)
			DTemp.Tableau[z+y] = DTSpecies[basSource].Tableau[z];

		TrierTableau(DTemp.Tableau,DTemp.nbSommet);

		for(pv=n+1;pv<=2*n-2;pv++){
			if(vecteursEgaux(DTemp,DTGene[pv])){

				for(tk=1;tk<=DTGene[pv].nbSommet;tk++){
					for(tl=1;tl<=DTGene[pv].nbSommet;tl++)
						DISTemp[tk][tl] = aTree.ADD[DTemp.Tableau[tk]][DTemp.Tableau[tl]];
				}

				//for(tk=1;tk<=DTGene[pv].nbSommet;tk++)
				//	DISTemp[DTGene[pv].nbSommet+1][tk] =  DISTemp[tk][DTGene[pv].nbSommet+1] = ;

				mk1=Bipartition_Table(DTGene[pv].Matrice,Bk1,PLACEk1,DTGene[pv].nbSommet);
				mk2=Bipartition_Table(DISTemp,Bk2,PLACEk2,DTemp.nbSommet);
				if(Table_Comparaison(Bk1,Bk2,PLACEk1,PLACEk2,mk1,mk2,DTGene[pv].nbSommet) == 0)
					LGTpossible = 1;
			}
		}
		free(DTemp.Tableau);	
	}

	free(PLACEk1);
	free(PLACEk2);

	for (i=0;i<2*n-2;i++)
	{
		free(Bk1[i]);
		free(Bk2[i]);
		free(DISTemp[i]);
	}
	free(DISTemp);	
	free(Bk1);
	free(Bk2);

	return LGTpossible;
		
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, -0x58(%rbp)
movq %rdx, -0x78(%rbp)
movl %esi, -0x38(%rbp)
movl %edi, -0x30(%rbp)
leaq 0x10(%rbp), %rax
movl (%rax), %r15d
movslq %r15d, %r13
movq %r13, -0x88(%rbp)
leaq (,%r13,2), %r14
shlq $0x4, %r13
orq $0x8, %r13
movq %r13, %rdi
callq 0x1130
movq %rax, %rbx
movslq %r14d, %r12
leaq -0x2(%r12), %rax
movq %rax, -0x50(%rbp)
leal -0x2(%r12), %eax
movq %rax, -0x90(%rbp)
leaq -0x8(,%r12,4), %r14
movq %r14, %rdi
callq 0x1130
movq %rax, -0x60(%rbp)
movq %r14, %rdi
callq 0x1130
movq %rax, -0x70(%rbp)
leaq -0x10(,%r12,8), %r14
movq %r14, %rdi
callq 0x1130
movq %rax, -0x40(%rbp)
movq %r14, %rdi
callq 0x1130
movq %rax, -0x68(%rbp)
movq -0x88(%rbp), %rax
leaq (,%rax,4), %r12
xorl %r14d, %r14d
movq -0x90(%rbp), %rax
testl %eax, %eax
cmovlel %r14d, %eax
movq %rax, -0x90(%rbp)
cmpq %r14, -0x90(%rbp)
je 0x7463
movq %r12, %rdi
callq 0x1130
movq -0x40(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
movq %r12, %rdi
callq 0x1130
movq -0x68(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
movq %r13, %rdi
callq 0x1130
movq %rax, (%rbx,%r14,8)
incq %r14
jmp 0x7429
leaq 0x10(%rbp), %rsi
movq 0x8(%rsi), %r14
movq 0x30(%rsi), %rax
movl -0x30(%rbp), %ecx
addl %ecx, %ecx
movslq %ecx, %rcx
movq -0x10(%rax,%rcx,8), %rdx
movq -0x8(%rax,%rcx,8), %r8
movq (%r14,%r8,8), %rcx
movslq 0x20(%rsi), %rsi
movq (%r14,%rdx,8), %rdi
movsd (%rdi,%rsi,8), %xmm0
ucomisd (%rcx,%rsi,8), %xmm0
cmovaq %rdx, %r8
movl -0x38(%rbp), %ecx
addl %ecx, %ecx
movslq %ecx, %rcx
movq -0x10(%rax,%rcx,8), %rdx
movq -0x8(%rax,%rcx,8), %rdi
movq (%r14,%rdi,8), %rax
movq (%r14,%rdx,8), %rcx
movsd (%rcx,%rsi,8), %xmm0
ucomisd (%rax,%rsi,8), %xmm0
cmovaq %rdx, %rdi
cmpl %r8d, %r15d
movq %r8, -0xa0(%rbp)
movq %rdi, -0x98(%rbp)
jle 0x74e6
pushq $0x1
popq %rcx
movq -0x58(%rbp), %r10
movq -0x50(%rbp), %r11
jmp 0x75df
movslq %r8d, %rax
imulq $0x18, %rax, %r13
addq -0x78(%rbp), %r13
movq $0x0, -0x38(%rbp)
movl %r15d, %eax
movq -0x58(%rbp), %r10
movq -0x50(%rbp), %r11
movq -0x68(%rbp), %r12
incl %eax
cmpl %r11d, %eax
jge 0x75cc
movl (%r13), %ecx
movslq %eax, %rdx
imulq $0x18, %rdx, %rdi
cmpl (%r10,%rdi), %ecx
jne 0x7508
movq 0x10(%r13), %rsi
leaq (%r10,%rdi), %rdx
movq 0x10(%r10,%rdi), %rdi
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x755a
movl 0x4(%rsi,%r9,4), %r10d
leaq 0x1(%r9), %r11
cmpl 0x4(%rdi,%r9,4), %r10d
movq %r11, %r9
je 0x7540
jmp 0x75bf
movq 0x8(%rdx), %rdi
incl %ecx
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x288f
movl %eax, -0x30(%rbp)
movq 0x8(%r13), %rdi
movl (%r13), %ecx
incl %ecx
movq %r12, %rsi
movq -0x70(%rbp), %rdx
callq 0x288f
movl (%r13), %ecx
incl %ecx
movl %ecx, (%rsp)
movq -0x40(%rbp), %rdi
movq %r12, %rsi
movq -0x60(%rbp), %rdx
movq -0x70(%rbp), %rcx
movl -0x30(%rbp), %r8d
movl %eax, %r9d
callq 0x2dd8
testl %eax, %eax
movq -0x38(%rbp), %rax
pushq $0x1
popq %rcx
cmovel %ecx, %eax
movq %rax, -0x38(%rbp)
movl $0xf423f, %eax          # imm = 0xF423F
movq -0x58(%rbp), %r10
movq -0x50(%rbp), %r11
jmp 0x7508
movq -0x38(%rbp), %rcx
testl %ecx, %ecx
movq -0x98(%rbp), %rdi
je 0x78e1
movb $0x1, %al
cmpl %edi, %r15d
jg 0x76e3
movq %rcx, -0x38(%rbp)
movslq %edi, %rax
imulq $0x18, %rax, %r13
addq -0x78(%rbp), %r13
movq $0x0, -0x30(%rbp)
incl %r15d
cmpl %r11d, %r15d
jge 0x76d1
movl (%r13), %ecx
movslq %r15d, %rax
imulq $0x18, %rax, %rdx
cmpl (%r10,%rdx), %ecx
jne 0x7601
movq 0x10(%r13), %rax
leaq (%r10,%rdx), %r12
movq 0x10(%r10,%rdx), %rdx
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x7651
movl 0x4(%rax,%rdi,4), %r8d
leaq 0x1(%rdi), %r9
cmpl 0x4(%rdx,%rdi,4), %r8d
movq %r9, %rdi
je 0x7637
jmp 0x7601
movq %r12, -0x80(%rbp)
movq 0x8(%r12), %rdi
incl %ecx
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x288f
movl %eax, -0x48(%rbp)
movq 0x8(%r13), %rdi
movl (%r13), %ecx
incl %ecx
movq -0x68(%rbp), %r12
movq %r12, %rsi
movq -0x70(%rbp), %r15
movq %r15, %rdx
callq 0x288f
movq -0x80(%rbp), %rcx
movl (%rcx), %ecx
incl %ecx
movl %ecx, (%rsp)
movq -0x40(%rbp), %rdi
movq %r12, %rsi
movq -0x60(%rbp), %rdx
movq %r15, %rcx
movl -0x48(%rbp), %r8d
movl %eax, %r9d
callq 0x2dd8
movq -0x50(%rbp), %r11
movq -0x58(%rbp), %r10
testl %eax, %eax
pushq $0x1
popq %rax
movq -0x30(%rbp), %rcx
cmovel %eax, %ecx
movq %rcx, -0x30(%rbp)
movl $0xf423f, %r15d         # imm = 0xF423F
jmp 0x7601
cmpl $0x1, -0x30(%rbp)
sete %al
movq -0x98(%rbp), %rdi
movq -0x38(%rbp), %rcx
xorl %r15d, %r15d
cmpl $0x1, %ecx
jne 0x78e4
testb %al, %al
je 0x78e4
movslq %edi, %rax
imulq $0x18, %rax, %rax
movq -0x78(%rbp), %rcx
leaq (%rcx,%rax), %r15
movl (%rcx,%rax), %r12d
movslq -0xa0(%rbp), %rax
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %rdx
movq %rdx, -0x48(%rbp)
movl (%rcx,%rax), %ecx
leal (%rcx,%r12), %eax
movl %eax, -0x30(%rbp)
movq %rcx, -0x80(%rbp)
leal (%rcx,%r12), %eax
incl %eax
movl %eax, -0x38(%rbp)
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x1130
movq %rax, %r13
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
cmpq %rax, %r12
je 0x7763
movq 0x10(%r15), %rcx
movl 0x4(%rcx,%rax,4), %ecx
movl %ecx, 0x4(%r13,%rax,4)
incq %rax
jmp 0x774c
xorl %eax, %eax
movq -0x80(%rbp), %r8
testl %r8d, %r8d
cmovlel %eax, %r8d
leaq 0x4(,%r12,4), %rcx
addq %r13, %rcx
movl -0x30(%rbp), %esi
movq -0x48(%rbp), %rdi
cmpq %rax, %r8
je 0x7797
movq 0x10(%rdi), %rdx
movl 0x4(%rdx,%rax,4), %edx
movl %edx, (%rcx,%rax,4)
incq %rax
jmp 0x7782
movq %r13, %rdi
callq 0x1b8f
movl -0x30(%rbp), %ecx
movq $0x0, -0x48(%rbp)
testl %ecx, %ecx
movl $0x0, %r10d
cmovgl %ecx, %r10d
leal 0x1(%r10), %r12d
movl -0x38(%rbp), %r15d
pushq $0x1
popq %r11
movq -0x58(%rbp), %rdi
movq -0x88(%rbp), %rdx
movq -0x50(%rbp), %r8
movq %r10, -0x78(%rbp)
cmpq %r8, %rdx
jge 0x78d3
incq %rdx
imulq $0x18, %rdx, %rax
cmpl (%rdi,%rax), %ecx
jne 0x77d5
movq %rdx, -0x88(%rbp)
leaq (%rdi,%rax), %rdx
movq %rdx, -0x38(%rbp)
movq 0x10(%rdi,%rax), %rax
xorl %r9d, %r9d
cmpq %r9, %r10
je 0x781e
movl 0x4(%r13,%r9,4), %edx
leaq 0x1(%r9), %rsi
cmpl 0x4(%rax,%r9,4), %edx
movq %rsi, %r9
je 0x7801
jmp 0x78c7
movq %r11, %rax
cmpq %r12, %rax
je 0x7854
movq %r11, %rdi
cmpq %rdi, %r15
je 0x784f
movslq (%r13,%rax,4), %rdx
movq (%r14,%rdx,8), %rdx
movslq (%r13,%rdi,4), %rsi
movsd (%rdx,%rsi,8), %xmm0
movq (%rbx,%rax,8), %rdx
movsd %xmm0, (%rdx,%rdi,8)
incq %rdi
jmp 0x7829
incq %rax
jmp 0x7821
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x288f
movl %eax, -0x80(%rbp)
movq %rbx, %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdx
movl -0x30(%rbp), %ecx
callq 0x288f
movq -0x38(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rsp)
movq -0x40(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x70(%rbp), %rcx
movl -0x80(%rbp), %r8d
movl %eax, %r9d
callq 0x2dd8
pushq $0x1
popq %r11
movq -0x78(%rbp), %r10
movl -0x30(%rbp), %ecx
movq -0x50(%rbp), %r8
movq -0x58(%rbp), %rdi
testl %eax, %eax
movq -0x48(%rbp), %rax
pushq $0x1
popq %rdx
cmovel %edx, %eax
movq %rax, -0x48(%rbp)
movq -0x88(%rbp), %rdx
jmp 0x77d5
movq %r13, %rdi
callq 0x10f0
movq -0x48(%rbp), %r15
jmp 0x78e4
xorl %r15d, %r15d
movq -0x60(%rbp), %rdi
callq 0x10f0
movq -0x70(%rbp), %rdi
callq 0x10f0
xorl %r14d, %r14d
cmpq %r14, -0x90(%rbp)
je 0x792a
movq -0x40(%rbp), %rax
movq (%rax,%r14,8), %rdi
callq 0x10f0
movq -0x68(%rbp), %rax
movq (%rax,%r14,8), %rdi
callq 0x10f0
movq (%rbx,%r14,8), %rdi
callq 0x10f0
incq %r14
jmp 0x78f9
movq %rbx, %rdi
callq 0x10f0
movq -0x40(%rbp), %rdi
callq 0x10f0
movq -0x68(%rbp), %rdi
callq 0x10f0
movl %r15d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq