// File: /workspace/github_cpp_makefile_star2_O3/MITRECND[P]snugglefish/src/nGramSearch.cpp
// Function: snugglefish::nGramSearch::searchNGramThread(void*)
void* nGramSearch::searchNGramThread(void* input){


    thread_data* tdata = (thread_data*) input;

    while(1){
        pthread_mutex_lock(& tdata->queueMutex);
        if (tdata->queue >= tdata->maximumIndex){
            pthread_mutex_unlock(& tdata->queueMutex);
            break;
        }

        uint32_t i = tdata->queue++;
        pthread_mutex_unlock(& tdata->queueMutex);

        indexSet* tIndex = new indexSet(tdata->baseFileName->c_str(), i, tdata->ngramLength);
        tIndex->open();

        //Get ordered list of NGrams
        // In ascending order by number of files that contain that ngram
        //list<index_entry> queryList = orderNGrams(nGramQuery);
        list< pair<uint64_t,size_t> > queryList = orderNGrams(tIndex, *(tdata->nGramQuery));

        //Get list of File Ids that match NGrams
        list<ngram_t_fidtype> matchedIds = searchAlpha((indexSet*) tIndex, queryList);

        //Convert File IDs to filenames
        list<ngram_t_fidtype>::iterator ft = matchedIds.begin();
        while(ft != matchedIds.end()){
            pthread_mutex_lock(& tdata->smFileMutex);
            string matched_filename = tdata->masterFile->getFilebyId(*ft);
            pthread_mutex_unlock(& tdata->smFileMutex);

            pthread_mutex_lock(& tdata->mfMutex);
            tdata->matchedFiles->push_back(matched_filename);
            pthread_mutex_unlock(& tdata->mfMutex);
            ft++;
        }

        tIndex->close();
        delete tIndex;
    }

    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %r12
leaq 0x30(%rdi), %rbx
movq %rbx, %rdi
callq 0x53d0
movl (%r12), %r13d
cmpl 0x4(%r12), %r13d
jae 0x7ff1
leaq 0x58(%r12), %rax
movq %rax, -0x38(%rbp)
leaq 0x80(%r12), %r15
leaq -0x98(%rbp), %r14
movq %rbx, -0x40(%rbp)
leal 0x1(%r13), %eax
movl %eax, (%r12)
movq %rbx, %rdi
callq 0x5200
movl $0x68, %edi
callq 0x5280
movq %rax, %rbx
movq 0x20(%r12), %rax
movq (%rax), %rsi
movzbl 0x8(%r12), %ecx
movq %rbx, %rdi
movl %r13d, %edx
callq 0x9b14
movq %rbx, %rdi
callq 0x9ef2
movq 0x28(%r12), %rdx
movq %rbx, %r13
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x8096
movq %r14, %rdi
movq %r13, -0x48(%rbp)
movq %r13, %rsi
movq %rbx, %rdx
callq 0x81dc
movq -0x98(%rbp), %rbx
cmpq %r14, %rbx
je 0x7f6b
movq -0x38(%rbp), %rdi
callq 0x53d0
movq 0x10(%r12), %rdi
movl 0x10(%rbx), %esi
callq 0xa574
leaq -0x80(%rbp), %r13
movq %r13, %rdi
movq %rax, %rsi
leaq -0x29(%rbp), %rdx
callq 0x6e7c
movq -0x38(%rbp), %rdi
callq 0x5200
movq %r15, %rdi
callq 0x53d0
movq 0x18(%r12), %rdi
movq %r13, %rsi
callq 0x6eba
movq %r15, %rdi
callq 0x5200
movq (%rbx), %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x7ef9
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x5290
jmp 0x7ef9
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x9be2
movq %rbx, %rdi
callq 0x9b58
movl $0x68, %esi
movq %rbx, %rdi
callq 0x5290
movq -0x98(%rbp), %rdi
cmpq %r14, %rdi
je 0x7fad
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x5290
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x7f98
movq -0x60(%rbp), %rdi
leaq -0x60(%rbp), %r14
cmpq %r14, %rdi
je 0x7fcf
movq (%rdi), %rbx
movl $0x20, %esi
callq 0x5290
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x7fba
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x53d0
movl (%r12), %r13d
cmpl 0x4(%r12), %r13d
leaq -0x98(%rbp), %r14
jb 0x7e8b
movq %rbx, %rdi
callq 0x5200
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8043
movq %rbx, %rdi
movq %rax, %rbx
movl $0x68, %esi
callq 0x5290
jmp 0x808d
movq %rax, %rbx
jmp 0x8067
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x8046
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x5290
jmp 0x8046
jmp 0x8043
movq %rax, %rbx
movq -0x98(%rbp), %rdi
cmpq %r14, %rdi
je 0x8067
movq (%rdi), %r15
movl $0x18, %esi
callq 0x5290
movq %r15, %rdi
cmpq %r14, %r15
jne 0x8052
movq -0x60(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x808d
movq (%rdi), %r14
movl $0x20, %esi
callq 0x5290
movq %r14, %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %r14
jne 0x8074
movq %rbx, %rdi
callq 0x54e0
nop