// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rdi, %r14
movl $0x18, %edi
callq 0x5280
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movl $0xa8, %edi
callq 0x5280
movq %rax, %rbx
movl $0xa8, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x5160
leaq -0x88(%rbp), %r12
movq %r12, %rdi
callq 0x5310
movq %r12, %rdi
xorl %esi, %esi
callq 0x5350
leaq 0x30(%rbx), %rdi
movq %rdi, -0x40(%rbp)
xorl %esi, %esi
callq 0x53f0
leaq 0x58(%rbx), %rdi
movq %rdi, -0x38(%rbp)
xorl %esi, %esi
callq 0x53f0
leaq 0x80(%rbx), %rdi
movq %rdi, -0x30(%rbp)
xorl %esi, %esi
callq 0x53f0
movzbl 0x28(%r14), %eax
movl %eax, 0x8(%rbx)
movl $0x0, (%rbx)
movl 0x48(%r14), %eax
movl %eax, 0x4(%rbx)
movq 0x40(%r14), %rax
movq %rax, 0x10(%rbx)
movq %r15, -0x48(%rbp)
movq %r15, 0x18(%rbx)
leaq 0x8(%r14), %rax
movq %rax, 0x20(%rbx)
movq %r13, 0x28(%rbx)
movl 0x38(%r14), %r12d
leaq (,%r12,8), %rdi
callq 0x5360
movq %rax, %r15
testq %r12, %r12
je 0x7dbc
xorl %r12d, %r12d
movq %r15, %r13
movq %r13, %rdi
leaq -0x88(%rbp), %rsi
leaq 0x9a(%rip), %rdx        # 0x7e3e
movq %rbx, %rcx
callq 0x5520
incq %r12
movl 0x38(%r14), %eax
addq $0x8, %r13
cmpq %rax, %r12
jb 0x7d93
movq %rbx, %r13
cmpl $0x0, 0x38(%r14)
je 0x7de5
xorl %r12d, %r12d
leaq -0x50(%rbp), %rbx
movq (%r15,%r12,8), %rdi
movq %rbx, %rsi
callq 0x5240
incq %r12
movl 0x38(%r14), %eax
cmpq %rax, %r12
jb 0x7dcd
movq -0x40(%rbp), %rdi
callq 0x52d0
movq -0x38(%rbp), %rdi
callq 0x52d0
movq -0x30(%rbp), %rdi
callq 0x52d0
leaq -0x88(%rbp), %rdi
callq 0x5480
movq %r15, %rdi
callq 0x52f0
movl $0xa8, %esi
movq %r13, %rdi
callq 0x5290
movq 0x40(%r14), %rdi
callq 0x9398
movq -0x48(%rbp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop