// File: /workspace/github_cpp_makefile_star2_O1/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, -0x90(%rbp)
movl %ecx, %ebx
movq %rdx, -0x80(%rbp)
movq %rsi, -0x68(%rbp)
movq %rdi, %r12
movl $0x2710, %edi           # imm = 0x2710
callq 0x1140
movq %rax, -0x70(%rbp)
movq %rbx, -0x40(%rbp)
leal (%rbx,%rbx), %eax
movq %rax, -0x88(%rbp)
movslq %eax, %rbx
leaq (,%rbx,4), %r15
movq %r15, %rdi
callq 0x1140
movq %rax, %r13
movq %r15, %rdi
callq 0x1140
movq %rax, -0x58(%rbp)
movq %r15, %rdi
callq 0x1140
movq %rax, %r14
leaq (,%rbx,8), %rdi
callq 0x1140
movq %rax, -0x48(%rbp)
movq %r15, %rdi
callq 0x1140
movq %rax, -0x38(%rbp)
movq %r15, %rdi
callq 0x1140
movq -0x70(%rbp), %rcx
movq %r12, -0xa8(%rbp)
testq %r12, %r12
je 0x79ab
cmpq $0x0, -0x68(%rbp)
je 0x79ab
cmpq $0x0, -0x38(%rbp)
je 0x79ab
cmpq $0x0, -0x48(%rbp)
je 0x79ab
cmpq $0x0, -0x58(%rbp)
je 0x79ab
testq %r13, %r13
je 0x79ab
testq %rcx, %rcx
je 0x79ab
testq %r14, %r14
je 0x79ab
testq %rax, %rax
je 0x79ab
movq %r13, -0x50(%rbp)
movq -0x40(%rbp), %rcx
leal -0x3(,%rcx,2), %r12d
cmpl $0x2, %ecx
jl 0x7651
movl -0x40(%rbp), %edi
cmpl $0x2, %r12d
movl $0x1, %ecx
cmovgel %r12d, %ecx
negq %rcx
movl $0x1, %edx
xorl %esi, %esi
cmpq %rdi, %rdx
seta %sil
leal 0x1(,%rsi,2), %esi
movl %esi, (%r14,%rdx,4)
leaq (%rcx,%rdx), %rsi
incq %rsi
incq %rdx
cmpq $0x1, %rsi
jne 0x762d
leal -0x2(%rbx), %r13d
movl $0x3, -0x8(%r14,%rbx,4)
cmpl $0x2, -0x40(%rbp)
setl -0x29(%rbp)
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, -0xa0(%rbp)
cmpl $0x2, %r13d
movl $0x1, %ecx
movl $0x1, %edx
cmovgel %r13d, %edx
shlq $0x2, %rdx
movq %rdx, -0x98(%rbp)
movslq %r12d, %rbx
cmpl $0x2, %r12d
cmovll %ecx, %r12d
addq $0x8, -0x68(%rbp)
negq %r12
xorl %r15d, %r15d
movl %r13d, -0x5c(%rbp)
movq %rax, -0x78(%rbp)
cmpl $0xf4240, %r15d         # imm = 0xF4240
je 0x7a50
cmpl $0x2, -0x40(%rbp)
jl 0x76db
movq -0xa0(%rbp), %rdi
xorl %esi, %esi
movq -0x98(%rbp), %rdx
callq 0x1060
movq -0x78(%rbp), %rax
cmpl $0x2, -0x40(%rbp)
jge 0x76ea
movb -0x29(%rbp), %r11b
jmp 0x780c
movl $0x1, %esi
movl $0x1, %edx
movq -0x68(%rbp), %rcx
movb -0x29(%rbp), %r11b
movq -0x8(%rcx), %rdi
movl (%r14,%rdi,4), %r10d
cmpl $0x1, %r10d
jne 0x7728
movq (%rcx), %r8
movl (%r14,%r8,4), %r9d
cmpl $0x2, %r9d
jl 0x7728
cmpl $0x0, (%rax,%r8,4)
jne 0x7728
cmpl $0x0, (%rax,%rdi,4)
je 0x77b0
movq (%rcx), %r8
movl (%r14,%r8,4), %r9d
cmpl $0x2, %r10d
jl 0x7748
cmpl $0x1, %r9d
jne 0x7748
cmpl $0x0, (%rax,%r8,4)
jne 0x7748
cmpl $0x0, (%rax,%rdi,4)
je 0x778a
cmpl $0x1, %r10d
jne 0x77e9
cmpl $0x1, %r9d
jne 0x77e9
cmpl $0x0, (%rax,%rdi,4)
jne 0x77e9
cmpl $0x0, (%rax,%r8,4)
jne 0x77e9
decl %r9d
movl %r9d, (%r14,%r8,4)
decl (%r14,%rdi,4)
movq -0x38(%rbp), %rsi
movl %r8d, (%rsi,%rdi,4)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %r8d, %r13d
jmp 0x77d1
decl %r9d
movl %r9d, (%r14,%r8,4)
decl (%r14,%rdi,4)
movq -0x38(%rbp), %r9
movl %edi, (%r9,%r8,4)
movl $0x1, %r9d
movl %r9d, (%rax,%r8,4)
movl %r9d, (%rax,%rdi,4)
movq %r8, %rdi
jmp 0x77d1
decl %r9d
movl %r9d, (%r14,%r8,4)
decl (%r14,%rdi,4)
movq -0x38(%rbp), %r9
movl %r8d, (%r9,%rdi,4)
movl $0x1, %r9d
movl %r9d, (%rax,%r8,4)
movl %r9d, (%rax,%rdi,4)
movq -0xa8(%rbp), %r8
movsd -0x8(%r8,%rdx,8), %xmm0
movq -0x48(%rbp), %r8
movsd %xmm0, (%r8,%rdi,8)
cmpl $-0x1, %esi
je 0x780c
cmpq %rbx, %rdx
leaq 0x1(%r12,%rdx), %rdi
leaq 0x1(%rdx), %rdx
setge %r11b
addq $0x10, %rcx
cmpq $0x1, %rdi
jne 0x76fc
incl %r15d
testb $0x1, %r11b
jne 0x76af
cmpl $0x2, -0x40(%rbp)
movq -0x50(%rbp), %r15
jl 0x7869
movl -0x5c(%rbp), %ecx
movl %ecx, %ebx
testl %ecx, %ecx
movl $0x1, %eax
cmovlel %ecx, %eax
movq -0x88(%rbp), %rcx
subl %eax, %ecx
addl $-0x2, %ecx
subq %rcx, %rbx
movq -0x58(%rbp), %rax
leaq (%rax,%rbx,4), %rdi
leaq 0x4(,%rcx,4), %r12
xorl %esi, %esi
movq %r12, %rdx
callq 0x1060
leaq (%r15,%rbx,4), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1060
movslq %r13d, %rax
movq -0x38(%rbp), %rcx
movl $0x0, (%rcx,%rax,4)
cmpl $0x2, -0x40(%rbp)
movq -0x70(%rbp), %r12
movq -0x58(%rbp), %r8
jl 0x78d1
movl %r13d, %eax
movl -0x5c(%rbp), %edx
cmpl $0x2, %edx
movl $0x1, %ecx
cmovgel %edx, %ecx
incl %ecx
movl $0x1, %edx
cmpq %rax, %rdx
je 0x78c9
movq -0x38(%rbp), %rsi
movslq (%rsi,%rdx,4), %rdi
movl (%r15,%rdi,4), %esi
testl %esi, %esi
je 0x78c3
movslq %esi, %rdi
movl (%r8,%rdi,4), %esi
testl %esi, %esi
jg 0x78b2
leaq (%r8,%rdi,4), %rsi
jmp 0x78c7
leaq (%r15,%rdi,4), %rsi
movl %edx, (%rsi)
incq %rdx
cmpq %rcx, %rdx
jne 0x789d
movb $0x0, (%r12)
movslq %r13d, %rbx
movl (%r15,%rbx,4), %r13d
testl %r13d, %r13d
jle 0x7901
movq %r12, %rdi
leaq 0xdf5f(%rip), %rsi      # 0x1584b
movq %r12, %rdx
xorl %eax, %eax
callq 0x1040
movl %r13d, %eax
negl %eax
movl %eax, (%r15,%rbx,4)
jmp 0x78d6
movq -0x58(%rbp), %rax
movl (%rax,%rbx,4), %r15d
testl %r15d, %r15d
je 0x7928
testl %r13d, %r13d
je 0x795d
movq -0x48(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r12, %rdi
leaq 0xdf2b(%rip), %rsi      # 0x15851
jmp 0x7951
movq -0x38(%rbp), %rax
movl (%rax,%rbx,4), %r15d
testl %r15d, %r15d
je 0x79ca
testl %r13d, %r13d
je 0x797a
movq -0x48(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r12, %rdi
leaq 0xdf0b(%rip), %rsi      # 0x1585c
movq %r12, %rdx
movb $0x1, %al
callq 0x1040
jmp 0x799f
movq -0x80(%rbp), %rax
movq (%rax,%rbx,8), %rcx
movq -0x48(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r12, %rdi
leaq 0xded7(%rip), %rsi      # 0x1584f
jmp 0x7995
movq -0x80(%rbp), %rax
movq (%rax,%rbx,8), %rcx
movq -0x48(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r12, %rdi
leaq 0xdec5(%rip), %rsi      # 0x1585a
movq %r12, %rdx
movb $0x1, %al
callq 0x1040
movl %r15d, %r13d
movq -0x50(%rbp), %r15
jmp 0x78d6
leaq 0xde7b(%rip), %rdi      # 0x1582d
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030
movq %r12, %rdi
callq 0x1050
movw $0x3b, (%r12,%rax)
leaq 0xddb5(%rip), %rsi      # 0x15795
movq -0x90(%rbp), %rdi
callq 0x10e0
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
callq 0x10c0
movq %r13, %rdi
callq 0x10a0
movq -0x50(%rbp), %rdi
callq 0x1100
movq -0x58(%rbp), %rdi
callq 0x1100
movq -0x38(%rbp), %rdi
callq 0x1100
movq -0x48(%rbp), %rdi
callq 0x1100
movq %r14, %rdi
callq 0x1100
movq -0x78(%rbp), %rdi
callq 0x1100
movq %r12, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1100
movl $0x1, %edi
callq 0x1110