// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rsi, -0x38(%rbp)
movq %rdi, %rbx
pushq $0x18
popq %rdi
callq 0x6280
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movl $0xa8, %edi
callq 0x6280
movq %rax, %r13
xorl %r15d, %r15d
movl $0xa8, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x6170
leaq -0x98(%rbp), %r12
movq %r12, %rdi
callq 0x6300
movq %r12, %rdi
xorl %esi, %esi
callq 0x6350
leaq 0x30(%r13), %rdi
movq %rdi, -0x50(%rbp)
xorl %esi, %esi
callq 0x63d0
leaq 0x58(%r13), %rdi
movq %rdi, -0x48(%rbp)
xorl %esi, %esi
callq 0x63d0
leaq 0x80(%r13), %rdi
movq %rdi, -0x40(%rbp)
xorl %esi, %esi
callq 0x63d0
movzbl 0x28(%rbx), %eax
movl %eax, 0x8(%r13)
andl $0x0, (%r13)
movl 0x48(%rbx), %eax
movl %eax, 0x4(%r13)
movq 0x40(%rbx), %rax
movq %rax, 0x10(%r13)
movq %r14, -0x58(%rbp)
movq %r14, 0x18(%r13)
leaq 0x8(%rbx), %rax
movq %rax, 0x20(%r13)
movq %r13, -0x30(%rbp)
movq -0x38(%rbp), %rax
movq %rax, 0x28(%r13)
movl 0x38(%rbx), %r14d
movq %r14, %rdi
shlq $0x3, %rdi
callq 0x6360
movq %rax, %r13
movq %rax, %r12
movl %r14d, %eax
cmpq %rax, %r15
jae 0x825f
movq %r12, %rdi
leaq -0x98(%rbp), %rsi
leaq 0x95(%rip), %rdx        # 0x82de
movq -0x30(%rbp), %rcx
callq 0x64f0
incq %r15
movl 0x38(%rbx), %r14d
addq $0x8, %r12
jmp 0x8230
xorl %r12d, %r12d
leaq -0x60(%rbp), %r15
movl %r14d, %eax
cmpq %rax, %r12
jae 0x8284
movq (%r13,%r12,8), %rdi
movq %r15, %rsi
callq 0x6250
incq %r12
movl 0x38(%rbx), %r14d
jmp 0x8266
movq -0x50(%rbp), %rdi
callq 0x62c0
movq -0x48(%rbp), %rdi
callq 0x62c0
movq -0x40(%rbp), %rdi
callq 0x62c0
leaq -0x98(%rbp), %rdi
callq 0x6450
movq %r13, %rdi
callq 0x62e0
movl $0xa8, %esi
movq -0x30(%rbp), %rdi
callq 0x6290
movq 0x40(%rbx), %rdi
callq 0x98c8
movq -0x58(%rbp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop