// File: /workspace/github_cpp_makefile_star2_O1/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 0x2c(%rsp), %r15
movq %rsi, %rdi
movl %eax, %esi
movq %r15, %rdx
callq 0x3546
movq %rax, %r14
movq 0x5b38(%rip), %rbx      # 0x7fd0
leaq 0x2b79(%rip), %rsi      # 0x5018
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
leaq 0x2b5a(%rip), %rsi      # 0x5034
movl $0x7, %edx
movq %rbx, %rdi
callq 0x2120
movl (%r15), %esi
movq %rbx, %rdi
callq 0x2090
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
leaq 0x2b19(%rip), %rsi      # 0x503c
movl $0xc, %edx
movq %rbx, %rdi
callq 0x2120
testq %r14, %r14
je 0x2551
movq %r14, %rdi
callq 0x2050
movq 0x5a8c(%rip), %rdi      # 0x7fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x2120
jmp 0x2568
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movq %r14, %rdi
callq 0x2c9c
movq %rax, %r14
movq %rax, %rdi
callq 0x2cb5
movl %eax, %ebp
movl %eax, %r12d
leaq (,%r12,8), %r15
movq %r15, %rdi
callq 0x2170
movq %rax, %r13
movq %r12, 0x20(%rsp)
leaq (,%r12,4), %rdi
callq 0x2170
movq %rax, 0x18(%rsp)
testl %ebp, %ebp
je 0x260c
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2080
movq %r13, %rdi
movq %r14, %rsi
callq 0x2d34
movl %eax, %ebp
movq %r14, %rdi
callq 0x20f0
testb %bpl, %bpl
jne 0x2c65
movl $0x10, %edi
callq 0x2100
movq %rax, %r14
movq %rax, %rdi
callq 0x40e0
movq %r14, 0x8(%rsp)
movq %r14, %rdi
movq %r13, 0x30(%rsp)
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %r12
movl %r12d, %ecx
callq 0x2ecf
movl %eax, %ebp
leaq 0x29df(%rip), %rsi      # 0x5049
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
leaq 0x29bf(%rip), %rsi      # 0x5064
movl $0x13, %edx
movq %rbx, %rdi
callq 0x2120
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2090
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
leaq 0x298a(%rip), %rsi      # 0x5078
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x2120
movl %ebp, %r14d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2090
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
cvtsi2sd %r14, %xmm1
cvtsi2sd %r12, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
leaq 0x291b(%rip), %rbp      # 0x5093
leaq 0x2934(%rip), %r12      # 0x50b3
movl $0x1f, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movl $0x3, %edi
callq 0x30a9
movq %rax, %r13
cmpb $0x0, (%rax)
je 0x283f
movq %r12, %rdi
movq %r13, %rsi
callq 0x2190
testl %eax, %eax
je 0x2bb9
movq %r13, %rdi
leaq 0x4(%rsp), %rsi
callq 0x2f97
movq %rax, %r15
movq (%rax), %r14
leaq 0x28c4(%rip), %rdi      # 0x50b9
movq %r14, %rsi
callq 0x2190
testl %eax, %eax
je 0x2844
leaq 0x28ed(%rip), %rdi      # 0x50f5
movq %r14, %rsi
callq 0x2190
testl %eax, %eax
je 0x286b
leaq 0x2961(%rip), %rdi      # 0x517c
movq %r14, %rsi
callq 0x2190
testl %eax, %eax
je 0x28f0
movl $0x25, %edx
movq %rbx, %rdi
leaq 0x297b(%rip), %rsi      # 0x51b5
jmp 0x2abe
jmp 0x2af6
movl 0x4(%rsp), %eax
cmpl $0x2, %eax
je 0x290b
cmpl $0x1, %eax
jne 0x2944
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x42f4
jmp 0x2aea
cmpl $0x3, 0x4(%rsp)
jne 0x2958
movq 0x8(%r15), %rdi
callq 0x2cf7
testb %al, %al
je 0x2a36
movq 0x8(%r15), %rdi
callq 0x21e0
movl %eax, %ebp
movq 0x10(%r15), %rsi
movq 0x8(%rsp), %rdi
callq 0x43f4
movq %rax, %r12
movq %rbx, %rdi
movl %ebp, 0x28(%rsp)
movl %ebp, %esi
callq 0x2250
movq %rax, %rbp
movl $0x2, %edx
movq %rax, %rdi
leaq 0x2c5d(%rip), %rsi      # 0x5520
callq 0x2120
movq 0x10(%r15), %r14
testq %r14, %r14
je 0x2b03
movq %r14, %rdi
callq 0x2050
movq %rbp, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2120
jmp 0x2b1d
cmpl $0x1, 0x4(%rsp)
jne 0x296c
movl $0x30, %edx
movq %rbx, %rdi
leaq 0x287e(%rip), %rsi      # 0x5184
jmp 0x2abe
movq 0x8(%r15), %rsi
movq 0x8(%rsp), %rdi
callq 0x43f4
movq %rax, %r14
movq 0x8(%r15), %r12
testq %r12, %r12
je 0x2a47
movq %r12, %rdi
callq 0x2050
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2120
jmp 0x2a5e
movl $0x35, %edx
movq %rbx, %rdi
leaq 0x276c(%rip), %rsi      # 0x50bf
jmp 0x2abe
movl $0x3c, %edx
movq %rbx, %rdi
leaq 0x2792(%rip), %rsi      # 0x50f9
jmp 0x2abe
movl $0x5413, %esi           # imm = 0x5413
movl $0x1, %edi
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x21b0
movl 0x4(%rsp), %eax
leal -0x1(%rax), %r14d
cmpl $0xc, %eax
movl $0xa, %eax
cmovael %eax, %r14d
leaq (,%r14,8), %rdi
callq 0x2170
movq %rax, %r12
movl $0x0, 0x14(%rsp)
movq %r15, %rdi
movq %rax, %rsi
movq 0x8(%rsp), %rdx
movl %r14d, %ecx
leaq 0x14(%rsp), %r8
callq 0x31bd
movq %rax, %rbp
movl 0x14(%rsp), %r9d
movl 0x2c(%rsp), %eax
movzwl 0x42(%rsp), %r10d
movq 0x30(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
movq %r12, %r8
movsd 0x38(%rsp), %xmm0
movsd 0x260f(%rip), %xmm1     # 0x5008
movsd 0x260f(%rip), %xmm2     # 0x5010
pushq %r10
pushq %rax
pushq 0x30(%rsp)
pushq %r14
callq 0x3836
addq $0x20, %rsp
movq %r12, %rdi
leaq 0x2696(%rip), %r12      # 0x50b3
callq 0x2140
movq %rbp, %rdi
leaq 0x2667(%rip), %rbp      # 0x5093
callq 0x2140
jmp 0x2aea
movl $0x45, %edx
movq %rbx, %rdi
leaq 0x26f1(%rip), %rsi      # 0x5136
jmp 0x2abe
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x2240
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x2ab3(%rip), %rsi      # 0x5520
callq 0x2120
testq %r14, %r14
leaq 0x2637(%rip), %r12      # 0x50b3
je 0x2aaf
movq %r14, %rdi
callq 0x4674
movl %eax, %esi
movq %rbx, %rdi
callq 0x2090
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %r14, %rdi
jmp 0x2add
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x25ff(%rip), %rsi      # 0x50bd
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movl 0x4(%rsp), %esi
movq %r15, %rdi
callq 0x3030
movq %r13, %rdi
callq 0x2140
jmp 0x277f
movq (%rbp), %rax
movq -0x18(%rax), %rax
movq %rbp, %rdi
addq %rax, %rdi
movl 0x20(%rbp,%rax), %esi
orl $0x1, %esi
callq 0x2240
movl $0x2, %edx
movq %rbp, %rdi
leaq 0x29f4(%rip), %rsi      # 0x5520
callq 0x2120
testq %r12, %r12
je 0x2b6b
movq %r12, %rdi
movl 0x28(%rsp), %esi
callq 0x47a4
movl %eax, %esi
movq %rbx, %rdi
callq 0x2090
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %r14, %rdi
jmp 0x2b99
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x2543(%rip), %rsi      # 0x50bd
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
leaq 0x24e6(%rip), %rbp      # 0x5093
leaq 0x24ff(%rip), %r12      # 0x50b3
jmp 0x2aea
movq %r13, %rdi
callq 0x2140
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x3060
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x40f0
movl $0x10, %esi
movq %r14, %rdi
callq 0x2110
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movq %rax, %rbx
leaq 0x25bb(%rip), %rsi      # 0x51db
movl $0x1c, %edx
movq %rax, %rdi
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x3060
movl $0x1, %edi
callq 0x2150
movq %rax, %rbx
movl $0x10, %esi
movq %r14, %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x2260
nopl (%rax)