// File: /workspace/github_cpp_makefile_star2_O3/dcarrey[P]hgt-detection/./fonctions.cpp
// Function: SAVEASNewick(double*, long*, char**, int, char const*)
void SAVEASNewick(double *LONGUEUR, long int *ARETE, char ** noms, int nn,const char* t) 
{
	int n,root,a;
	int Ns;
	int i, j, sd, sf, *Suc, *Fre, *Tree, *degre, *Mark;
	double *Long;
	int *boot; 
	char *string = (char*)malloc(10000);	
	n = nn;
	Ns=2*n-3;
	
	double * bootStrap= NULL;
	
	Suc =(int*) malloc((2*n) * sizeof(int));
	Fre =(int*) malloc((2*n) * sizeof(int));
	degre =(int*) malloc((2*n) * sizeof(int));
	Long = (double*) malloc((2*n) * sizeof(double));	
	boot = (int*) malloc((2*n) * sizeof(int));
	Tree = (int*) malloc((2*n) * sizeof(int));
	Mark =(int*) malloc((2*n) * sizeof(int));
	
	if ((degre==NULL)||(Mark==NULL)||(string==NULL)||(Suc==NULL)||(Fre==NULL)||(Long==NULL)||(Tree==NULL)||(ARETE==NULL)||(LONGUEUR==NULL))	
		{ printf("Tree is too large to be saved"); return;} 
	
	for (i=1;i<=2*n-3;i++)
	{ 
		
		if (i<=n) degre[i]=1;
		else degre[i]=3;
	} degre[2*n-2]=3;
	
	root=Ns+1;
	
	int cpt=0;
	
	for (;;)
	{
		cpt++;
		if(cpt > 1000000) exit(1);
		a=0; a++;
		for (j=1;j<=2*n-2;j++)
			Mark[j]=0;
		
		for (i=1;i<=2*n-3;i++)
		{ 	  									
			if ((degre[ARETE[2*i-2]]==1)&&(degre[ARETE[2*i-1]]>1)&&(Mark[ARETE[2*i-1]]==0)&&(Mark[ARETE[2*i-2]]==0))
			{
				Tree[ARETE[2*i-2]]=ARETE[2*i-1]; degre[ARETE[2*i-1]]--; degre[ARETE[2*i-2]]--; Mark[ARETE[2*i-1]]=1; Mark[ARETE[2*i-2]]=1;
				Long[ARETE[2*i-2]]=LONGUEUR[i-1];
				if(bootStrap != NULL) boot[ARETE[2*i-2]] = (int) bootStrap[i-1];
				
			}
			else if ((degre[ARETE[2*i-1]]==1)&&(degre[ARETE[2*i-2]]>1)&&(Mark[ARETE[2*i-1]]==0)&&(Mark[ARETE[2*i-2]]==0))
			{
				Tree[ARETE[2*i-1]]=ARETE[2*i-2]; degre[ARETE[2*i-1]]--; degre[ARETE[2*i-2]]--; Mark[ARETE[2*i-1]]=1; Mark[ARETE[2*i-2]]=1;
				Long[ARETE[2*i-1]]=LONGUEUR[i-1];
				if(bootStrap != NULL) boot[ARETE[2*i-1]] = (int) bootStrap[i-1];
				
			}
			else if ((degre[ARETE[2*i-2]]==1)&&(degre[ARETE[2*i-1]]==1)&&(Mark[ARETE[2*i-2]]==0)&&(Mark[ARETE[2*i-1]]==0))
			{
				Tree[ARETE[2*i-2]]=ARETE[2*i-1]; root=ARETE[2*i-1]; degre[ARETE[2*i-1]]--; degre[ARETE[2*i-2]]--; a=-1;
				Long[ARETE[2*i-2]]=LONGUEUR[i-1];
				if(bootStrap != NULL) boot[ARETE[2*i-2]] = (int) bootStrap[i-1];
			}
			if (a==-1) break;
		}
		if (a==-1) break;
	}
	
	
	/*  On decale et on complete la structure d'arbre avec Successeurs et Freres  */
	for (i=Ns+1;i>0;i--)
	{ 	Fre[i]=0; Suc[i]=0;
		//Tree[i]=Tree[i-1]+1; Long[i]=Long[i-1];
	}	Tree[root]=0;/*Tree[Ns+1]=0;*/
	
	for (i=1;i<=Ns+1/*Ns*/;i++)
	{	
		if (i!=root) 
		{
			sd=i; sf=Tree[i];
			if (Suc[sf]==0) Suc[sf]=sd;
			else {	
				sf=Suc[sf];
				while (Fre[sf]>0) sf=Fre[sf];
				Fre[sf]=sd;
			}		 
		}
	}
	
	
	/* On compose la chaine parenthesee */
	string[0]=0; i=root;/*i=Ns+1;*/
	cpt=0;
	for (;;)
	{	
		if(cpt > 1000000) exit(1);
		
		if (Suc[i]>0)
		{	sprintf(string,"%s(",string);
		Suc[i]=-Suc[i]; i=-Suc[i]; }
		else if (Fre[i]!=0)
		{	if (Suc[i]==0) sprintf(string,"%s%s:%.4f,",string,noms[i],Long[i]);
			else {
				if(bootStrap != NULL)
					sprintf(string,"%s%d:%.4f,",string,boot[i],Long[i]);
				else
					sprintf(string,"%s:%.4f,",string,Long[i]);
			}
		i=Fre[i]; }
		else if (Tree[i]!=0)
		{	if (Suc[i]==0) sprintf(string,"%s%s:%.4f)",string,noms[i],Long[i]);
		else {
			if(bootStrap != NULL)
				sprintf(string,"%s%d:%.4f)",string,boot[i],Long[i]);
			else
				sprintf(string,"%s:%.4f)",string,Long[i]);
			}
		i=Tree[i]; }
		else break;
	}	
	strcat(string,";");
	
	FILE *pt_t = fopen(t,"w+");
	fprintf(pt_t,"%s",string);
	fclose(pt_t);
	
	free(Suc); free(Fre); free(Tree); free(Long); free(degre); free(Mark);	free(string);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, -0x98(%rbp)
movl %ecx, %r14d
movq %rdx, -0x70(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, %r12
movl $0x2710, %edi           # imm = 0x2710
callq 0x1140
movq %rax, -0x68(%rbp)
movq %r14, -0x80(%rbp)
leal (%r14,%r14), %eax
movq %rax, -0x58(%rbp)
movslq %eax, %rbx
leaq (,%rbx,4), %r15
movq %r15, %rdi
callq 0x1140
movq %rax, %r13
movq %r15, %rdi
callq 0x1140
movq %rax, -0x48(%rbp)
movq %r15, %rdi
callq 0x1140
movq %rax, %r14
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x1140
movq %rax, -0x38(%rbp)
movq %r15, %rdi
callq 0x1140
movq %rax, -0x30(%rbp)
movq %r15, %rdi
movq %r13, %r15
callq 0x1140
movq -0x68(%rbp), %rcx
movq %r12, -0x88(%rbp)
testq %r12, %r12
je 0x751d
cmpq $0x0, -0x60(%rbp)
je 0x751d
cmpq $0x0, -0x30(%rbp)
je 0x751d
cmpq $0x0, -0x38(%rbp)
je 0x751d
cmpq $0x0, -0x48(%rbp)
je 0x751d
testq %r15, %r15
je 0x751d
testq %rcx, %rcx
je 0x751d
testq %r14, %r14
je 0x751d
movq %rax, %r10
testq %rax, %rax
je 0x751d
movq -0x80(%rbp), %r11
leal -0x3(,%r11,2), %eax
cmpl $0x1, %r11d
jg 0x753c
cmpl $0x2, %eax
movl $0x1, %ecx
cmovgel %eax, %ecx
incl %ecx
jmp 0x757b
leaq 0xe309(%rip), %rdi      # 0x1582d
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030
cmpl $0x2, %eax
movl $0x1, %edx
cmovgel %eax, %edx
movl %r11d, %esi
leal 0x1(%rdx), %ecx
negq %rdx
movl $0x1, %edi
xorl %r8d, %r8d
cmpq %rsi, %rdi
seta %r8b
leal 0x1(,%r8,2), %r8d
movl %r8d, (%r14,%rdi,4)
leaq (%rdx,%rdi), %r8
incq %r8
incq %rdi
cmpq $0x1, %r8
jne 0x7555
movq -0x58(%rbp), %rsi
addl $-0x2, %esi
movslq %esi, %rdx
movl $0x3, (%r14,%rdx,4)
leaq 0x4(%r10), %rdi
movq %rdi, -0xa8(%rbp)
cmpl $0x2, %edx
movl $0x1, %edx
cmovgel %esi, %edx
movq %rdx, -0x90(%rbp)
leaq (,%rdx,4), %rdx
movq %rdx, -0xa0(%rbp)
movslq %eax, %r13
movl %ecx, %ebx
addq $0x8, -0x60(%rbp)
xorl %eax, %eax
movq %rsi, -0x58(%rbp)
movl %esi, -0x4c(%rbp)
movq %r15, -0x40(%rbp)
movq %r10, -0x78(%rbp)
cmpl $0xf4240, %eax          # imm = 0xF4240
je 0x7925
incl %eax
cmpl $0x2, %r11d
jl 0x75d4
movl %eax, -0x50(%rbp)
xorl %r15d, %r15d
movq -0xa8(%rbp), %rdi
xorl %esi, %esi
movq -0xa0(%rbp), %rdx
callq 0x1060
movq -0x80(%rbp), %r11
movq -0x78(%rbp), %r10
movl $0x1, %edx
movq -0x60(%rbp), %rcx
movq %rdx, %rax
movq -0x8(%rcx), %rdx
movq (%rcx), %r12
movl (%r14,%rdx,4), %edi
movl (%r14,%r12,4), %esi
cmpl $0x1, %edi
jne 0x7658
cmpl $0x2, %esi
jl 0x76ad
cmpl $0x0, (%r10,%r12,4)
jne 0x76c0
cmpl $0x0, (%r10,%rdx,4)
jne 0x76c0
leaq (%r10,%r12,4), %rdi
leaq (%r10,%rdx,4), %r8
movq -0x30(%rbp), %r9
movl %r12d, (%r9,%rdx,4)
decl %esi
movq %rdx, %r9
jmp 0x7682
jle 0x76c0
cmpl $0x1, %esi
jne 0x76c0
cmpl $0x0, (%r10,%r12,4)
jne 0x76c0
cmpl $0x0, (%r10,%rdx,4)
jne 0x76c0
leaq (%r10,%r12,4), %rdi
leaq (%r10,%rdx,4), %r8
movq -0x30(%rbp), %rsi
movl %edx, (%rsi,%r12,4)
xorl %esi, %esi
movq %r12, %r9
movl %esi, (%r14,%r12,4)
decl (%r14,%rdx,4)
movl $0x1, %edx
movl %edx, (%rdi)
movl %edx, (%r8)
movq -0x88(%rbp), %rdx
movsd -0x8(%rdx,%rax,8), %xmm0
movq -0x38(%rbp), %rdx
movsd %xmm0, (%rdx,%r9,8)
jmp 0x76c0
cmpl $0x1, %esi
jne 0x76c0
cmpl $0x0, (%r10,%rdx,4)
jne 0x76c0
cmpl $0x0, (%r10,%r12,4)
je 0x76ea
leaq 0x1(%rax), %rdx
cmpq %r13, %rax
setge %r15b
addq $0x10, %rcx
cmpq %rdx, %rbx
jne 0x7613
cmpq %r13, %rax
movq -0x40(%rbp), %r15
movl -0x50(%rbp), %eax
jge 0x75d4
jmp 0x772b
movq -0x30(%rbp), %rcx
movl %r12d, (%rcx,%rdx,4)
movl $0x0, (%r14,%r12,4)
decl (%r14,%rdx,4)
movq -0x88(%rbp), %rcx
movsd -0x8(%rcx,%rax,8), %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax,%rdx,8)
movl %r12d, -0x4c(%rbp)
testb $0x1, %r15b
movq -0x40(%rbp), %r15
movl -0x50(%rbp), %eax
jne 0x75d4
jmp 0x7731
movl -0x4c(%rbp), %eax
movl %eax, %r12d
movq -0x58(%rbp), %rcx
movl %ecx, %ebx
testl %ecx, %ecx
movl $0x1, %eax
cmovlel %ecx, %eax
subl %eax, %ecx
subq %rcx, %rbx
movq -0x48(%rbp), %rax
leaq (%rax,%rbx,4), %rdi
leaq 0x4(,%rcx,4), %r13
xorl %esi, %esi
movq %r13, %rdx
callq 0x1060
leaq (%r15,%rbx,4), %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x1060
movslq %r12d, %rax
movq -0x30(%rbp), %rcx
movl $0x0, (%rcx,%rax,4)
movl %eax, %eax
movq -0x90(%rbp), %r8
incl %r8d
movl $0x1, %ecx
movq -0x68(%rbp), %r13
movq -0x48(%rbp), %rdi
cmpq %rax, %rcx
je 0x77c0
movq -0x30(%rbp), %rdx
movslq (%rdx,%rcx,4), %rsi
movl (%r15,%rsi,4), %edx
testl %edx, %edx
je 0x77ba
movslq %edx, %rsi
movl (%rdi,%rsi,4), %edx
testl %edx, %edx
jg 0x77aa
leaq (%rdi,%rsi,4), %rdx
jmp 0x77be
leaq (%r15,%rsi,4), %rdx
movl %ecx, (%rdx)
incq %rcx
cmpq %r8, %rcx
jne 0x7795
movb $0x0, (%r13)
movslq %r12d, %rbx
movl (%r15,%rbx,4), %r12d
testl %r12d, %r12d
jle 0x77f8
movq %r13, %rdi
leaq 0xe068(%rip), %rsi      # 0x1584b
movq %r13, %rdx
xorl %eax, %eax
callq 0x1040
movl %r12d, %eax
negl %eax
movl %eax, (%r15,%rbx,4)
jmp 0x77cd
movq -0x48(%rbp), %rax
movl (%rax,%rbx,4), %r15d
testl %r15d, %r15d
je 0x781f
testl %r12d, %r12d
je 0x7850
movq -0x38(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r13, %rdi
leaq 0xe034(%rip), %rsi      # 0x15851
jmp 0x7844
movq -0x30(%rbp), %rax
movl (%rax,%rbx,4), %r15d
testl %r15d, %r15d
je 0x789e
testl %r12d, %r12d
je 0x786d
movq -0x38(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r13, %rdi
leaq 0xe018(%rip), %rsi      # 0x1585c
movq %r13, %rdx
movb $0x1, %al
callq 0x1040
jmp 0x7892
movq -0x70(%rbp), %rax
movq (%rax,%rbx,8), %rcx
movq -0x38(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r13, %rdi
leaq 0xdfe4(%rip), %rsi      # 0x1584f
jmp 0x7888
movq -0x70(%rbp), %rax
movq (%rax,%rbx,8), %rcx
movq -0x38(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r13, %rdi
leaq 0xdfd2(%rip), %rsi      # 0x1585a
movq %r13, %rdx
movb $0x1, %al
callq 0x1040
movl %r15d, %r12d
movq -0x40(%rbp), %r15
jmp 0x77cd
movq %r13, %rdi
callq 0x1050
movw $0x3b, (%r13,%rax)
leaq 0xdee0(%rip), %rsi      # 0x15795
movq -0x98(%rbp), %rdi
callq 0x10e0
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
callq 0x10c0
movq %rbx, %rdi
callq 0x10a0
movq -0x40(%rbp), %rdi
callq 0x1100
movq -0x48(%rbp), %rdi
callq 0x1100
movq -0x30(%rbp), %rdi
callq 0x1100
movq -0x38(%rbp), %rdi
callq 0x1100
movq %r14, %rdi
callq 0x1100
movq -0x78(%rbp), %rdi
callq 0x1100
movq %r13, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1100
movl $0x1, %edi
callq 0x1110