// File: JohnPapad[P]Mini-Search-Engine/minisearch.cpp
// Function: print_k_docs(char**, char**, unsigned int*, unsigned int*, posting_list**, unsigned int, unsigned int, double, unsigned int, unsigned int, double, double, unsigned int)
void print_k_docs(char ** docs, char ** queries, unsigned int * unq_doc_ids, unsigned int * docs_word_counter, posting_list ** pl_array,
     unsigned int unq_doc_ids_size, unsigned int nqs, double avgdl, unsigned int ndocs, unsigned int K, double k1,
     double b, unsigned int lwidth)
{
    double * unq_docs_relev_score = (double *) malloc(unq_doc_ids_size * sizeof(double)); //stores each unique doc's score

    for(unsigned int i = 0; i < unq_doc_ids_size; i++)
    {//for each unique doc
        double total_relev_score = 0.0 ;
        for(unsigned int j = 0 ; j < nqs; j++)
        {//for each query
            posting_list * query_pl_ptr = pl_array[j];
            unsigned int term_freq, doc_freq ;

            if(query_pl_ptr == NULL)
            {
                term_freq = 0;
                doc_freq = 0;
            }
            else
            {
                term_freq = query_pl_ptr->get_term_freq(unq_doc_ids[i]);
                doc_freq = query_pl_ptr->get_size();
            }

            total_relev_score += get_relevance_score(term_freq, doc_freq, avgdl, docs_word_counter[unq_doc_ids[i]], ndocs, k1, b);
        }

        unq_docs_relev_score[i] = total_relev_score;
    }


    //sorting unique doc ids and their corresponding relevance scores at the same time based on scores' values
    for(unsigned int i = 0; i < unq_doc_ids_size; i++)
    {
        for(unsigned int j = i+1; j < unq_doc_ids_size; j++)
        {
            if(unq_docs_relev_score[i] < unq_docs_relev_score[j])
            {
                double temp = unq_docs_relev_score[i];
                unsigned int temp2 = unq_doc_ids[i];

                unq_docs_relev_score[i] = unq_docs_relev_score[j];
                unq_docs_relev_score[j] = temp;

                unq_doc_ids[i] = unq_doc_ids[j];
                unq_doc_ids[j] = temp2;
            }
        }
    }

    for(unsigned int i = 0 ; i < unq_doc_ids_size; i++)
    {//for each unique doc
        if (i == K) //print only the top K docs
        {
            break;
        }

        //calculating the width for doc's info (score, doc id, index, etc )
        unsigned int doc_id_len = get_number_of_digits(int(unq_doc_ids[i]));
        unsigned int i_len = get_number_of_digits(int(i));
        double integral;
        modf(unq_docs_relev_score[i], &integral);
        unsigned int score_dec_part_len = get_number_of_digits(int(integral));
        unsigned int init_llen= doc_id_len + i_len + score_dec_part_len + 8 + 5 + 1;
        if(unq_docs_relev_score[i] < 0)
        {
            init_llen++;
        }

        char * doc = docs[unq_doc_ids[i]];
        char * doc_cpy = (char *)malloc((strlen(doc) + 1) * sizeof(char));
        strcpy(doc_cpy, doc);
        char *rest_line;
        char *curr_word = strtok_r(doc_cpy, " \t", &rest_line);

        int ** underline_info = NULL; //indicates which words needs underlining
        int underline_info_size = 0;
        unsigned int curr_llen = init_llen; //indicates how nuch of terminal's usefull printing space is occupied at every moment
        bool next_word = 0;

        cout.precision(5);
        cout<<i+1<<".( "<<unq_doc_ids[i]<<") ["<<fixed<< unq_docs_relev_score[i]  <<"] ";

            while(curr_word != NULL)
            {
                if ((strlen(curr_word) > (lwidth - init_llen)) && (curr_llen != lwidth))
                {//if word's length is bigger than terminal's printing space
                    unsigned int init_curr_llen = curr_llen;
                    bool new_line = 0 ;
                    bool word_exist = search_doc_word_into_queries(curr_word, queries, nqs);

                    for(unsigned int i = 0; i < strlen(curr_word); i++)
                    {//printing word letter by letter - word will be seperated in different lines
                        if ((curr_llen + 1) > lwidth)
                        {
                            if (word_exist == 1)
                            {
                                if (new_line == 0)
                                {//if word has not yet been seperated in different lines
                                    underline_info_size += 2;
                                    underline_info = reallocate_underline_info(underline_info, underline_info_size, init_curr_llen, lwidth);
                                }
                                else
                                {
                                    underline_info_size = 2;
                                    underline_info = reallocate_underline_info(underline_info, underline_info_size, init_llen, lwidth);
                                }

                                underline(init_llen, curr_llen, underline_info, underline_info_size, 0);
                            }
                            else
                            {
                                if (underline_info_size > 0)
                                {
                                    underline(init_llen, curr_llen, underline_info, underline_info_size, 0);
                                }
                                else
                                {
                                    init_newline(init_llen, curr_llen);
                                }
                            }

                            new_line = 1;
                        }

                        curr_llen++;
                        cout<<curr_word[i];
                    }

                    if (word_exist == 1)
                    {
                        underline_info_size = 2;
                        underline_info = reallocate_underline_info(underline_info, underline_info_size, init_llen, curr_llen);
                    }

                    bool exit = manage_whitespace(rest_line, lwidth, init_llen, curr_llen, underline_info, underline_info_size);
                    if(exit == 1)
                    {
                        break;
                    }

                    next_word = 1;
                }
                else if (strlen(curr_word) <= (lwidth - curr_llen))
                {//if word can fit in remaining terminal's line space
                    bool word_exist = search_doc_word_into_queries(curr_word, queries, nqs);
                    if (word_exist == 1)
                    {
                        underline_info_size += 2;
                        underline_info = reallocate_underline_info(underline_info, underline_info_size, curr_llen, curr_llen + strlen(curr_word));
                    }

                    cout<<curr_word;
                    curr_llen += strlen(curr_word);
                    next_word = 1;

                    bool exit = manage_whitespace(rest_line, lwidth, init_llen, curr_llen, underline_info, underline_info_size);
                    if (exit == 1)
                    {
                        break;
                    }
                }
                else
                {//if word can not fit in remaining terminal's line space
                    next_word = 0;

                    if (underline_info_size > 0)
                    {
                        underline(init_llen, curr_llen, underline_info, underline_info_size, 0);
                    }
                    else
                    {
                        init_newline(init_llen, curr_llen);
                    }
                }

                if(next_word == 1)
                {//take next word for printing
                    curr_word = strtok_r(rest_line, " \t", &rest_line);
                }
            }

            cout<<endl;
            for(unsigned int i = 0; i < lwidth; i++)
            {
                cout<<"=";
            }
            cout<<endl;

            free(underline_info);
            free(doc_cpy);
    }

    free(unq_docs_relev_score);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movsd %xmm0, -0x38(%rbp)
movsd %xmm1, -0x40(%rbp)
movsd %xmm2, -0x48(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x2160
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x3c8b
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl 0x10(%rbp), %eax
jae 0x3c6c
movq -0x28(%rbp), %rax
movl -0x64(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0x3c04
movl $0x0, -0x74(%rbp)
movl $0x0, -0x78(%rbp)
jmp 0x3c26
movq -0x70(%rbp), %rdi
movq -0x18(%rbp), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,4), %esi
callq 0x4fd0
movl %eax, -0x74(%rbp)
movq -0x70(%rbp), %rdi
callq 0x4d90
movl %eax, -0x78(%rbp)
movl -0x74(%rbp), %edi
movl -0x78(%rbp), %esi
movsd -0x38(%rbp), %xmm0
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
movl -0x54(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %edx
movl 0x18(%rbp), %ecx
movsd -0x40(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
callq 0x32c0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x3bd2
movsd -0x60(%rbp), %xmm0
movq -0x50(%rbp), %rax
movl -0x54(%rbp), %ecx
movsd %xmm0, (%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x3bb7
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x3d65
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x3d55
movq -0x50(%rbp), %rax
movl -0x7c(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm1
movq -0x50(%rbp), %rax
movl -0x80(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3d45
movq -0x50(%rbp), %rax
movl -0x7c(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x18(%rbp), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x8c(%rbp)
movq -0x50(%rbp), %rax
movl -0x80(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
movq -0x50(%rbp), %rax
movl -0x7c(%rbp), %ecx
movsd %xmm0, (%rax,%rcx,8)
movsd -0x88(%rbp), %xmm0
movq -0x50(%rbp), %rax
movl -0x80(%rbp), %ecx
movsd %xmm0, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movl -0x80(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x18(%rbp), %rax
movl -0x7c(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x8c(%rbp), %edx
movq -0x18(%rbp), %rax
movl -0x80(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x3d47
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x3ca7
jmp 0x3d57
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x3c92
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x442a
movl -0x90(%rbp), %eax
cmpl 0x20(%rbp), %eax
jne 0x3d8e
jmp 0x442a
movq -0x18(%rbp), %rax
movl -0x90(%rbp), %ecx
movl (%rax,%rcx,4), %edi
callq 0x33f0
movl %eax, -0x94(%rbp)
movl -0x90(%rbp), %edi
callq 0x33f0
movl %eax, -0x98(%rbp)
movq -0x50(%rbp), %rax
movl -0x90(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
leaq -0xa0(%rbp), %rdi
callq 0x2260
cvttsd2si -0xa0(%rbp), %edi
callq 0x33f0
movl %eax, -0xa4(%rbp)
movl -0x94(%rbp), %eax
addl -0x98(%rbp), %eax
addl -0xa4(%rbp), %eax
addl $0x8, %eax
addl $0x5, %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
movq -0x50(%rbp), %rax
movl -0x90(%rbp), %ecx
xorps %xmm0, %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x3e29
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
movq -0x8(%rbp), %rax
movq -0x18(%rbp), %rcx
movl -0x90(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x2070
movq %rax, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x2160
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x20b0
movq -0xb8(%rbp), %rdi
leaq 0x23a7(%rip), %rsi      # 0x6230
leaq -0xc0(%rbp), %rdx
callq 0x2270
movq %rax, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
movl $0x0, -0xd4(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0xd8(%rbp)
movb $0x0, -0xd9(%rbp)
movq 0x5105(%rip), %rax      # 0x8fd0
movq (%rax), %rax
movq 0x50fb(%rip), %rdi      # 0x8fd0
addq -0x18(%rax), %rdi
movl $0x5, %esi
callq 0x4460
movl -0x90(%rbp), %esi
addl $0x1, %esi
movq 0x50dd(%rip), %rdi      # 0x8fd0
callq 0x2170
movq %rax, %rdi
leaq 0x2555(%rip), %rsi      # 0x6457
callq 0x20c0
movq %rax, %rdi
movq -0x18(%rbp), %rax
movl -0x90(%rbp), %ecx
movl (%rax,%rcx,4), %esi
callq 0x2170
movq %rax, %rdi
leaq 0x2535(%rip), %rsi      # 0x645b
callq 0x20c0
movq %rax, %rdi
leaq 0x55b(%rip), %rsi       # 0x4490
callq 0x2050
movq %rax, %rdi
movq -0x50(%rbp), %rax
movl -0x90(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
callq 0x2280
movq %rax, %rdi
leaq 0x2504(%rip), %rsi      # 0x645f
callq 0x20c0
cmpq $0x0, -0xc8(%rbp)
je 0x439f
movq -0xc8(%rbp), %rdi
callq 0x2070
movl 0x28(%rbp), %ecx
subl -0xa8(%rbp), %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x41d6
movl -0xd8(%rbp), %eax
cmpl 0x28(%rbp), %eax
je 0x41d6
movl -0xd8(%rbp), %eax
movl %eax, -0xe0(%rbp)
movb $0x0, -0xe1(%rbp)
movq -0xc8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl 0x10(%rbp), %edx
callq 0x3b00
andb $0x1, %al
movb %al, -0xe2(%rbp)
movl $0x0, -0xe8(%rbp)
movl -0xe8(%rbp), %eax
movq %rax, -0xf8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x2070
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
jae 0x4144
movl -0xd8(%rbp), %eax
addl $0x1, %eax
cmpl 0x28(%rbp), %eax
jbe 0x4104
movb -0xe2(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x40bb
movb -0xe1(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x406a
movl -0xd4(%rbp), %eax
addl $0x2, %eax
movl %eax, -0xd4(%rbp)
movq -0xd0(%rbp), %rdi
movl -0xd4(%rbp), %esi
movl -0xe0(%rbp), %edx
movl 0x28(%rbp), %ecx
callq 0x3a60
movq %rax, -0xd0(%rbp)
jmp 0x4096
movl $0x2, -0xd4(%rbp)
movq -0xd0(%rbp), %rdi
movl -0xd4(%rbp), %esi
movl -0xa8(%rbp), %edx
movl 0x28(%rbp), %ecx
callq 0x3a60
movq %rax, -0xd0(%rbp)
movl -0xa8(%rbp), %edi
movq -0xd0(%rbp), %rdx
leaq -0xd8(%rbp), %rsi
leaq -0xd4(%rbp), %rcx
xorl %r8d, %r8d
callq 0x34e0
jmp 0x40fd
cmpl $0x0, -0xd4(%rbp)
jle 0x40e9
movl -0xa8(%rbp), %edi
movq -0xd0(%rbp), %rdx
leaq -0xd8(%rbp), %rsi
leaq -0xd4(%rbp), %rcx
xorl %r8d, %r8d
callq 0x34e0
jmp 0x40fb
movl -0xa8(%rbp), %edi
leaq -0xd8(%rbp), %rsi
callq 0x3680
jmp 0x40fd
movb $0x1, -0xe1(%rbp)
movl -0xd8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8(%rbp)
movq -0xc8(%rbp), %rax
movl -0xe8(%rbp), %ecx
movq 0x4ea9(%rip), %rdi      # 0x8fd0
movsbl (%rax,%rcx), %esi
callq 0x2110
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x3fd5
movb -0xe2(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x4183
movl $0x2, -0xd4(%rbp)
movq -0xd0(%rbp), %rdi
movl -0xd4(%rbp), %esi
movl -0xa8(%rbp), %edx
movl -0xd8(%rbp), %ecx
callq 0x3a60
movq %rax, -0xd0(%rbp)
movq -0xc0(%rbp), %rdi
movl 0x28(%rbp), %esi
movl -0xa8(%rbp), %edx
movq -0xd0(%rbp), %r8
leaq -0xd8(%rbp), %rcx
leaq -0xd4(%rbp), %r9
callq 0x36e0
andb $0x1, %al
movb %al, -0xe9(%rbp)
movb -0xe9(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x41ca
jmp 0x439f
movb $0x1, -0xd9(%rbp)
jmp 0x4369
movq -0xc8(%rbp), %rdi
callq 0x2070
movl 0x28(%rbp), %ecx
subl -0xd8(%rbp), %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
ja 0x431e
movq -0xc8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl 0x10(%rbp), %edx
callq 0x3b00
andb $0x1, %al
movb %al, -0xea(%rbp)
movb -0xea(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x429d
movl -0xd4(%rbp), %eax
addl $0x2, %eax
movl %eax, -0xd4(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x110(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0x108(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0x104(%rbp)
movl -0xd8(%rbp), %eax
movq %rax, -0x100(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x2070
movq -0x110(%rbp), %rdi
movl -0x108(%rbp), %esi
movl -0x104(%rbp), %edx
movq %rax, %rcx
movq -0x100(%rbp), %rax
addq %rcx, %rax
movl %eax, %ecx
callq 0x3a60
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rsi
movq 0x4d25(%rip), %rdi      # 0x8fd0
callq 0x20c0
movq -0xc8(%rbp), %rdi
callq 0x2070
movq %rax, %rcx
movl -0xd8(%rbp), %eax
addq %rcx, %rax
movl %eax, -0xd8(%rbp)
movb $0x1, -0xd9(%rbp)
movq -0xc0(%rbp), %rdi
movl 0x28(%rbp), %esi
movl -0xa8(%rbp), %edx
movq -0xd0(%rbp), %r8
leaq -0xd8(%rbp), %rcx
leaq -0xd4(%rbp), %r9
callq 0x36e0
andb $0x1, %al
movb %al, -0xeb(%rbp)
movb -0xeb(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x431c
jmp 0x439f
jmp 0x4367
movb $0x0, -0xd9(%rbp)
cmpl $0x0, -0xd4(%rbp)
jle 0x4353
movl -0xa8(%rbp), %edi
movq -0xd0(%rbp), %rdx
leaq -0xd8(%rbp), %rsi
leaq -0xd4(%rbp), %rcx
xorl %r8d, %r8d
callq 0x34e0
jmp 0x4365
movl -0xa8(%rbp), %edi
leaq -0xd8(%rbp), %rsi
callq 0x3680
jmp 0x4367
jmp 0x4369
movb -0xd9(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x439a
movq -0xc0(%rbp), %rdi
leaq 0x1ea9(%rip), %rsi      # 0x6230
leaq -0xc0(%rbp), %rdx
callq 0x2270
movq %rax, -0xc8(%rbp)
jmp 0x3f60
movq 0x4c2a(%rip), %rdi      # 0x8fd0
movq 0x4c13(%rip), %rsi      # 0x8fc0
callq 0x2100
movl $0x0, -0xf0(%rbp)
movl -0xf0(%rbp), %eax
cmpl 0x28(%rbp), %eax
jae 0x43eb
movq 0x4c02(%rip), %rdi      # 0x8fd0
leaq 0x208d(%rip), %rsi      # 0x6462
callq 0x20c0
movl -0xf0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf0(%rbp)
jmp 0x43bc
movq 0x4bde(%rip), %rdi      # 0x8fd0
movq 0x4bc7(%rip), %rsi      # 0x8fc0
callq 0x2100
movq -0xd0(%rbp), %rdi
callq 0x2130
movq -0xb8(%rbp), %rdi
callq 0x2130
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x3d6f
movq -0x50(%rbp), %rdi
callq 0x2130
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)