// File: /workspace/github_cpp_makefile_star2_O2/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, -0x48(%rbp)
movl %edx, %r13d
movq %rsi, %r12
movq %rdi, -0x30(%rbp)
pushq $0x70
popq %rdi
callq 0x6280
movq %rax, %rbx
movl %r14d, %eax
movq %rax, -0x50(%rbp)
leaq (,%rax,8), %rdi
movq %rdi, -0x58(%rbp)
callq 0x6360
movq %rax, -0x38(%rbp)
leaq -0xb8(%rbp), %r14
movq %r14, %rdi
callq 0x6300
movq %r14, %rdi
xorl %esi, %esi
callq 0x6350
leaq 0x20(%rbx), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x63d0
leaq 0x48(%rbx), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x63d0
movq %r12, (%rbx)
movl %r13d, 0x18(%rbx)
andl $0x0, 0x8(%rbx)
leaq -0x78(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x6370
leaq -0x140(%rbp), %rdi
leaq -0x78(%rbp), %rdx
movl %r13d, %esi
callq 0x8ad4
movq %r15, -0x40(%rbp)
movq %r14, -0x30(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x60f0
movq -0x48(%rbp), %rax
testq %rax, %rax
je 0x715d
movq %rax, -0x108(%rbp)
leaq -0x140(%rbp), %rax
movq %rax, 0x10(%rbx)
xorl %r15d, %r15d
leaq 0x3bf(%rip), %r13       # 0x7531
leaq -0xb8(%rbp), %r14
cmpq %r15, -0x58(%rbp)
je 0x719b
movq -0x38(%rbp), %rax
leaq (%rax,%r15), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x64f0
addq $0x8, %r15
jmp 0x7179
leaq -0x140(%rbp), %r15
pushq $0x1
popq %r13
movl 0x8(%rbx), %esi
movq 0x8(%r12), %rdx
subq (%r12), %rdx
sarq $0x5, %rdx
cmpq %rsi, %rdx
jbe 0x71cd
movq %r15, %rdi
callq 0x7404
movl %r13d, %edi
callq 0x61f0
jmp 0x71a6
xorl %r14d, %r14d
leaq -0x80(%rbp), %r15
cmpq %r14, -0x50(%rbp)
je 0x71ef
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %rdi
movq %r15, %rsi
callq 0x6250
incq %r14
jmp 0x71d4
movl 0x8(%rbx), %esi
movq 0x8(%r12), %rdx
subq (%r12), %rdx
sarq $0x5, %rdx
leaq -0x140(%rbp), %rdi
callq 0x7404
movq 0x7dd6(%rip), %rdi      # 0xefe8
callq 0x6110
leaq -0x140(%rbp), %rdi
callq 0x8c50
movq -0x30(%rbp), %rdi
callq 0x62c0
movq -0x40(%rbp), %rdi
callq 0x62c0
leaq -0xb8(%rbp), %rdi
callq 0x6450
pushq $0x70
popq %rsi
movq %rbx, %rdi
callq 0x6290
movq -0x38(%rbp), %rdi
callq 0x62e0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r14
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x60f0
jmp 0x7296
movq %rdx, %r14
movq %rax, %rbx
jmp 0x7296
jmp 0x7284
jmp 0x7284
movq %rdx, %r14
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x8c50
cmpl $0x1, %r14d
jne 0x72e9
movq %rbx, %rdi
callq 0x60d0
movq %rax, %rbx
movq 0x7d02(%rip), %rdi      # 0xefb0
leaq 0x43d3(%rip), %rsi      # 0xb688
callq 0x6270
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x6270
movq %rax, %rdi
callq 0x6110
pushq $0xb
popq %rdi
callq 0x67ee
movq %rax, %rbx
callq 0x6430
movq %rbx, %rdi
callq 0x64b0
movq %rax, %rdi
callq 0x777b