// File: /workspace/github_cpp_makefile_star2_O1/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 $0x138, %rsp            # imm = 0x138
movl %edx, %r12d
movq %rsi, -0x40(%rbp)
xorl %eax, %eax
movq %rax, -0xa0(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x80(%rbp)
movq %rax, -0x70(%rbp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, -0x88(%rbp)
movl %r15d, -0xa8(%rbp)
movl $0x0, -0x60(%rbp)
movups %xmm0, -0x98(%rbp)
movl (%rdi), %eax
addl %eax, %eax
movq %rdi, %r13
subl 0x48(%rdi), %eax
leal -0x3(%rax), %ecx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
testl %ecx, %ecx
jle 0xf104
movq 0x30(%r13), %rcx
leal (%r12,%r12), %edx
movslq %edx, %rdx
movq -0x8(%rcx,%rdx,8), %rsi
movl %r12d, %edi
addl $-0x2, %eax
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0x1, %r8d
movq %rcx, %r9
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movq 0x8(%r9), %r10
cmpq %rsi, %r10
je 0xf01e
movq (%r9), %r11
cmpq %rsi, %r11
jne 0xf03c
cmpq %r8, %rdi
setne %r11b
cmpl $-0x1, %r15d
sete %bl
andb %r11b, %bl
cmpb $0x1, %bl
jne 0xf04b
movl %r8d, %r15d
jmp 0xf0e1
movq -0x10(%rcx,%rdx,8), %rbx
cmpq %rbx, %r10
je 0xf01e
cmpq %rbx, %r11
je 0xf01e
cmpq %rsi, %r10
je 0xf058
movq (%r9), %r11
cmpq %rsi, %r11
jne 0xf073
cmpq %r8, %rdi
setne %r11b
cmpl $-0x1, %r14d
sete %bl
andb %r11b, %bl
cmpb $0x1, %bl
jne 0xf082
movl %r8d, %r14d
jmp 0xf0e1
movq -0x10(%rcx,%rdx,8), %rbx
cmpq %rbx, %r10
je 0xf058
cmpq %rbx, %r11
je 0xf058
cmpq %rsi, %r10
je 0xf08f
movq (%r9), %r11
cmpq %rsi, %r11
jne 0xf0ab
cmpq %r8, %rdi
setne %r11b
cmpl $-0x1, -0x30(%rbp)
sete %bl
andb %r11b, %bl
cmpb $0x1, %bl
jne 0xf0ba
movl %r8d, -0x30(%rbp)
jmp 0xf0e1
movq -0x10(%rcx,%rdx,8), %rbx
cmpq %rbx, %r10
je 0xf08f
cmpq %rbx, %r11
je 0xf08f
cmpq %rsi, %r10
je 0xf0c7
movq (%r9), %r11
cmpq %rsi, %r11
jne 0xf0f3
movl -0x2c(%rbp), %r11d
cmpl $-0x1, %r11d
movl %r8d, %r10d
cmovnel %r11d, %r10d
cmpq %r8, %rdi
cmovnel %r10d, %r11d
movl %r11d, -0x2c(%rbp)
incq %r8
addq $0x10, %r9
cmpq %r8, %rax
jne 0xf00d
jmp 0xf104
movq -0x10(%rcx,%rdx,8), %rbx
cmpq %rbx, %r10
je 0xf0c7
cmpq %rbx, %r11
jne 0xf0e1
jmp 0xf0c7
movl %r14d, -0x44(%rbp)
movl $0xb, %ecx
movq %rsp, %rdi
movq %r13, -0x50(%rbp)
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0xa8(%rbp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0xa621
movq %rbx, %rdi
movl %r12d, %esi
callq 0x71e1
movq %r12, -0x38(%rbp)
movl (%rbx), %r12d
movl %r12d, 0x20(%rbx)
leaq -0x108(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0xb933
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rbx), %rsi
movl %r12d, %edx
movq %r14, %rcx
callq 0x8d82
movl 0x30(%r14), %eax
movl $0xf423f, %r13d         # imm = 0xF423F
cmpl %r13d, %eax
cmovll %eax, %r13d
movl $0xb, %ecx
movq %rsp, %rdi
movq -0x50(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xa621
movq %rbx, %rdi
movl %r15d, %esi
callq 0x71e1
movl (%rbx), %r12d
movl %r12d, 0x20(%rbx)
movq %r14, %rdi
movl %r12d, %esi
callq 0xb9fb
movq %r14, %rdi
movl %r12d, %esi
callq 0xb933
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rbx), %rsi
movl %r12d, %edx
movq %r14, %rcx
callq 0x8d82
movl 0x30(%r14), %eax
cmpl %r13d, %eax
movq -0x38(%rbp), %r12
cmovll %r15d, %r12d
cmovll %eax, %r13d
movl $0xb, %ecx
movq %rsp, %rdi
movq -0x50(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xa621
movq %rbx, %rdi
movl -0x44(%rbp), %esi
callq 0x71e1
movl (%rbx), %r15d
movl %r15d, 0x20(%rbx)
movq %r14, %rdi
movl %r15d, %esi
callq 0xb9fb
movq %r14, %rdi
movl %r15d, %esi
callq 0xb933
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rbx), %rsi
movl %r15d, %edx
movq %r14, %rcx
callq 0x8d82
movl 0x30(%r14), %eax
cmpl %r13d, %eax
cmovll -0x44(%rbp), %r12d
movq %r12, -0x38(%rbp)
cmovll %eax, %r13d
movl $0xb, %ecx
movq %rsp, %rdi
movq -0x50(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xa621
movq %rbx, %rdi
movl -0x30(%rbp), %r15d
movl %r15d, %esi
callq 0x71e1
movl (%rbx), %r14d
movl %r14d, 0x20(%rbx)
leaq -0x108(%rbp), %r12
movq %r12, %rdi
movl %r14d, %esi
callq 0xb9fb
movq %r12, %rdi
movl %r14d, %esi
callq 0xb933
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rbx), %rsi
movl %r14d, %edx
movq %r12, %rcx
callq 0x8d82
movl 0x30(%r12), %eax
cmpl %r13d, %eax
movq -0x38(%rbp), %rcx
cmovll %r15d, %ecx
movq %rcx, -0x38(%rbp)
cmovll %eax, %r13d
movl $0xb, %ecx
movq %rsp, %rdi
movq -0x50(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xa621
movq %rbx, %rdi
movl -0x2c(%rbp), %r15d
movl %r15d, %esi
callq 0x71e1
movl (%rbx), %r14d
movl %r14d, 0x20(%rbx)
movq %r12, %rdi
movl %r14d, %esi
callq 0xb9fb
movq %r12, %rdi
movl %r14d, %esi
callq 0xb933
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x8(%rbx), %rsi
movl %r14d, %edx
movq %r12, %rcx
callq 0x8d82
cmpl %r13d, 0x30(%r12)
movq -0x38(%rbp), %rbx
cmovll %r15d, %ebx
movq %r12, %rdi
movl %r14d, %esi
callq 0xb9fb
movl %ebx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq