// File: /workspace/github_cpp_makefile_star2_O3/dcarrey[P]hgt-detection/./fonctions.cpp
// Function: addRoot(InputTree*, InputTree*, char const*, char const*, char*, char*, int*, char const*)
int addRoot(struct InputTree *aTree,struct InputTree *refTree,const char * message, const char * add,char *fichier,char *fichier_leaves,int * listLeaves,const char * version){
	
	int choix=-1,choix2=-1;
	FILE *in;
	int R1,R2,i,j;
	
	if(strcmp(add,"prompt") == 0 ){
		printf("\n%s",message);
		printEdges(NULL,1,aTree->ARETE,aTree->LONGUEUR,aTree->SpeciesName,aTree->size,NULL,0,aTree->kt);
		
		while(choix<1 || choix > 2*aTree->size-3-aTree->kt){
			printf("\n\nSelect the root branch :");
			scanf("%d",&choix2);
		}
	}
	else if(strcmp(add,"file") == 0 ){
		
		if(file_exists(fichier_leaves)){
			int * listLeaves2 = (int*) malloc((aTree->size+1)*sizeof(int));
			/*if(listLeaves2 == NULL){
				printf("\nlistLeaves2 == NULL");
			}*/
			for(i=1;i<=aTree->size;i++)
				listLeaves2[i] = 2;
			
			if((in=fopen(fichier_leaves,"r"))==NULL){
				printf("\nHGT-DETECTION : Can't open root file (%s)",fichier_leaves);
				exit(-1);
			}
	
			char * tmp;
			tmp = (char*)malloc(100);
			//printf("\nHGT-DETECTION (size = %d): ",aTree->size);
			
			do{
				fscanf(in,"%s",tmp);
				//printf("\n%s ",tmp);
				if(strcmp(tmp,"<>") != 0){
					for(i=1;i<=aTree->size;i++){
						if(strcmp(aTree->SpeciesName[i],tmp) == 0){
							listLeaves2[i] = 0;
							//printf(" (0) ");
						}
					}
				}			
			}while(strcmp(tmp,"<>") != 0);
			
			while(fscanf(in,"%s",tmp) != -1){
				//printf("\n%s ",tmp);
				if(strcmp(tmp,"<>") != 0){
					for(i=1;i<=aTree->size;i++){
						if(strcmp(aTree->SpeciesName[i],tmp) == 0){
							listLeaves2[i] = 1;
							//printf(" (1) ");
						}
					}	
				}
			}
			
			fclose(in);
			
			/*printf("\n");
			for(i=1;i<=aTree->size;i++){
				printf(">%s<",aTree->SpeciesName[i]);
			}	
			printf("\nHGT-DETECTION : \n\t\t  ");
			for(i=1;i<=aTree->size;i++){
				printf("%d ",listLeaves2[i]);
			}	*/
			
			choix = choix2 = findApproxRootBranch(listLeaves2,aTree,10000);
			
			// fscanf(in,"%d %d",&R1,&R2);
			// for(i=1;i<=2*aTree->size-3-aTree->kt;i++){
				// if((aTree->ARETE[2*i-1] == R1 && aTree->ARETE[2*i-2] == R2) || (aTree->ARETE[2*i-1] == R2 && aTree->ARETE[2*i-2] == R1))
					// choix2 = choix = i;
			// }
			
			//printf("\n\n====%d====\n",choix);
			
		
		}else{
			if((in=fopen(fichier,"r"))==NULL){
				printf("\nCan't open root file (%s)",fichier);
				exit(-1);
			}
			fscanf(in,"%d %d",&R1,&R2);
			for(i=1;i<=2*aTree->size-3-aTree->kt;i++){
				if((aTree->ARETE[2*i-1] == R1 && aTree->ARETE[2*i-2] == R2) || (aTree->ARETE[2*i-1] == R2 && aTree->ARETE[2*i-2] == R1))
					choix2 = choix = i;
			}
			fclose(in);
		}
	}
	else if(strcmp(add,"bestrfbranch") == 0){
		printf("choix = %d",choix);	
		choix2 = choix = bestRFBranch(aTree,refTree);
		printf("choix = %d",choix);	
		//listLeaves = NULL;
	}
	/*else if(strcmp(add,"bestbipartition") == 0){
		printf("choix = %d",choix);	
		choix2 = choix = bestbipartition(aTree,refTree);
		printf("choix = %d",choix);	
		//listLeaves = NULL;
	}*/
	else{
		choix2 = choix = midPoint(aTree->ARETE,aTree->ADD,aTree->size,aTree->kt);
		if(refTree != NULL){
			choix2 = choix = bestRFBranch(aTree,refTree);
			//choix2 = choix = bestRFNeighbor(aTree,refTree,choix);
		}
	}
	
	//printf("\nchoix = %d",choix);
	if(listLeaves != NULL){
		//== sauvegarder la liste des feuilles de chaque cot� de l'arete.
		if(listLeaves[0] == -1){
			for(i=1;i<=aTree->size;i++)
				if(aTree->ADD[i][aTree->ARETE[2*choix-1]] < aTree->ADD[i][aTree->ARETE[2*choix-2]])
					listLeaves[i] = 0;
				else
					listLeaves[i] = 1;
			listLeaves[0]=1;
		}
		else{
			//printf("\nHGT-DETECTION : Recherche de la racine en fonction de la bipartition");
			if(strcmp(add,"bestbipartition") == 0){
				choix2 = findApproxRootBranch(listLeaves,aTree,10000);
			}
			//printf("\nHGT-DETECTION : choix = %d",choix2);
		}

		if(choix2==-1)
			return -1;
		else
			choix=choix2;
	}

	printRoot(fichier,aTree->ARETE[2*choix-1],aTree->ARETE[2*choix-2]);
	printRootByLeaves(fichier_leaves,choix,aTree);

	addLeafAndUpdate(aTree,choix);
	aTree->Root = aTree->size;
	
	//===mise a jour de degre
	for(i=1;i<=2*(aTree->size)-2-aTree->kt;i++){
		aTree->degre[i]=0;
		for(j=1;j<=2*aTree->size-2-aTree->kt;j++)
			if(aTree->Adjacence[i][j] < INFINI) aTree->degre[i]++;
	}
	return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r12
movq %r8, -0x30(%rbp)
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %rbx
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
leaq 0x6f3a(%rip), %rsi      # 0x16374
movq %rcx, %rdi
callq 0x1170
testl %eax, %eax
je 0xf4a9
leaq 0x7437(%rip), %rsi      # 0x16884
movq %r15, %rdi
callq 0x1170
testl %eax, %eax
movq %r15, -0x60(%rbp)
je 0xf507
leaq 0x6f5c(%rip), %rsi      # 0x163c4
movq %r15, %rdi
callq 0x1170
testl %eax, %eax
movq %r12, %r13
je 0xf67e
movq 0x30(%rbx), %rdi
movq 0x8(%rbx), %rsi
movl (%rbx), %edx
movl 0x48(%rbx), %ecx
callq 0xb5e6
testq %r14, %r14
je 0xf770
movq %rbx, %rdi
callq 0xf32b
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0xf6b1
leaq 0x6f73(%rip), %rdi      # 0x16423
movq %r13, %rsi
xorl %eax, %eax
callq 0x1030
movq 0x30(%rbx), %rdx
movq 0x38(%rbx), %rcx
movq 0x18(%rbx), %r8
movl (%rbx), %r9d
movl 0x48(%rbx), %eax
movl %eax, 0x10(%rsp)
xorl %edi, %edi
movl $0x1, %esi
callq 0x1e3d
leaq 0x6e98(%rip), %rbx      # 0x1637b
leaq 0x6cf0(%rip), %r14      # 0x161da
leaq -0x44(%rbp), %r15
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1030
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10d0
jmp 0xf4ee
movq %r12, %rdi
callq 0xf3d9
testb %al, %al
je 0xf6bc
movl (%rbx), %r15d
leal 0x1(%r15), %r14d
movslq %r14d, %rdi
shlq $0x2, %rdi
callq 0x1140
testl %r15d, %r15d
jle 0xf546
movl %r14d, %edx
movl $0x1, %ecx
movl $0x2, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rdx
jne 0xf537
movq %rax, -0x38(%rbp)
leaq 0x6e21(%rip), %rsi      # 0x16372
movq %r12, -0x50(%rbp)
movq %r12, %rdi
callq 0x10e0
testq %rax, %rax
je 0xf8fb
movq %rax, %r15
movl $0x64, %edi
callq 0x1140
movq %rax, %r14
leaq 0x6ea7(%rip), %rsi      # 0x16424
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x11e0
leaq 0x6e30(%rip), %rsi      # 0x163c1
movq %r14, %rdi
callq 0x1170
testl %eax, %eax
movq %r15, -0x58(%rbp)
je 0xf605
movl (%rbx), %r12d
testl %r12d, %r12d
jle 0xf5da
movq 0x18(%rbx), %r15
incq %r12
movl $0x1, %r13d
movq (%r15,%r13,8), %rdi
movq %r14, %rsi
callq 0x1170
testl %eax, %eax
jne 0xf5d2
movq -0x38(%rbp), %rax
movl $0x0, (%rax,%r13,4)
incq %r13
cmpq %r13, %r12
jne 0xf5b6
movq -0x58(%rbp), %r15
movq %r15, %rdi
leaq 0x6e3c(%rip), %rsi      # 0x16424
movq %r14, %rdx
xorl %eax, %eax
callq 0x11e0
movq %r14, %rdi
leaq 0x6dc5(%rip), %rsi      # 0x163c1
callq 0x1170
testl %eax, %eax
jne 0xf5a1
leaq 0x6e18(%rip), %rsi      # 0x16424
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11e0
cmpl $-0x1, %eax
je 0xf74b
movq %r14, %rdi
leaq 0x6d95(%rip), %rsi      # 0x163c1
callq 0x1170
testl %eax, %eax
je 0xf66e
movl (%rbx), %r12d
testl %r12d, %r12d
jle 0xf66e
movq 0x18(%rbx), %r15
incq %r12
movl $0x1, %r13d
movq (%r15,%r13,8), %rdi
movq %r14, %rsi
callq 0x1170
testl %eax, %eax
jne 0xf666
movq -0x38(%rbp), %rax
movl $0x1, (%rax,%r13,4)
incq %r13
cmpq %r13, %r12
jne 0xf64a
movq -0x58(%rbp), %r15
movq %r15, %rdi
leaq 0x6da8(%rip), %rsi      # 0x16424
jmp 0xf60f
leaq 0x6d4c(%rip), %r14      # 0x163d1
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x1030
movq %rbx, %rdi
callq 0xf32b
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0xf78f
leaq 0x6caf(%rip), %rsi      # 0x16372
movq -0x30(%rbp), %rdi
callq 0x10e0
testq %rax, %rax
je 0xf908
movq %rax, %r13
leaq 0x7450(%rip), %rsi      # 0x16b2f
leaq -0x40(%rbp), %rdx
leaq -0x3c(%rbp), %rcx
movq %rax, %rdi
xorl %eax, %eax
callq 0x11e0
movl (%rbx), %eax
addl %eax, %eax
subl 0x48(%rbx), %eax
leal -0x3(%rax), %ecx
testl %ecx, %ecx
jle 0xf778
movq 0x30(%rbx), %rcx
movslq -0x40(%rbp), %rdx
movslq -0x3c(%rbp), %rsi
addl $-0x2, %eax
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x1, %edi
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x8(%rcx), %r8
cmpq %rdx, %r8
jne 0xf72d
cmpq %rsi, (%rcx)
je 0xf737
cmpq %rsi, %r8
jne 0xf73d
cmpq %rdx, (%rcx)
jne 0xf73d
movl %edi, %r15d
movl %edi, %r14d
incq %rdi
addq $0x10, %rcx
cmpq %rdi, %rax
jne 0xf71f
jmp 0xf784
movq %r15, %rdi
callq 0x10a0
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
movl $0x2710, %edx           # imm = 0x2710
callq 0xed44
movl %eax, %r15d
movl %eax, %r14d
movq -0x50(%rbp), %r13
jmp 0xf78f
movl %eax, %r15d
movl %eax, %r14d
jmp 0xf78f
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq %r13, %rdi
callq 0x10a0
movq %r12, %r13
movq 0x10(%rbp), %r12
testq %r12, %r12
je 0xf826
cmpl $-0x1, (%r12)
je 0xf7cc
leaq 0x6c32(%rip), %rsi      # 0x163dc
movq -0x60(%rbp), %rdi
callq 0x1170
testl %eax, %eax
jne 0xf819
movq %r12, %rdi
movq %rbx, %rsi
movl $0x2710, %edx           # imm = 0x2710
callq 0xed44
movl %eax, %r15d
jmp 0xf819
cmpl $0x0, (%rbx)
jle 0xf811
movq 0x8(%rbx), %rax
movq 0x30(%rbx), %rdx
addl %r14d, %r14d
movslq %r14d, %rsi
movq -0x10(%rdx,%rsi,8), %rcx
movq -0x8(%rdx,%rsi,8), %rdx
xorl %esi, %esi
movq 0x8(%rax,%rsi,8), %rdi
movsd (%rdi,%rcx,8), %xmm0
xorl %r8d, %r8d
ucomisd (%rdi,%rdx,8), %xmm0
setbe %r8b
movl %r8d, 0x4(%r12,%rsi,4)
movslq (%rbx), %rdi
incq %rsi
cmpq %rdi, %rsi
jl 0xf7eb
movl $0x1, (%r12)
movl %r15d, %r14d
cmpl $-0x1, %r15d
je 0xf8e7
movq 0x30(%rbx), %rax
leal (%r14,%r14), %ecx
movslq %ecx, %rcx
movl -0x10(%rax,%rcx,8), %edx
movl -0x8(%rax,%rcx,8), %esi
movq -0x30(%rbp), %rdi
callq 0x7b57
movq %r13, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0x7bbf
movq %rbx, %rdi
movl %r14d, %esi
callq 0x711c
movl (%rbx), %ecx
movl %ecx, 0x20(%rbx)
addl %ecx, %ecx
subl 0x48(%rbx), %ecx
addl $-0x2, %ecx
xorl %eax, %eax
testl %ecx, %ecx
jle 0xf8ec
movq 0x50(%rbx), %rcx
movl $0x1, %edx
movsd 0x57c9(%rip), %xmm0     # 0x15048
movl $0x0, (%rcx,%rdx,4)
movl (%rbx), %esi
movl 0x48(%rbx), %edi
leal (%rsi,%rsi), %r8d
subl %edi, %r8d
addl $-0x2, %r8d
testl %r8d, %r8d
jle 0xf8d6
movq 0x28(%rbx), %r8
movq (%r8,%rdx,8), %r8
xorl %r9d, %r9d
xorl %r10d, %r10d
ucomisd 0x8(%r8,%r9,8), %xmm0
jbe 0xf8be
incl %r10d
movl %r10d, (%rcx,%rdx,4)
movl (%rbx), %esi
movl 0x48(%rbx), %edi
leal (%rsi,%rsi), %r11d
subl %edi, %r11d
addl $-0x2, %r11d
movslq %r11d, %r11
incq %r9
cmpq %r11, %r9
jl 0xf8a9
jmp 0xf8d9
movslq %r8d, %r11
leaq 0x1(%rdx), %rsi
cmpq %r11, %rdx
movq %rsi, %rdx
jl 0xf87f
jmp 0xf8ec
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6a94(%rip), %rdi      # 0x16396
movq -0x50(%rbp), %rsi
jmp 0xf913
leaq 0x6003(%rip), %rdi      # 0x15912
movq -0x30(%rbp), %rsi
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110