// File: /workspace/github_cpp_makefile_star2_O3/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, %r15d
movl 0x10(%rbp), %esi
leaq -0x120(%rbp), %rdi
movl %esi, -0x2c(%rbp)
callq 0xb8ba
xorl %ebx, %ebx
movq %rbx, -0xb8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x98(%rbp)
movq %rbx, -0x88(%rbp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, -0xa0(%rbp)
movl %r12d, -0xc0(%rbp)
movl $0x0, -0x78(%rbp)
movups %xmm0, -0xb0(%rbp)
leaq 0x7b30(%rip), %rdi      # 0x159f7
movl %r15d, %esi
xorl %eax, %eax
callq 0x1030
testl %r15d, %r15d
jle 0xe125
leal 0x1(%r15), %ecx
leaq 0xa8(%r14), %rbx
movl $0x1, %r12d
leaq 0x7b57(%rip), %r13      # 0x15a49
movq %rcx, -0x38(%rbp)
movl (%rbx), %edx
movq %r13, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1030
movq -0x38(%rbp), %rcx
incq %r12
addq $0xa8, %rbx
cmpq %r12, %rcx
jne 0xdef6
testl %r15d, %r15d
jle 0xe11d
leaq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
imulq $0xa8, %rcx, %rax
addq $-0xa8, %rax
movq %rax, -0x68(%rbp)
movl $0x1, %ebx
xorl %r12d, %r12d
movq $0x0, -0x40(%rbp)
imulq $0xa8, %rbx, %rsi
movl (%r14,%rsi), %eax
cmpl $0x6, %eax
ja 0xe0cd
movl $0x52, %edx
btl %eax, %edx
jae 0xe0cd
movq %rbx, -0x48(%rbp)
addq %r14, %rsi
movq %rsi, -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 0xa587
xorl %r15d, %r15d
movl 0xa8(%r14,%r15), %eax
cmpl $0x6, %eax
ja 0xe05a
movl $0x52, %ecx
btl %eax, %ecx
jae 0xe05a
cmpq %r15, %r12
je 0xe05a
movq 0xb8(%r14,%r15), %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 0xd232
movq 0xc0(%r14,%r15), %rax
movl $0xb, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0x4c(%rbp), %rdi
movq %rax, %rsi
callq 0xd232
movl -0x50(%rbp), %ebx
testl %ebx, %ebx
setne %al
movl -0x4c(%rbp), %r13d
testl %r13d, %r13d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xe05a
movl $0xb, %ecx
movq %rsp, %rdi
leaq -0xc0(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %ebx, %edi
movl %r13d, %esi
callq 0xa42b
testl %eax, %eax
je 0xe05a
leaq -0xc0(%rbp), %rsi
movl %ebx, %edx
movl %r13d, %ecx
callq 0x8d4e
addq $0xa8, %r15
cmpq %r15, -0x68(%rbp)
jne 0xdf9d
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 0xb822
testl %eax, %eax
jne 0xe0c5
movq -0xb8(%rbp), %rsi
movq -0x58(%rbp), %rdi
movl -0x2c(%rbp), %edx
leaq -0x120(%rbp), %rcx
callq 0x8c46
movl -0xf0(%rbp), %ebx
leaq 0x79b2(%rip), %rdi      # 0x15a6a
movl %ebx, %esi
xorl %eax, %eax
callq 0x1030
testl %ebx, %ebx
je 0xe0e2
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rbx
incq %rbx
addq $0xa8, %r12
cmpq %rcx, %rbx
jne 0xdf4e
jmp 0xe110
leaq 0x79af(%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)
movq -0x38(%rbp), %rcx
jmp 0xe0cd
movl -0xc0(%rbp), %r12d
movq -0x40(%rbp), %rbx
jmp 0xe125
xorl %ebx, %ebx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq -0xc0(%rbp), %rdi
movl %r12d, %esi
callq 0xb07d
leaq -0x120(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0xb982
movl %ebx, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq