// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movsd %xmm2, 0x28(%rsp)
movsd %xmm1, 0x38(%rsp)
movsd %xmm0, 0x60(%rsp)
movl %r9d, %ebx
movq %r8, 0x58(%rsp)
movq %rcx, %r12
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, 0x78(%rsp)
movl %r9d, %r15d
leaq (,%r15,8), %rdi
callq 0x2170
movq %rax, 0x40(%rsp)
movq %rbx, 0x30(%rsp)
testl %ebx, %ebx
je 0x3ff9
movq %r15, 0x68(%rsp)
movq %r14, 0x50(%rsp)
movsd 0x19ba(%rip), %xmm0     # 0x5200
movsd 0x38(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x88(%rsp)
movsd 0x28(%rsp), %xmm3
subsd %xmm3, %xmm0
movsd %xmm0, 0x80(%rsp)
movl 0xd0(%rsp), %r13d
xorl %r15d, %r15d
movq %rbp, 0x70(%rsp)
xorpd %xmm0, %xmm0
cmpl $0x0, 0xd0(%rsp)
je 0x3975
xorl %ebx, %ebx
movsd %xmm0, 0x10(%rsp)
movq 0x58(%rsp), %rax
movq (%rax,%rbx,8), %r14
testq %r14, %r14
je 0x38cc
movl (%rbp,%r15,4), %esi
movq %r14, %rdi
callq 0x4694
movl %eax, %ebp
movq %r14, %rdi
callq 0x456c
movl %ebp, %ecx
movq 0x70(%rsp), %rbp
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
jmp 0x38d2
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movl (%rbp,%r15,4), %ecx
movl (%r12,%rcx,4), %r14d
movl 0xd8(%rsp), %ecx
subl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd 0x18fe(%rip), %xmm2     # 0x51f8
addsd %xmm2, %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
callq 0x2070
movsd 0x88(%rsp), %xmm1
movsd 0x20(%rsp), %xmm5
mulsd %xmm5, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r14, %xmm2
divsd 0x60(%rsp), %xmm2
movsd 0x28(%rsp), %xmm3
mulsd %xmm3, %xmm2
addsd 0x80(%rsp), %xmm2
movsd 0x38(%rsp), %xmm4
mulsd %xmm4, %xmm2
addsd %xmm5, %xmm2
divsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
incq %rbx
cmpq %rbx, %r13
jne 0x3890
movq 0x40(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
cmpq 0x68(%rsp), %r15
jne 0x387c
cmpl $0x0, 0x30(%rsp)
movq 0x50(%rsp), %r13
movl 0xd0(%rsp), %r12d
movq 0x68(%rsp), %rbx
je 0x3ff9
xorl %esi, %esi
movq %rbx, %rax
movq %rbp, %rcx
movq 0x40(%rsp), %r11
movq %r11, %rdx
movq %rsi, %rdi
incq %rsi
cmpq %rbx, %rsi
jae 0x3a04
movl $0x1, %r8d
movsd (%r11,%rdi,8), %xmm0
movsd (%rdx,%r8,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x39fc
movl (%rbp,%rdi,4), %r9d
movsd %xmm1, (%r11,%rdi,8)
movsd %xmm0, (%rdx,%r8,8)
movl (%rcx,%r8,4), %r10d
movl %r10d, (%rbp,%rdi,4)
movl %r9d, (%rcx,%r8,4)
incq %r8
cmpl %r8d, %eax
jne 0x39cc
addq $0x8, %rdx
addq $0x4, %rcx
decq %rax
cmpq %rbx, %rsi
jne 0x39bb
cmpl $0x0, 0x30(%rsp)
sete %al
cmpl $0x0, 0xe0(%rsp)
sete %cl
orb %al, %cl
jne 0x3ff9
movl 0xe0(%rsp), %ecx
decl %ecx
movq 0x30(%rsp), %rax
decl %eax
cmpl %eax, %ecx
cmovbl %ecx, %eax
incl %eax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
movl (%rbp,%r15,4), %eax
movl $0x1, %ebx
movl $0x1, %ecx
testl %eax, %eax
je 0x3a86
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
callq 0x2070
callq 0x2230
addsd 0x177f(%rip), %xmm0     # 0x5200
cvttsd2si %xmm0, %rcx
movq %rcx, 0x20(%rsp)
testq %r15, %r15
je 0x3ab7
movl %r15d, %eax
negl %eax
cmovsl %r15d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0x2070
callq 0x2230
addsd 0x174e(%rip), %xmm0     # 0x5200
cvttsd2si %xmm0, %rbx
movq 0x40(%rsp), %rax
movsd (%rax,%r15,8), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x2270
cvttsd2si 0x90(%rsp), %eax
testl %eax, %eax
je 0x3b09
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
callq 0x2070
callq 0x2230
addsd 0x16fe(%rip), %xmm0     # 0x5200
cvttsd2si %xmm0, %rax
jmp 0x3b0e
movl $0x1, %eax
addl 0x20(%rsp), %ebx
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
ucomisd 0x10(%rsp), %xmm0
seta %cl
addl %ebx, %ecx
leal (%rax,%rcx), %r14d
addl $0xe, %r14d
movq %r15, 0x28(%rsp)
movl (%rbp,%r15,4), %eax
movq 0x78(%rsp), %rcx
movq (%rcx,%rax,8), %rbx
movq %rbx, %rdi
callq 0x2050
leaq 0x1(%rax), %rdi
callq 0x2170
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20c0
movq %r15, 0x60(%rsp)
movq %r15, %rdi
leaq 0x16cc(%rip), %rsi      # 0x5238
leaq 0x48(%rsp), %rdx
callq 0x2280
movq %rbp, %r15
movq %rax, %rbp
movl $0x0, 0x8(%rsp)
movl %r14d, 0x20(%rsp)
movl %r14d, 0xc(%rsp)
movq 0x443b(%rip), %rdi      # 0x7fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x5, 0x8(%rdi,%rax)
movq 0x28(%rsp), %r14
leaq 0x1(%r14), %rsi
movq %rsi, 0x58(%rsp)
callq 0x2090
movq %rax, %rbx
movl $0x3, %edx
movq %rax, %rdi
leaq 0x188f(%rip), %rsi      # 0x5459
callq 0x2120
movl (%r15,%r14,4), %esi
movq %rbx, %rdi
callq 0x2090
movq %rax, %rbx
movl $0x3, %edx
movq %rax, %rdi
leaq 0x1870(%rip), %rsi      # 0x545d
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rax
movl 0x18(%rbx,%rax), %ecx
movl $0xfffffefb, %edx       # imm = 0xFFFFFEFB
andl %edx, %ecx
orl $0x4, %ecx
movl %ecx, 0x18(%rbx,%rax)
movq %rbx, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x2210
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1839(%rip), %rsi      # 0x5461
callq 0x2120
testq %rbp, %rbp
je 0x3f47
movl 0xe8(%rsp), %eax
movl %eax, %r14d
movl 0x20(%rsp), %ebx
subl %ebx, %r14d
movq $0x0, 0x10(%rsp)
movq %r14, 0x38(%rsp)
movq %rbp, %rdi
callq 0x2050
cmpq %r14, %rax
setbe %dl
movl 0xc(%rsp), %esi
movl 0xe8(%rsp), %ecx
movl %esi, 0x28(%rsp)
subl %esi, %ecx
sete %sil
orb %dl, %sil
je 0x3cab
cmpq %rcx, %rax
jbe 0x3cd9
cmpl $0x0, 0x8(%rsp)
jle 0x3e8a
movl %ebx, %edi
leaq 0xc(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rcx
xorl %r8d, %r8d
callq 0x328b
jmp 0x3f3c
testl %r12d, %r12d
je 0x3d0a
movl $0x2, %ebx
leal -0x1(%rbx), %eax
movq (%r13,%rax,8), %rdi
movq %rbp, %rsi
callq 0x2190
testl %eax, %eax
sete %r15b
je 0x3d0d
leal 0x1(%rbx), %eax
cmpl %r12d, %ebx
movl %eax, %ebx
jbe 0x3cb5
jmp 0x3d0d
testl %r12d, %r12d
je 0x3ed7
movl $0x1, %ebx
movl %ebx, %eax
movq (%r13,%rax,8), %rdi
movq %rbp, %rsi
callq 0x2190
testl %eax, %eax
je 0x3e9b
incl %ebx
cmpl %r12d, %ebx
jbe 0x3ce7
jmp 0x3ed3
xorl %r15d, %r15d
cmpb $0x0, (%rbp)
je 0x3e16
movl $0x1, %r14d
xorl %r13d, %r13d
movl 0x28(%rsp), %r12d
xorl %ebx, %ebx
incl %r12d
cmpl 0xe8(%rsp), %r12d
jbe 0x3dc1
testb %r15b, %r15b
movq 0x10(%rsp), %rdx
je 0x3d6b
testb $0x1, %bl
jne 0x3d81
movl 0x8(%rsp), %esi
addl $0x2, %esi
movl %esi, 0x8(%rsp)
movq %rdx, %rdi
movl 0x28(%rsp), %edx
movl 0xe8(%rsp), %ecx
callq 0x3736
movl 0x20(%rsp), %ebx
jmp 0x3da3
cmpl $0x0, 0x8(%rsp)
jle 0x3e06
movl 0x20(%rsp), %edi
leaq 0xc(%rsp), %rsi
jmp 0x3db2
movl $0x2, 0x8(%rsp)
movq %rdx, %rdi
movl $0x2, %esi
movl 0x20(%rsp), %ebx
movl %ebx, %edx
movl 0xe8(%rsp), %ecx
callq 0x3736
movl %ebx, %edi
leaq 0xc(%rsp), %rsi
movq %rax, 0x10(%rsp)
movq %rax, %rdx
leaq 0x8(%rsp), %rcx
xorl %r8d, %r8d
callq 0x328b
movb $0x1, %bl
movl 0xc(%rsp), %r12d
incl %r12d
movl %r12d, 0xc(%rsp)
movb (%rbp,%r13), %al
movb %al, 0x1f(%rsp)
movl $0x1, %edx
movq 0x41ed(%rip), %rdi      # 0x7fd0
leaq 0x1f(%rsp), %rsi
callq 0x2120
movl %r14d, %r13d
movq %rbp, %rdi
callq 0x2050
incl %r14d
cmpq %r13, %rax
ja 0x3d27
jmp 0x3e1b
movl 0x20(%rsp), %edi
leaq 0xc(%rsp), %rsi
callq 0x3411
jmp 0x3dbf
movl 0x28(%rsp), %r12d
testb %r15b, %r15b
je 0x3e52
movl $0x2, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movl $0x2, %esi
movl 0x20(%rsp), %ebx
movl %ebx, %edx
movl %r12d, %ecx
callq 0x3736
movq %rax, %r8
movq 0x50(%rsp), %r13
movl 0xd0(%rsp), %r12d
jmp 0x3e68
movq 0x50(%rsp), %r13
movl 0xd0(%rsp), %r12d
movl 0x20(%rsp), %ebx
movq 0x10(%rsp), %r8
movq 0x38(%rsp), %r14
movq 0x48(%rsp), %rdi
movl 0xe8(%rsp), %esi
movl %ebx, %edx
leaq 0xc(%rsp), %rcx
movq %r8, 0x10(%rsp)
jmp 0x3f15
movl %ebx, %edi
leaq 0xc(%rsp), %rsi
callq 0x3411
jmp 0x3f3c
movl 0x8(%rsp), %r12d
addl $0x2, %r12d
movl %r12d, 0x8(%rsp)
movq %rbp, %rdi
callq 0x2050
movl 0x28(%rsp), %edx
movl %edx, %ecx
addl %eax, %ecx
movq 0x10(%rsp), %rdi
movl %r12d, %esi
movl 0xd0(%rsp), %r12d
callq 0x3736
movq %rax, 0x10(%rsp)
movl 0x20(%rsp), %ebx
movq %rbp, %rdi
callq 0x2050
movq 0x40ea(%rip), %rdi      # 0x7fd0
movq %rbp, %rsi
movq %rax, %rdx
callq 0x2120
movq %rbp, %rdi
callq 0x2050
addl %eax, 0xc(%rsp)
movq 0x48(%rsp), %rdi
movl 0xe8(%rsp), %esi
movl %ebx, %edx
leaq 0xc(%rsp), %rcx
movq 0x10(%rsp), %r8
leaq 0x8(%rsp), %r9
callq 0x3478
testb %al, %al
jne 0x3f50
movq 0x48(%rsp), %rdi
leaq 0x1309(%rip), %rsi      # 0x5238
leaq 0x48(%rsp), %rdx
callq 0x2280
movq %rax, %rbp
testq %rbp, %rbp
jne 0x3c55
jmp 0x3f50
movq $0x0, 0x10(%rsp)
movq 0x4079(%rip), %r14      # 0x7fd0
movq (%r14), %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
movl 0xe8(%rsp), %eax
movl %eax, %ebx
testl %eax, %eax
leaq 0x14d4(%rip), %r15      # 0x5464
je 0x3fa6
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2120
decl %ebx
jne 0x3f92
movq (%r14), %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
movq 0x10(%rsp), %rdi
callq 0x2140
movq 0x60(%rsp), %rdi
callq 0x2140
movq 0x58(%rsp), %rax
movq %rax, %r15
cmpq 0x30(%rsp), %rax
movq 0x70(%rsp), %rbp
jne 0x3a4e
movq 0x40(%rsp), %rdi
callq 0x2140
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)