// File: /workspace/github_cpp_makefile_star2_O1/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, -0x38(%rbp)
movq %r8, %r12
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %rbx
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
leaq 0x6ecd(%rip), %rsi      # 0x16374
movq %rcx, %rdi
callq 0x1170
testl %eax, %eax
je 0xf51d
leaq 0x73ca(%rip), %rsi      # 0x16884
movq %r15, %rdi
callq 0x1170
testl %eax, %eax
movq %r15, -0x68(%rbp)
je 0xf57b
leaq 0x6eef(%rip), %rsi      # 0x163c4
movq %r15, %rdi
callq 0x1170
testl %eax, %eax
je 0xf6d0
movq 0x30(%rbx), %rdi
movq 0x8(%rbx), %rsi
movl (%rbx), %edx
movl 0x48(%rbx), %ecx
callq 0xb661
movl %eax, -0x2c(%rbp)
testq %r14, %r14
je 0xf7c1
movq %rbx, %rdi
callq 0xf398
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0xf7d7
leaq 0x6eff(%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 0x1ebe
leaq 0x6e24(%rip), %rbx      # 0x1637b
leaq 0x6c7c(%rip), %r14      # 0x161da
leaq -0x2c(%rbp), %r15
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1030
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10d0
jmp 0xf562
movq -0x38(%rbp), %rdi
callq 0xf446
testb %al, %al
je 0xf70f
movslq (%rbx), %r14
leaq 0x4(,%r14,4), %rdi
callq 0x1140
testq %r14, %r14
jle 0xf5bb
movl %r14d, %edx
incq %rdx
movl $0x1, %ecx
movl $0x2, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rdx
jne 0xf5ac
movq %rax, -0x48(%rbp)
movq %r12, -0x60(%rbp)
leaq 0x6da8(%rip), %rsi      # 0x16372
movq -0x38(%rbp), %rdi
callq 0x10e0
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0xf934
movl $0x64, %edi
callq 0x1140
movq %rax, %r14
movq -0x40(%rbp), %rdi
leaq 0x6e2c(%rip), %rsi      # 0x16424
movq %r14, %rdx
xorl %eax, %eax
callq 0x11e0
movq %r14, %rdi
leaq 0x6db5(%rip), %rsi      # 0x163c1
callq 0x1170
testl %eax, %eax
je 0xf658
movl %eax, -0x4c(%rbp)
movl (%rbx), %r15d
testl %r15d, %r15d
jle 0xf652
movq 0x18(%rbx), %r13
incq %r15
movl $0x1, %r12d
movq (%r13,%r12,8), %rdi
movq %r14, %rsi
callq 0x1170
testl %eax, %eax
jne 0xf64a
movq -0x48(%rbp), %rax
movl $0x0, (%rax,%r12,4)
incq %r12
cmpq %r12, %r15
jne 0xf62d
cmpl $0x0, -0x4c(%rbp)
jne 0xf5ed
leaq 0x6dc5(%rip), %rsi      # 0x16424
movq -0x40(%rbp), %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x11e0
cmpl $-0x1, %eax
je 0xf79b
movq %r14, %rdi
leaq 0x6d41(%rip), %rsi      # 0x163c1
callq 0x1170
testl %eax, %eax
je 0xf6c3
movl (%rbx), %r15d
testl %r15d, %r15d
jle 0xf6c3
movq 0x18(%rbx), %r13
incq %r15
movl $0x1, %r12d
movq (%r13,%r12,8), %rdi
movq %r14, %rsi
callq 0x1170
testl %eax, %eax
jne 0xf6bb
movq -0x48(%rbp), %rax
movl $0x1, (%rax,%r12,4)
incq %r12
cmpq %r12, %r15
jne 0xf69e
movq -0x40(%rbp), %rdi
leaq 0x6d56(%rip), %rsi      # 0x16424
jmp 0xf663
leaq 0x6cfa(%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 0xf398
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x1030
jmp 0xf7d7
leaq 0x6c5c(%rip), %rsi      # 0x16372
movq %r12, %rdi
callq 0x10e0
testq %rax, %rax
je 0xf941
movq %rax, %r14
leaq 0x73fe(%rip), %rsi      # 0x16b2f
leaq -0x54(%rbp), %rdx
leaq -0x50(%rbp), %rcx
movq %rax, %rdi
xorl %eax, %eax
callq 0x11e0
movl (%rbx), %eax
addl %eax, %eax
subl 0x48(%rbx), %eax
leal -0x3(%rax), %edx
movl -0x2c(%rbp), %ecx
testl %edx, %edx
jle 0xf7c6
movq 0x30(%rbx), %rdx
movslq -0x54(%rbp), %rsi
movslq -0x50(%rbp), %rdi
addl $-0x2, %eax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0x1, %r8d
movq 0x8(%rdx), %r9
cmpq %rsi, %r9
jne 0xf77d
cmpq %rdi, (%rdx)
je 0xf787
cmpq %rdi, %r9
jne 0xf78d
cmpq %rsi, (%rdx)
jne 0xf78d
movl %r8d, %ecx
movl %r8d, %r15d
incq %r8
addq $0x10, %rdx
cmpq %r8, %rax
jne 0xf76f
jmp 0xf7cc
movq -0x40(%rbp), %rdi
callq 0x10a0
movq -0x48(%rbp), %rdi
movq %rbx, %rsi
movl $0x2710, %edx           # imm = 0x2710
callq 0xee3e
movl %eax, %r15d
movl %eax, -0x2c(%rbp)
movq -0x60(%rbp), %r12
jmp 0xf7d7
movl %eax, %r15d
jmp 0xf7d7
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %ecx, -0x2c(%rbp)
movq %r14, %rdi
callq 0x10a0
movq 0x10(%rbp), %r14
testq %r14, %r14
je 0xf86d
cmpl $-0x1, (%r14)
je 0xf813
leaq 0x6beb(%rip), %rsi      # 0x163dc
movq -0x68(%rbp), %rdi
callq 0x1170
testl %eax, %eax
jne 0xf85f
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2710, %edx           # imm = 0x2710
callq 0xee3e
movl %eax, -0x2c(%rbp)
jmp 0xf85f
cmpl $0x0, (%rbx)
jle 0xf858
movq 0x8(%rbx), %rax
movq 0x30(%rbx), %rdx
addl %r15d, %r15d
movslq %r15d, %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(%r14,%rsi,4)
movslq (%rbx), %rdi
incq %rsi
cmpq %rdi, %rsi
jl 0xf832
movl $0x1, (%r14)
movl -0x2c(%rbp), %r15d
cmpl $-0x1, %r15d
je 0xf920
movq 0x30(%rbx), %rax
leal (%r15,%r15), %ecx
movslq %ecx, %rcx
movl -0x10(%rax,%rcx,8), %edx
movl -0x8(%rax,%rcx,8), %esi
movq %r12, %rdi
callq 0x7c82
movq -0x38(%rbp), %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x7cea
movq %rbx, %rdi
movl %r15d, %esi
callq 0x71e1
movl (%rbx), %eax
movl %eax, 0x20(%rbx)
addl %eax, %eax
subl 0x48(%rbx), %eax
addl $-0x2, %eax
testl %eax, %eax
jle 0xf91c
movq 0x50(%rbx), %rax
movl $0x1, %ecx
movsd 0x5784(%rip), %xmm0     # 0x15048
movl $0x0, (%rax,%rcx,4)
movl (%rbx), %edx
addl %edx, %edx
subl 0x48(%rbx), %edx
addl $-0x2, %edx
testl %edx, %edx
jle 0xf903
movq 0x28(%rbx), %rdx
movq (%rdx,%rcx,8), %rdx
xorl %esi, %esi
ucomisd 0x8(%rdx,%rsi,8), %xmm0
jbe 0xf8ee
incl (%rax,%rcx,4)
movl (%rbx), %edi
addl %edi, %edi
subl 0x48(%rbx), %edi
addl $-0x2, %edi
movslq %edi, %rdi
incq %rsi
cmpq %rdi, %rsi
jl 0xf8e3
leaq 0x1(%rcx), %rdx
movl (%rbx), %esi
addl %esi, %esi
subl 0x48(%rbx), %esi
addl $-0x2, %esi
movslq %esi, %rsi
cmpq %rsi, %rcx
movq %rdx, %rcx
jl 0xf8c4
xorl %eax, %eax
jmp 0xf925
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6a5b(%rip), %rdi      # 0x16396
movq -0x38(%rbp), %rsi
jmp 0xf94b
leaq 0x5fca(%rip), %rdi      # 0x15912
movq %r12, %rsi
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110