// File: JohnPapad[P]Mini-Search-Engine/posting_list.cpp
// Function: posting_list::update(char*, unsigned int)
void posting_list::update(char * word, unsigned int doc_id)
{ //create new list node if doc id is not contained in the list or increase its term frequency otherwise
  // posting list is sorted
    node * temp = first;
    node * prev_temp;
    node * n;

    if(first == NULL)
    { // list is empty
        n = create_node(doc_id);
        first = n;
        this->word = (char *) malloc((strlen(word) + 1) * sizeof(char));
        strcpy(this->word, word);
        pl_size++;
    }
    else
    { // list already exists
        while(1)
        {
            if(temp == NULL)
            {// we have iterated the whole list and doc id does not exist. so it will be added at the end of the list
                n = create_node(doc_id);
                pl_size++;
                prev_temp->next = n;
                break;
            }

            if (temp->doc_id == doc_id)
            { // doc id exists
                temp->freq++;     //increase its term frequency
                break;
            }
            else if(temp->doc_id < doc_id)
            { //continue searching
                prev_temp = temp;
                temp = temp->next;
            }
            else
            {//because the list is sorted and the current doc id is bigger than the inserting one
             //we do not have to keep looking the rest of the list (the rest stored doc ids will be different than the inserting one)
             // doc id will be added here
                n = create_node(doc_id);
                pl_size++;
                n->next = temp;

                if(prev_temp == NULL)
                { //we be added at the start of the list
                    first = n;
                }
                else
                {
                    prev_temp->next = n;
                }

                break;
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x20(%rbp)
cmpq $0x0, 0x8(%rax)
jne 0x4efa
movq -0x38(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x4e20
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rdi
callq 0x2070
movq %rax, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x2160
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x20b0
movq -0x38(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x4fc3
jmp 0x4efc
cmpq $0x0, -0x20(%rbp)
jne 0x4f32
movq -0x38(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x4e20
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, -0x30(%rbp)
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x4fc1
movq -0x20(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x4f4c
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
jmp 0x4fc1
movq -0x20(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jae 0x4f6d
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x4fba
movq -0x38(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x4e20
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, -0x30(%rbp)
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
cmpq $0x0, -0x28(%rbp)
jne 0x4fac
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x4fb8
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x4fc1
jmp 0x4fbc
jmp 0x4efc
jmp 0x4fc3
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)