// File: /workspace/github_cpp_makefile_star2_O1/JohnPapad[P]Mini-Search-Engine/trie.cpp
// Function: my_trie::insert_word(char*, unsigned int)
void my_trie::insert_word(char * word, unsigned int doc_id)
{ // inseting a word into the trie or updating its posting list if word is already contained
  // every orizontal list is sorted alphabetically
    node * parent_node = NULL; //will indicate the parent of the first node of an orizontal list
    node * start_node = first; //will indicate the first node of an orizontal list

    for(unsigned int i = 0; i < strlen(word); i++)
    { //iterating word letter by letter
        node * prev_temp = NULL;  // will be used for iterating an orizontal list
        node * temp = start_node; // will be used for iterating an orizontal list
        node * n = NULL;
        bool reallocate_parent_flag = 0;

        if(temp == NULL)
        { // a new orizontal list will be created
            n = create_node(word[i]);  //this will be its first node
            reallocate_parent_flag = 1;
        }
        else
        { // a orizontal list already exists
            while(1) //iterating the orizontal list
            {
                if(temp == NULL)
                { //we have iterated the whole list and no same letter has been found. new letter will be added at the end of the list
                    n = create_node(word[i]);
                    prev_temp->right = n;
                    break;
                }

                if(temp->ch == word[i])
                { // we have found a same letter
                    parent_node = temp;
                    start_node = temp->down; //we will continue looking downwards
                    break;
                }
                else if(temp->ch < word[i])
                {// keep searching the orizontal list
                    prev_temp = temp;
                    temp = temp->right;
                }
                else
                {// no same letter found so far and no same letter will be found in this orizontal list because it is sorted alphabetically
                 // and current letter is bigger than the inserting one
                    n = create_node(word[i]);
                    n->right = temp;

                    if(prev_temp == NULL) //adding new letter at the start of the orizontal list
                    {
                        reallocate_parent_flag = 1;
                    }
                    else //adding new letter in the middle of the orizontal list
                    {
                        prev_temp->right = n ;
                    }

                    break;
                }
            }
        }

        if(n != NULL)  // a new node has been created (a new letter has been added)
        { // checking if parent's node down pointer needs to be reallocated
            if (reallocate_parent_flag == 1)
            {
                if (parent_node != NULL)
                {
                    parent_node->down = n;
                }
                else
                {
                    first = n;
                }
            }

            trie_size++;
            parent_node = n;
            start_node = n->down; //NULL
        }
    }

    //all letters of the inserting word have been consumed
    if(parent_node->pl_ptr == NULL)
    { // if the last's letter node does not have an existing posting list (inserting word is not contained in the trie)
        parent_node->pl_ptr = new posting_list(); //create one
    }

    parent_node->pl_ptr->update(word, doc_id);  //inserting word already exists, therefore update its posting list

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, 0x14(%rsp)
movq %rsi, %r14
leaq 0x8(%rdi), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rdi), %r12
movq %rdi, 0x20(%rsp)
movl (%rdi), %ebx
xorl %r13d, %r13d
xorl %ebp, %ebp
movb (%r14,%r13), %r15b
testq %r12, %r12
je 0x41f3
movq %rbp, 0x8(%rsp)
movl %ebx, 0x4(%rsp)
movq %r12, %rax
xorl %ebp, %ebp
movq %rax, %rbx
cmpb %r15b, (%rax)
je 0x4216
jge 0x4227
movq 0x8(%rbx), %rax
movq %rbx, %rbp
testq %rax, %rax
jne 0x41b7
movb (%r14,%r13), %bpl
movl $0x20, %edi
callq 0x2100
movb %bpl, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movq %rax, 0x8(%rbx)
jmp 0x4250
movl $0x20, %edi
callq 0x2100
movb %r15b, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movb $0x1, %cl
xorl %r12d, %r12d
jmp 0x425b
movq 0x10(%rbx), %r12
xorl %eax, %eax
xorl %ecx, %ecx
movq %rbx, %rbp
movl 0x4(%rsp), %ebx
jmp 0x425b
movl $0x20, %edi
callq 0x2100
movb %r15b, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movq %rbx, 0x8(%rax)
testq %rbp, %rbp
je 0x429a
movq %rax, 0x8(%rbp)
xorl %ecx, %ecx
movl 0x4(%rsp), %ebx
movq 0x8(%rsp), %rbp
testq %rax, %rax
je 0x4284
testb %cl, %cl
je 0x4274
leaq 0x10(%rbp), %rcx
testq %rbp, %rbp
cmoveq 0x18(%rsp), %rcx
movq %rax, (%rcx)
incl %ebx
movq 0x20(%rsp), %rcx
movl %ebx, (%rcx)
movq 0x10(%rax), %r12
movq %rax, %rbp
incl %r13d
movq %r14, %rdi
callq 0x2050
cmpq %r13, %rax
ja 0x41a0
jmp 0x429e
movb $0x1, %cl
jmp 0x4252
cmpq $0x0, 0x18(%rbp)
jne 0x42be
movl $0x18, %edi
callq 0x2100
movq %rax, %r12
movq %rax, %rdi
callq 0x4470
movq %r12, 0x18(%rbp)
movq 0x18(%rbp), %rdi
movq %r14, %rsi
movl 0x14(%rsp), %edx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x46d0
movq %rax, %rbx
movl $0x18, %esi
movq %r12, %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x2260