// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rdi, %r14
leaq 0x30(%rdi), %r13
leaq 0x58(%rdi), %r15
leaq 0x80(%rdi), %r12
leaq -0x90(%rbp), %rbx
movq %r13, -0x40(%rbp)
movq %r13, %rdi
callq 0x63b0
movl (%r14), %ecx
cmpl 0x4(%r14), %ecx
jae 0x841f
leal 0x1(%rcx), %eax
movl %eax, (%r14)
movq %r13, %rdi
movq %rcx, -0x38(%rbp)
callq 0x6200
pushq $0x68
popq %rdi
callq 0x6280
movq %rax, %r13
movq 0x20(%r14), %rax
movq (%rax), %rsi
movzbl 0x8(%r14), %ecx
movq %r13, %rdi
movq -0x38(%rbp), %rdx
callq 0x9fdc
movq %r13, %rdi
callq 0xa340
movq 0x28(%r14), %rdx
movq %r13, %rsi
leaq -0x70(%rbp), %rdi
callq 0x8480
leaq -0x58(%rbp), %rdi
movq %r13, -0x38(%rbp)
movq %r13, %rsi
leaq -0x70(%rbp), %rdx
callq 0x857e
movq -0x58(%rbp), %r13
leaq -0x58(%rbp), %rax
cmpq %rax, %r13
je 0x83e5
movq %r15, %rdi
callq 0x63b0
movq 0x10(%r14), %rdi
movl 0x10(%r13), %esi
callq 0xa940
movq %rbx, %rdi
movq %rax, %rsi
leaq -0x29(%rbp), %rdx
callq 0x7684
movq %r15, %rdi
callq 0x6200
movq %r12, %rdi
callq 0x63b0
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0x76c0
movq %r12, %rdi
callq 0x6200
movq (%r13), %r13
movq %rbx, %rdi
callq 0x60f0
jmp 0x8386
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0xa094
movq %r13, %rdi
callq 0xa020
movq %r13, %rdi
pushq $0x68
popq %rsi
callq 0x6290
leaq -0x58(%rbp), %rdi
callq 0x8720
leaq -0x70(%rbp), %rdi
callq 0x8754
movq -0x40(%rbp), %r13
jmp 0x830c
movq %r13, %rdi
callq 0x6200
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8451
movq %rax, %rbx
jmp 0x846e
movq %rax, %rbx
pushq $0x68
popq %rsi
movq %r13, %rdi
callq 0x6290
jmp 0x8477
jmp 0x8451
movq %rax, %rbx
jmp 0x8465
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x60f0
leaq -0x58(%rbp), %rdi
callq 0x8720
leaq -0x70(%rbp), %rdi
callq 0x8754
movq %rbx, %rdi
callq 0x64b0
nop