// File: /workspace/github_cpp_makefile_star2_O2/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 $0x148, %rsp            # imm = 0x148
movq %rsi, %r14
movl %edi, %r15d
leaq 0x10(%rbp), %rax
movl (%rax), %esi
leaq -0x118(%rbp), %rdi
movl %esi, -0x30(%rbp)
callq 0xa979
andq $0x0, -0xb0(%rbp)
xorps %xmm0, %xmm0
andq $0x0, -0x80(%rbp)
movups %xmm0, -0x90(%rbp)
orl $-0x1, -0x98(%rbp)
orl $-0x1, -0xb8(%rbp)
andl $0x0, -0x70(%rbp)
movups %xmm0, -0xa8(%rbp)
leaq 0x7d5b(%rip), %rdi      # 0x149f7
xorl %ebx, %ebx
movl %r15d, %esi
xorl %eax, %eax
callq 0x1030
testl %r15d, %r15d
cmovlel %ebx, %r15d
leal 0x1(%r15), %eax
movq %rax, -0x60(%rbp)
movq %r15, -0x38(%rbp)
imulq $0xa8, %r15, %r13
pushq $0x1
popq %r15
leaq 0x7d7c(%rip), %r12      # 0x14a49
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0xccf5
movl 0xa8(%r14,%rbx), %edx
movq %r12, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x1030
addq $0xa8, %rbx
incl %r15d
jmp 0xcccf
leaq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movl -0x38(%rbp), %eax
imulq $0xa8, %rax, %r15
pushq $0x1
popq %rbx
movq $0x0, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
cmpq -0x60(%rbp), %rbx
je 0xceca
imulq $0xa8, %rbx, %rdx
movl (%r14,%rdx), %eax
cmpl $0x6, %eax
ja 0xceba
pushq $0x52
popq %rcx
btl %eax, %ecx
jae 0xceba
movq %rbx, -0x48(%rbp)
addq %r14, %rdx
movq %rdx, -0x58(%rbp)
pushq $0xb
popq %rcx
movq %rsp, %rdi
leaq 0x68(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x976c
xorl %r13d, %r13d
cmpq %r13, %r15
je 0xce40
movl 0xa8(%r14,%r13), %eax
cmpl $0x6, %eax
ja 0xce34
pushq $0x52
popq %rcx
btl %eax, %ecx
jae 0xce34
cmpq %r13, -0x38(%rbp)
je 0xce34
movq 0xb8(%r14,%r13), %rax
pushq $0xb
popq %rbx
movq %rbx, %rcx
movq %rsp, %rdi
leaq -0xb8(%rbp), %r12
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0xc174
movq 0xc0(%r14,%r13), %rax
movq %rbx, %rcx
movq %rsp, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0x3c(%rbp), %rdi
movq %rax, %rsi
callq 0xc174
movl -0x40(%rbp), %r12d
testl %r12d, %r12d
setne %al
movl -0x3c(%rbp), %ebx
testl %ebx, %ebx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xce34
pushq $0xb
popq %rcx
movq %rsp, %rdi
leaq -0xb8(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %r12d, %edi
movl %ebx, %esi
callq 0x9610
testl %eax, %eax
je 0xce34
leaq -0xb8(%rbp), %rsi
movl %r12d, %edx
movl %ebx, %ecx
callq 0x804e
addq $0xa8, %r13
jmp 0xcd72
pushq $0xb
popq %rcx
movq %rsp, %rdi
leaq 0x10(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0xa8e3
testl %eax, %eax
je 0xce68
movq -0x48(%rbp), %rbx
jmp 0xceba
movq -0xb0(%rbp), %rsi
movq -0x50(%rbp), %rdi
movl -0x30(%rbp), %edx
leaq -0x118(%rbp), %rcx
callq 0x7f5c
movl -0xe8(%rbp), %ebx
leaq 0x7bdb(%rip), %rdi      # 0x14a6a
movl %ebx, %esi
xorl %eax, %eax
callq 0x1030
testl %ebx, %ebx
movq -0x48(%rbp), %rbx
jne 0xceba
leaq 0x7bf1(%rip), %rdi      # 0x14a98
movl %ebx, %esi
xorl %eax, %eax
callq 0x1030
movq -0x58(%rbp), %rax
andl $0x0, (%rax)
incl -0x2c(%rbp)
incq %rbx
addq $0xa8, -0x38(%rbp)
jmp 0xcd1d
leaq -0xb8(%rbp), %rdi
movl (%rdi), %esi
callq 0xa1b9
leaq -0x118(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0xaa34
movl -0x2c(%rbp), %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq