// File: /workspace/github_cpp_makefile_star2_O1/dcarrey[P]hgt-detection/./fonctions.cpp
// Function: findBestHGT(int, InputTree, InputTree, Parameters, HGT*)
int findBestHGT(int initial,struct InputTree SpeciesTree,struct InputTree GeneTree,struct Parameters param,struct HGT *aHGT){

	struct InputTree tmpTree;
	int i,j,k,l,first=1,ret=0;
	int size = SpeciesTree.size;
	int ktSpecies;
	struct CRITERIA aCrit,aCritRef;
	struct DescTree *DTSpecies,*DTGene;

	initInputTree(&tmpTree);

	//printf("\ndebut findBestHgt");
	//== compute criteria
	InitCriteria(&aCrit,size);
	InitCriteria(&aCritRef,size);
	computeCriteria(SpeciesTree.ADD,GeneTree.ADD,size,&aCrit,SpeciesTree.LONGUEUR,SpeciesTree.ARETE,GeneTree.LONGUEUR,GeneTree.ARETE);
	computeCriteria(SpeciesTree.ADD,GeneTree.ADD,size,&aCritRef,SpeciesTree.LONGUEUR,SpeciesTree.ARETE,GeneTree.LONGUEUR,GeneTree.ARETE);
	loadCriteria(aCrit,aHGT);

	DTGene = (struct DescTree*)malloc((2*GeneTree.size-2-GeneTree.kt+1)*sizeof(struct DescTree));
	RechercherBipartition(GeneTree.ARETE,GeneTree.ADD,GeneTree.Root,GeneTree.Adjacence,DTGene,GeneTree.size,GeneTree.kt);	

	DTSpecies = (struct DescTree*)malloc((2*SpeciesTree.size-2-SpeciesTree.kt+1)*sizeof(struct DescTree));
	RechercherBipartition(SpeciesTree.ARETE,SpeciesTree.ADD,SpeciesTree.Root,SpeciesTree.Adjacence,DTSpecies,SpeciesTree.size,SpeciesTree.kt);
	//printf("\nRF = %d , LS = %lf , BD = %lf , QD = %d",aCrit.RF,aCrit.LS,aCrit.BD,aCrit.QD);

	/*for(i=1;i<=2*SpeciesTree.size-3-SpeciesTree.kt;i++)
		printf("\n%d-%d -->%lf",SpeciesTree.ARETE[2*i-1],SpeciesTree.ARETE[2*i-2],SpeciesTree.LONGUEUR[i-1]);
	printf("\n");*/

	//== Test all possible HGT
	for(i=1;i<2*size-3-SpeciesTree.kt;i++)
		for(j=1;j<2*size-3-SpeciesTree.kt;j++){
			//printf("\nicit : %d %d",i,j);
			//== is it a valid hgt ?
			if(isAValidHGT(SpeciesTree,i,j)==1 && i!=j ){

				//printf("\n%d-%d -> %d-%d",SpeciesTree.ARETE[2*i-1],SpeciesTree.ARETE[2*i-2],SpeciesTree.ARETE[2*j-1],SpeciesTree.ARETE[2*j-2]);
				//printf(".");

				copyInputTree(&tmpTree,SpeciesTree,0,0);
				
				//printf("\nTransfert : %d--%d -> %d--%d",tmpTree.ARETE[2*i-1],tmpTree.ARETE[2*i-2],tmpTree.ARETE[2*j-1],tmpTree.ARETE[2*j-2]);
				
				//if(tmpTree.ARETE[2*i-1]==6 && tmpTree.ARETE[2*i-2]==15 && tmpTree.ARETE[2*j-1]==7 && tmpTree.ARETE[2*j-2]==14)
				//	printf("");

				if(strcmp(param.subtree,"yes") == 0)
					if(TestSubTreeConstraint(SpeciesTree,i,j,DTSpecies,DTGene) == 0) {
						continue;
						//FreeMemory_InputTree(&tmpTree);
						tmpTree.ADD = NULL;
						tmpTree.ARETE = NULL;
						
					}
				
				//printf("\n\n");
				//printEdges(0,tmpTree.ARETE,tmpTree.LONGUEUR,SpeciesTree.SpeciesName,tmpTree.size);
				//printf("\navant apply");
				applyHGT(GeneTree.ADD,&tmpTree,i,j);
				//printf("\napres apply");

				//printEdges(0,tmpTree.ARETE,tmpTree.LONGUEUR,SpeciesTree.SpeciesName,tmpTree.size);
				//printEdges(0,GeneTree.ARETE,GeneTree.LONGUEUR,SpeciesTree.SpeciesName,GeneTree.size);
				/*for(k=1;k<=2*tmpTree.size-2-tmpTree.kt;k++){
					printf("\n");
					for(l=1;l<=2*tmpTree.size-2-tmpTree.kt;l++)ListeSommets_taille_0(SpeciesTree.Input,tab_sommet);
						printf("%lf ",tmpTree.ADD[k][l]);
				}
				for(k=1;k<=2*tmpTree.size-3-tmpTree.kt;k++)
					printf("\n%d-%d -->%lf",tmpTree.ARETE[2*k-1],tmpTree.ARETE[2*k-2],tmpTree.LONGUEUR[k-1]);
				printf("\n");*/
				//printMatrix(SpeciesTree.SpeciesName,GeneTree.ADD,GeneTree.size);
				
				AdjustBranchLength(&tmpTree,GeneTree,0,1);
				//printMatrix(SpeciesTree.SpeciesName,tmpTree.ADD,tmpTree.size);
				
				
			//	computeCriteria(tmpTree.ADD,GeneTree.ADD,size,&aCrit);
			//	printf("<br>Transfert : %d--%d (%d)-> %d--%d (%d)",SpeciesTree.ARETE[2*i-1],SpeciesTree.ARETE[2*i-2],i,SpeciesTree.ARETE[2*j-1],SpeciesTree.ARETE[2*j-2],j);
			//	printf("=====>RF = %d, LS = %lf",aCrit.RF,aCrit.LS);
				
			//	if(i==12 && j==7){
			//		aCrit.RF = 100;
			//	}	
				if(initial == 1){
					computeCriteria(tmpTree.ADD,SpeciesTree.ADD,size,&aCrit,tmpTree.LONGUEUR,tmpTree.ARETE,SpeciesTree.LONGUEUR,SpeciesTree.ARETE);
					if(aCrit.RF == 1){
						ret += TestCriterionAndUpdate(&first,param.criterion,aCrit,aHGT,i,j,0,0);
						i=j=(int)INFINI;
						break;
					}
				}
				else{
					computeCriteria(tmpTree.ADD,GeneTree.ADD,size,&aCrit,tmpTree.LONGUEUR,tmpTree.ARETE,GeneTree.LONGUEUR,GeneTree.ARETE);
			//		printf("<br>aCrit : RF = %d, LS = %lf",aCrit.RF,aCrit.LS);
			//		printf("<br>aHGT : RF = %d, LS = %lf",aHGT->crit.RF,aHGT->crit.LS);
		//			if(i==13 && j==3){
						//printf("Le gros changement");
		//				aCrit.RF=7;
		//			}
					ret += TestCriterionAndUpdate(&first,param.criterion,aCrit,aHGT,i,j,0,0);
					if(aHGT->crit.RF == 0 /*|| aHGT->crit.LS < epsilon */|| aHGT->crit.BD == 0 || aHGT->crit.QD == 0) { i=j=(int)INFINI;}
				}

				
				//FreeMemory_InputTree(&tmpTree);
				//tmpTree.ADD = NULL;
				//tmpTree.ARETE = NULL;

			}
			//printf("apres");
		}
	//printf("\n===> %d-%d -> %d-%d , RF = %d , LS = %lf QD = %d",SpeciesTree.ARETE[2*(aHGT->source)-1],SpeciesTree.ARETE[2*(aHGT->source)-2],SpeciesTree.ARETE[2*(aHGT->destination)-1],SpeciesTree.ARETE[2*(aHGT->destination)-2],aHGT->crit.RF,aHGT->crit.LS,aHGT->crit.QD);
	//printf("icit");
	if(ret > 0 )
		findListSpecies(aHGT,DTSpecies,SpeciesTree);
	
	deleteBipartition(DTSpecies,SpeciesTree);
	deleteBipartition(DTGene,GeneTree);
	FreeCriteria(&aCrit,size);
	FreeCriteria(&aCritRef,size);
	FreeMemory_InputTree(&tmpTree,tmpTree.size);

	//printf("\nfin findBestHgt");
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, -0x30(%rbp)
movl %edi, -0x54(%rbp)
leaq 0x68(%rbp), %r13
leaq 0x10(%rbp), %r12
movl $0x1, %r15d
movl %r15d, -0x44(%rbp)
movl (%r12), %r14d
xorl %eax, %eax
movq %rax, -0xd0(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0xb0(%rbp)
movq $0x0, -0x38(%rbp)
movq %rax, -0xa0(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xb8(%rbp)
movl %eax, -0xd8(%rbp)
movl $0x0, -0x90(%rbp)
movups %xmm0, -0xc8(%rbp)
leaq -0x138(%rbp), %rbx
movq %rbx, %rdi
movl %r14d, %esi
callq 0xb933
leaq -0x198(%rbp), %rdi
movl %r14d, %esi
callq 0xb933
movq 0x8(%r12), %rdi
movq 0x30(%r12), %rax
movq %rax, -0x50(%rbp)
movq 0x8(%r13), %r12
movq 0x30(%r13), %rax
movq %rax, -0x40(%rbp)
movq %rdi, %r13
movq %rdi, -0x70(%rbp)
movq %r12, %rsi
movl %r14d, %edx
movq %rbx, %rcx
callq 0x8d82
movq %r13, %rdi
movq %r12, %rsi
movl %r14d, %edx
leaq -0x198(%rbp), %rcx
callq 0x8d82
movsd (%rbx), %xmm0
movsd 0x10(%rbx), %xmm1
movq 0x30(%rbx), %rax
movq -0x30(%rbp), %rcx
movsd %xmm0, 0x20(%rcx)
movq %rax, 0x50(%rcx)
movsd %xmm1, 0x30(%rcx)
movl %r15d, (%rcx)
leaq 0x68(%rbp), %rax
movl (%rax), %ebx
movl 0x48(%rax), %r13d
movl %r13d, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %ecx, %eax
leal (%rax,%rbx,2), %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x1140
leaq 0x68(%rbp), %rcx
movl 0x20(%rcx), %edx
movq 0x28(%rcx), %rcx
movl %r13d, (%rsp)
movq -0x40(%rbp), %rdi
movq %r12, -0x80(%rbp)
movq %r12, %rsi
movq %rax, -0x68(%rbp)
movq %rax, %r8
movl %ebx, %r9d
callq 0x4ef6
leal (%r14,%r14), %r12d
leaq 0x10(%rbp), %r13
movl 0x48(%r13), %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ebx, %eax
leal (%rax,%r14,2), %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x1140
movl 0x20(%r13), %edx
movq 0x28(%r13), %rcx
movl %ebx, (%rsp)
movq -0x50(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq %rax, -0x60(%rbp)
movq %rax, %r8
movq %r14, -0x40(%rbp)
movl %r14d, %r9d
callq 0x4ef6
movq -0x30(%rbp), %rdx
subl %ebx, %r12d
addl $-0x3, %r12d
movl %r12d, -0x50(%rbp)
cmpl $0x2, %r12d
jl 0xc25a
leaq 0xc0(%rbp), %rax
addq $0x3fc, %rax            # imm = 0x3FC
movq %rax, -0x78(%rbp)
movq $0x0, -0x38(%rbp)
leaq -0xd8(%rbp), %r14
leaq -0x138(%rbp), %r13
movl $0x1, %ebx
movq %r13, %r12
movq %r14, %r13
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0x10(%rbp), %r14
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %r15d, %edi
movl %ebx, %esi
callq 0xa4c5
testl %eax, %eax
sete %al
cmpl %ebx, %r15d
sete %cl
orb %al, %cl
je 0xc0b6
movq -0x30(%rbp), %rdx
movq %r13, %r14
movq %r12, %r13
jmp 0xc23d
movl $0xb, %ecx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r13, %r14
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xa621
leaq 0xc0(%rbp), %rax
cmpl $0x736579, 0xe9e(%rax)  # imm = 0x736579
jne 0xc10f
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0x10(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %r15d, %edi
movl %ebx, %esi
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rcx
callq 0x8016
testl %eax, %eax
je 0xc22e
movq %r14, %rsi
movl %r15d, %edx
movl %ebx, %ecx
callq 0x8e8a
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0x68(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xb80c
movq -0xd0(%rbp), %rdi
cmpl $0x1, -0x54(%rbp)
movq %r12, %r13
jne 0xc1b2
movq -0x70(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %r13, %rcx
callq 0x8d82
cmpl $0x1, -0x108(%rbp)
jne 0xc228
movl $0xc, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x0, 0x60(%rsp)
leaq -0x44(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x30(%rbp), %r12
movq %r12, %rdx
movl %r15d, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0xa331
movq %r12, %rdx
movq -0x38(%rbp), %rcx
addl %eax, %ecx
movq %rcx, -0x38(%rbp)
movl $0xf4240, %r15d         # imm = 0xF4240
jmp 0xc24b
movq -0x80(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %r13, %rcx
callq 0x8d82
movl $0xc, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x0, 0x60(%rsp)
leaq -0x44(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x30(%rbp), %r12
movq %r12, %rdx
movl %r15d, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0xa331
movq %r12, %rdx
movq -0x38(%rbp), %rcx
addl %eax, %ecx
movq %rcx, -0x38(%rbp)
cmpl $0x0, 0x50(%r12)
je 0xc21b
movsd 0x30(%rdx), %xmm0
ucomisd 0x8e59(%rip), %xmm0     # 0x15070
jne 0xc237
jp 0xc237
movl $0xf423f, %ebx          # imm = 0xF423F
movl $0xf423f, %r15d         # imm = 0xF423F
jmp 0xc23d
movq -0x30(%rbp), %rdx
jmp 0xc23d
movq -0x30(%rbp), %rdx
jmp 0xc0ae
cmpl $0x0, 0x54(%rdx)
je 0xc21b
incl %ebx
cmpl -0x50(%rbp), %ebx
jl 0xc076
incl %r15d
movl $0x1, %ebx
cmpl -0x50(%rbp), %r15d
jl 0xc076
movq -0x38(%rbp), %rbx
testl %ebx, %ebx
leaq 0x10(%rbp), %r14
movq -0x60(%rbp), %r15
jle 0xc283
movl $0xb, %ecx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rdx, %rdi
movq %r15, %rsi
callq 0xa1f8
movl $0xb, %ecx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
callq 0x7a5a
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0x68(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq -0x68(%rbp), %rdi
callq 0x7a5a
leaq -0x138(%rbp), %rdi
movq -0x40(%rbp), %r14
movl %r14d, %esi
callq 0xb9fb
leaq -0x198(%rbp), %rdi
movl %r14d, %esi
callq 0xb9fb
leaq -0xd8(%rbp), %rdi
movl (%rdi), %esi
callq 0xb0f6
movl %ebx, %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq