// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm2, 0x18(%rsp)
movsd %xmm1, 0x28(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, %r15
movq %rdx, 0x20(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdi, 0x68(%rsp)
movq %r9, 0x38(%rsp)
movl %r9d, %eax
movq %rax, 0x58(%rsp)
leaq (,%rax,8), %rdi
callq 0x2130
movq %rax, %r12
xorl %r14d, %r14d
movq %rax, 0x40(%rsp)
cmpq 0x58(%rsp), %r14
je 0x338c
xorpd %xmm0, %xmm0
xorl %r12d, %r12d
movl 0xb0(%rsp), %eax
cmpq %r12, %rax
je 0x3379
movsd %xmm0, 0x10(%rsp)
movq 0x48(%rsp), %rax
movq (%rax,%r12,8), %rbp
testq %rbp, %rbp
je 0x3330
movq 0x20(%rsp), %rbx
movl (%rbx,%r14,4), %esi
movq %rbp, %rdi
callq 0x3cc8
movl %eax, %r13d
movq %rbp, %rdi
callq 0x3be4
movl %eax, %esi
jmp 0x333a
xorl %esi, %esi
xorl %r13d, %r13d
movq 0x20(%rsp), %rbx
movl (%rbx,%r14,4), %eax
movl (%r15,%rax,4), %edx
movl %r13d, %edi
movsd 0x50(%rsp), %xmm0
movl 0xb8(%rsp), %ecx
movsd 0x28(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
callq 0x2cdc
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
incq %r12
jmp 0x32f0
movq 0x40(%rsp), %r12
movsd %xmm0, (%r12,%r14,8)
incq %r14
jmp 0x32de
pushq $0x1
popq %rax
xorl %ecx, %ecx
movq 0x20(%rsp), %r14
movq 0x58(%rsp), %r8
cmpq %r8, %rcx
je 0x33e1
movl %eax, %edx
cmpq %r8, %rdx
jae 0x33da
movsd (%r12,%rcx,8), %xmm0
movsd (%r12,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x33d5
movl (%r14,%rcx,4), %esi
movsd %xmm1, (%r12,%rcx,8)
movsd %xmm0, (%r12,%rdx,8)
movl (%r14,%rdx,4), %edi
movl %edi, (%r14,%rcx,4)
movl %esi, (%r14,%rdx,4)
incq %rdx
jmp 0x33a2
incq %rcx
incl %eax
jmp 0x339b
movq 0x38(%rsp), %rax
movl 0xc0(%rsp), %ecx
cmpl %eax, %ecx
cmovbl %ecx, %eax
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
movq 0x3bd0(%rip), %r15      # 0x6fd0
cmpq 0x38(%rsp), %rbx
je 0x3802
movl (%r14,%rbx,4), %edi
callq 0x2dcc
movq %r15, %r13
movl %eax, %ebp
movl %ebx, %edi
callq 0x2dcc
movl %eax, %r15d
movsd (%r12,%rbx,8), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x2220
cvttsd2si 0x70(%rsp), %edi
callq 0x2dcc
addl %ebp, %r15d
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
ucomisd 0x18(%rsp), %xmm0
seta %cl
addl %r15d, %ecx
addl %ecx, %eax
addl $0xe, %eax
movl %eax, 0x10(%rsp)
movl (%r14,%rbx,4), %eax
movq 0x68(%rsp), %rcx
movq (%rcx,%rax,8), %r15
movq %r15, %rdi
callq 0x2040
leaq 0x1(%rax), %rdi
callq 0x2130
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
callq 0x2090
movq %rbp, 0x50(%rsp)
movq %rbp, %rdi
leaq 0xd9b(%rip), %rsi       # 0x4238
leaq 0x30(%rsp), %rdx
callq 0x2230
movq %rax, %rbp
andl $0x0, 0x8(%rsp)
movl 0x10(%rsp), %r15d
movl %r15d, 0xc(%rsp)
movq (%r13), %rax
movq -0x18(%rax), %rax
movq $0x5, 0x8(%r13,%rax)
leaq 0x1(%rbx), %rsi
movq %r13, %rdi
movq %rsi, 0x48(%rsp)
callq 0x2070
movq %rax, %rdi
leaq 0xf74(%rip), %rsi       # 0x4459
callq 0x20a0
movl (%r14,%rbx,4), %esi
movq %rax, %rdi
callq 0x2070
movq %rax, %rdi
leaq 0xf5d(%rip), %rsi       # 0x445d
callq 0x20a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
movl $0xfffffefb, %esi       # imm = 0xFFFFFEFB
andl %esi, %edx
orl $0x4, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x21d0
movq %rax, %rdi
leaq 0xf2b(%rip), %rsi       # 0x4461
callq 0x20a0
movl 0xc8(%rsp), %eax
subl %r15d, %eax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
leaq 0xc(%rsp), %rbx
testq %rbp, %rbp
je 0x37b2
movq %rbp, %rdi
callq 0x2040
cmpq 0x18(%rsp), %rax
setbe %dl
movl 0xc(%rsp), %r15d
movl 0xc8(%rsp), %ecx
subl %r15d, %ecx
sete %sil
orb %dl, %sil
je 0x35ba
cmpq %rcx, %rax
jbe 0x36ed
cmpl $0x0, 0x8(%rsp)
jle 0x35ac
movl 0x10(%rsp), %edi
movq %rbx, %rsi
movq %r13, %rdx
leaq 0x8(%rsp), %rcx
xorl %r8d, %r8d
callq 0x2e2f
jmp 0x3552
movl 0x10(%rsp), %edi
movq %rbx, %rsi
callq 0x2f67
jmp 0x3552
movq %rbp, %rdi
movq 0x60(%rsp), %rsi
movl 0xb0(%rsp), %edx
callq 0x323e
movb %al, 0x28(%rsp)
xorl %r14d, %r14d
movl %r15d, %ebx
xorl %eax, %eax
movl %eax, %r12d
movq %rbp, %rdi
callq 0x2040
cmpq %r12, %rax
jbe 0x36b5
incl %ebx
cmpl 0xc8(%rsp), %ebx
jbe 0x3681
cmpb $0x0, 0x28(%rsp)
leaq 0xc(%rsp), %rbx
je 0x3636
testb $0x1, %r14b
jne 0x3643
movl 0x8(%rsp), %esi
addl $0x2, %esi
movl %esi, 0x8(%rsp)
movq %r13, %rdi
movl %r15d, %edx
movl 0xc8(%rsp), %ecx
callq 0x31e0
movq %rax, %r13
movl 0x10(%rsp), %r14d
jmp 0x3668
cmpl $0x0, 0x8(%rsp)
jle 0x36a7
movl 0x10(%rsp), %edi
jmp 0x366b
movl $0x2, 0x8(%rsp)
movq %r13, %rdi
pushq $0x2
popq %rsi
movl 0x10(%rsp), %r14d
movl %r14d, %edx
movl 0xc8(%rsp), %ecx
callq 0x31e0
movq %rax, %r13
movl %r14d, %edi
movq %rbx, %rsi
movq %r13, %rdx
leaq 0x8(%rsp), %rcx
xorl %r8d, %r8d
callq 0x2e2f
movb $0x1, %r14b
movl 0xc(%rsp), %ebx
incl %ebx
movl %ebx, 0xc(%rsp)
movsbl (%rbp,%r12), %esi
movq 0x3938(%rip), %rdi      # 0x6fd0
callq 0x20e0
leal 0x1(%r12), %eax
jmp 0x35da
movl 0x10(%rsp), %edi
movq %rbx, %rsi
callq 0x2f67
jmp 0x367e
cmpb $0x0, 0x28(%rsp)
je 0x375f
movl $0x2, 0x8(%rsp)
movq %r13, %rdi
pushq $0x2
popq %rsi
movl 0x10(%rsp), %ebp
movl %ebp, %edx
movl %ebx, %ecx
callq 0x31e0
movq %rax, %r13
movq 0x20(%rsp), %r14
movq 0x40(%rsp), %r12
jmp 0x376d
movq %rbp, %rdi
movq 0x60(%rsp), %rsi
movl 0xb0(%rsp), %edx
callq 0x323e
testb %al, %al
je 0x3732
movl 0x8(%rsp), %ebx
addl $0x2, %ebx
movl %ebx, 0x8(%rsp)
movq %rbp, %rdi
callq 0x2040
movl %r15d, %ecx
addl %eax, %ecx
movq %r13, %rdi
movl %ebx, %esi
leaq 0xc(%rsp), %rbx
movl %r15d, %edx
callq 0x31e0
movq %rax, %r13
movq 0x3897(%rip), %rdi      # 0x6fd0
movq %rbp, %rsi
callq 0x20a0
movq %rbp, %rdi
callq 0x2040
addl %eax, 0xc(%rsp)
movq 0x30(%rsp), %rdi
movl 0xc8(%rsp), %esi
movl 0x10(%rsp), %edx
jmp 0x3780
movq 0x20(%rsp), %r14
movq 0x40(%rsp), %r12
movl 0x10(%rsp), %ebp
movq 0x30(%rsp), %rdi
movl 0xc8(%rsp), %esi
movl %ebp, %edx
leaq 0xc(%rsp), %rbx
movq %rbx, %rcx
movq %r13, %r8
leaq 0x8(%rsp), %r9
callq 0x2fa2
testb %al, %al
jne 0x37b2
movq 0x30(%rsp), %rdi
leaq 0xa98(%rip), %rsi       # 0x4238
leaq 0x30(%rsp), %rdx
callq 0x2230
movq %rax, %rbp
jmp 0x3552
movq 0x3817(%rip), %r15      # 0x6fd0
movq %r15, %rdi
callq 0x2050
movl 0xc8(%rsp), %ebx
leaq 0xc95(%rip), %rbp       # 0x4464
movq %r15, %rdi
subl $0x1, %ebx
jb 0x37e1
movq %rbp, %rsi
callq 0x20a0
jmp 0x37cf
callq 0x2050
movq %r13, %rdi
callq 0x2100
movq 0x50(%rsp), %rdi
callq 0x2100
movq 0x48(%rsp), %rbx
jmp 0x3400
movq %r12, %rdi
callq 0x2100
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq