// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, -0x48(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, -0x50(%rbp)
movq %rdi, %rbx
xorl %edx, %edx
movb $0x20, %sil
movq %rdi, %rax
xorl %r15d, %r15d
movl %edx, %ecx
movb (%rax), %dil
xorl %r8d, %r8d
cmpb $0x3a, %dil
sete %r8b
leal (%rcx,%r8), %edx
cmpb $0x29, %sil
setne %sil
andb %sil, %r8b
movzbl %r8b, %esi
addl %esi, %r15d
incq %rax
movl %edi, %esi
testb %dil, %dil
jne 0x531b
testl %ecx, %ecx
je 0x5831
xorl %eax, %eax
movq %rbx, %rcx
movb (%rcx), %dl
xorl %esi, %esi
cmpb $0x28, %dl
sete %sil
addl %esi, %eax
incq %rcx
testb %dl, %dl
jne 0x5358
xorl %esi, %esi
movq %rbx, %rcx
movl %esi, %edx
movb (%rcx), %dil
xorl %esi, %esi
cmpb $0x29, %dil
sete %sil
addl %edx, %esi
incq %rcx
testb %dil, %dil
jne 0x5371
cmpl %eax, %edx
jne 0x583a
pushq $-0x1
popq %rax
xorl %esi, %esi
movq %rbx, %rdx
movl %esi, %ecx
movb (%rdx), %dil
xorl %esi, %esi
cmpb $0x3b, %dil
sete %sil
addl %ecx, %esi
incq %rdx
testb %dil, %dil
jne 0x539a
cmpl $0x1, %ecx
jne 0x5824
movq %rbx, %rcx
movzbl (%rcx), %edx
incq %rcx
cmpl $0x20, %edx
je 0x53bf
cmpl $0x28, %edx
jne 0x580e
xorl %edx, %edx
movq %rbx, %rcx
movl %edx, %esi
movb (%rcx), %dil
xorl %edx, %edx
cmpb $0x25, %dil
sete %dl
addl %esi, %edx
incq %rcx
testb %dil, %dil
jne 0x53d8
testl %esi, %esi
jne 0x584c
xorl %ecx, %ecx
movabsq $0xc00110000000000, %rdx # imm = 0xC00110000000000
movslq %ecx, %rsi
incl %ecx
movb (%rbx,%rsi), %sil
movl %esi, %edi
andb $-0x5, %dil
cmpb $0x28, %dil
jne 0x542c
movslq %ecx, %rdi
movzbl (%rbx,%rdi), %esi
cmpq $0x3b, %rsi
ja 0x5433
btq %rsi, %rdx
jae 0x5433
testb %sil, %sil
jne 0x5404
jmp 0x545f
addq %rbx, %rdi
movl %eax, %r8d
movzbl (%rdi), %esi
incl %ecx
incl %r8d
cmpl $0x3a, %esi
je 0x544d
incq %rdi
testl %esi, %esi
jne 0x5439
cmpl $0x32, %r8d
jb 0x542c
leaq 0xf212(%rip), %rdi      # 0x1466c
jmp 0x5815
imull $0x3e8, %r15d, %r14d    # imm = 0x3E8
movq %r14, %rdi
callq 0x1130
movq %rax, %r12
movq %r14, %rdi
callq 0x1130
movq %rax, %r13
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x1130
testq %r13, %r13
je 0x5855
testq %r12, %r12
je 0x5855
movq %rax, %r10
testq %rax, %rax
je 0x5855
xorl %esi, %esi
movabsq $0x100000600, %rax      # imm = 0x100000600
movzbl (%rbx), %ecx
cmpq $0x20, %rcx
ja 0x54bf
btq %rcx, %rax
jb 0x54c8
movslq %esi, %rdx
incl %esi
movb %cl, (%r12,%rdx)
incq %rbx
testb %cl, %cl
jne 0x54b0
movl %r15d, %ebx
pushq $0x1
popq %rax
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rbx, -0x58(%rbp)
movl %eax, -0x2c(%rbp)
movl %eax, -0x30(%rbp)
movq %r10, -0x40(%rbp)
movq %r15, -0x80(%rbp)
movq %rbx, -0x78(%rbp)
cmpb $0x28, (%r12)
movq %r12, -0x38(%rbp)
jne 0x577f
movq %r12, %rdi
movq %rsi, -0x98(%rbp)
movb $0x28, %al
xorl %edx, %edx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r8d, %r8d
movl -0x5c(%rbp), %esi
cmpb $0x28, %al
je 0x552c
movzbl %al, %eax
cmpl $0x29, %eax
jne 0x552f
jmp 0x553e
movl %r15d, %r8d
movb 0x1(%rdi,%r15), %al
incq %r15
addq %rcx, %r14
incl %edx
jmp 0x551e
movq %rdx, -0x90(%rbp)
movq %r8, -0x88(%rbp)
leal 0x1(%r8), %r11d
incq -0x58(%rbp)
movslq %r11d, %r12
sarq $0x20, %r14
movl -0x60(%rbp), %ecx
movq %r13, -0x70(%rbp)
movl %esi, -0x5c(%rbp)
movslq %esi, %r13
movl %ecx, -0x60(%rbp)
movslq %ecx, %r8
leaq (%rdi,%r13), %rbx
incq %r12
leaq -0x1(%r12), %rax
cmpq %r14, %rax
jg 0x56a3
movzbl -0x1(%rdi,%r12), %ecx
cmpl $0x29, %ecx
je 0x559a
cmpl $0x2c, %ecx
je 0x559a
cmpl $0x3a, %ecx
jne 0x5572
jmp 0x560e
xorl %ecx, %ecx
leaq (%rcx,%r13), %rdx
cmpq %rax, %rdx
jge 0x55b1
movb (%rbx,%rcx), %dl
movb %dl, (%r10,%rcx)
incq %rcx
jmp 0x559c
movl %ecx, %eax
movb $0x0, (%r10,%rax)
movq %r10, %rdi
movq %r8, -0xa0(%rbp)
callq 0x1110
movq -0xa0(%rbp), %r8
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %r10
movl -0x30(%rbp), %eax
movslq %eax, %rsi
incl %eax
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq %rcx, (%rdx,%rsi,8)
leal 0x2(%rsi), %ecx
movq %r8, 0x8(%rdx,%rsi,8)
cltd
pushq $0x2
popq %rsi
idivl %esi
cltq
movq -0x68(%rbp), %rdx
movsd %xmm0, (%rdx,%rax,8)
movl %ecx, -0x30(%rbp)
movl %r12d, %r11d
jmp 0x5572
movslq %r11d, %rcx
cmpb $0x25, (%rdi,%rcx)
jne 0x5658
incq %rcx
xorl %edx, %edx
cmpq %rax, %rcx
jge 0x5631
movb (%rdi,%rcx), %sil
movb %sil, (%r10,%rdx)
incq %rdx
incq %rcx
jmp 0x561c
movl %edx, %eax
movb $0x0, (%r10,%rax)
movq %r10, %rdi
movl %r11d, %ebx
callq 0x1190
movl %ebx, %r11d
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %r10
movl %eax, %ecx
movl %r12d, %esi
jmp 0x5562
movslq -0x2c(%rbp), %rdx
addq %rcx, %rdi
xorl %esi, %esi
leaq (%rcx,%rsi), %r8
cmpq %rax, %r8
jge 0x5680
movb (%rdi,%rsi), %r8b
movq -0x48(%rbp), %r9
movq -0x8(%r9,%rdx,8), %r9
movb %r8b, (%r9,%rsi)
incq %rsi
jmp 0x5661
movq -0x48(%rbp), %rax
movq -0x8(%rax,%rdx,8), %rax
movl %esi, %ecx
movb $0x0, (%rax,%rcx)
incl %edx
movl %r12d, %esi
movl -0x2c(%rbp), %ecx
movl %edx, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
jmp 0x5562
movq -0x88(%rbp), %r14
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
movq %r14, %rax
negq %rax
pushq $0x1
popq %rbx
movq -0x70(%rbp), %r12
leaq (%rax,%rbx), %rcx
cmpq $0x1, %rcx
je 0x56db
movb -0x1(%rdi,%rbx), %cl
movb %cl, -0x1(%r12,%rbx)
incq %rbx
jmp 0x56c3
movq -0x58(%rbp), %rdi
movq %r10, %rsi
pushq $0xa
popq %rdx
callq 0x49f4
movq -0x40(%rbp), %r13
addq %r12, %r14
incq %r14
movb $0x25, -0x1(%r14)
xorl %r12d, %r12d
movq %r13, %rdi
callq 0x1050
cltq
cmpq %rax, %r12
jge 0x5719
movb (%r13,%r12), %al
movb %al, (%r14,%r12)
incq %r12
jmp 0x56fc
addq %r12, %rbx
movq -0x98(%rbp), %rax
movq -0x90(%rbp), %rdx
cmpl %edx, %eax
cmovgl %eax, %edx
movq %rbx, %rsi
movq %r13, %r10
movq -0x38(%rbp), %r13
movq -0x78(%rbp), %rbx
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq -0x70(%rbp), %r12
cmpq %r15, %rdx
je 0x5761
movb 0x1(%r13,%r15), %al
incq %r15
movb %al, (%r12,%rsi)
incq %rsi
jmp 0x574b
movq -0x80(%rbp), %r15
jmp 0x54f5
movq -0x48(%rbp), %rax
movq -0x8(%rax,%rbx,8), %rsi
movq (%rax,%rbx,8), %rdi
callq 0x1070
decq %rbx
testl %ebx, %ebx
jg 0x576a
leal -0x3(,%r15,2), %eax
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
movslq -0x30(%rbp), %rsi
xorps %xmm0, %xmm0
movq -0x50(%rbp), %rdi
movups %xmm0, (%rdi,%rsi,8)
shlq $0x3, %rdx
movq -0x38(%rbp), %rdi
movq -0x68(%rbp), %rsi
cmpq %rcx, %rdx
je 0x57c8
movsd 0x8(%rsi,%rcx), %xmm0
movsd %xmm0, (%rsi,%rcx)
addq $0x8, %rcx
jmp 0x57b2
addl %eax, %eax
cltq
pushq $0x1
popq %rcx
movq -0x50(%rbp), %rsi
cmpq %rax, %rcx
jg 0x57e6
movq (%rsi,%rcx,8), %rdx
movq %rdx, -0x8(%rsi,%rcx,8)
incq %rcx
jmp 0x57d3
callq 0x10f0
movq -0x40(%rbp), %rdi
callq 0x10f0
movq %r13, %rdi
callq 0x10f0
movl %r15d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xedbf(%rip), %rdi      # 0x145d4
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %rdi
callq 0x1100
testl %ecx, %ecx
jne 0x5843
leaq 0xecf6(%rip), %rdi      # 0x14525
jmp 0x5815
leaq 0xec31(%rip), %rdi      # 0x14469
jmp 0x5815
leaq 0xec7d(%rip), %rdi      # 0x144be
jmp 0x5815
leaq 0xed2c(%rip), %rdi      # 0x14576
jmp 0x5815
leaq 0xedce(%rip), %rdi      # 0x14621
jmp 0x5815
leaq 0xee5e(%rip), %rdi      # 0x146ba
jmp 0x5815