// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %rsp
movl %edx, 0x4(%rsp)
movq %rsi, %r14
leaq 0x8(%rdi), %rbx
movq %rdi, 0x10(%rsp)
movl (%rdi), %ebp
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %rbx, 0x8(%rsp)
movq (%rbx), %rax
movb (%r14,%r13), %r12b
testq %rax, %rax
je 0x4122
movl %ebp, (%rsp)
movq %r14, %rbp
xorl %r14d, %r14d
movq %rax, %rbx
cmpb %r12b, (%rax)
je 0x4149
jge 0x4154
movq 0x8(%rbx), %rax
movq %rbx, %r14
testq %rax, %rax
jne 0x40ea
movl $0x20, %edi
callq 0x2100
movb %r12b, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movq %rax, 0x8(%rbx)
jmp 0x4175
movl $0x20, %edi
callq 0x2100
movb %r12b, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
testq %r15, %r15
je 0x417d
movq %rax, 0x10(%r15)
jmp 0x4185
movq %rbx, %r15
movq %rbp, %r14
movl (%rsp), %ebp
jmp 0x4191
movl $0x20, %edi
callq 0x2100
movb %r12b, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq %rbx, 0x8(%rax)
testq %r14, %r14
je 0x41ab
movq %rax, 0x8(%r14)
movq %rbp, %r14
movl (%rsp), %ebp
jmp 0x4185
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
incl %ebp
movq 0x10(%rsp), %rcx
movl %ebp, (%rcx)
movq %rax, %r15
leaq 0x10(%r15), %rbx
incl %r13d
movq %r14, %rdi
callq 0x2050
cmpq %r13, %rax
ja 0x40d5
jmp 0x41b3
movq %rbp, %r14
movl (%rsp), %ebp
jmp 0x413e
movq 0x18(%r15), %r12
testq %r12, %r12
jne 0x41d5
movl $0x18, %edi
callq 0x2100
movq %rax, %r12
movq %rax, %rdi
callq 0x4368
movq %r12, 0x18(%r15)
movq %r12, %rdi
movq %r14, %rsi
movl 0x4(%rsp), %edx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x45c8
movq %rax, %rbx
movl $0x18, %esi
movq %r12, %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x2260