// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %r8, -0x80(%rbp)
movl %ecx, %r14d
movq %rdx, -0x70(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, %rbx
movl $0x2710, %edi           # imm = 0x2710
callq 0x1130
movq %rax, -0x48(%rbp)
movq %r14, -0x58(%rbp)
leal (%r14,%r14), %eax
movslq %eax, %r12
leaq (,%r12,4), %r15
movq %r15, %rdi
callq 0x1130
movq %rax, -0x38(%rbp)
movq %r15, %rdi
callq 0x1130
movq %rax, %r13
movq %r15, %rdi
callq 0x1130
movq %rax, %r14
leaq (,%r12,8), %rdi
callq 0x1130
movq %rax, -0x40(%rbp)
movq %r15, %rdi
callq 0x1130
movq %rax, -0x30(%rbp)
movq %r15, %rdi
callq 0x1130
movq %rbx, -0x78(%rbp)
testq %rbx, %rbx
je 0x6c2a
cmpq $0x0, -0x60(%rbp)
je 0x6c2a
cmpq $0x0, -0x30(%rbp)
je 0x6c2a
cmpq $0x0, -0x40(%rbp)
je 0x6c2a
movq %r13, %r9
testq %r13, %r13
je 0x6c2a
cmpq $0x0, -0x38(%rbp)
je 0x6c2a
cmpq $0x0, -0x48(%rbp)
je 0x6c2a
testq %r14, %r14
je 0x6c2a
movq %rax, %r15
testq %rax, %rax
je 0x6c2a
movq -0x58(%rbp), %rdx
leal -0x3(,%rdx,2), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovgl %eax, %ecx
movslq %edx, %rdx
movq %rcx, %rsi
negq %rsi
pushq $0x1
popq %rdi
leaq (%rsi,%rdi), %r8
cmpq $0x1, %r8
je 0x6ac3
xorl %r8d, %r8d
cmpq %rdx, %rdi
setg %r8b
leal 0x1(,%r8,2), %r8d
movl %r8d, (%r14,%rdi,4)
incq %rdi
jmp 0x6a9e
leal -0x2(%r12), %r13d
movl $0x3, -0x8(%r14,%r12,4)
addq $-0x2, %r12
cltq
movq %rax, -0x58(%rbp)
addq $0x8, -0x60(%rbp)
movl %ecx, %edx
negq %rdx
xorl %esi, %esi
movl %r13d, -0x64(%rbp)
movq -0x38(%rbp), %rbx
movq %r9, -0x50(%rbp)
cmpl $0xf4240, %esi          # imm = 0xF4240
je 0x6df2
incl %esi
pushq $0x1
popq %rax
cmpq %r12, %rax
jg 0x6b13
andl $0x0, (%r15,%rax,4)
incq %rax
jmp 0x6b04
movq -0x60(%rbp), %r10
pushq $0x1
popq %r9
leaq (%rdx,%r9), %rax
cmpq $0x1, %rax
je 0x6c07
movq -0x8(%r10), %r11
movq (%r10), %rax
movl (%r14,%r11,4), %edi
movl (%r14,%rax,4), %ecx
cmpl $0x1, %edi
jne 0x6b6b
cmpl $0x2, %ecx
jl 0x6bbf
cmpl $0x0, (%r15,%rax,4)
jne 0x6bd2
cmpl $0x0, (%r15,%r11,4)
jne 0x6bd2
leaq (%r15,%rax,4), %rbx
leaq (%r15,%r11,4), %rdi
movq -0x30(%rbp), %r8
movl %eax, (%r8,%r11,4)
decl %ecx
movq %r11, %r8
jmp 0x6b95
jle 0x6bd2
cmpl $0x1, %ecx
jne 0x6bd2
cmpl $0x0, (%r15,%rax,4)
jne 0x6bd2
cmpl $0x0, (%r15,%r11,4)
jne 0x6bd2
leaq (%r15,%rax,4), %rbx
leaq (%r15,%r11,4), %rdi
movq -0x30(%rbp), %rcx
movl %r11d, (%rcx,%rax,4)
xorl %ecx, %ecx
movq %rax, %r8
movl %ecx, (%r14,%rax,4)
decl (%r14,%r11,4)
pushq $0x1
popq %rax
movl %eax, (%rbx)
movl %eax, (%rdi)
movq -0x78(%rbp), %rax
movsd -0x8(%rax,%r9,8), %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, (%rax,%r8,8)
movq -0x38(%rbp), %rbx
jmp 0x6bd2
cmpl $0x1, %ecx
jne 0x6bd2
cmpl $0x0, (%r15,%r11,4)
jne 0x6bd2
cmpl $0x0, (%r15,%rax,4)
je 0x6bde
incq %r9
addq $0x10, %r10
jmp 0x6b1b
andl $0x0, (%r14,%rax,4)
decl (%r14,%r11,4)
movq -0x30(%rbp), %rcx
movl %eax, (%rcx,%r11,4)
movq -0x78(%rbp), %rcx
movsd -0x8(%rcx,%r9,8), %xmm0
movq -0x40(%rbp), %rcx
movsd %xmm0, (%rcx,%r11,8)
movl %eax, %r13d
cmpq -0x58(%rbp), %r9
movq -0x50(%rbp), %r9
jg 0x6af3
movl -0x64(%rbp), %eax
testl %eax, %eax
jle 0x6c46
andl $0x0, (%r9,%rax,4)
andl $0x0, (%rbx,%rax,4)
decq %rax
jmp 0x6c18
leaq 0xdbfc(%rip), %rdi      # 0x1482d
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030
movslq %r13d, %rax
movq -0x30(%rbp), %rcx
andl $0x0, (%rcx,%rax,4)
movl %eax, %eax
pushq $0x1
popq %rcx
cmpq %r12, %rcx
jg 0x6c8b
cmpq %rax, %rcx
je 0x6c86
movq -0x30(%rbp), %rdx
movslq (%rdx,%rcx,4), %rsi
movl (%rbx,%rsi,4), %edx
testl %edx, %edx
je 0x6c80
movslq %edx, %rsi
movl (%r9,%rsi,4), %edx
testl %edx, %edx
jg 0x6c6f
leaq (%r9,%rsi,4), %rdx
jmp 0x6c84
leaq (%rbx,%rsi,4), %rdx
movl %ecx, (%rdx)
incq %rcx
jmp 0x6c56
movq -0x48(%rbp), %rax
movb $0x0, (%rax)
movslq %r13d, %r12
movl (%rbx,%r12,4), %r13d
testl %r13d, %r13d
jle 0x6cc2
movq -0x48(%rbp), %rdi
leaq 0xdba2(%rip), %rsi      # 0x1484b
movq %rdi, %rdx
xorl %eax, %eax
callq 0x1040
movq -0x50(%rbp), %r9
movl %r13d, %eax
negl %eax
movl %eax, (%rbx,%r12,4)
jmp 0x6c92
movl (%r9,%r12,4), %ebx
testl %ebx, %ebx
je 0x6ce6
testl %r13d, %r13d
je 0x6d18
movq -0x40(%rbp), %rax
movsd (%rax,%r12,8), %xmm0
movq -0x48(%rbp), %rdi
leaq 0xdb6d(%rip), %rsi      # 0x14851
jmp 0x6d0c
movq -0x30(%rbp), %rax
movl (%rax,%r12,4), %ebx
testl %ebx, %ebx
je 0x6d6e
testl %r13d, %r13d
je 0x6d37
movq -0x40(%rbp), %rax
movsd (%rax,%r12,8), %xmm0
movq -0x48(%rbp), %rdi
leaq 0xdb50(%rip), %rsi      # 0x1485c
movq %rdi, %rdx
movb $0x1, %al
callq 0x1040
jmp 0x6d5e
movq -0x70(%rbp), %rax
movq (%rax,%r12,8), %rcx
movq -0x40(%rbp), %rax
movsd (%rax,%r12,8), %xmm0
movq -0x48(%rbp), %rdi
leaq 0xdb1a(%rip), %rsi      # 0x1484f
jmp 0x6d54
movq -0x70(%rbp), %rax
movq (%rax,%r12,8), %rcx
movq -0x40(%rbp), %rax
movsd (%rax,%r12,8), %xmm0
movq -0x48(%rbp), %rdi
leaq 0xdb06(%rip), %rsi      # 0x1485a
movq %rdi, %rdx
movb $0x1, %al
callq 0x1040
movl %ebx, %r13d
movq -0x38(%rbp), %rbx
movq -0x50(%rbp), %r9
jmp 0x6c92
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
movq %r9, %r12
callq 0x1050
movw $0x3b, (%rbx,%rax)
leaq 0xda0b(%rip), %rsi      # 0x14795
movq -0x80(%rbp), %rdi
callq 0x10d0
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
callq 0x10b0
movq %r13, %rdi
callq 0x1080
movq -0x38(%rbp), %rdi
callq 0x10f0
movq %r12, %rdi
callq 0x10f0
movq -0x30(%rbp), %rdi
callq 0x10f0
movq -0x40(%rbp), %rdi
callq 0x10f0
movq %r14, %rdi
callq 0x10f0
movq %r15, %rdi
callq 0x10f0
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f0
pushq $0x1
popq %rdi
callq 0x1100