// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edi, %eax
leaq 0x24(%rsp), %r15
movq %rsi, %rdi
movl %eax, %esi
movq %r15, %rdx
callq 0x305e
movq %rax, %rbx
movq 0x4bb8(%rip), %r14      # 0x6fd0
leaq 0x1bf9(%rip), %rsi      # 0x4018
movq %r14, %rdi
callq 0x20a0
movq %rax, %rdi
callq 0x2050
leaq 0x1bfe(%rip), %rsi      # 0x4034
movq %r14, %rdi
callq 0x20a0
movl (%r15), %esi
movq %rax, %rdi
callq 0x2070
movq %rax, %rdi
callq 0x2050
leaq 0x1be4(%rip), %rsi      # 0x403c
movq %r14, %rdi
callq 0x20a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20a0
movq %rax, %rdi
callq 0x2050
movq %rax, %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x2918
movq %rax, %r15
movq %rax, %rdi
callq 0x2950
movl %eax, %r14d
leaq (,%r14,8), %rdi
callq 0x2130
movq %rax, %r12
leaq (,%r14,4), %rdi
callq 0x2130
movq %rax, 0x10(%rsp)
xorl %eax, %eax
cmpq %rax, %r14
je 0x24c4
andq $0x0, (%r12,%rax,8)
incq %rax
jmp 0x24b5
movq %r12, %rdi
movq %r15, %rsi
callq 0x29b4
movl %eax, %ebx
movq %r15, %rdi
callq 0x20b0
testb %bl, %bl
jne 0x28ea
pushq $0x10
popq %rdi
callq 0x20c0
movq %rax, %r15
movq %rax, %rdi
callq 0x3824
movq %r15, %rdi
movq %r12, 0x28(%rsp)
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movl %r14d, %ecx
callq 0x2ac3
movl %eax, %ebp
movq 0x4abb(%rip), %r13      # 0x6fd0
leaq 0x1b2d(%rip), %rsi      # 0x4049
movq %r13, %rdi
callq 0x20a0
movq %rax, %rdi
callq 0x2050
leaq 0x1b31(%rip), %rsi      # 0x4064
movq %r13, %rdi
callq 0x20a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x2070
movq %rax, %rdi
callq 0x2050
leaq 0x1b23(%rip), %rsi      # 0x4078
movq %r13, %rdi
callq 0x20a0
movl %ebp, %ebx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2070
movq %rax, %rdi
callq 0x2050
movq %rax, %rdi
callq 0x2050
cvtsi2sd %rbx, %xmm1
movq %r14, 0x30(%rsp)
cvtsi2sd %r14, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
leaq 0x1af9(%rip), %r14      # 0x4093
movq %r15, 0x18(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x20a0
movq %rax, %rdi
callq 0x2050
pushq $0x3
popq %rdi
callq 0x2c69
movq %rax, %r12
cmpb $0x0, (%rax)
je 0x2638
leaq 0x1aea(%rip), %rdi      # 0x40b3
movq %r12, %rsi
callq 0x2150
testl %eax, %eax
je 0x2887
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0x2b78
movq %rax, %rbp
movq (%rax), %rbx
leaq 0x1ac6(%rip), %rdi      # 0x40b9
movq %rbx, %rsi
callq 0x2150
testl %eax, %eax
je 0x263d
leaq 0x1aef(%rip), %rdi      # 0x40f5
movq %rbx, %rsi
callq 0x2150
testl %eax, %eax
je 0x2662
leaq 0x1b63(%rip), %rdi      # 0x417c
movq %rbx, %rsi
callq 0x2150
testl %eax, %eax
je 0x26f1
movq %r13, %rdi
leaq 0x1b82(%rip), %rsi      # 0x41b5
jmp 0x2843
jmp 0x285c
movl 0x8(%rsp), %eax
cmpl $0x2, %eax
je 0x2707
cmpl $0x1, %eax
jne 0x2751
movq %r15, %rdi
xorl %esi, %esi
callq 0x39a0
jmp 0x2850
cmpl $0x3, 0x8(%rsp)
jne 0x2760
movq 0x8(%rbp), %rdi
callq 0x298b
testb %al, %al
je 0x282d
movq 0x8(%rbp), %rdi
callq 0x21a0
movq %r15, %rdi
movl %eax, %r15d
movq 0x10(%rbp), %rsi
callq 0x3a4c
movq %rax, %rbx
movq %r13, %rdi
movl %r15d, %esi
callq 0x2200
movq %rax, %rdi
leaq 0x1e58(%rip), %rsi      # 0x4506
callq 0x20a0
movq 0x10(%rbp), %rsi
movq %rax, %rdi
callq 0x20a0
movq %rax, %rdi
leaq 0x1e3d(%rip), %rsi      # 0x4506
callq 0x20a0
testq %rbx, %rbx
je 0x2869
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3cc8
movl %eax, %esi
movq %r13, %rdi
callq 0x2070
jmp 0x2878
cmpl $0x1, 0x8(%rsp)
jne 0x276f
movq %r13, %rdi
leaq 0x1a82(%rip), %rsi      # 0x4184
jmp 0x2843
movq 0x8(%rbp), %rsi
movq %r15, %rdi
callq 0x3a4c
movq %rax, %rbx
movq 0x8(%rbp), %rsi
movq %r13, %rdi
callq 0x20a0
movq %rax, %rdi
leaq 0x1dda(%rip), %rsi      # 0x4506
callq 0x20a0
testq %rbx, %rbx
je 0x2839
movq %rbx, %rdi
callq 0x3be4
movl %eax, %esi
movq %r13, %rdi
callq 0x2070
jmp 0x2848
movq %r13, %rdi
leaq 0x1964(%rip), %rsi      # 0x40bf
jmp 0x2843
movq %r13, %rdi
leaq 0x198f(%rip), %rsi      # 0x40f9
jmp 0x2843
movl $0x5413, %esi           # imm = 0x5413
pushq $0x1
popq %rdi
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
movl 0x8(%rsp), %eax
leal -0x1(%rax), %r15d
cmpl $0xc, %eax
pushq $0xa
popq %rax
cmovael %eax, %r15d
leaq (,%r15,8), %rdi
callq 0x2130
movq %rax, %r14
andl $0x0, 0xc(%rsp)
movq %rbp, %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rdx
movl %r15d, %ecx
leaq 0xc(%rsp), %r8
callq 0x2d54
movq %rax, %rbx
movl 0xc(%rsp), %r9d
movl 0x24(%rsp), %eax
movzwl 0x42(%rsp), %r10d
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
movsd 0x38(%rsp), %xmm0
movsd 0x1813(%rip), %xmm1     # 0x4008
movsd 0x1813(%rip), %xmm2     # 0x4010
pushq %r10
pushq %rax
pushq 0x40(%rsp)
pushq %r15
movq 0x38(%rsp), %r15
callq 0x3282
addq $0x20, %rsp
movq %r14, %rdi
leaq 0x1875(%rip), %r14      # 0x4093
callq 0x2100
movq %rbx, %rdi
callq 0x2100
jmp 0x2850
movq %r13, %rdi
leaq 0x18ff(%rip), %rsi      # 0x4136
jmp 0x2843
movq %r13, %rdi
leaq 0x187a(%rip), %rsi      # 0x40bd
callq 0x20a0
movq %rax, %rdi
callq 0x2050
movl 0x8(%rsp), %esi
movq %rbp, %rdi
callq 0x2bf9
movq %r12, %rdi
callq 0x2100
jmp 0x259f
movq %r13, %rdi
leaq 0x184a(%rip), %rsi      # 0x40bd
callq 0x20a0
movq %rax, %rdi
callq 0x2050
movq 0x18(%rsp), %r15
jmp 0x2850
movq %r12, %rdi
callq 0x2100
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x2c27
movq %r15, %rdi
callq 0x382e
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x20d0
movq 0x4713(%rip), %rdi      # 0x6fd0
callq 0x2050
leaq 0x1912(%rip), %rsi      # 0x41db
movq %rax, %rdi
callq 0x20a0
movq %rax, %rdi
callq 0x2050
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movl %r14d, %edx
callq 0x2c27
pushq $0x1
popq %rdi
callq 0x2110
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x2210