// File: /workspace/github_cpp_makefile_star2_O1/MITRECND[P]snugglefish/src/snugglefish.cpp
// Function: make_index
void make_index(string indexFileName, vector <string> fileNames, uint32_t ngramSize, uint32_t max_files, uint64_t max_buffer, uint32_t threads){

    pthread_t* indexers;
    mi_data* midata;
    void* status;


    midata = new mi_data;
    indexers = (pthread_t*) malloc(threads * sizeof(pthread_t));


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

    pthread_mutex_init(& (midata->filesMutex), NULL);
    pthread_mutex_init(& (midata->nGramIndexMutex), NULL);

    midata->fileList = & fileNames;
    midata->ngramSize = ngramSize;
    midata->queue = 0;

    try{
        nGramIndex ngramindex(ngramSize, indexFileName);
        if (max_buffer > 0){
            ngramindex.setmaxBufferSize(max_buffer);
        }

        midata->ngramindex = &ngramindex;

        for(uint32_t i = 0; i < threads; i++){
            pthread_create(& indexers[i], & attr, indexerThread, (void*) midata);
        }

        while(1){
            //Usage of mutex shouldn't matter
            if (midata->queue >= fileNames.size()){
                break;
            }
            
            printStats(&ngramindex, midata->queue, fileNames.size());
            sleep(1);
        }


        for(uint32_t i = 0; i < threads; i++){
            pthread_join(indexers[i], &status);
        }

        //Print some final stats
        printStats(&ngramindex, midata->queue, fileNames.size());
        cerr << endl;

    } catch (exception& e){
        cout << "Error:" << e.what() << endl;
        handler(SIGSEGV);
    }


    pthread_mutex_destroy(&(midata->filesMutex));
    pthread_mutex_destroy(&(midata->nGramIndexMutex));
    pthread_attr_destroy(&attr);

    delete midata;
    free(indexers);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %r9d, %r14d
movq %r8, -0x58(%rbp)
movl %edx, %r15d
movq %rsi, %r13
movq %rdi, %r12
movl $0x70, %edi
callq 0x5280
movq %rax, %rbx
movl %r14d, -0x2c(%rbp)
movl %r14d, %eax
movq %rax, -0x40(%rbp)
leaq (,%rax,8), %rdi
callq 0x5360
movq %rax, -0x38(%rbp)
leaq -0xb8(%rbp), %r14
movq %r14, %rdi
callq 0x5310
movq %r14, %rdi
xorl %esi, %esi
callq 0x5350
leaq 0x20(%rbx), %rdi
movq %rdi, -0x50(%rbp)
xorl %esi, %esi
callq 0x53f0
leaq 0x48(%rbx), %rdi
movq %rdi, -0x48(%rbp)
xorl %esi, %esi
callq 0x53f0
movq %r13, (%rbx)
movl %r15d, 0x18(%rbx)
movl $0x0, 0x8(%rbx)
leaq -0x68(%rbp), %r14
movq %r14, -0x10(%r14)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq -0x78(%rbp), %rdi
callq 0x7090
leaq -0x140(%rbp), %r12
leaq -0x78(%rbp), %rdx
movq %r12, %rdi
movl %r15d, %esi
callq 0x84dc
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x6826
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x58(%rbp), %rax
testq %rax, %rax
je 0x6836
movq %rax, -0x108(%rbp)
movq %r12, 0x10(%rbx)
cmpl $0x0, -0x2c(%rbp)
je 0x6885
leaq -0xb8(%rbp), %r12
movq -0x40(%rbp), %r15
movq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
leaq 0x46e(%rip), %rdx       # 0x6cca
movq %rbx, %rcx
callq 0x5520
addq $0x8, %r14
decq %r15
jne 0x684f
jmp 0x6885
leaq -0x140(%rbp), %rdi
callq 0x6b7e
movl $0x1, %edi
callq 0x51f0
movl 0x8(%rbx), %esi
movq 0x8(%r13), %rdx
subq (%r13), %rdx
sarq $0x5, %rdx
cmpq %rsi, %rdx
ja 0x686f
cmpl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %r12
je 0x68bf
xorl %r14d, %r14d
leaq -0x80(%rbp), %r15
movq (%r12,%r14,8), %rdi
movq %r15, %rsi
callq 0x5240
incq %r14
cmpq %r14, -0x40(%rbp)
jne 0x68aa
movl 0x8(%rbx), %esi
movq 0x8(%r13), %rdx
subq (%r13), %rdx
sarq $0x5, %rdx
leaq -0x140(%rbp), %rdi
callq 0x6b7e
movq 0x8707(%rip), %rdi      # 0xefe8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x5260
movsbl %al, %esi
movq 0x86ec(%rip), %rdi      # 0xefe8
callq 0x5040
movq %rax, %rdi
callq 0x51a0
leaq -0x140(%rbp), %rdi
callq 0x86be
movq -0x50(%rbp), %rdi
callq 0x52d0
movq -0x48(%rbp), %rdi
callq 0x52d0
leaq -0xb8(%rbp), %rdi
callq 0x5480
movl $0x70, %esi
movq %rbx, %rdi
callq 0x5290
movq %r12, %rdi
callq 0x52f0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r15
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x6995
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x5290
jmp 0x6995
movq %rdx, %r15
movq %rax, %rbx
jmp 0x6995
jmp 0x6983
jmp 0x6983
movq %rdx, %r15
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x86be
cmpl $0x1, %r15d
jne 0x69f0
movq %rbx, %rdi
callq 0x50e0
movq %rax, %rbx
movq 0x8603(%rip), %rdi      # 0xefb0
leaq 0x4cd4(%rip), %rsi      # 0xb688
movl $0x6, %edx
callq 0x52c0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0x85e2(%rip), %rdi      # 0xefb0
movq %rax, %rsi
callq 0x5270
movq %rax, %rdi
callq 0x5100
movl $0xb, %edi
callq 0x58e9
movq %rax, %rbx
callq 0x5460
movq %rbx, %rdi
callq 0x54e0
movq %rax, %rdi
callq 0x7002