// File: /workspace/github_cpp_makefile_star2_O3/dcarrey[P]hgt-detection/./fonctions.cpp
// Function: findBestHGT_nombreLimite(DescTree*, DescTree*, int*, int, InputTree, InputTree, Parameters, HGT*)
int findBestHGT_nombreLimite(struct DescTree *DTSpecies,struct DescTree *DTGene,int * tab_branches,int nb_branches,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;
	
	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<=nb_branches;i++){
		for(j=1;j<=nb_branches;j++){
			if(isAValidHGT(SpeciesTree,tab_branches[i],tab_branches[j])==1 && i!=j ){
				//printf("\n%d-%d -->%d--%d",SpeciesTree.ARETE[2*tab_branches[i]-1],SpeciesTree.ARETE[2*tab_branches[i]-2],SpeciesTree.ARETE[2*tab_branches[j]-1],SpeciesTree.ARETE[2*tab_branches[j]-2]);
			
				copyInputTree(&tmpTree,SpeciesTree,0,0);

				if(strcmp(param.subtree,"yes") == 0)
					if(TestSubTreeConstraint(SpeciesTree,tab_branches[i],tab_branches[j],DTSpecies,DTGene) == 0) {
						continue;
						//FreeMemory_InputTree(&tmpTree);
						tmpTree.ADD = NULL;
						tmpTree.ARETE = NULL;
						
					}

				applyHGT(GeneTree.ADD,&tmpTree,tab_branches[i],tab_branches[j]);
				AdjustBranchLength(&tmpTree,GeneTree,0,1);
	
				computeCriteria(tmpTree.ADD,GeneTree.ADD,size,&aCrit,tmpTree.LONGUEUR,tmpTree.ARETE,GeneTree.LONGUEUR,GeneTree.ARETE);

				ret += TestCriterionAndUpdate(&first,param.criterion,aCrit,aHGT,tab_branches[i],tab_branches[j],0,0);
				if(aHGT->crit.RF == 0 /*|| aHGT->crit.LS < epsilon */|| aHGT->crit.BD == 0 || aHGT->crit.QD == 0) { i=j=(int)INFINI;}

			}
		}
		
	}
	/*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 %r8, -0x58(%rbp)
movl %ecx, -0x2c(%rbp)
movq %rdx, %r13
movq %rsi, -0x68(%rbp)
movq %rdi, -0x60(%rbp)
movl $0x1, %eax
movl %eax, -0x4c(%rbp)
movl 0x10(%rbp), %r12d
xorl %r15d, %r15d
movq %r15, -0xc8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0xa8(%rbp)
movq %r15, -0x98(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xb0(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl %eax, -0xd0(%rbp)
movl $0x0, -0x88(%rbp)
movups %xmm0, -0xc0(%rbp)
leaq -0x130(%rbp), %rdi
movl %r12d, %esi
callq 0xb8ba
leaq -0x190(%rbp), %rdi
movl %r12d, %esi
callq 0xb8ba
movq 0x18(%rbp), %rbx
movq 0x70(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
leaq -0x130(%rbp), %rcx
callq 0x8c46
movq %rbx, %rdi
movq %r14, -0x78(%rbp)
movq %r14, %rsi
movl %r12d, -0x3c(%rbp)
movl %r12d, %edx
leaq -0x190(%rbp), %rcx
callq 0x8c46
movl $0x1, %ecx
leaq -0x130(%rbp), %rax
movsd (%rax), %xmm0
movsd 0x10(%rax), %xmm1
movq 0x30(%rax), %rax
movq -0x58(%rbp), %rdx
movsd %xmm0, 0x20(%rdx)
movq %rax, 0x50(%rdx)
movsd %xmm1, 0x30(%rdx)
movl %ecx, (%rdx)
cmpl $0x0, -0x2c(%rbp)
jle 0xc54b
leaq 0xc0(%rbp), %rax
leaq 0x10(%rbp), %rbx
addq $0x3fc, %rax            # imm = 0x3FC
movq %rax, -0x70(%rbp)
xorl %r15d, %r15d
movl $0x1, %r14d
movq %r15, -0x38(%rbp)
movq %rcx, -0x48(%rbp)
movslq %ecx, %r15
movl (%r13,%r15,4), %eax
movslq %r14d, %r12
movl (%r13,%r12,4), %edx
movl $0xb, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %eax, %edi
movl %edx, %esi
callq 0xa42b
testl %eax, %eax
sete %al
cmpl %r12d, %r15d
sete %cl
orb %al, %cl
je 0xc3e8
movq -0x38(%rbp), %r15
jmp 0xc519
movl $0xb, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xa587
leaq 0xc0(%rbp), %rax
cmpl $0x736579, 0xe9e(%rax)  # imm = 0x736579
jne 0xc44e
movl (%r13,%r15,4), %eax
movl (%r13,%r12,4), %edx
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0x10(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %eax, %edi
movl %edx, %esi
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rcx
callq 0x7eee
testl %eax, %eax
je 0xc511
movl (%r13,%r15,4), %edx
movl (%r13,%r12,4), %ecx
movq %rbx, %rsi
callq 0x8d4e
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0x68(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xb793
movq -0xc8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl -0x3c(%rbp), %edx
leaq -0x130(%rbp), %rbx
movq %rbx, %rcx
callq 0x8c46
movl (%r13,%r15,4), %eax
movl (%r13,%r12,4), %r8d
movl $0xc, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x0, 0x60(%rsp)
leaq -0x4c(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x58(%rbp), %r12
movq %r12, %rdx
movl %eax, %ecx
xorl %r9d, %r9d
callq 0xa27b
movq -0x38(%rbp), %r15
addl %eax, %r15d
cmpl $0x0, 0x50(%r12)
je 0xc4f6
movsd 0x30(%r12), %xmm0
ucomisd 0x8b7e(%rip), %xmm0     # 0x15070
jne 0xc507
jp 0xc507
movl $0xf423f, %r14d         # imm = 0xF423F
movl $0xf423f, %ecx          # imm = 0xF423F
leaq 0x10(%rbp), %rbx
jmp 0xc51d
cmpl $0x0, 0x54(%r12)
jne 0xc515
jmp 0xc4f6
movq -0x38(%rbp), %r15
leaq 0x10(%rbp), %rbx
movq -0x48(%rbp), %rcx
leal 0x1(%r14), %eax
cmpl -0x2c(%rbp), %r14d
movl %eax, %r14d
jl 0xc3a1
leal 0x1(%rcx), %eax
movl $0x1, %r14d
cmpl -0x2c(%rbp), %ecx
movl %eax, %ecx
jl 0xc3a1
movl -0xd0(%rbp), %eax
movl %eax, -0x38(%rbp)
leaq -0x130(%rbp), %rdi
movl -0x3c(%rbp), %ebx
movl %ebx, %esi
callq 0xb982
leaq -0x190(%rbp), %rdi
movl %ebx, %esi
callq 0xb982
leaq -0xd0(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0xb07d
movl %r15d, %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq