// File: /workspace/github_cpp_makefile_star2_O3/MITRECND[P]snugglefish/src/nGramSearch.cpp
// Function: snugglefish::nGramSearch::searchNGrams[abi:cxx11](std::vector<unsigned long, std::allocator<unsigned long>>)
vector<string>* nGramSearch::searchNGrams(vector<uint64_t> nGramQuery){
    vector<string>* matchedFiles = new vector<string>;
    pthread_t * threads;
    thread_data* tdata;
    void* status;

    tdata = new thread_data();

    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    pthread_mutex_init(&(tdata->queueMutex), NULL);
    pthread_mutex_init(&(tdata->smFileMutex), NULL);
    pthread_mutex_init(&(tdata->mfMutex), NULL);

    tdata->ngramLength = ngramLength;
    tdata->queue = 0;
    tdata->maximumIndex = numIndexFiles;

    tdata->masterFile = (this->masterFile);
    tdata->matchedFiles = matchedFiles;
    tdata->baseFileName = &baseFileName;
    tdata->nGramQuery = &(nGramQuery);

    threads = (pthread_t*) malloc(numThreads * sizeof(pthread_t));

    //Create the threads
    for(uint32_t i = 0; i < numThreads; i++){
        pthread_create(& threads[i], &attr, searchNGramThread, (void*) (tdata));
    }
    
    //Join on them
    for(uint32_t i = 0; i < numThreads; i++){
        pthread_join(threads[i], &status);
    }

    pthread_mutex_destroy(&(tdata->queueMutex));
    pthread_mutex_destroy(&(tdata->smFileMutex));
    pthread_mutex_destroy(&(tdata->mfMutex));
    pthread_attr_destroy(&attr);

    free(threads);
    delete tdata;
    masterFile->close();
    return matchedFiles;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r12
movq %rdi, %r14
movl $0x18, %edi
callq 0x5280
movq %rax, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movl $0xa8, %edi
callq 0x5280
movq %rax, %r15
movl $0xa8, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x5160
leaq -0x90(%rbp), %rbx
movq %rbx, %rdi
callq 0x5310
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5350
leaq 0x30(%r15), %rdi
movq %rdi, -0x48(%rbp)
xorl %esi, %esi
callq 0x53f0
leaq 0x58(%r15), %rdi
movq %rdi, -0x40(%rbp)
xorl %esi, %esi
callq 0x53f0
leaq 0x80(%r15), %rdi
movq %rdi, -0x38(%rbp)
xorl %esi, %esi
callq 0x53f0
movzbl 0x28(%r14), %eax
movl %eax, 0x8(%r15)
movl $0x0, (%r15)
movl 0x48(%r14), %eax
movl %eax, 0x4(%r15)
movq 0x40(%r14), %rax
movq %rax, 0x10(%r15)
movq %r13, -0x50(%rbp)
movq %r13, 0x18(%r15)
leaq 0x8(%r14), %rax
movq %rax, 0x20(%r15)
movq %r12, 0x28(%r15)
movq %r15, %r12
movl 0x38(%r14), %ebx
leaq (,%rbx,8), %rdi
callq 0x5360
movq %rax, %r13
testq %rbx, %rbx
je 0x7de8
xorl %r15d, %r15d
leaq -0x90(%rbp), %rbx
movq %r13, -0x30(%rbp)
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x98(%rip), %rdx        # 0x7e40
movq %r12, %rcx
callq 0x5520
incq %r15
movl 0x38(%r14), %eax
addq $0x8, %r13
cmpq %rax, %r15
jb 0x7d9b
testl %eax, %eax
movq -0x30(%rbp), %r13
je 0x7de8
xorl %r15d, %r15d
leaq -0x58(%rbp), %rbx
movq (%r13,%r15,8), %rdi
movq %rbx, %rsi
callq 0x5240
incq %r15
movl 0x38(%r14), %eax
cmpq %rax, %r15
jb 0x7dcf
movq -0x48(%rbp), %rdi
callq 0x52d0
movq -0x40(%rbp), %rdi
callq 0x52d0
movq -0x38(%rbp), %rdi
callq 0x52d0
leaq -0x90(%rbp), %rdi
callq 0x5480
movq %r13, %rdi
callq 0x52f0
movl $0xa8, %esi
movq %r12, %rdi
callq 0x5290
movq 0x40(%r14), %rdi
callq 0x9394
movq -0x50(%rbp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq