// File: /workspace/github_cpp_makefile_star2_O1/dcarrey[P]hgt-detection/./fonctions.cpp
// Function: DeleteUseLessHGT(int, HGT*, InputTree, InputTree)
int DeleteUseLessHGT(int nbHGT,struct HGT *bestHGT, struct InputTree SpeciesTree, struct InputTree SaveTree){

	int i,j,source,destination,p,q;
	struct InputTree tmpTree;
	struct CRITERIA aCrit;
	int cpt=0,retour=0;

	InitCriteria(&aCrit,SpeciesTree.size);
	initInputTree(&tmpTree);
	printf("\nHGT_INT : Procedure de suppression des transferts inutiles, il y a %d transferts",nbHGT);
	for(i=1;i<=nbHGT;i++){
		printf("\nHGT_INT : bestHGT[%d].valide=%d",i,bestHGT[i].valide);
	}
	for(i=1;i<=nbHGT;i++){
		if((bestHGT[i].valide == 1) || (bestHGT[i].valide == 6) || (bestHGT[i].valide == 4))
		{
			copyInputTree(&tmpTree,SaveTree,0,0);
			for(j=1;j<=nbHGT;j++){
				if((bestHGT[j].valide == 1) || (bestHGT[j].valide == 6) || (bestHGT[j].valide == 4))
				{
					if (i!=j){
						findBranch(tmpTree,&source,bestHGT[j].listSource);
						findBranch(tmpTree,&destination,bestHGT[j].listDestination);
						if(source != 0 && destination !=0){
							if(isAValidHGT(tmpTree,source,destination)==1){
								applyHGT(SpeciesTree.ADD,&tmpTree,source,destination);
							}
						}
					}
				}
			}
			if(AdjustBranchLength2(&tmpTree,SpeciesTree,0,1) == 0){
				//printf("\nHGT-DETECTION : on calcule les criteres");
				computeCriteria(SpeciesTree.ADD,tmpTree.ADD,SpeciesTree.size,&aCrit,SpeciesTree.LONGUEUR,SpeciesTree.ARETE,tmpTree.LONGUEUR,tmpTree.ARETE);
				printf("\nHGT-DETECTION : on a termine le calcul RF=%d",aCrit.RF);
				if(aCrit.RF == 0){
					printf("\nHGT_INT : Le transfert %d est inutile",i);
					bestHGT[i].valide = 0;
					retour++;
				}
			}
		}
	}
	FreeMemory_InputTree(&tmpTree,tmpTree.size);
	FreeCriteria(&aCrit,SpeciesTree.size);
	return retour;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rsi, %r14
movl %edi, %ebx
movl 0x10(%rbp), %esi
leaq -0x120(%rbp), %rdi
movl %esi, -0x34(%rbp)
callq 0xb933
xorl %eax, %eax
movq %rax, -0xb8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x98(%rbp)
movq $0x0, -0x40(%rbp)
movq %rax, -0x88(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xa0(%rbp)
movl %eax, -0xc0(%rbp)
movl $0x0, -0x78(%rbp)
movups %xmm0, -0xb0(%rbp)
leaq 0x7a0c(%rip), %rdi      # 0x159f7
movl %ebx, %esi
xorl %eax, %eax
callq 0x1030
movq %rbx, -0x30(%rbp)
testl %ebx, %ebx
jle 0xe036
movq -0x30(%rbp), %rax
leal 0x1(%rax), %r12d
leaq 0xa8(%r14), %r13
movl $0x1, %ebx
leaq 0x7a32(%rip), %r15      # 0x15a49
movl (%r13), %edx
movq %r15, %rdi
movl %ebx, %esi
xorl %eax, %eax
callq 0x1030
incq %rbx
addq $0xa8, %r13
cmpq %rbx, %r12
jne 0xe017
cmpl $0x0, -0x30(%rbp)
jle 0xe232
leaq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
incl %eax
movq %rax, -0x30(%rbp)
imulq $0xa8, %rax, %rax
addq $-0xa8, %rax
movq %rax, -0x68(%rbp)
movl $0x1, %ebx
xorl %r13d, %r13d
movq $0x0, -0x40(%rbp)
imulq $0xa8, %rbx, %rdx
movl (%r14,%rdx), %eax
cmpl $0x6, %eax
ja 0xe1f2
movl $0x52, %ecx
btl %eax, %ecx
jae 0xe1f2
movq %rbx, -0x48(%rbp)
addq %r14, %rdx
movq %rdx, -0x60(%rbp)
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0x68(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xa621
xorl %r12d, %r12d
movl 0xa8(%r14,%r12), %eax
cmpl $0x6, %eax
ja 0xe183
movl $0x52, %ecx
btl %eax, %ecx
jae 0xe183
cmpq %r12, %r13
je 0xe183
movq 0xb8(%r14,%r12), %rax
movl $0xb, %ecx
movq %rsp, %rdi
leaq -0xc0(%rbp), %rbx
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0xd2eb
movq 0xc0(%r14,%r12), %rax
movl $0xb, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0x4c(%rbp), %rdi
movq %rax, %rsi
callq 0xd2eb
movl -0x50(%rbp), %ebx
testl %ebx, %ebx
setne %al
movl -0x4c(%rbp), %r15d
testl %r15d, %r15d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xe183
movl $0xb, %ecx
movq %rsp, %rdi
leaq -0xc0(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %ebx, %edi
movl %r15d, %esi
callq 0xa4c5
testl %eax, %eax
je 0xe183
leaq -0xc0(%rbp), %rsi
movl %ebx, %edx
movl %r15d, %ecx
callq 0x8e8a
addq $0xa8, %r12
cmpq %r12, -0x68(%rbp)
jne 0xe0c6
movl $0xb, %ecx
movq %rsp, %rdi
leaq 0x10(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xb89b
testl %eax, %eax
jne 0xe1ee
movq -0xb8(%rbp), %rsi
movq -0x58(%rbp), %rdi
movl -0x34(%rbp), %edx
leaq -0x120(%rbp), %rcx
callq 0x8d82
movl -0xf0(%rbp), %ebx
leaq 0x7889(%rip), %rdi      # 0x15a6a
movl %ebx, %esi
xorl %eax, %eax
callq 0x1030
testl %ebx, %ebx
je 0xe208
movq -0x48(%rbp), %rbx
incq %rbx
addq $0xa8, %r13
cmpq -0x30(%rbp), %rbx
jne 0xe077
jmp 0xe232
leaq 0x7889(%rip), %rdi      # 0x15a98
movq -0x48(%rbp), %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x1030
movq -0x60(%rbp), %rax
movl $0x0, (%rax)
movq -0x40(%rbp), %rax
incl %eax
movq %rax, -0x40(%rbp)
jmp 0xe1f2
leaq -0xc0(%rbp), %rdi
movl (%rdi), %esi
callq 0xb0f6
leaq -0x120(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0xb9fb
movq -0x40(%rbp), %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq