// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %r9, %r14
movq %r8, %r13
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, -0x30(%rbp)
movq %rdi, %rbx
orl $-0x1, -0x3c(%rbp)
leaq 0x724e(%rip), %rsi      # 0x15374
movq %rcx, %rdi
callq 0x1160
testl %eax, %eax
je 0xe195
leaq 0x774b(%rip), %rsi      # 0x15884
movq %r15, %rdi
callq 0x1160
testl %eax, %eax
movq %r13, -0x50(%rbp)
je 0xe1f1
leaq 0x7270(%rip), %rsi      # 0x153c4
movq %r15, %rdi
callq 0x1160
testl %eax, %eax
movq %r14, %r12
je 0xe240
movq 0x30(%rbx), %rdi
movq 0x8(%rbx), %rsi
movl (%rbx), %edx
movl 0x48(%rbx), %ecx
callq 0xa69a
cmpq $0x0, -0x30(%rbp)
je 0xe42d
pushq $-0x1
popq %r13
movq %rbx, %rdi
callq 0xe024
jmp 0xe430
leaq 0x7287(%rip), %rdi      # 0x15423
movq %r12, %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)
pushq $0x1
popq %rsi
xorl %edi, %edi
callq 0x1dc8
leaq 0x71ae(%rip), %rbx      # 0x1537b
leaq 0x7006(%rip), %r14      # 0x151da
leaq -0x3c(%rbp), %r15
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1030
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10c0
jmp 0xe1d8
movq %r14, %r12
movq %r14, %rdi
callq 0xe0c8
testb %al, %al
movq %r14, -0x38(%rbp)
movq %r15, -0x48(%rbp)
je 0xe272
movslq (%rbx), %r14
leaq 0x4(,%r14,4), %rdi
callq 0x1130
movq %rax, %r13
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
cmpq %rax, %rcx
je 0xe308
movl $0x2, 0x4(%r13,%rax,4)
incq %rax
jmp 0xe229
leaq 0x718a(%rip), %r14      # 0x153d1
pushq $-0x1
popq %r13
movq %r14, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x1030
movq %rbx, %rdi
callq 0xe024
movq %r14, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x1030
jmp 0xe430
leaq 0x70f9(%rip), %rsi      # 0x15372
movq %r13, %rdi
callq 0x10d0
testq %rax, %rax
je 0xe57a
movq %rax, %r14
leaq 0x789b(%rip), %rsi      # 0x15b2f
xorl %r12d, %r12d
leaq -0x58(%rbp), %r15
leaq -0x54(%rbp), %r13
movq %rax, %rdi
movq %r15, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x11c0
movl (%rbx), %eax
addl %eax, %eax
subl 0x48(%rbx), %eax
addl $-0x3, %eax
movslq (%r15), %rcx
testl %eax, %eax
cmovlel %r12d, %eax
movslq (%r13), %rdx
shlq $0x4, %rax
pushq $-0x1
popq %r13
pushq $0x1
popq %rsi
movq -0x48(%rbp), %r15
cmpq %r12, %rax
je 0xe41f
movq 0x30(%rbx), %rdi
movq 0x8(%rdi,%r12), %r8
cmpq %rcx, %r8
jne 0xe2f2
cmpq %rdx, (%rdi,%r12)
je 0xe2fd
cmpq %rdx, %r8
jne 0xe300
cmpq %rcx, (%rdi,%r12)
jne 0xe300
movl %esi, %r13d
addq $0x10, %r12
incl %esi
jmp 0xe2d5
leaq 0x7063(%rip), %rsi      # 0x15372
movq %r12, %rdi
callq 0x10d0
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0xe587
pushq $0x64
popq %rdi
callq 0x1130
movq %rax, %r14
movq -0x30(%rbp), %rdi
leaq 0x70ea(%rip), %rsi      # 0x15424
movq %r14, %rdx
xorl %eax, %eax
callq 0x11c0
movq %r14, %rdi
leaq 0x7073(%rip), %rsi      # 0x153c1
callq 0x1160
testl %eax, %eax
je 0xe3df
movl (%rbx), %r12d
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
xorl %r15d, %r15d
cmpq %r15, %r12
je 0xe32f
movq 0x18(%rbx), %rax
movq 0x8(%rax,%r15,8), %rdi
movq %r14, %rsi
callq 0x1160
testl %eax, %eax
jne 0xe38d
andl $0x0, 0x4(%r13,%r15,4)
incq %r15
jmp 0xe36d
movq %r14, %rdi
leaq 0x7025(%rip), %rsi      # 0x153c1
callq 0x1160
testl %eax, %eax
je 0xe3df
movl (%rbx), %r12d
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
xorl %r15d, %r15d
cmpq %r15, %r12
je 0xe3df
movq 0x18(%rbx), %rax
movq 0x8(%rax,%r15,8), %rdi
movq %r14, %rsi
callq 0x1160
testl %eax, %eax
jne 0xe3da
movl $0x1, 0x4(%r13,%r15,4)
incq %r15
jmp 0xe3b7
movq -0x30(%rbp), %rdi
leaq 0x703a(%rip), %rsi      # 0x15424
movq %r14, %rdx
xorl %eax, %eax
callq 0x11c0
cmpl $-0x1, %eax
jne 0xe392
movq -0x30(%rbp), %rdi
callq 0x1080
movq %r13, %rdi
movq %rbx, %rsi
movl $0x2710, %edx           # imm = 0x2710
callq 0xda35
movl %eax, %r13d
movq -0x38(%rbp), %r12
movq -0x48(%rbp), %r15
jmp 0xe430
movq %r14, %rdi
callq 0x1080
movq -0x38(%rbp), %r12
jmp 0xe430
movl %eax, %r13d
movq 0x10(%rbp), %r14
testq %r14, %r14
je 0xe4bf
cmpl $-0x1, (%r14)
je 0xe46b
leaq 0x6f92(%rip), %rsi      # 0x153dc
movq %r15, %rdi
callq 0x1160
testl %eax, %eax
jne 0xe4b5
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2710, %edx           # imm = 0x2710
callq 0xda35
movl %eax, %r13d
jmp 0xe4b5
pushq $0x1
popq %rax
movl %r13d, %ecx
addl %r13d, %ecx
movslq %ecx, %rcx
movslq (%rbx), %rdx
cmpq %rdx, %rax
jg 0xe4ae
movq 0x8(%rbx), %rdx
movq 0x30(%rbx), %rsi
movq (%rdx,%rax,8), %rdx
movq -0x10(%rsi,%rcx,8), %rdi
movq -0x8(%rsi,%rcx,8), %rsi
movsd (%rdx,%rdi,8), %xmm0
xorl %edi, %edi
ucomisd (%rdx,%rsi,8), %xmm0
setbe %dil
movl %edi, (%r14,%rax,4)
incq %rax
jmp 0xe477
movl $0x1, (%r14)
cmpl $-0x1, %r13d
je 0xe568
movq 0x30(%rbx), %rax
leal (,%r13,2), %ecx
movslq %ecx, %rcx
movl -0x10(%rax,%rcx,8), %edx
movl -0x8(%rax,%rcx,8), %esi
movq -0x50(%rbp), %rdi
callq 0x6fff
movq %r12, %rdi
movl %r13d, %esi
movq %rbx, %rdx
callq 0x7065
movq %rbx, %rdi
movl %r13d, %esi
callq 0x6697
movl (%rbx), %edi
movl 0x48(%rbx), %r9d
movl %edi, 0x20(%rbx)
pushq $0x1
popq %rax
movsd 0x5b3c(%rip), %xmm0     # 0x14048
movq %rax, %rcx
addl %edi, %edi
subl %r9d, %edi
addl $-0x2, %edi
movslq %edi, %rdx
cmpq %rdx, %rcx
jg 0xe564
movq 0x50(%rbx), %rdx
andl $0x0, (%rdx,%rcx,4)
xorl %esi, %esi
movq %rax, %r8
movl (%rbx), %edi
movl 0x48(%rbx), %r9d
leal (%rdi,%rdi), %r10d
subl %r9d, %r10d
addl $-0x2, %r10d
movslq %r10d, %r10
cmpq %r10, %r8
jg 0xe55f
movq 0x28(%rbx), %rdi
movq (%rdi,%rcx,8), %rdi
ucomisd (%rdi,%r8,8), %xmm0
jbe 0xe55a
incl %esi
movl %esi, (%rdx,%rcx,4)
incq %r8
jmp 0xe52c
incq %rcx
jmp 0xe50f
xorl %eax, %eax
jmp 0xe56b
pushq $-0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6391(%rip), %rdi      # 0x14912
movq -0x50(%rbp), %rsi
jmp 0xe592
leaq 0x6e08(%rip), %rdi      # 0x15396
movq -0x38(%rbp), %rsi
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %rdi
callq 0x1100