// File: MITRECND[P]snugglefish/src/snugglefish.cpp
// Function: indexerThread(void*)
void* indexerThread(void* input){
    mi_data* midata = (mi_data*) input;
    nGramIndex* ngramindex = (nGramIndex*) midata->ngramindex;
    fileIndexer indexer(midata->ngramSize);

    while(1){
        pthread_mutex_lock(& midata->filesMutex);
        if (midata->queue >= midata->fileList->size()){
            pthread_mutex_unlock(& midata->filesMutex);
            break;
        }
        uint32_t i = midata->queue++; 
        pthread_mutex_unlock(& midata->filesMutex); 

        try{
            vector<uint32_t>* processedFile = indexer.processFile((*(midata->fileList))[i].c_str());
            if(processedFile != 0){
                pthread_mutex_lock(& midata->nGramIndexMutex);
                ngramindex->addNGrams(processedFile, (*(midata->fileList))[i]);
                pthread_mutex_unlock(& midata->nGramIndexMutex);
            }
        }catch(exception& e){
            cout << "Error in thread:" << e.what() << endl;
            handler(SIGSEGV);
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
leaq -0x38(%rbp), %rdi
movzbl %al, %esi
callq 0x15cc0
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0xe4c0
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x105a0
movq %rax, %rcx
movq -0x88(%rbp), %rax
cmpq %rcx, %rax
jb 0x100de
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0xe260
jmp 0x10279
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %eax
movl %eax, %edx
incl %edx
movl %edx, 0x8(%rcx)
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0xe260
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
callq 0x105c0
movq %rax, %rdi
callq 0xe120
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0x15d90
movq %rax, -0x90(%rbp)
jmp 0x1012a
movq -0x90(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x10262
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
callq 0xe4c0
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
callq 0x105c0
movq %rax, %rsi
leaq -0x78(%rbp), %rdi
callq 0xe130
jmp 0x10182
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x154d0
jmp 0x1019b
leaq -0x78(%rbp), %rdi
callq 0xe1d0
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
callq 0xe260
jmp 0x10262
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x101d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0xe1d0
movl -0x54(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x10284
movq -0x50(%rbp), %rdi
callq 0xe110
movq %rax, -0x80(%rbp)
movq 0x10db3(%rip), %rdi     # 0x20fb0
leaq 0xa473(%rip), %rsi      # 0x1a677
callq 0xe2f0
movq %rax, -0xa8(%rbp)
jmp 0x10212
movq -0x80(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
callq 0xe2f0
movq %rax, -0xb0(%rbp)
jmp 0x10237
movq -0xb0(%rbp), %rdi
movq 0x10d4b(%rip), %rsi     # 0x20f90
callq 0xe340
jmp 0x1024c
movl $0xb, %edi
callq 0xe9c0
jmp 0x10258
callq 0xe550
jmp 0x10096
jmp 0x1025d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
callq 0xe550
jmp 0x10277
jmp 0x10284
xorl %eax, %eax
addq $0xb0, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0xe5d0
movq %rax, %rdi
callq 0x105e0
nopw %cs:(%rax,%rax)
nop