// File: /workspace/github_c_makefile_star2_O3/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, -0x38(%rbp)
movl %r8d, -0x40(%rbp)
movq %rcx, -0x60(%rbp)
movl %edx, -0x3c(%rbp)
movq %rsi, -0x68(%rbp)
movb (%rsi), %r13b
movq %rdi, -0x58(%rbp)
movq (%rdi), %rbx
movl $0x1, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
movq $0x0, -0x50(%rbp)
movl $0x1, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x3143
movl %r15d, -0x44(%rbp)
movq %rbx, %rdi
callq 0x2e8b
xorl %r15d, %r15d
cmpb %al, %r13b
je 0x31d5
movq %r14, -0x30(%rbp)
movq %rbx, %r14
movq %rbx, %rdi
callq 0x2e8b
cmpl $0x0, -0x38(%rbp)
je 0x3100
cmpb %al, %r13b
jl 0x3130
movq %r14, %rdi
callq 0x2e9d
testq %rax, %rax
je 0x3138
movq %r14, %rdi
callq 0x2e9d
movq %rax, %rbx
movq %rax, %rdi
callq 0x2e8b
movq %r14, %r12
cmpb %al, %r13b
jne 0x30ea
movq %r14, %r12
jmp 0x31d1
movl $0x2, %r15d
jmp 0x3150
movq %r14, %rbx
movq -0x30(%rbp), %r14
movl -0x44(%rbp), %r15d
cmpl $0x1, %r15d
je 0x3185
movq %r14, -0x30(%rbp)
movq %rbx, %r14
cmpl $0x0, -0x38(%rbp)
sete %al
cmpl $0x2, %r15d
setne %cl
orb %al, %cl
jne 0x3180
movsbl %r13b, %edi
callq 0x2e20
movq %rax, %rbx
testq %r12, %r12
je 0x31a9
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2e93
jmp 0x31c3
movq %r14, %rbx
jmp 0x31d1
movsbl %r13b, %edi
callq 0x2e20
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2e93
movq %rbx, %rdi
callq 0x2e9d
movq %rax, %rbx
xorl %r15d, %r15d
jmp 0x31d5
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x31bc
movq %rbx, %rsi
callq 0x2ea7
jmp 0x31c3
movq -0x58(%rbp), %rax
movq %rbx, (%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2e93
xorl %r15d, %r15d
movq -0x30(%rbp), %r14
movq -0x68(%rbp), %rax
movb 0x1(%rax,%r14), %r13b
testb %r13b, %r13b
je 0x322d
movq %rbx, %rdi
callq 0x2eb1
testq %rax, %rax
je 0x31ff
movl $0x1, %r15d
xorl %r12d, %r12d
movq %rbx, -0x50(%rbp)
jmp 0x321a
movsbl %r13b, %edi
callq 0x2e20
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2ea7
movl $0x0, -0x34(%rbp)
incq %r14
movq %rbx, %rdi
callq 0x2eb1
movq %rax, %rbx
jmp 0x30c8
movq %rbx, %rdi
movl -0x3c(%rbp), %esi
movq -0x60(%rbp), %rdx
movl -0x40(%rbp), %ecx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2f0b