// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movl %r9d, -0x34(%rbp)
movl %r8d, -0x3c(%rbp)
movq %rcx, -0x50(%rbp)
movl %edx, -0x38(%rbp)
movq %rsi, -0x60(%rbp)
movb (%rsi), %r15b
movq %rdi, -0x58(%rbp)
movq (%rdi), %r13
movl $0x1, -0x2c(%rbp)
xorl %r14d, %r14d
xorl %r12d, %r12d
movq $0x0, -0x48(%rbp)
movl $0x1, -0x30(%rbp)
movq %r14, -0x68(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x30f7
movq %r13, %rdi
callq 0x2e43
movq %r13, %r14
cmpb %al, %r15b
jne 0x30a2
movq %r13, %r14
xorl %r13d, %r13d
jmp 0x30fe
movq %r14, %rbx
movq %r14, %rdi
callq 0x2e43
cmpl $0x0, -0x34(%rbp)
je 0x30bc
cmpb %al, %r15b
jl 0x316d
movq %rbx, %rdi
callq 0x2e55
testq %rax, %rax
je 0x3178
movq %rbx, %rdi
callq 0x2e55
movq %rax, %r14
cmpl $0x0, -0x30(%rbp)
je 0x3180
movq %r14, %rdi
callq 0x2e43
movq %rbx, %r12
cmpb %al, %r15b
jne 0x30a2
movq %rbx, %r12
jmp 0x309d
movq %r13, %r14
movl -0x2c(%rbp), %r13d
cmpl $0x1, %r13d
je 0x3139
cmpl $0x0, -0x34(%rbp)
sete %al
cmpl $0x2, %r13d
setne %cl
orb %al, %cl
jne 0x3134
movsbl %r15b, %edi
callq 0x2dd8
movq %rax, %rbx
testq %r12, %r12
je 0x315a
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2e4b
jmp 0x318f
movq %r14, %rbx
jmp 0x319d
movsbl %r15b, %edi
callq 0x2dd8
movq %r14, %rdi
movq %rax, %rsi
callq 0x2e4b
movq %r14, %rdi
callq 0x2e55
movq %rax, %rbx
jmp 0x319a
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x3188
movq %rbx, %rsi
callq 0x2e5f
jmp 0x318f
movl $0x2, %r13d
movq %rbx, %r14
jmp 0x30fe
movq %rbx, %r14
jmp 0x30fa
movq %r13, %r12
jmp 0x30fa
movq -0x58(%rbp), %rax
movq %rbx, (%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2e4b
xorl %r13d, %r13d
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %r14
movb 0x1(%rax,%r14), %r15b
testb %r15b, %r15b
je 0x31fe
movq %rbx, %rdi
callq 0x2e69
testq %rax, %rax
je 0x31cc
movl $0x1, -0x2c(%rbp)
xorl %r12d, %r12d
movq %rbx, -0x48(%rbp)
jmp 0x31eb
movl %r13d, -0x2c(%rbp)
movsbl %r15b, %edi
callq 0x2dd8
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2e5f
movl $0x0, -0x30(%rbp)
incq %r14
movq %rbx, %rdi
callq 0x2e69
movq %rax, %r13
jmp 0x3080
movq %rbx, %rdi
movl -0x38(%rbp), %esi
movq -0x50(%rbp), %rdx
movl -0x3c(%rbp), %ecx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2ec3