// File: 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
subq $0x90, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rdi
leaq 0xb315(%rip), %rsi      # 0x26419
callq 0x1130
cmpl $0x0, %eax
jne 0x1b1c7
movq -0x20(%rbp), %rsi
leaq 0xb3ab(%rip), %rdi      # 0x264c8
movb $0x0, %al
callq 0x1030
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movq -0x10(%rbp), %rax
movl 0x48(%rax), %eax
xorl %esi, %esi
movl %esi, %edi
movl $0x1, %esi
xorl %r10d, %r10d
movq $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x2c20
movb $0x1, %al
cmpl $0x1, -0x3c(%rbp)
movb %al, -0x69(%rbp)
jl 0x1b197
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
shll %ecx
subl $0x3, %ecx
movq -0x10(%rbp), %rdx
subl 0x48(%rdx), %ecx
cmpl %ecx, %eax
setg %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x1b1a0
jmp 0x1b1c2
leaq 0xb279(%rip), %rdi      # 0x26420
movb $0x0, %al
callq 0x1030
leaq 0xb0ca(%rip), %rdi      # 0x2627f
leaq -0x40(%rbp), %rsi
movb $0x0, %al
callq 0x10b0
jmp 0x1b16f
jmp 0x1b566
movq -0x28(%rbp), %rdi
leaq 0xb77f(%rip), %rsi      # 0x26951
callq 0x1130
cmpl $0x0, %eax
jne 0x1b4d2
movq -0x38(%rbp), %rdi
callq 0x1b070
testb $0x1, %al
jne 0x1b1f2
jmp 0x1b3b7
movq -0x10(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x1110
movq %rax, -0x60(%rbp)
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x1b237
movq -0x60(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl $0x2, (%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b212
movq -0x38(%rbp), %rdi
leaq 0xb1d5(%rip), %rsi      # 0x26417
callq 0x10c0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x1b26d
movq -0x38(%rbp), %rsi
leaq 0xb1df(%rip), %rdi      # 0x2643b
movb $0x0, %al
callq 0x1030
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x10f0
movl $0x64, %edi
callq 0x1110
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq 0xb23f(%rip), %rsi      # 0x264c9
movb $0x0, %al
callq 0x1190
movq -0x68(%rbp), %rdi
leaq 0xb1ca(%rip), %rsi      # 0x26466
callq 0x1130
cmpl $0x0, %eax
je 0x1b2f4
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x1b2f2
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x68(%rbp), %rsi
callq 0x1130
cmpl $0x0, %eax
jne 0x1b2e5
movq -0x60(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x1b2e7
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b2ad
jmp 0x1b2f4
jmp 0x1b2f6
movq -0x68(%rbp), %rdi
leaq 0xb165(%rip), %rsi      # 0x26466
callq 0x1130
cmpl $0x0, %eax
jne 0x1b27b
jmp 0x1b311
movq -0x48(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq 0xb1a9(%rip), %rsi      # 0x264c9
movb $0x0, %al
callq 0x1190
cmpl $-0x1, %eax
je 0x1b391
movq -0x68(%rbp), %rdi
leaq 0xb12f(%rip), %rsi      # 0x26466
callq 0x1130
cmpl $0x0, %eax
je 0x1b38f
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x1b38d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x68(%rbp), %rsi
callq 0x1130
cmpl $0x0, %eax
jne 0x1b380
movq -0x60(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0x1b382
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b348
jmp 0x1b38f
jmp 0x1b311
movq -0x48(%rbp), %rdi
callq 0x1090
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x2710, %edx           # imm = 0x2710
callq 0x1a410
movl %eax, -0x40(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b4cd
movq -0x30(%rbp), %rdi
leaq 0xb055(%rip), %rsi      # 0x26417
callq 0x10c0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x1b3ed
movq -0x30(%rbp), %rsi
leaq 0xa582(%rip), %rdi      # 0x2595e
movb $0x0, %al
callq 0x1030
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x10f0
movq -0x48(%rbp), %rdi
leaq 0xb890(%rip), %rsi      # 0x26c88
leaq -0x4c(%rbp), %rdx
leaq -0x50(%rbp), %rcx
movb $0x0, %al
callq 0x1190
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
shll %ecx
subl $0x3, %ecx
movq -0x10(%rbp), %rdx
subl 0x48(%rdx), %ecx
cmpl %ecx, %eax
jg 0x1b4c4
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x54(%rbp), %ecx
shll %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x4c(%rbp), %rcx
cmpq %rcx, %rax
jne 0x1b46b
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x54(%rbp), %ecx
shll %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x50(%rbp), %rcx
cmpq %rcx, %rax
je 0x1b4ab
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x54(%rbp), %ecx
shll %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x50(%rbp), %rcx
cmpq %rcx, %rax
jne 0x1b4b4
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x54(%rbp), %ecx
shll %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x4c(%rbp), %rcx
cmpq %rcx, %rax
jne 0x1b4b4
movl -0x54(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl %eax, -0x40(%rbp)
jmp 0x1b4b6
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b40e
movq -0x48(%rbp), %rdi
callq 0x1090
jmp 0x1b564
movq -0x28(%rbp), %rdi
leaq 0xaf8c(%rip), %rsi      # 0x26469
callq 0x1130
cmpl $0x0, %eax
jne 0x1b51e
movl -0x3c(%rbp), %esi
leaq 0xaf85(%rip), %rdi      # 0x26476
movb $0x0, %al
callq 0x1030
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1af60
movl %eax, -0x3c(%rbp)
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %esi
leaq 0xaf61(%rip), %rdi      # 0x26476
movb $0x0, %al
callq 0x1030
jmp 0x1b562
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %edx
movq -0x10(%rbp), %rax
movl 0x48(%rax), %ecx
callq 0x13e90
movl %eax, -0x3c(%rbp)
movl %eax, -0x40(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x1b560
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1af60
movl %eax, -0x3c(%rbp)
movl %eax, -0x40(%rbp)
jmp 0x1b562
jmp 0x1b564
jmp 0x1b566
cmpq $0x0, 0x10(%rbp)
je 0x1b674
movq 0x10(%rbp), %rax
cmpl $-0x1, (%rax)
jne 0x1b62e
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x1b622
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x3c(%rbp), %edx
shll %edx
subl $0x1, %edx
movslq %edx, %rdx
movq (%rcx,%rdx,8), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x3c(%rbp), %edx
shll %edx
subl $0x2, %edx
movslq %edx, %rdx
movq (%rcx,%rdx,8), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1b603
movq 0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x1b612
movq 0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0x1b614
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b585
movq 0x10(%rbp), %rax
movl $0x1, (%rax)
jmp 0x1b65a
movq -0x28(%rbp), %rdi
leaq 0xae48(%rip), %rsi      # 0x26481
callq 0x1130
cmpl $0x0, %eax
jne 0x1b658
movq 0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x2710, %edx           # imm = 0x2710
callq 0x1a410
movl %eax, -0x40(%rbp)
jmp 0x1b65a
cmpl $-0x1, -0x40(%rbp)
jne 0x1b66c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b78b
movl -0x40(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x1b674
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %esi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %edx
callq 0xd0c0
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x10(%rbp), %rdx
callq 0xd130
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0xbc50
movq -0x10(%rbp), %rax
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
shll %ecx
subl $0x2, %ecx
movq -0x10(%rbp), %rdx
subl 0x48(%rdx), %ecx
cmpl %ecx, %eax
jg 0x1b784
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x54(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl $0x1, -0x58(%rbp)
movl -0x58(%rbp), %eax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
shll %ecx
subl $0x2, %ecx
movq -0x10(%rbp), %rdx
subl 0x48(%rdx), %ecx
cmpl %ecx, %eax
jg 0x1b774
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x58(%rbp), %rcx
movsd 0x98dd(%rip), %xmm0     # 0x25028
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x1b767
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x1b769
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x1b716
jmp 0x1b776
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b6df
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)