// File: JohnPapad[P]Mini-Search-Engine/main.cpp
// Function: main
int main(int argc, char *argv[])
{
    unsigned int K;
    char * docfile = check_program_args(argv, argc, K);
    double k1 = 1.2;
    double b = 0.75;

    cout<<"---> Parameteres given <---"<<endl;
    cout<<"-> K = "<<K<<endl;
    cout<<"-> docfile: "<<docfile<<endl<<endl;

    FILE * fp = open_file(docfile);
    unsigned int nlines = get_number_of_lines(fp);
    char **docs = (char**) malloc(nlines * sizeof(char *));
    unsigned int * docs_word_counter = (unsigned int*) malloc(nlines * sizeof(unsigned int)); // keeps number of words for each doc

    for(unsigned int i = 0; i < nlines; i++)
    {
        docs[i] = NULL;
    }

    bool parsing_err = parse_docs(docs, fp);
    fclose(fp);

    if (parsing_err == 1)
    {
        delete_docs(docs, docs_word_counter, nlines);
        exit(1);
    }

    my_trie * my_trie_ptr = new my_trie();
    unsigned int total_nwords = insert_docs_into_trie(my_trie_ptr, docs, docs_word_counter, nlines);
    //my_trie_ptr->print(1);
    cout<<"-> Trie has been created !"<<endl;
    cout<<"-> Number of docs: "<<nlines<<endl;
    cout<<"-> Number of total words: "<<total_nwords<<endl<<endl;
    double avgdl = double(total_nwords) / double(nlines) ;

    while(1)
    {
        cout<<"-> Please type a trie command.."<<endl;
        char * cmd = read_str(3); //read trie command

        if ( strlen(cmd) == 0)
        {
            free(cmd);
            continue;
        }

        if (strcmp("/exit", cmd) == 0)
        {
            free(cmd);
            break;
        }
        else
        {
            unsigned int cmd_nwords;
            char ** cmd_words = strip_line_into_words(cmd, cmd_nwords); //get arguments from trie command

            if (strcmp("/df", cmd_words[0]) == 0)
            {
                if (cmd_nwords == 1)
                {
                    my_trie_ptr->print(0);
                }
                else if (cmd_nwords == 2)
                {
                    posting_list * pl_ptr = my_trie_ptr->search_word(cmd_words[1]);
                    cout<<cmd_words[1]<<"  ";
                    if(pl_ptr == NULL)
                    {
                        cout<<"0"<<endl;
                    }
                    else
                    {
                        cout<<pl_ptr->get_size()<<endl;
                    }
                }
                else
                {
                    cout<<"-! ERROR - too many arguments for /df trie command !-"<<endl;
                }

            }
            else if (strcmp("/tf", cmd_words[0]) == 0)
            {
                if (cmd_nwords != 3)
                {
                    cout<<"-! ERROR - wrong number of arguments for /tf trie command !-"<<endl;
                }
                else
                {
                    if(!is_number(cmd_words[1]))
                    {
                        cout<<"-! ERROR - first argument must be a number in the /tf trie command !-"<<endl;
                    }
                    else
                    {
                        int doc_id = atoi(cmd_words[1]);
                        posting_list * pl_ptr = my_trie_ptr->search_word(cmd_words[2]);
                        cout<<doc_id<<"  "<<cmd_words[2]<<"  ";
                        if(pl_ptr == NULL)
                        {
                            cout<<"0"<<endl;
                        }
                        else
                        {
                            cout<<pl_ptr->get_term_freq(doc_id)<<endl;
                        }
                    }
                }
            }
            else if (strcmp("/search", cmd_words[0]) == 0)
            {
                if (cmd_nwords == 1)
                {
                    cout<<" -! ERROR - you must enter at least one query !-"<<endl;
                }
                else
                {
                    struct winsize w;
                    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

                    unsigned int nqs;
                    if(cmd_nwords > 11)
                    {//if there are more than 10 searching queries
                        nqs = 10; //keep only the first 10
                    }
                    else
                    {
                        nqs = cmd_nwords - 1;
                    }

                    posting_list ** pl_array = (posting_list **) malloc(nqs * sizeof(posting_list *));
                    unsigned int unq_doc_ids_size = 0;
                    unsigned int * unq_doc_ids = get_unq_doc_ids(cmd_words, pl_array, my_trie_ptr, nqs, unq_doc_ids_size);

                    print_k_docs(docs, cmd_words, unq_doc_ids, docs_word_counter,
                                 pl_array, unq_doc_ids_size, nqs, avgdl, nlines, K, k1, b, w.ws_col);

                    free(pl_array);
                    free(unq_doc_ids);
                }
            }
            else
            {
                cout<<" -! ERROR - trie command not found !-"<<endl;
            }

            delete_cmd_words(cmd_words, cmd_nwords);
            free(cmd);
        }
    }

    delete_docs(docs, docs_word_counter, nlines);
    delete my_trie_ptr;
    cout<<endl<<"-> Trie has been destroyed !"<<endl;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl $0x0, -0xc(%rbp)
movl %edi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movl -0x10(%rbp), %esi
leaq -0x1c(%rbp), %rdx
callq 0x37c0
movq %rax, -0x28(%rbp)
movsd 0x3b4a(%rip), %xmm0     # 0x6010
movsd %xmm0, -0x30(%rbp)
movsd 0x3b35(%rip), %xmm0     # 0x6008
movsd %xmm0, -0x38(%rbp)
movq 0x6af1(%rip), %rdi      # 0x8fd0
leaq 0x3b32(%rip), %rsi      # 0x6018
callq 0x20c0
movq %rax, %rdi
movq 0x6acb(%rip), %rsi      # 0x8fc0
callq 0x2100
movq 0x6acf(%rip), %rdi      # 0x8fd0
leaq 0x3b2c(%rip), %rsi      # 0x6034
callq 0x20c0
movq %rax, %rdi
movl -0x1c(%rbp), %esi
callq 0x2170
movq %rax, %rdi
movq 0x6a9e(%rip), %rsi      # 0x8fc0
callq 0x2100
movq 0x6aa2(%rip), %rdi      # 0x8fd0
leaq 0x3b07(%rip), %rsi      # 0x603c
callq 0x20c0
movq %rax, %rdi
movq -0x28(%rbp), %rsi
callq 0x20c0
movq %rax, %rdi
movq 0x6a70(%rip), %rsi      # 0x8fc0
callq 0x2100
movq %rax, %rdi
movq 0x6a61(%rip), %rsi      # 0x8fc0
callq 0x2100
movq -0x28(%rbp), %rdi
callq 0x2c40
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x2ca0
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x2160
movq %rax, -0x50(%rbp)
movl -0x44(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x2160
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x25ca
movq -0x50(%rbp), %rax
movl -0x5c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x25a8
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x2d70
andb $0x1, %al
movb %al, -0x5d(%rbp)
movq -0x40(%rbp), %rdi
callq 0x20d0
movb -0x5d(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x260c
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl -0x44(%rbp), %edx
callq 0x3150
movl $0x1, %edi
callq 0x2140
movl $0x10, %edi
callq 0x20e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
callq 0x45d0
jmp 0x262a
movq -0xe0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x44(%rbp), %ecx
callq 0x2f40
movl %eax, -0x78(%rbp)
movq 0x697d(%rip), %rdi      # 0x8fd0
movq %rdi, -0xf0(%rbp)
leaq 0x39e8(%rip), %rsi      # 0x6049
callq 0x20c0
movq %rax, %rdi
movq 0x6950(%rip), %rsi      # 0x8fc0
movq %rsi, -0xe8(%rbp)
callq 0x2100
movq -0xf0(%rbp), %rdi
leaq 0x39da(%rip), %rsi      # 0x6064
callq 0x20c0
movq %rax, %rdi
movl -0x44(%rbp), %esi
callq 0x2170
movq -0xe8(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movq -0xf0(%rbp), %rdi
leaq 0x39c1(%rip), %rsi      # 0x6078
callq 0x20c0
movq %rax, %rdi
movl -0x78(%rbp), %esi
callq 0x2170
movq -0xe8(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movq -0xe8(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movl -0x78(%rbp), %eax
cvtsi2sd %rax, %xmm0
movl -0x44(%rbp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movq 0x68cb(%rip), %rdi      # 0x8fd0
leaq 0x3987(%rip), %rsi      # 0x6093
callq 0x20c0
movq %rax, %rdi
movq 0x68a5(%rip), %rsi      # 0x8fc0
callq 0x2100
movl $0x3, %edi
callq 0x31c0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x2070
cmpq $0x0, %rax
jne 0x2773
movq -0x88(%rbp), %rdi
callq 0x2130
jmp 0x26fe
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
movl $0x10, %esi
callq 0x20f0
jmp 0x2c36
movq -0x88(%rbp), %rsi
leaq 0x3932(%rip), %rdi      # 0x60b3
callq 0x2190
cmpl $0x0, %eax
jne 0x279c
movq -0x88(%rbp), %rdi
callq 0x2130
jmp 0x2bbb
movq -0x88(%rbp), %rdi
leaq -0x8c(%rbp), %rsi
callq 0x3030
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq (%rax), %rsi
leaq 0x38f2(%rip), %rdi      # 0x60b9
callq 0x2190
cmpl $0x0, %eax
jne 0x28be
cmpl $0x1, -0x8c(%rbp)
jne 0x27ee
movq -0x68(%rbp), %rdi
xorl %esi, %esi
callq 0x4980
jmp 0x28b9
cmpl $0x2, -0x8c(%rbp)
jne 0x2895
movq -0x68(%rbp), %rdi
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x4ac0
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rsi
movq 0x67a8(%rip), %rdi      # 0x8fd0
callq 0x20c0
movq %rax, %rdi
leaq 0x3ccd(%rip), %rsi      # 0x6504
callq 0x20c0
cmpq $0x0, -0xa0(%rbp)
jne 0x286a
movq 0x6783(%rip), %rdi      # 0x8fd0
leaq 0x3869(%rip), %rsi      # 0x60bd
callq 0x20c0
movq %rax, %rdi
movq 0x675d(%rip), %rsi      # 0x8fc0
callq 0x2100
jmp 0x2893
movq -0xa0(%rbp), %rdi
callq 0x4d90
movl %eax, %esi
movq 0x6751(%rip), %rdi      # 0x8fd0
callq 0x2170
movq %rax, %rdi
movq 0x6732(%rip), %rsi      # 0x8fc0
callq 0x2100
jmp 0x28b7
movq 0x6734(%rip), %rdi      # 0x8fd0
leaq 0x381c(%rip), %rsi      # 0x60bf
callq 0x20c0
movq %rax, %rdi
movq 0x670e(%rip), %rsi      # 0x8fc0
callq 0x2100
jmp 0x28b9
jmp 0x2b98
movq -0x98(%rbp), %rax
movq (%rax), %rsi
leaq 0x3826(%rip), %rdi      # 0x60f5
callq 0x2190
cmpl $0x0, %eax
jne 0x2a22
cmpl $0x3, -0x8c(%rbp)
je 0x290d
movq 0x66e3(%rip), %rdi      # 0x8fd0
leaq 0x3805(%rip), %rsi      # 0x60f9
callq 0x20c0
movq %rax, %rdi
movq 0x66bd(%rip), %rsi      # 0x8fc0
callq 0x2100
jmp 0x2a1d
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2d00
testb $0x1, %al
jne 0x2948
movq 0x66a8(%rip), %rdi      # 0x8fd0
leaq 0x3807(%rip), %rsi      # 0x6136
callq 0x20c0
movq %rax, %rdi
movq 0x6682(%rip), %rsi      # 0x8fc0
callq 0x2100
jmp 0x2a1b
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x21f0
movl %eax, -0xa4(%rbp)
movq -0x68(%rbp), %rdi
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x4ac0
movq %rax, -0xb0(%rbp)
movl -0xa4(%rbp), %esi
movq 0x664a(%rip), %rdi      # 0x8fd0
callq 0x2240
movq %rax, %rdi
leaq 0x3b6f(%rip), %rsi      # 0x6504
callq 0x20c0
movq %rax, %rdi
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x20c0
movq %rax, %rdi
leaq 0x3b4d(%rip), %rsi      # 0x6504
callq 0x20c0
cmpq $0x0, -0xb0(%rbp)
jne 0x29ea
movq 0x6603(%rip), %rdi      # 0x8fd0
leaq 0x36e9(%rip), %rsi      # 0x60bd
callq 0x20c0
movq %rax, %rdi
movq 0x65dd(%rip), %rsi      # 0x8fc0
callq 0x2100
jmp 0x2a19
movq -0xb0(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x4fd0
movl %eax, %esi
movq 0x65cb(%rip), %rdi      # 0x8fd0
callq 0x2170
movq %rax, %rdi
movq 0x65ac(%rip), %rsi      # 0x8fc0
callq 0x2100
jmp 0x2a1b
jmp 0x2a1d
jmp 0x2b96
movq -0x98(%rbp), %rax
movq (%rax), %rsi
leaq 0x3749(%rip), %rdi      # 0x617c
callq 0x2190
cmpl $0x0, %eax
jne 0x2b72
cmpl $0x1, -0x8c(%rbp)
jne 0x2a71
movq 0x657f(%rip), %rdi      # 0x8fd0
leaq 0x372c(%rip), %rsi      # 0x6184
callq 0x20c0
movq %rax, %rdi
movq 0x6559(%rip), %rsi      # 0x8fc0
callq 0x2100
jmp 0x2b70
movl $0x1, %edi
movl $0x5413, %esi           # imm = 0x5413
leaq -0xb8(%rbp), %rdx
movb $0x0, %al
callq 0x21b0
cmpl $0xb, -0x8c(%rbp)
jbe 0x2a9e
movl $0xa, -0xbc(%rbp)
jmp 0x2aad
movl -0x8c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x2160
movq %rax, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movq -0x98(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl -0xbc(%rbp), %ecx
leaq -0xcc(%rbp), %r8
callq 0x3350
movq %rax, -0xd8(%rbp)
movq -0x50(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0xd8(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0xc8(%rbp), %r8
movl -0xcc(%rbp), %r9d
movl -0xbc(%rbp), %ebx
movsd -0x80(%rbp), %xmm0
movl -0x44(%rbp), %r11d
movl -0x1c(%rbp), %r10d
movsd -0x30(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
movzwl -0xb6(%rbp), %eax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x3b60
movq -0xc8(%rbp), %rdi
callq 0x2130
movq -0xd8(%rbp), %rdi
callq 0x2130
jmp 0x2b94
movq 0x6457(%rip), %rdi      # 0x8fd0
leaq 0x3635(%rip), %rsi      # 0x61b5
callq 0x20c0
movq %rax, %rdi
movq 0x6431(%rip), %rsi      # 0x8fc0
callq 0x2100
jmp 0x2b96
jmp 0x2b98
movq -0x98(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0x3100
movq -0x88(%rbp), %rdi
callq 0x2130
jmp 0x26fe
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl -0x44(%rbp), %edx
callq 0x3150
movq -0x68(%rbp), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, %rax
je 0x2bf9
movq -0xf8(%rbp), %rdi
callq 0x45f0
movq -0xf8(%rbp), %rdi
movl $0x10, %esi
callq 0x20f0
movq 0x63d0(%rip), %rdi      # 0x8fd0
movq 0x63b9(%rip), %rsi      # 0x8fc0
callq 0x2100
movq %rax, %rdi
leaq 0x35c5(%rip), %rsi      # 0x61db
callq 0x20c0
movq %rax, %rdi
movq 0x639b(%rip), %rsi      # 0x8fc0
callq 0x2100
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x2250
nop