// File: /workspace/github_cpp_makefile_star2_O3/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, -0x88(%rbp)
movq %rdi, %rbx
xorl %edx, %edx
movb $0x20, %r10b
movq %rdi, %rax
xorl %r12d, %r12d
movl %r12d, %r13d
movl %edx, %r9d
movl %r10d, %r11d
movb (%rax), %r10b
xorl %r8d, %r8d
cmpb $0x3a, %r10b
sete %r8b
leal (%r9,%r8), %edx
cmpb $0x29, %r11b
setne %dil
andb %dil, %r8b
movzbl %r8b, %r12d
addl %r13d, %r12d
incq %rax
testb %r10b, %r10b
jne 0x5bcf
testl %r9d, %r9d
je 0x61a4
xorl %eax, %eax
movq %rbx, %r8
movb (%r8), %dl
xorl %edi, %edi
cmpb $0x28, %dl
sete %dil
addl %edi, %eax
incq %r8
testb %dl, %dl
jne 0x5c12
xorl %r8d, %r8d
movq %rbx, %r9
movl %r8d, %edx
movb (%r9), %dil
xorl %r8d, %r8d
cmpb $0x29, %dil
sete %r8b
addl %edx, %r8d
incq %r9
testb %dil, %dil
jne 0x5c2d
cmpl %eax, %edx
jne 0x61ad
xorl %edx, %edx
movq %rbx, %r8
movl %edx, %eax
movb (%r8), %dil
xorl %edx, %edx
cmpb $0x3b, %dil
sete %dl
addl %eax, %edx
incq %r8
testb %dil, %dil
jne 0x5c56
cmpl $0x1, %eax
jne 0x6197
movq %rbx, %rax
movzbl (%rax), %edx
incq %rax
cmpl $0x20, %edx
je 0x5c7a
cmpl $0x28, %edx
jne 0x617f
xorl %r8d, %r8d
movq %rbx, %rax
movl %r8d, %edx
movb (%rax), %dil
xorl %r8d, %r8d
cmpb $0x25, %dil
sete %r8b
addl %edx, %r8d
incq %rax
testb %dil, %dil
jne 0x5c94
testl %edx, %edx
jne 0x61bf
movq %rcx, -0x48(%rbp)
xorl %eax, %eax
movabsq $0xc00110000000000, %rcx # imm = 0xC00110000000000
movslq %eax, %rdx
incl %eax
movb (%rbx,%rdx), %dl
movl %edx, %edi
andb $-0x5, %dil
cmpb $0x28, %dil
jne 0x5cf0
movslq %eax, %r8
movzbl (%rbx,%r8), %edx
cmpq $0x3b, %rdx
ja 0x5cf6
btq %rdx, %rcx
jae 0x5cf6
testb %dl, %dl
jne 0x5cc8
jmp 0x5d23
addq %rbx, %r8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movzbl (%r8), %edx
incl %eax
incl %edi
cmpl $0x3a, %edx
je 0x5d12
incq %r8
testl %edx, %edx
jne 0x5cfe
cmpl $0x32, %edi
jb 0x5cf0
leaq 0xf94e(%rip), %rdi      # 0x1566c
jmp 0x6186
movb %r11b, -0x29(%rbp)
movb %r10b, -0x2a(%rbp)
movq %rsi, -0x78(%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 0x61c8
cmpq $0x0, -0x38(%rbp)
je 0x61c8
movq %rax, %r15
testq %rax, %rax
je 0x61c8
movl $0x0, -0x3c(%rbp)
movabsq $0x100000600, %rax      # imm = 0x100000600
movq -0x48(%rbp), %r8
movzbl (%rbx), %ecx
cmpq $0x20, %rcx
ja 0x5d9b
btq %rcx, %rax
jb 0x5dad
movl -0x3c(%rbp), %esi
movslq %esi, %rdx
incl %esi
movl %esi, -0x3c(%rbp)
movq -0x38(%rbp), %rsi
movb %cl, (%rsi,%rdx)
incq %rbx
testb %cl, %cl
jne 0x5d8c
movq %r13, -0x98(%rbp)
movl $0x1, %ebx
movq -0x38(%rbp), %rax
cmpb $0x28, (%rax)
movq %r12, -0x60(%rbp)
movl %r12d, %eax
movq %rax, -0x90(%rbp)
jne 0x6081
movl $0x1, %ecx
movq %rcx, -0x50(%rbp)
movq %rax, -0x80(%rbp)
movl $0x1, -0x40(%rbp)
movq -0x38(%rbp), %rcx
movq %rcx, %r13
movq %r14, %rcx
movb $0x28, %al
xorl %r14d, %r14d
xorl %edx, %edx
cmpb $0x28, %al
je 0x5e0e
movzbl %al, %eax
cmpl $0x29, %eax
jne 0x5e11
jmp 0x5e1b
movl %r14d, %edx
movb 0x1(%r13,%r14), %al
incq %r14
jmp 0x5e00
movq %rcx, -0x38(%rbp)
incq -0x80(%rbp)
cmpl %r14d, %edx
movq %rdx, -0x68(%rbp)
jge 0x5f9a
leal 0x1(%rdx), %r9d
movslq %edx, %r12
incq %r12
movzbl (%r13,%r12), %eax
cmpl $0x29, %eax
je 0x5e9b
cmpl $0x2c, %eax
je 0x5e9b
cmpl $0x3a, %eax
jne 0x5f1c
leaq 0x1(%r12), %rbx
movslq %r9d, %rax
cmpb $0x25, (%r13,%rax)
jne 0x5f24
incq %rax
cmpq %rax, %r12
movl %r9d, -0x70(%rbp)
jle 0x5f4c
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 0x5e7d
movl %edx, %eax
jmp 0x5f4e
movslq -0x54(%rbp), %rcx
cmpq %rcx, %r12
jle 0x5ec0
movl %r12d, %eax
subl %ecx, %eax
addq %r13, %rcx
xorl %edx, %edx
movb (%rcx,%rdx), %sil
movb %sil, (%r15,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x5eae
jmp 0x5ec2
xorl %eax, %eax
incq %r12
movb $0x0, (%r15,%rax)
movq %r15, %rdi
callq 0x1120
movq -0x50(%rbp), %rsi
movslq %esi, %rax
incl %esi
movq -0x78(%rbp), %rdx
movq -0x80(%rbp), %rcx
movq %rcx, (%rdx,%rax,8)
movslq -0x58(%rbp), %rcx
movq %rcx, 0x8(%rdx,%rax,8)
leal 0x2(%rax), %ecx
shrl $0x1f, %esi
addl %esi, %eax
incl %eax
sarl %eax
cltq
movq -0x88(%rbp), %rdx
movsd %xmm0, (%rdx,%rax,8)
movl %r12d, %ebx
movl %ecx, %eax
movq %rax, -0x50(%rbp)
movl %r12d, %r9d
movq -0x48(%rbp), %r8
jmp 0x5f8c
incq %r12
movl %r12d, %ebx
jmp 0x5f8c
movslq -0x40(%rbp), %rcx
cmpq %rax, %r12
jle 0x5f6e
subl %eax, %r12d
addq %r13, %rax
xorl %edx, %edx
movb (%rax,%rdx), %sil
movq -0x8(%r8,%rcx,8), %rdi
movb %sil, (%rdi,%rdx)
incq %rdx
cmpq %rdx, %r12
jne 0x5f35
jmp 0x5f71
xorl %eax, %eax
movb $0x0, (%r15,%rax)
movq %r15, %rdi
callq 0x11a0
movl %eax, -0x58(%rbp)
movq %rbx, %r12
movl %ebx, -0x54(%rbp)
movq -0x48(%rbp), %r8
movl -0x70(%rbp), %r9d
jmp 0x5f8c
xorl %r12d, %r12d
movq -0x8(%r8,%rcx,8), %rax
movb $0x0, (%rax,%r12)
incl %ecx
movq %rbx, %r12
movl %ebx, -0x54(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x58(%rbp)
movl %ecx, -0x40(%rbp)
movl %r14d, %eax
subl %ebx, %eax
cmpl $-0x1, %eax
jne 0x5e3a
movq -0x68(%rbp), %rbx
testl %ebx, %ebx
jle 0x5fb2
movl %ebx, %edx
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x1080
jmp 0x5fb4
xorl %ebx, %ebx
movq -0x80(%rbp), %rdi
movq %r15, %rsi
movl $0xa, %edx
callq 0x50c3
movl %ebx, %r12d
movq -0x38(%rbp), %rax
movb $0x25, (%rax,%r12)
movq %r15, %rdi
callq 0x1050
testl %eax, %eax
jle 0x6017
movq -0x38(%rbp), %rax
leaq (%r12,%rax), %rbx
incq %rbx
incq %r12
movq %r12, -0x70(%rbp)
xorl %r12d, %r12d
movb (%r15,%r12), %al
movb %al, (%rbx,%r12)
incq %r12
movq %r15, %rdi
callq 0x1050
cltq
cmpq %rax, %r12
jl 0x5ff2
movq -0x70(%rbp), %rax
addl %r12d, %eax
movl %eax, %ebx
jmp 0x6019
incl %ebx
movq -0x38(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl %r14d, %eax
jle 0x6068
movl %ebx, %ebx
movq %rbx, -0x68(%rbp)
leaq (%rcx,%rbx), %rdi
leaq (%r14,%r13), %rsi
incq %rsi
movl %r14d, %edx
notl %edx
addl %eax, %edx
incq %rdx
movl %eax, %r12d
callq 0x1080
movl %r12d, %eax
subq %rax, %r14
xorl %eax, %eax
decq %rax
cmpq %rax, %r14
jne 0x6050
movq -0x68(%rbp), %rbx
subl %eax, %ebx
movq -0x48(%rbp), %r8
movq -0x38(%rbp), %rcx
jmp 0x606c
movq -0x48(%rbp), %r8
cmpb $0x28, (%rcx)
movq %r13, %r14
movl %ebx, -0x3c(%rbp)
je 0x5df3
movslq -0x50(%rbp), %rbx
jmp 0x6084
movq %r14, %r13
cmpl $0x0, -0x60(%rbp)
je 0x6145
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
movq -0x90(%rbp), %rax
leaq (%r8,%rax,8), %r12
movq -0x8(%r12), %rsi
movq (%r12), %rdi
addq $-0x8, %r12
callq 0x1090
decl %r14d
cmpl $0x1, %r14d
jg 0x60be
xorps %xmm0, %xmm0
movq -0x78(%rbp), %r12
movups %xmm0, (%r12,%rbx,8)
movq -0x60(%rbp), %r14
cmpl $0x1, %r14d
je 0x6154
leal (%r14,%r14), %eax
movq %rax, -0x50(%rbp)
leal -0x3(,%r14,2), %edx
movq -0x88(%rbp), %rdi
leaq 0x8(%rdi), %rsi
cmpl $0x2, %edx
movl $0x1, %ebx
cmovll %ebx, %edx
shlq $0x3, %rdx
callq 0x11c0
movq -0x50(%rbp), %rax
leal -0x6(,%rax,2), %eax
leaq 0x8(%r12), %rsi
cmpl $0x2, %eax
cmovgel %eax, %ebx
shlq $0x3, %rbx
movq %r12, %rdi
movq %rbx, %rdx
callq 0x11c0
jmp 0x6154
xorps %xmm0, %xmm0
movq -0x78(%rbp), %rax
movups %xmm0, (%rax,%rbx,8)
movq -0x60(%rbp), %r14
movq -0x38(%rbp), %rdi
callq 0x1100
movq %r15, %rdi
callq 0x1100
movq %r13, %rdi
callq 0x1100
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf44e(%rip), %rdi      # 0x155d4
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
testl %eax, %eax
jne 0x61b6
leaq 0xf383(%rip), %rdi      # 0x15525
jmp 0x6186
leaq 0xf2be(%rip), %rdi      # 0x15469
jmp 0x6186
leaq 0xf30a(%rip), %rdi      # 0x154be
jmp 0x6186
leaq 0xf3b9(%rip), %rdi      # 0x15576
jmp 0x6186
leaq 0xf45b(%rip), %rdi      # 0x15621
jmp 0x6186
leaq 0xf4eb(%rip), %rdi      # 0x156ba
jmp 0x6186