// File: MITRECND[P]snugglefish/src/nGramIndex.cpp
// Function: snugglefish::nGramIndex::addNGrams(std::vector<unsigned int, std::allocator<unsigned int>>*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void nGramIndex::addNGrams(vector<uint32_t>* nGramList, string filename){
    //POSIX basename may modify argument so create a copy
    char* temp_filename = new char[filename.length() + 1];
    strncpy(temp_filename, filename.c_str(), filename.length() + 1);
    filename = basename(temp_filename);
    delete[] temp_filename;

    ngram_t_fidtype file_id = numFilesProcessed++;
    fileNameList.push_back(filename);

    // Insert ngram into the list, add the new node to the memory usage variable,
    // and check if the maximum memory has been used, and if so, indicate that the
    // nGrams should be flushed to disk
    for(uint32_t i = 0; i < nGramList->size(); i++){
        uint32_t nGram = (*nGramList)[i];
        output_buffer[nGram].elements_size++;
        output_buffer[nGram].elements->push_back(file_id);

        buffer_memory_usage += BUFFER_NODE_SIZE; //Add size of node

        if(buffer_memory_usage >= bufferMax){ 
            flush = true;
        }
    }

    //We cleanup the memory
    delete nGramList;

    if (flush){
        flushAll();
        flush = false;
    }


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdx, -0x60(%rbp)
movq %rdi, %rax
movq -0x60(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movq %rax, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
callq 0xe4f0
movq %rax, %rdi
addq $0x1, %rdi
callq 0xe030
movq -0x40(%rbp), %rdi
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x50(%rbp)
callq 0xe120
movq -0x40(%rbp), %rdi
movq %rax, -0x48(%rbp)
callq 0xe4f0
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0xe180
movq -0x20(%rbp), %rdi
callq 0xe5c0
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0xe460
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0x1556e
movq -0x38(%rbp), %rdi
callq 0xe420
movq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdi
movq 0x78(%rdi), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x78(%rdi)
movl %eax, -0x24(%rbp)
addq $0x50, %rdi
callq 0x103e0
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
callq 0x15960
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
jae 0x15633
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x15980
movq %rax, %rcx
movq -0x58(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0x2c(%rbp)
movq 0x40(%rax), %rcx
movl -0x2c(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
movq 0x40(%rax), %rax
movl -0x2c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
leaq -0x24(%rbp), %rsi
callq 0x13b60
movq -0x58(%rbp), %rcx
movq 0x48(%rcx), %rax
addq $0x1c, %rax
movq %rax, 0x48(%rcx)
movq 0x48(%rcx), %rax
cmpq 0x38(%rcx), %rax
jb 0x15623
movq -0x58(%rbp), %rax
movb $0x1, 0x68(%rax)
jmp 0x15625
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x15598
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x15658
movq -0x70(%rbp), %rdi
callq 0x159a0
movq -0x70(%rbp), %rdi
movl $0x18, %esi
callq 0xe310
movq -0x58(%rbp), %rax
testb $0x1, 0x68(%rax)
je 0x15673
movq -0x58(%rbp), %rdi
callq 0x15460
movq -0x58(%rbp), %rax
movb $0x0, 0x68(%rax)
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)