// File: /workspace/github_cpp_makefile_star2_O3/dcarrey[P]hgt-detection/./fonctions.cpp
// Function: bestRFNeighbor(InputTree*, InputTree*, int)
int	bestRFNeighbor(struct InputTree *geneTree,struct InputTree *speciesTree,int position){
	int choix=position;
	int v1=-1,v2=-1,v3=-1,v4=-1,i;
	struct InputTree tmpTree;	//= arbre temporaire qui va etre modifier pour calculer RF
	struct CRITERIA aCrit;		//== struture of all the criteria
	int min= (int) INFINI;

	initInputTree(&tmpTree);	

	//== recherche des voisins
	for(i=1;i<=2*geneTree->size-3-geneTree->kt;i++){

		if( (geneTree->ARETE[2*i-1] == geneTree->ARETE[2*position-1] || geneTree->ARETE[2*i-2] == geneTree->ARETE[2*position-1] || 
		     geneTree->ARETE[2*i-1] == geneTree->ARETE[2*position-2] || geneTree->ARETE[2*i-2] == geneTree->ARETE[2*position-2] ) && (i!=position) && (v1==-1)){
		
			v1 = i; //printf("\n(v1=%d,",i);
		}
		else if( (geneTree->ARETE[2*i-1] == geneTree->ARETE[2*position-1] || geneTree->ARETE[2*i-2] == geneTree->ARETE[2*position-1] || 
		     geneTree->ARETE[2*i-1] == geneTree->ARETE[2*position-2] || geneTree->ARETE[2*i-2] == geneTree->ARETE[2*position-2] ) && (i!=position) && (v2==-1)){
		
			v2 = i; //printf("v2=%d,",i);
		}
		else if( (geneTree->ARETE[2*i-1] == geneTree->ARETE[2*position-1] || geneTree->ARETE[2*i-2] == geneTree->ARETE[2*position-1] || 
		     geneTree->ARETE[2*i-1] == geneTree->ARETE[2*position-2] || geneTree->ARETE[2*i-2] == geneTree->ARETE[2*position-2] ) && (i!=position) && (v3==-1)){
		
			v3 = i; //printf("v2=%d,",i);
		}
		else if( (geneTree->ARETE[2*i-1] == geneTree->ARETE[2*position-1] || geneTree->ARETE[2*i-2] == geneTree->ARETE[2*position-1] || 
		     geneTree->ARETE[2*i-1] == geneTree->ARETE[2*position-2] || geneTree->ARETE[2*i-2] == geneTree->ARETE[2*position-2] ) && (i!=position) && (v4==-1)){
		
			v4 = i; //printf("v3=%d)",i);
		}
	}
	
	//printf("\n Selection de la racine :");

	copyInputTree(&tmpTree,(*geneTree),1,1);
	addLeafAndUpdate(&tmpTree,position);
	tmpTree.Root = tmpTree.size;
		
	InitCriteria(&aCrit,tmpTree.size);
	computeCriteria(speciesTree->ADD,tmpTree.ADD,tmpTree.size,&aCrit,speciesTree->LONGUEUR,speciesTree->ARETE,tmpTree.LONGUEUR,tmpTree.ARETE);

	//printf("\n(%d), %d--%d = %d",position,geneTree->ARETE[2*position-1],geneTree->ARETE[2*position-2],aCrit.RF);
	if(aCrit.RF < min){
		min=aCrit.RF;
		choix = position;
	}
		
	copyInputTree(&tmpTree,(*geneTree),1,0);
	addLeafAndUpdate(&tmpTree,v1);
	tmpTree.Root = tmpTree.size;
		
	FreeCriteria(&aCrit,tmpTree.size);
	InitCriteria(&aCrit,tmpTree.size);

	computeCriteria(speciesTree->ADD,tmpTree.ADD,tmpTree.size,&aCrit,speciesTree->LONGUEUR,speciesTree->ARETE,tmpTree.LONGUEUR,tmpTree.ARETE);

	//printf("\n(%d), %d--%d = %d",v1,geneTree->ARETE[2*v1-1],geneTree->ARETE[2*v1-2],aCrit.RF);
	if(aCrit.RF < min){
		min=aCrit.RF;
		choix = v1;
	}
	
	copyInputTree(&tmpTree,(*geneTree),1,0);
	addLeafAndUpdate(&tmpTree,v2);
	tmpTree.Root = tmpTree.size;
		
	FreeCriteria(&aCrit,tmpTree.size);
	InitCriteria(&aCrit,tmpTree.size);
	computeCriteria(speciesTree->ADD,tmpTree.ADD,tmpTree.size,&aCrit,speciesTree->LONGUEUR,speciesTree->ARETE,tmpTree.LONGUEUR,tmpTree.ARETE);

	//printf("\n(%d), %d--%d = %d",v2,geneTree->ARETE[2*v2-1],geneTree->ARETE[2*v2-2],aCrit.RF);
	if(aCrit.RF < min){
		min=aCrit.RF;
		choix = v2;
	}
		
	copyInputTree(&tmpTree,(*geneTree),1,0);
	addLeafAndUpdate(&tmpTree,v3);
	tmpTree.Root = tmpTree.size;
		
	FreeCriteria(&aCrit,tmpTree.size);
	InitCriteria(&aCrit,tmpTree.size);
	computeCriteria(speciesTree->ADD,tmpTree.ADD,tmpTree.size,&aCrit,speciesTree->LONGUEUR,speciesTree->ARETE,tmpTree.LONGUEUR,tmpTree.ARETE);
	//printf("\n(%d), %d--%d = %d",v3,geneTree->ARETE[2*v3-1],geneTree->ARETE[2*v3-2],aCrit.RF);
	if(aCrit.RF < min){
		min=aCrit.RF;
		choix = v3;
	}
	
	copyInputTree(&tmpTree,(*geneTree),1,0);
	addLeafAndUpdate(&tmpTree,v4);
	tmpTree.Root = tmpTree.size;
		
	FreeCriteria(&aCrit,tmpTree.size);
	InitCriteria(&aCrit,tmpTree.size);
	computeCriteria(speciesTree->ADD,tmpTree.ADD,tmpTree.size,&aCrit,speciesTree->LONGUEUR,speciesTree->ARETE,tmpTree.LONGUEUR,tmpTree.ARETE);
	//printf("\n(%d), %d--%d = %d",v4,geneTree->ARETE[2*v4-1],geneTree->ARETE[2*v4-2],aCrit.RF);
	if(aCrit.RF < min){
		min=aCrit.RF;
		choix = v4;
	}
	
	
	FreeCriteria(&aCrit,tmpTree.size);
	return choix;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdx, -0x30(%rbp)
movq %rsi, -0x50(%rbp)
xorl %eax, %eax
movq %rax, -0xa8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x88(%rbp)
movq %rax, -0x78(%rbp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, -0x90(%rbp)
movl %r15d, -0xb0(%rbp)
movl $0x0, -0x68(%rbp)
movups %xmm0, -0xa0(%rbp)
movl (%rdi), %eax
addl %eax, %eax
movq %rdi, -0x40(%rbp)
subl 0x48(%rdi), %eax
leal -0x3(%rax), %ecx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %ecx, %ecx
jle 0xf083
movq -0x40(%rbp), %rcx
movq 0x30(%rcx), %rcx
movq -0x30(%rbp), %rdi
leal (%rdi,%rdi), %edx
movslq %edx, %rdx
movq -0x8(%rcx,%rdx,8), %rsi
movl %edi, %edi
addl $-0x2, %eax
movl $0x1, %r8d
movq %rcx, %r9
movq %rdx, -0x38(%rbp)
movq 0x8(%r9), %r10
cmpq %rsi, %r10
jne 0xef52
cmpq %r8, %rdi
setne %r10b
cmpl $-0x1, %r15d
sete %r11b
andb %r10b, %r11b
cmpb $0x1, %r11b
je 0xefa8
cmpq %r8, %rdi
setne %r11b
cmpl $-0x1, %r14d
sete %r10b
andb %r11b, %r10b
cmpb $0x1, %r10b
je 0xefd5
jmp 0xefe2
movq (%r9), %r11
cmpq %rsi, %r11
jne 0xef72
cmpq %r8, %rdi
setne %r11b
cmpl $-0x1, %r15d
sete %bl
andb %r11b, %bl
cmpb $0x1, %bl
je 0xefa8
jmp 0xefbf
movq -0x10(%rcx,%rdx,8), %rbx
cmpq %rbx, %r10
je 0xef81
cmpq %rbx, %r11
jne 0xefb0
cmpq %r8, %rdi
setne %bl
cmpl $-0x1, %r15d
movl %r13d, %edx
movl %r12d, %r13d
sete %r12b
andb %bl, %r12b
cmpb $0x1, %r12b
movl %r13d, %r12d
movl %edx, %r13d
movq -0x38(%rbp), %rdx
jne 0xefb0
movl %r8d, %r15d
jmp 0xf073
movq -0x10(%rcx,%rdx,8), %rbx
cmpq %rbx, %r10
je 0xefbf
cmpq %rbx, %r11
jne 0xeff8
cmpq %r8, %rdi
setne %r11b
cmpl $-0x1, %r14d
sete %bl
andb %r11b, %bl
cmpb $0x1, %bl
jne 0xefdd
movl %r8d, %r14d
jmp 0xf073
cmpq %rsi, %r10
jne 0xeff5
cmpl $-0x1, %r13d
sete %r10b
andb %r11b, %r10b
cmpb $0x1, %r10b
je 0xf04d
jmp 0xf061
movq (%r9), %r11
cmpq %rsi, %r11
jne 0xf017
cmpq %r8, %rdi
setne %r10b
cmpl $-0x1, %r13d
sete %r11b
andb %r10b, %r11b
cmpb $0x1, %r11b
je 0xf04d
jmp 0xf061
movq -0x10(%rcx,%rdx,8), %rbx
cmpq %rbx, %r10
je 0xf026
cmpq %rbx, %r11
jne 0xf052
cmpq %r8, %rdi
setne %bl
cmpl $-0x1, %r13d
movl %r13d, %edx
movl %r12d, %r13d
sete %r12b
andb %bl, %r12b
cmpb $0x1, %r12b
movl %r13d, %r12d
movl %edx, %r13d
movq -0x38(%rbp), %rdx
jne 0xf052
movl %r8d, %r13d
jmp 0xf073
movq -0x10(%rcx,%rdx,8), %rbx
cmpq %rbx, %r10
je 0xf061
cmpq %rbx, %r11
jne 0xf073
cmpl $-0x1, %r12d
movl %r8d, %r10d
cmovnel %r12d, %r10d
cmpq %r8, %rdi
cmovnel %r10d, %r12d
incq %r8
addq $0x10, %r9
cmpq %r8, %rax
jne 0xef10
movl %r14d, -0x44(%rbp)
movl %r13d, -0x38(%rbp)
movl %r12d, -0x54(%rbp)
movl $0xb, %ecx
movq %rsp, %rdi
movq -0x40(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0xb0(%rbp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0xa587
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x711c
movl (%rbx), %r12d
movl %r12d, 0x20(%rbx)
leaq -0x110(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0xb8ba
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rbx), %rsi
movl %r12d, %edx
movq %r14, %rcx
callq 0x8c46
movl 0x30(%r14), %eax
movl $0xf423f, %r13d         # imm = 0xF423F
cmpl %r13d, %eax
cmovll %eax, %r13d
movl $0xb, %ecx
movq %rsp, %rdi
movq -0x40(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xa587
movq %rbx, %rdi
movl %r15d, %esi
callq 0x711c
movl (%rbx), %r12d
movl %r12d, 0x20(%rbx)
movq %r14, %rdi
movl %r12d, %esi
callq 0xb982
movq %r14, %rdi
movl %r12d, %esi
callq 0xb8ba
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rbx), %rsi
movl %r12d, %edx
movq %r14, %r12
movq %r14, %rcx
callq 0x8c46
movl 0x30(%r14), %eax
cmpl %r13d, %eax
movq -0x30(%rbp), %rcx
cmovll %r15d, %ecx
movq %rcx, -0x30(%rbp)
cmovll %eax, %r13d
movl $0xb, %ecx
movq %rsp, %rdi
movq -0x40(%rbp), %r14
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xa587
movq %rbx, %rdi
movl -0x44(%rbp), %esi
callq 0x711c
movl (%rbx), %r15d
movl %r15d, 0x20(%rbx)
movq %r12, %rdi
movl %r15d, %esi
callq 0xb982
movq %r12, %rdi
movl %r15d, %esi
callq 0xb8ba
movq -0x50(%rbp), %r12
movq 0x8(%r12), %rdi
movq 0x8(%rbx), %rsi
movl %r15d, %edx
leaq -0x110(%rbp), %r15
movq %r15, %rcx
callq 0x8c46
movl 0x30(%r15), %eax
cmpl %r13d, %eax
movq -0x30(%rbp), %r15
cmovll -0x44(%rbp), %r15d
cmovll %eax, %r13d
movl $0xb, %ecx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xa587
movq %rbx, %rdi
movl -0x38(%rbp), %esi
callq 0x711c
movl (%rbx), %r14d
movl %r14d, 0x20(%rbx)
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
movl %r14d, %esi
callq 0xb982
movq %rbx, %rdi
movl %r14d, %esi
callq 0xb8ba
movq 0x8(%r12), %rdi
leaq -0xb0(%rbp), %rax
movq 0x8(%rax), %rsi
movl %r14d, %edx
movq %rbx, %rcx
callq 0x8c46
movl 0x30(%rbx), %eax
movq %rbx, %r12
cmpl %r13d, %eax
cmovll -0x38(%rbp), %r15d
movq %r15, -0x30(%rbp)
cmovll %eax, %r13d
movl $0xb, %ecx
movq %rsp, %rdi
movq -0x40(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0xb0(%rbp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xa587
movq %rbx, %rdi
movl -0x54(%rbp), %r15d
movl %r15d, %esi
callq 0x711c
movl (%rbx), %r14d
movq %r12, %rdi
movl %r14d, %esi
callq 0xb982
movq %r12, %rdi
movl %r14d, %esi
callq 0xb8ba
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rbx), %rsi
movl %r14d, %edx
movq %r12, %rcx
callq 0x8c46
cmpl %r13d, 0x30(%r12)
movq -0x30(%rbp), %rbx
cmovll %r15d, %ebx
movq %r12, %rdi
movl %r14d, %esi
callq 0xb982
movl %ebx, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq