// File: /workspace/github_c_makefile_star2_O0/stefpant[P]TCPserver/PantoulasStefanosProject2/trie/trieInsertion.c
// Function: insertInTrie
void insertInTrie(node** trie,char* word,int line,char* name,int id,int sorted){
	int i=0;//character's index
	char c = word[i];
	node* temp=*trie;
	node* prevN=NULL;
	node* newN=NULL;
	node* parent=NULL;
	int flag=1,flag2=1;
	//flag:if temp->next==NULL then create new next
	//frag2:if child==NULL skip the second while loop

	while(1){//for all word's chars(has at least 1 char)
		while(flag2){//for all childs in temp's list(temp -> next -> next -> ...)
			if(c==getValue(temp)){
				flag=0;
//printf("'%c' found in trie go down!\n",c);
				break;
			}
			else if((c<getValue(temp))&&sorted){
				flag=2;
				break;
			}
			if(getNext(temp)==NULL) {/*printf("'%c' not found create next node!\n",c);*/break;}
			prevN = temp;//keep previous node
			temp = getNext(temp);//search until c found in trie
		}//or we reached the end of temp's list
		if(flag==1){//c not found in trie and greater than any other value in list
			setNext(temp,nodeConstruct(c));
			//setParent(getNext(temp),getParent(temp));
//printf("Next made with value:'%c'\n",c);
			temp = getNext(temp);
			flag=0;//from now on just create childs
		}
		else if((flag==2)&&sorted){
			newN = nodeConstruct(c);
			if(prevN==NULL){
				if(parent!=NULL)
					setChild(parent,newN);
				else *trie = newN;//change root
			}
			else
				setNext(prevN,newN);
			//setParent(newN,getParent(temp));
			setNext(newN,temp);
			temp = newN;
			flag=0;
		}
		//now temp has the 'c' value so we check its child
		if((c=word[++i])=='\0'){
			updateEPL(temp,line,name,id);
//printf("reached the end update PL!\n");
			return ;
		}
		if(getChild(temp)!=NULL) {
			/*printf("Search down for next char='%c'!\n",c);*/
			prevN = NULL;//no previous node here
			flag=1;
			parent = temp;
			temp=getChild(temp);
		}
		else{
			flag2=0;//from now on until the end of the word
			setChild(temp,nodeConstruct(c));//run these 4 lines
//printf("Made child with value '%c'!\n",c);
			//setParent(getChild(temp),temp);
			temp = getChild(temp);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x2d(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x1, -0x54(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x2687
cmpl $0x0, -0x58(%rbp)
je 0x2707
movsbl -0x2d(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x38(%rbp), %rdi
callq 0x21c0
movb %al, %cl
movl -0x5c(%rbp), %eax
movsbl %cl, %ecx
cmpl %ecx, %eax
jne 0x26b2
movl $0x0, -0x54(%rbp)
jmp 0x2707
movsbl -0x2d(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x38(%rbp), %rdi
callq 0x21c0
movb %al, %cl
movl -0x60(%rbp), %eax
movsbl %cl, %ecx
cmpl %ecx, %eax
jge 0x26dd
cmpl $0x0, -0x28(%rbp)
je 0x26dd
movl $0x2, -0x54(%rbp)
jmp 0x2707
jmp 0x26df
movq -0x38(%rbp), %rdi
callq 0x21f0
cmpq $0x0, %rax
jne 0x26f0
jmp 0x2707
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x21f0
movq %rax, -0x38(%rbp)
jmp 0x2687
cmpl $0x1, -0x54(%rbp)
jne 0x2740
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
movsbl -0x2d(%rbp), %edi
callq 0x20e0
movq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0x21d0
movq -0x38(%rbp), %rdi
callq 0x21f0
movq %rax, -0x38(%rbp)
movl $0x0, -0x54(%rbp)
jmp 0x27ae
cmpl $0x2, -0x54(%rbp)
jne 0x27ac
cmpl $0x0, -0x28(%rbp)
je 0x27ac
movsbl -0x2d(%rbp), %edi
callq 0x20e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x2783
cmpq $0x0, -0x50(%rbp)
je 0x2776
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x2210
jmp 0x2781
movq -0x48(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x2790
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x21d0
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x21d0
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x54(%rbp)
jmp 0x27ae
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x2d(%rbp)
movsbl %al, %eax
cmpl $0x0, %eax
jne 0x27e5
movq -0x38(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
callq 0x2340
addq $0x70, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x2230
cmpq $0x0, %rax
je 0x281a
movq $0x0, -0x40(%rbp)
movl $0x1, -0x54(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rdi
callq 0x2230
movq %rax, -0x38(%rbp)
jmp 0x284b
movl $0x0, -0x58(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movsbl -0x2d(%rbp), %edi
callq 0x20e0
movq -0x70(%rbp), %rdi
movq %rax, %rsi
callq 0x2210
movq -0x38(%rbp), %rdi
callq 0x2230
movq %rax, -0x38(%rbp)
jmp 0x2685