// File: /workspace/github_cpp_makefile_star2_O1/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, -0x48(%rbp)
movq %rdx, -0x50(%rbp)
movl %esi, -0x30(%rbp)
movl %edi, -0x40(%rbp)
leaq 0x10(%rbp), %rax
movslq (%rax), %r13
leaq (,%r13,2), %rbx
movq %r13, %r12
shlq $0x4, %r12
orq $0x8, %r12
movq %r12, %rdi
callq 0x1140
movq %rax, -0x78(%rbp)
movslq %ebx, %r15
addl $-0x2, %ebx
movq %rbx, -0x60(%rbp)
leaq -0x8(,%r15,4), %r14
movq %r14, %rdi
callq 0x1140
movq %rax, -0x70(%rbp)
movq %r14, %rdi
callq 0x1140
movq %rax, -0x80(%rbp)
leaq -0x10(,%r15,8), %r15
movq %r15, %rdi
callq 0x1140
movq %rax, -0x68(%rbp)
movq %r15, %rdi
callq 0x1140
movq %rax, %rbx
movq %r13, -0x38(%rbp)
cmpq $0x2, %r13
jl 0x8105
movq -0x38(%rbp), %rax
leaq (,%rax,4), %r13
movq -0x60(%rbp), %rax
cmpl $0x2, %eax
movl $0x1, %r14d
cmovgel %eax, %r14d
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x1140
movq -0x68(%rbp), %rcx
movq %rax, (%rcx,%r15,8)
movq %r13, %rdi
callq 0x1140
movq %rax, (%rbx,%r15,8)
movq %r12, %rdi
callq 0x1140
movq -0x78(%rbp), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
cmpq %r15, %r14
jne 0x80d1
leaq 0x10(%rbp), %rsi
movq 0x8(%rsi), %r8
movq 0x30(%rsi), %rax
movl -0x40(%rbp), %ecx
addl %ecx, %ecx
movslq %ecx, %rcx
movq -0x10(%rax,%rcx,8), %rdx
movq -0x8(%rax,%rcx,8), %r9
movq (%r8,%r9,8), %rcx
movslq 0x20(%rsi), %rsi
movq (%r8,%rdx,8), %rdi
movsd (%rdi,%rsi,8), %xmm0
ucomisd (%rcx,%rsi,8), %xmm0
cmovaq %rdx, %r9
movl -0x30(%rbp), %ecx
addl %ecx, %ecx
movslq %ecx, %rcx
movq -0x10(%rax,%rcx,8), %rdx
movq -0x8(%rax,%rcx,8), %rdi
movq (%r8,%rdi,8), %rax
movq %r8, -0xa0(%rbp)
movq (%r8,%rdx,8), %rcx
movsd (%rcx,%rsi,8), %xmm0
ucomisd (%rax,%rsi,8), %xmm0
cmovaq %rdx, %rdi
movq %rdi, -0x88(%rbp)
movl $0x1, %r15d
movq %r9, -0x98(%rbp)
cmpl %r9d, -0x38(%rbp)
movq -0x68(%rbp), %r13
movq -0x60(%rbp), %r12
movq -0x48(%rbp), %r11
movq %rbx, -0x30(%rbp)
jg 0x8287
movq -0x38(%rbp), %rax
incl %eax
xorl %r15d, %r15d
cmpl %r12d, %eax
jge 0x8287
movslq -0x98(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq -0x50(%rbp), %rdx
leaq (%rdx,%rcx,8), %r14
xorl %r15d, %r15d
movl (%r14), %edx
movslq %eax, %rcx
leaq (%rcx,%rcx,2), %rsi
movl (%r11,%rsi,8), %ecx
cmpl %ecx, %edx
jne 0x827c
testl %edx, %edx
jle 0x8204
movq 0x10(%r14), %rdi
movq 0x10(%r11,%rsi,8), %r8
incq %rdx
movl $0x1, %r9d
movl (%rdi,%r9,4), %r10d
cmpl (%r8,%r9,4), %r10d
jne 0x827c
incq %r9
cmpq %r9, %rdx
jne 0x81ee
leaq (%r11,%rsi,8), %rax
movq 0x8(%rax), %rdi
incl %ecx
movq %r13, %rsi
movl %r15d, -0x58(%rbp)
movq -0x70(%rbp), %r15
movq %r15, %rdx
callq 0x2bcf
movl %eax, %r12d
movq 0x8(%r14), %rdi
movl (%r14), %ecx
incl %ecx
movq %rbx, %rsi
movq -0x80(%rbp), %rbx
movq %rbx, %rdx
callq 0x2bcf
movl (%r14), %ecx
incl %ecx
movl %ecx, (%rsp)
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movq %r15, %rdx
movl -0x58(%rbp), %r15d
movq %rbx, %rcx
movq -0x30(%rbp), %rbx
movl %r12d, %r8d
movq -0x60(%rbp), %r12
movl %eax, %r9d
callq 0x3238
movq -0x48(%rbp), %r11
testl %eax, %eax
movl $0x1, %eax
cmovel %eax, %r15d
movl $0xf423f, %eax          # imm = 0xF423F
incl %eax
cmpl %r12d, %eax
jl 0x81c2
movl $0x0, -0x40(%rbp)
testl %r15d, %r15d
je 0x83b6
movb $0x1, %r14b
movq -0x88(%rbp), %rax
cmpl %eax, -0x38(%rbp)
movq -0x50(%rbp), %rdx
jg 0x83bd
movq -0x38(%rbp), %rax
incl %eax
xorl %r14d, %r14d
cmpl %r12d, %eax
jge 0x83bd
movl %r15d, -0x58(%rbp)
movslq -0x88(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %r15
movl (%r15), %edx
movslq %eax, %rcx
leaq (%rcx,%rcx,2), %rsi
movl (%r11,%rsi,8), %ecx
cmpl %ecx, %edx
jne 0x8399
testl %edx, %edx
jle 0x8315
movq 0x10(%r15), %rdi
movq 0x10(%r11,%rsi,8), %r8
incq %rdx
movl $0x1, %r9d
movl (%rdi,%r9,4), %r10d
cmpl (%r8,%r9,4), %r10d
jne 0x8399
incq %r9
cmpq %r9, %rdx
jne 0x82ff
leaq (%r11,%rsi,8), %r13
movq 0x8(%r13), %rdi
incl %ecx
movq -0x68(%rbp), %rsi
movl %r14d, -0x90(%rbp)
movq -0x70(%rbp), %r14
movq %r14, %rdx
callq 0x2bcf
movl %eax, %r12d
movq 0x8(%r15), %rdi
movl (%r15), %ecx
incl %ecx
movq %rbx, %rsi
movq -0x80(%rbp), %rbx
movq %rbx, %rdx
callq 0x2bcf
movl (%r13), %ecx
movq -0x68(%rbp), %r13
incl %ecx
movl %ecx, (%rsp)
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movq %r14, %rdx
movl -0x90(%rbp), %r14d
movq %rbx, %rcx
movq -0x30(%rbp), %rbx
movl %r12d, %r8d
movq -0x60(%rbp), %r12
movl %eax, %r9d
callq 0x3238
movq -0x48(%rbp), %r11
testl %eax, %eax
movl $0x1, %eax
cmovel %eax, %r14d
movl $0xf423f, %eax          # imm = 0xF423F
incl %eax
cmpl %r12d, %eax
jl 0x82d3
cmpl $0x1, %r14d
sete %r14b
movq -0x50(%rbp), %rdx
movl -0x58(%rbp), %r15d
jmp 0x83bd
xorl %r14d, %r14d
movq -0x50(%rbp), %rdx
cmpl $0x1, %r15d
jne 0x8612
testb %r14b, %r14b
je 0x8612
movslq -0x88(%rbp), %rax
leaq (%rax,%rax,2), %r15
movslq (%rdx,%r15,8), %r14
movslq -0x98(%rbp), %rax
leaq (%rax,%rax,2), %rax
movq %rax, -0x58(%rbp)
movl (%rdx,%rax,8), %eax
movq %rax, -0x40(%rbp)
addl %r14d, %eax
movl %eax, -0x30(%rbp)
cltq
leaq 0x4(,%rax,4), %rdi
movq %rdx, %r13
callq 0x1140
movq %rax, %r12
testq %r14, %r14
jle 0x8447
leaq (,%r15,8), %rax
addq %r13, %rax
movq 0x10(%rax), %rsi
leaq 0x4(%r12), %rdi
addq $0x4, %rsi
leaq (,%r14,4), %rdx
callq 0x1080
movl %r14d, %eax
leaq 0x4(,%rax,4), %rdi
jmp 0x844c
movl $0x4, %edi
movq -0x48(%rbp), %r8
movq -0x40(%rbp), %r15
testl %r15d, %r15d
jle 0x8480
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rcx
leaq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rsi
addq %r12, %rdi
addq $0x4, %rsi
movq %r15, %rdx
shlq $0x2, %rdx
callq 0x1080
movq -0x48(%rbp), %r8
movl -0x30(%rbp), %r10d
cmpl $0x2, %r10d
jl 0x84cb
movl %r10d, %eax
xorl %ecx, %ecx
movl $0x1, %edx
movl (%r12,%rdx,4), %esi
movl 0x4(%r12,%rdx,4), %edi
cmpl %edi, %esi
jle 0x84af
movl %edi, (%r12,%rdx,4)
movl %esi, 0x4(%r12,%rdx,4)
movl $0x1, %ecx
incq %rdx
cmpq %rax, %rdx
jne 0x8494
cmpl $0x2, %r10d
jl 0x84cb
movl $0x1, %edx
testl %ecx, %ecx
movl $0x0, %ecx
jne 0x8494
xorl %r9d, %r9d
movq -0x38(%rbp), %rax
cmpl -0x60(%rbp), %eax
jge 0x85fe
leal (%r15,%r14), %eax
incl %eax
movl %eax, %r13d
movslq -0x60(%rbp), %r11
xorl %r9d, %r9d
movq -0x38(%rbp), %r15
movq %r11, -0x50(%rbp)
incq %r15
leaq (%r15,%r15,2), %rax
movl (%r8,%rax,8), %ecx
cmpl %ecx, %r10d
jne 0x85f5
testl %r10d, %r10d
movq -0xa0(%rbp), %r14
jle 0x8532
movq 0x10(%r8,%rax,8), %rdx
movl $0x1, %esi
movl (%r12,%rsi,4), %edi
cmpl (%rdx,%rsi,4), %edi
jne 0x85f5
incq %rsi
cmpq %rsi, %r13
jne 0x851d
movl %r9d, -0x40(%rbp)
testl %ecx, %ecx
jle 0x857b
leaq 0x1(%rcx), %rdx
movl %edx, %esi
movl $0x1, %edi
movslq (%r12,%rdi,4), %r8
movq (%r14,%r8,8), %r8
movq -0x78(%rbp), %r9
movq (%r9,%rdi,8), %r9
movl $0x1, %r10d
movslq (%r12,%r10,4), %r11
movsd (%r8,%r11,8), %xmm0
movsd %xmm0, (%r9,%r10,8)
incq %r10
cmpq %r10, %rsi
jne 0x855b
incq %rdi
cmpq %rdx, %rdi
jne 0x8545
movq -0x48(%rbp), %rdx
leaq (%rdx,%rax,8), %rax
movq %rax, -0x90(%rbp)
movq 0x8(%rax), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x2bcf
movl %eax, -0x58(%rbp)
movq -0x78(%rbp), %rdi
movq %rbx, %rsi
movq -0x80(%rbp), %r14
movq %r14, %rdx
movl -0x30(%rbp), %ecx
callq 0x2bcf
movq -0x90(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rsp)
movq -0x68(%rbp), %rdi
movq %rbx, %rsi
movq -0x70(%rbp), %rdx
movq %r14, %rcx
movl -0x58(%rbp), %r8d
movl %eax, %r9d
callq 0x3238
movl -0x30(%rbp), %r10d
movq -0x48(%rbp), %r8
testl %eax, %eax
movl -0x40(%rbp), %r9d
movl $0x1, %eax
cmovel %eax, %r9d
movq -0x50(%rbp), %r11
cmpq %r11, %r15
jne 0x84f3
movl %r9d, -0x40(%rbp)
movq %r12, %rdi
callq 0x1100
movq -0x68(%rbp), %r13
movq -0x60(%rbp), %r12
movq -0x70(%rbp), %rdi
callq 0x1100
movq -0x80(%rbp), %rdi
callq 0x1100
cmpl $0x2, -0x38(%rbp)
jl 0x8663
cmpl $0x2, %r12d
movl $0x1, %r14d
cmovgel %r12d, %r14d
xorl %r15d, %r15d
movq (%r13,%r15,8), %rdi
callq 0x1100
movq (%rbx,%r15,8), %rdi
callq 0x1100
movq -0x78(%rbp), %rax
movq (%rax,%r15,8), %rdi
callq 0x1100
incq %r15
cmpq %r15, %r14
jne 0x863b
movq -0x78(%rbp), %rdi
callq 0x1100
movq %r13, %rdi
callq 0x1100
movq %rbx, %rdi
callq 0x1100
movl -0x40(%rbp), %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq