// File: /workspace/github_cpp_makefile_star2_O1/dcarrey[P]hgt-detection/./utils_tree.cpp
// Function: lectureNewick1(char const*, long*, double*, char**)
int lectureNewick1(const char * newick, long int * ARETE, double * LONGUEUR, char ** lesNoms)
{
	int n;                                     

	// Ce sous programme permet de lire un arbre au format newick et de le transcrire dans
	// des vecteurs arete-longueur commencant � 1
	// ATTENTION: les noms commencent � 0
	// 7 octobre 2004
	// Elmaestro

	// TODO: Add your command handler code here
	int i,j,j1,k, a, a1, a2,a3, VertexNumber,numero;
	char symbol, *string, *string1, *string2/* *string3,c ,**Et*/;
	int taxaPos; // le nombre de taxas recup�r�
	int aretePos; // le nombre d'aretes recup�r�
	char symbolOld =' ';
	int zz, xx,jj,ii;
	double longueur;
	char * tempString;
	int cpt=0;
	//long int *ARETE;
	//double *LONGUEUR; 	

	//Correctness of the Newick format verification
	i=0;
	n = 0;

	do
	{
		symbol = newick[cpt++];
		if (symbol==':') i++;
		if (symbol == ':' && symbolOld != ')') n++;
		symbolOld = symbol;
	}  while(symbol != '\0');

	cpt=0;

	if (i == 0) { printf("Incorrect Newick file format. Edge lengths must be indicated after a ':' characters."); exit(FAIL);}

	j=0;
	do{ 
	   symbol=newick[cpt++]; 
	   if (symbol=='(') j++; 
	}while(symbol != '\0');
	
	cpt=0;	
	j1=0;
	
	do{
		symbol=newick[cpt++];
		if (symbol==')') j1++; 
	}while(symbol != '\0');  
	
	cpt=0;

	// verification des arit�s de l'arbre
	if (j1!=j) { printf("Incorrect Newick file format. Number of right parentheses must be equal to number of left parentheses."); exit(FAIL);}
	//else if (j!=n-2) { printf("Incorrect Newick file format. Only trees with vertices of degree 1 and 3 are allowed by T-REX."); fclose (data); exit(FAIL);}

	k=0;

	do{ 
		symbol=newick[cpt++];
		if (symbol==',') k++; 
	}while(symbol != '\0');   
	
	cpt=0;
	//if (k!=(n-1)) { printf("Incorrect Newick file format. Number of objects must be equal to number of commas plus 1."); fclose (data); exit(FAIL);}

	a=0;

	do{ 
		symbol=newick[cpt++];
		if (symbol==';') a++; 
	}while(symbol != '\0');    
	cpt=0;

	if (a==0) { printf("Incorrect Newick file format. Newick string must be followed by a ';' character."); exit(FAIL);}
	else if (a>1) { printf("Incorrect Newick file format. Newick string must contain (in the end) only one ';' character."); exit(FAIL);}

	a=0;
	do{
		symbol=newick[cpt++];a++;
	}while(symbol == ' '); 
	
	cpt=0;

	if (symbol!='(') { printf("Incorrect Newick file format. Newick string must begin with a '(' character."); exit(FAIL);}  

	a=0;
	
	do{ symbol=newick[cpt++];
		if (symbol=='%') a++; 
	}while(symbol != '\0');

	cpt=0;

	if (a>0) { printf("Incorrect Newick file format. Newick string cannot contain \'%%\' character."); exit(FAIL);}

	do
	{
		symbol=newick[cpt++];
		if ((symbol=='(')||(symbol==','))		
		{ 
			symbol=newick[cpt++]; a=0;
			if ((symbol!='(')&&(symbol!=',')&&(symbol!=';')&&(symbol!=':'))		   
			{ 
				cpt--; 
				do{
					symbol=newick[cpt++];a++;
				}while ((symbol!=':')&&(symbol!='\0'));  
			}
			else cpt--;
			if (a>50) { printf("Incorrect Newick file format. Names of objects must not exceed 50 characters.");  exit(FAIL);}   
		}	
	}while(symbol != '\0');
	cpt=0;

	string = (char*)malloc((1000*n) * sizeof(char));
	string2 = (char*)malloc((1000*n) * sizeof(char));
	//string3 = (char*)malloc((1000*n) * sizeof(char));
	string1 = (char*)malloc((2000) * sizeof(char));

	if ((string == NULL)||(string1 == NULL)||(string2 == NULL)/*||(string3 == NULL)*/)
	{ printf("Input data are too large or not a correct Newick file chosen"); exit(FAIL);}

	a=0;

	do{		
		symbol=newick[cpt++];
		if ((symbol!=' ')&&(symbol!='\n')&&(symbol!='\t')) { string[a++]=symbol; } 
	}while(symbol !='\0'); 

	k=0; VertexNumber=n;
	//a1 = 0;
	//a2 = 0;
	taxaPos =1;    // nous allons commencer � mettre les taxas � la position 1
	aretePos = 1;
	while (string[0] == '(')   // traiter toute la chaine
	{
		a1 = 0;
		a2 = 0;
		while( string[a2] != ')')  // traiter la paire () la plus profonde
		{
			if(string[a2] == '(') a1 = a2;  // retrouver ;a parenth�se ouvrante
			a2++;
		}
		

		// a   => contient la longueur de la chaine
		// a1  => contient le debut d'un noeud � traiter
		// a2  => contient la fin d'un noeud � traiter
		// a3  => d�limite le noeud et sa longueur

		zz = a1+1;
		VertexNumber++;  // augmenter le nombre de noeuds
		for ( ii = a1+1; ii <= a2; ii++)
		{// decortiquer cette chaine

			if (string[ii] == ':')
			{
				xx = 0;
				a3 = ii+1;

				if( string[zz] == '%')
				{ // cela veut dire que c'est un  noeud que l'on traite

					for ( jj = zz+1; jj < ii; jj++)
					{
						string1[xx++] = string[jj]; 
					}
					string1[xx++] = '\0';
					numero = atoi(string1);
				}
				else
				{
					// on recup�re le nom du taxa

					for(jj = zz; jj < ii; jj++)
					{
						lesNoms[taxaPos-1][xx++] = string[jj];
					}
					numero = taxaPos;
					lesNoms[taxaPos-1][xx] = '\0';  // mettre la fin de chaine
					taxaPos++;  // augmenter le nombre de taxas pris
				}

			}
			else if(string[ii] == ','  || string[ii] == ')')
			{
				xx = 0;
				zz = ii +1;   // faire pointer sur le prochain noeud
				for ( jj = a3; jj < ii; jj++)
				{
					string1[xx++] = string[jj]; 
				}
				string1[xx++] = '\0';
				longueur = atof(string1);
				ARETE[aretePos++] = VertexNumber;
				ARETE[aretePos++] = numero;
				LONGUEUR[(aretePos-1)/ 2] = longueur;
			}

		}

		// fin for pour traiter noeud
		//transcrire la nouvelle chaine
		xx = 0;
		for ( jj = 0; jj < (int)a1; jj++)
		{string2[xx++] = string[jj];}

		// ecrire le vertex
		//	char buffer[50];
		itoa_(VertexNumber,string1,10);
		string2[xx++] = '%';   // indiquer que c'est un noeud
		for( jj = 0; jj < (int) strlen(string1); jj++)
		{string2[xx++] = string1[jj];}

		// transcrire la fin
		for( jj = a2+1; jj <= a; jj++)  // il faut voir si c'est  <= a ou c < a
		{string2[xx++] = string[jj];}

		// remplacer string par string2 en remplacant les pointeurs
		
		tempString = string;
		string = string2;
		string2 = tempString;
		tempString = 0;
		a = xx;  // mettre la longueur � jour 

	} // fin du while pour traiter toute la string

	for( jj=n;jj>0;jj--)
		strcpy(lesNoms[jj],lesNoms[jj-1]);

	ARETE[aretePos++] = 0;
	ARETE[aretePos++] = 0;


	for(i=1;i<=2*n-3;i++){
		LONGUEUR[i-1] = LONGUEUR[i];
	}
	for(i=1;i<=2*(2*n-3);i++){
		ARETE[i-1] = ARETE[i];
	}


	free(string);
	free(string1);
	free(string2);
	//free(string3);

	return n;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, -0x80(%rbp)
movq %rsi, -0x88(%rbp)
movq %rdi, %rbx
xorl %eax, %eax
movb $0x20, %r9b
xorl %r12d, %r12d
movl %r12d, %r13d
movl %r9d, %r10d
movb (%rdi), %r9b
xorl %edx, %edx
cmpb $0x3a, %r9b
sete %dl
addl %edx, %eax
cmpb $0x29, %r10b
setne %sil
andb %sil, %dl
movzbl %dl, %r12d
addl %r13d, %r12d
incq %rdi
testb %r9b, %r9b
jne 0x5cd1
testl %eax, %eax
je 0x6283
xorl %eax, %eax
movq %rbx, %rdi
movb (%rdi), %dl
xorl %esi, %esi
cmpb $0x28, %dl
sete %sil
addl %esi, %eax
incq %rdi
testb %dl, %dl
jne 0x5d0c
xorl %r8d, %r8d
movq %rbx, %rdx
movb (%rdx), %sil
xorl %edi, %edi
cmpb $0x29, %sil
sete %dil
addl %edi, %r8d
incq %rdx
testb %sil, %sil
jne 0x5d26
cmpl %eax, %r8d
jne 0x628c
xorl %eax, %eax
movq %rbx, %rdi
movb (%rdi), %dl
xorl %esi, %esi
cmpb $0x3b, %dl
sete %sil
addl %esi, %eax
incq %rdi
testb %dl, %dl
jne 0x5d4c
cmpl $0x1, %eax
jne 0x6276
movq %rbx, %rax
movzbl (%rax), %edx
incq %rax
cmpl $0x20, %edx
je 0x5d6c
cmpl $0x28, %edx
jne 0x6255
xorl %eax, %eax
movq %rbx, %rdi
movb (%rdi), %dl
xorl %esi, %esi
cmpb $0x25, %dl
sete %sil
addl %esi, %eax
incq %rdi
testb %dl, %dl
jne 0x5d85
testl %eax, %eax
jne 0x629e
movq %rcx, -0x50(%rbp)
xorl %eax, %eax
movabsq $0xc00110000000000, %rcx # imm = 0xC00110000000000
movslq %eax, %rdx
incl %eax
movb (%rbx,%rdx), %dl
movl %edx, %esi
andb $-0x5, %sil
cmpb $0x28, %sil
jne 0x5de3
movslq %eax, %rsi
movzbl (%rbx,%rsi), %edx
cmpq $0x3b, %rdx
ja 0x5de9
btq %rdx, %rcx
jae 0x5de9
xorl %esi, %esi
testb %sil, %sil
jne 0x625e
testb %dl, %dl
jne 0x5db1
jmp 0x5e0d
addq %rbx, %rsi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movzbl (%rsi), %edx
incl %eax
incl %edi
cmpl $0x3a, %edx
je 0x5e04
incq %rsi
testl %edx, %edx
jne 0x5df1
cmpl $0x32, %edi
setae %sil
jmp 0x5dda
movb %r10b, -0x29(%rbp)
movb %r9b, -0x2a(%rbp)
imull $0x3e8, %r12d, %r14d    # imm = 0x3E8
movq %r14, %rdi
callq 0x1140
movq %rax, -0x38(%rbp)
movq %r14, %rdi
callq 0x1140
movq %rax, %r14
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x1140
testq %r14, %r14
je 0x62a7
cmpq $0x0, -0x38(%rbp)
je 0x62a7
movq %rax, %r15
testq %rax, %rax
je 0x62a7
xorl %edi, %edi
movabsq $0x100000600, %rax      # imm = 0x100000600
movq -0x50(%rbp), %r9
movq -0x38(%rbp), %rsi
movzbl (%rbx), %ecx
cmpq $0x20, %rcx
ja 0x5e80
btq %rcx, %rax
jb 0x5e88
movslq %edi, %rdx
incl %edi
movb %cl, (%rsi,%rdx)
incq %rbx
testb %cl, %cl
jne 0x5e71
movq %r13, -0x98(%rbp)
movl $0x1, %ebx
movq -0x38(%rbp), %rax
cmpb $0x28, (%rax)
movq %r12, -0x60(%rbp)
movl %r12d, %edx
jne 0x6173
movl $0x1, %eax
movq %rax, -0x70(%rbp)
movq %rdx, -0x90(%rbp)
movq %rdx, %rbx
movl $0x1, -0x44(%rbp)
movq -0x38(%rbp), %r12
movl %edi, -0x74(%rbp)
movq %r12, %r13
movq %r14, %rcx
xorl %r14d, %r14d
xorl %edx, %edx
movzbl (%r13,%r14), %eax
cmpl $0x28, %eax
je 0x5eed
cmpl $0x29, %eax
jne 0x5ef0
jmp 0x5ef5
movl %r14d, %edx
incq %r14
jmp 0x5edb
movq %rcx, -0x38(%rbp)
incq %rbx
cmpl %r14d, %edx
movq %rbx, -0x68(%rbp)
movq %rdx, -0x40(%rbp)
jge 0x6079
leaq 0x1(%r14), %rax
movq %rax, -0xa0(%rbp)
leal 0x1(%rdx), %r10d
movslq %edx, %r12
incq %r12
movzbl (%r13,%r12), %eax
cmpl $0x29, %eax
je 0x5f88
cmpl $0x2c, %eax
je 0x5f88
cmpl $0x3a, %eax
jne 0x6001
movslq %r10d, %rax
leal 0x1(%r12), %ecx
movl %ecx, -0x78(%rbp)
cmpb $0x25, (%r13,%rax)
jne 0x6013
incq %rax
movslq %eax, %rcx
cmpq %rcx, %r12
jle 0x603f
leaq (%rax,%r13), %rcx
xorl %edx, %edx
movb (%rcx,%rdx), %sil
movb %sil, (%r15,%rdx)
leaq (%rax,%rdx), %rsi
incq %rsi
incq %rdx
cmpq %r12, %rsi
jl 0x5f67
movl %r10d, %ebx
movl %edx, %eax
jmp 0x6044
movslq -0x78(%rbp), %rax
cmpq %rax, %r12
jle 0x5faf
movl %r12d, %ecx
subl %eax, %ecx
addq %r13, %rax
xorl %edx, %edx
movb (%rax,%rdx), %sil
movb %sil, (%r15,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x5f9b
movl %edx, %eax
jmp 0x5fb1
xorl %eax, %eax
movb $0x0, (%r15,%rax)
movq %r15, %rdi
callq 0x1120
movq -0x70(%rbp), %rdx
movslq %edx, %rax
incl %edx
movq -0x88(%rbp), %rsi
movq %rbx, (%rsi,%rax,8)
movslq -0x54(%rbp), %rcx
movq %rcx, 0x8(%rsi,%rax,8)
leal 0x2(%rax), %ecx
shrl $0x1f, %edx
addl %edx, %eax
incl %eax
sarl %eax
cltq
movq -0x80(%rbp), %rdx
movsd %xmm0, (%rdx,%rax,8)
leal 0x1(%r12), %r10d
movl %ecx, %eax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %r9
incq %r12
cmpl %r12d, -0xa0(%rbp)
jne 0x5f22
jmp 0x6079
movslq -0x44(%rbp), %rcx
cmpq %rax, %r12
jle 0x6061
movl %r12d, %edx
subl %eax, %edx
addq %r13, %rax
xorl %esi, %esi
movb (%rax,%rsi), %dil
movq -0x8(%r9,%rcx,8), %r8
movb %dil, (%r8,%rsi)
incq %rsi
cmpq %rsi, %rdx
jne 0x6026
movl %esi, %eax
jmp 0x6063
movl %r10d, %ebx
xorl %eax, %eax
movb $0x0, (%r15,%rax)
movq %r15, %rdi
callq 0x11a0
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %r9
movl %ebx, %r10d
movq -0x68(%rbp), %rbx
jmp 0x6001
xorl %eax, %eax
movq -0x8(%r9,%rcx,8), %rdx
movb $0x0, (%rdx,%rax)
incl %ecx
movl -0x44(%rbp), %eax
movl %eax, -0x54(%rbp)
movl %ecx, -0x44(%rbp)
jmp 0x6001
movq -0x40(%rbp), %r12
testl %r12d, %r12d
jle 0x6093
movl %r12d, %edx
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x1080
jmp 0x6096
xorl %r12d, %r12d
movl %ebx, %edi
movq %r15, %rsi
movl $0xa, %edx
callq 0x51ca
movl %r12d, %ebx
movq -0x38(%rbp), %rax
movb $0x25, (%rax,%rbx)
movq %r15, %rdi
callq 0x1050
incl %r12d
testl %eax, %eax
jle 0x60f7
movq %r12, -0x40(%rbp)
movq -0x38(%rbp), %rax
addq %rax, %rbx
incq %rbx
xorl %r12d, %r12d
movb (%r15,%r12), %al
movb %al, (%rbx,%r12)
incq %r12
movq %r15, %rdi
callq 0x1050
cltq
cmpq %rax, %r12
jl 0x60d0
movq -0x40(%rbp), %rax
addl %r12d, %eax
movq %rax, -0x40(%rbp)
jmp 0x60fb
movq %r12, -0x40(%rbp)
movq -0x38(%rbp), %r12
movl -0x74(%rbp), %ebx
cmpl %r14d, %ebx
jle 0x614a
movq -0x40(%rbp), %rax
movl %eax, %eax
movq %rax, -0x40(%rbp)
leaq (%r12,%rax), %rdi
leaq (%r14,%r13), %rsi
incq %rsi
movl %r14d, %edx
notl %edx
addl %ebx, %edx
incq %rdx
callq 0x1080
movl %ebx, %eax
subq %rax, %r14
xorl %eax, %eax
decq %rax
cmpq %rax, %r14
jne 0x6132
movq -0x40(%rbp), %rcx
subl %eax, %ecx
movq -0x50(%rbp), %r9
movq -0x68(%rbp), %rbx
jmp 0x6156
movq -0x50(%rbp), %r9
movq -0x68(%rbp), %rbx
movq -0x40(%rbp), %rcx
cmpb $0x28, (%r12)
movq %r13, %r14
movl %ecx, %edi
je 0x5ecd
movslq -0x70(%rbp), %rbx
movq -0x90(%rbp), %rdx
jmp 0x6176
movq %r14, %r13
cmpl $0x0, -0x60(%rbp)
je 0x61c0
xorl %eax, %eax
cmpb $0x29, -0x29(%rbp)
setne %al
xorl %ecx, %ecx
cmpb $0x3a, -0x2a(%rbp)
sete %cl
cmpl %ecx, %eax
cmovbl %eax, %ecx
movq -0x98(%rbp), %rax
leal (%rax,%rcx), %r14d
incl %r14d
leaq (%r9,%rdx,8), %r12
movq -0x8(%r12), %rsi
movq (%r12), %rdi
addq $-0x8, %r12
callq 0x1090
decl %r14d
cmpl $0x1, %r14d
jg 0x61a5
movq -0x60(%rbp), %rcx
leal -0x3(,%rcx,2), %r14d
xorps %xmm0, %xmm0
movq -0x88(%rbp), %rax
movups %xmm0, (%rax,%rbx,8)
movq %rax, %rbx
cmpl $0x2, %ecx
jb 0x6200
movq -0x80(%rbp), %rdi
leaq 0x8(%rdi), %rsi
cmpl $0x2, %r14d
movl $0x1, %edx
cmovgel %r14d, %edx
shlq $0x3, %rdx
callq 0x11c0
cmpl $0x2, -0x60(%rbp)
movq -0x38(%rbp), %r12
jb 0x622a
addl %r14d, %r14d
leaq 0x8(%rbx), %rsi
cmpl $0x2, %r14d
movl $0x1, %edx
cmovgel %r14d, %edx
shlq $0x3, %rdx
movq %rbx, %rdi
callq 0x11c0
movq %r12, %rdi
callq 0x1100
movq %r15, %rdi
callq 0x1100
movq %r13, %rdi
callq 0x1100
movq -0x60(%rbp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf378(%rip), %rdi      # 0x155d4
jmp 0x6265
leaq 0xf407(%rip), %rdi      # 0x1566c
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
testl %eax, %eax
jne 0x6295
leaq 0xf2a4(%rip), %rdi      # 0x15525
jmp 0x6265
leaq 0xf1df(%rip), %rdi      # 0x15469
jmp 0x6265
leaq 0xf22b(%rip), %rdi      # 0x154be
jmp 0x6265
leaq 0xf2da(%rip), %rdi      # 0x15576
jmp 0x6265
leaq 0xf37c(%rip), %rdi      # 0x15621
jmp 0x6265
leaq 0xf40c(%rip), %rdi      # 0x156ba
jmp 0x6265