// File: 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
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
callq 0xe4c0
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jb 0x12ec5
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
callq 0xe260
jmp 0x13150
movq -0x10(%rbp), %rcx
movl (%rcx), %eax
movl %eax, %edx
incl %edx
movl %edx, (%rcx)
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
callq 0xe260
movl $0x68, %edi
callq 0xe300
movq %rax, -0xa8(%rbp)
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xe120
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rax
movzbl 0x8(%rax), %ecx
callq 0x18690
jmp 0x12f22
movq -0xb0(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x18c70
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
leaq -0x48(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x13170
movq -0xb8(%rbp), %rdx
movq -0x20(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x133e0
jmp 0x12f68
leaq -0x60(%rbp), %rdi
callq 0x13780
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x137e0
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x137b0
testb $0x1, %al
jne 0x12f98
jmp 0x130ec
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0xe4c0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xc8(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x13810
movq -0xc8(%rbp), %rdi
movl (%rax), %eax
movl %eax, %esi
callq 0x196f0
movq %rax, -0xc0(%rbp)
jmp 0x12fd6
leaq -0x91(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0xe5e0
movq -0xc0(%rbp), %rsi
movq -0xd0(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x102a0
jmp 0x13005
leaq -0x91(%rbp), %rdi
callq 0xe360
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0xe260
movq -0x10(%rbp), %rdi
subq $-0x80, %rdi
callq 0xe4c0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x90(%rbp), %rsi
callq 0x103e0
jmp 0x13041
movq -0x10(%rbp), %rdi
addq $0x80, %rdi
callq 0xe260
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
callq 0x13830
movq %rax, -0xa0(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xe1d0
jmp 0x12f75
movq -0xb0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x68, %esi
callq 0xe310
jmp 0x1315b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x13145
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x1313c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x91(%rbp), %rdi
callq 0xe360
jmp 0x1313c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xe1d0
jmp 0x1313c
movq -0x20(%rbp), %rdi
callq 0x18800
jmp 0x130f7
movq -0x20(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, %rax
je 0x13125
movq -0xd8(%rbp), %rdi
callq 0x18740
movq -0xd8(%rbp), %rdi
movl $0x68, %esi
callq 0xe310
leaq -0x60(%rbp), %rdi
callq 0x13860
leaq -0x48(%rbp), %rdi
callq 0x13880
jmp 0x12e97
leaq -0x60(%rbp), %rdi
callq 0x13860
leaq -0x48(%rbp), %rdi
callq 0x13880
jmp 0x1315b
xorl %eax, %eax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0xe5d0
nopw %cs:(%rax,%rax)