// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, -0x3c(%rbp)
movl %r8d, -0x44(%rbp)
movq %rcx, -0x60(%rbp)
movl %edx, -0x40(%rbp)
movq %rsi, -0x70(%rbp)
movb (%rsi), %r15b
movq %rdi, -0x68(%rbp)
movq (%rdi), %rax
pushq $0x1
popq %rbx
xorl %r13d, %r13d
xorl %r14d, %r14d
movq $0x0, -0x50(%rbp)
movl %ebx, -0x2c(%rbp)
movq $0x0, -0x58(%rbp)
movq %r13, -0x38(%rbp)
movq %rax, -0x78(%rbp)
movsbl %r15b, %r13d
testl %ebx, %ebx
je 0x2f93
movq %rax, %r12
movq %rax, %rdi
callq 0x2d0e
cmpb %al, %r15b
je 0x2fc1
movq %r12, %rdi
callq 0x2d0e
cmpl $0x0, -0x3c(%rbp)
je 0x2f79
cmpb %al, %r15b
jl 0x2fca
movq %r12, %rdi
callq 0x2d20
testq %rax, %rax
je 0x2f9b
movq %r12, %rdi
callq 0x2d20
movq %r12, %r14
jmp 0x2f4e
movq -0x58(%rbp), %r14
movq -0x78(%rbp), %r12
movl -0x2c(%rbp), %edx
cmpl $0x1, %edx
jne 0x2fcd
movl %r13d, %edi
callq 0x2ca8
movq %r12, %rdi
movq %rax, %rsi
callq 0x2d16
movq %r12, %rdi
callq 0x2d20
movq %rax, %r12
movl $0x0, -0x2c(%rbp)
jmp 0x3001
pushq $0x2
popq %rdx
cmpl $0x0, -0x3c(%rbp)
sete %al
cmpl $0x2, %edx
setne %cl
orb %al, %cl
jne 0x2ffe
movq %r12, %r15
movl %r13d, %edi
callq 0x2ca8
movq %rax, %r12
testq %r14, %r14
je 0x3007
movq %r14, %rdi
movq %r12, %rsi
callq 0x2d16
jmp 0x3021
movl %edx, -0x2c(%rbp)
movq -0x38(%rbp), %r13
jmp 0x3037
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x301a
movq %r12, %rsi
callq 0x2d2a
jmp 0x3021
movq -0x68(%rbp), %rax
movq %r12, (%rax)
movq -0x38(%rbp), %r13
movq %r12, %rdi
movq %r15, %rsi
callq 0x2d16
movl $0x0, -0x2c(%rbp)
movq -0x70(%rbp), %rax
movb 0x1(%rax,%r13), %r15b
testb %r15b, %r15b
je 0x308b
movq %r12, %rdi
callq 0x2d34
testq %rax, %rax
je 0x3061
xorl %r14d, %r14d
movq %r12, -0x50(%rbp)
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
jmp 0x3077
movsbl %r15b, %edi
callq 0x2ca8
movq %r12, %rdi
movq %rax, %rsi
callq 0x2d2a
xorl %ebx, %ebx
incq %r13
movq %r12, %rdi
callq 0x2d34
movq %r14, -0x58(%rbp)
jmp 0x2f46
movq %r12, %rdi
movl -0x40(%rbp), %esi
movq -0x60(%rbp), %rdx
movl -0x44(%rbp), %ecx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d8e