// File: MITRECND[P]snugglefish/src/fileIndexer.cpp
// Function: snugglefish::fileIndexer::processFile(char const*)
vector<uint32_t>* fileIndexer::processFile(const char* fileName){

    vector<uint32_t>* ngramList = 0;
    file* inputFile = new file(fileName);
    size_t fileSize = inputFile->get_size();
    uint8_t* inputFileMap = inputFile->mmap();

    if (!fileSize){
        delete inputFile;
        return NULL;
    } 


    this->filesProcessed++;

    //It is much faster to just use an array that holds a boolean
    //for every element, this requires only ~64MB per file for 3-byte Ngrams
    //but 16GB for 4 byte ngrams, so only do this for 3-byte ngrams
    //and do a map instead for 4-byte
    if(this->ngramLength == 3){
        ngramList = new vector<uint32_t>;
        bool* bngramList = new bool[this->maxNgram]();

        try{
            processNgrams(inputFileMap, fileSize, bngramList);

            for(uint32_t k = 0; k < this->maxNgram; k++){
                if(bngramList[k]){
                    ngramList->push_back(k);
                }
            }
            
            delete[] bngramList;

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

    }else{ //ngramLength == 4
        try {
            ngramList = processNgrams(inputFileMap, fileSize);
        } catch(exception &e){
            cout << "Error processing ngrams: " << e.what() << endl;
        }
    }

    inputFile->close();
    delete inputFile;

    return ngramList;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x40, %edi
callq 0xe300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rsi
movl $0x10000000, %edx       # imm = 0x10000000
callq 0x17aa0
jmp 0x15dd7
movq -0x70(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x18050
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
callq 0x18110
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x15e51
movq -0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x15e25
movq -0x80(%rbp), %rdi
callq 0x17b50
movq -0x80(%rbp), %rdi
movl $0x40, %esi
callq 0xe310
movq $0x0, -0x8(%rbp)
jmp 0x160ec
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x40, %esi
callq 0xe310
jmp 0x160f9
movq -0x78(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
cmpl $0x3, 0x8(%rax)
jne 0x15fe8
movl $0x18, %edi
callq 0xe300
movq %rax, %rdi
movq %rdi, -0x98(%rbp)
callq 0x164b0
movq -0x98(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq 0x10(%rax), %rdi
movq %rdi, -0x90(%rbp)
callq 0xe030
movq -0x90(%rbp), %rdx
movq %rax, %rdi
movq %rdi, -0x88(%rbp)
xorl %esi, %esi
callq 0xe1a0
movq -0x78(%rbp), %rdi
movq -0x88(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x16110
jmp 0x15edb
movl $0x0, -0x54(%rbp)
movq -0x78(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpq 0x10(%rcx), %rax
jae 0x15fb1
movq -0x50(%rbp), %rax
movl -0x54(%rbp), %ecx
testb $0x1, (%rax,%rcx)
je 0x15fa1
movq -0x20(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0x164d0
jmp 0x15f13
jmp 0x15fa1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x160f9
movq -0x30(%rbp), %rdi
callq 0xe110
movq %rax, -0x60(%rbp)
movq 0xb068(%rip), %rdi      # 0x20fb0
leaq 0x47db(%rip), %rsi      # 0x1a72a
callq 0xe2f0
movq %rax, -0xa0(%rbp)
jmp 0x15f5d
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq -0xa0(%rbp), %rdi
movq %rax, %rsi
callq 0xe2f0
movq %rax, -0xa8(%rbp)
jmp 0x15f82
movq -0xa8(%rbp), %rdi
movq 0xb000(%rip), %rsi      # 0x20f90
callq 0xe340
jmp 0x15f97
callq 0xe550
jmp 0x160ad
jmp 0x15fa3
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x15ee2
movq -0x50(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x15fce
movq -0xb0(%rbp), %rdi
callq 0xe420
jmp 0x15f9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0xe550
jmp 0x15fe3
jmp 0x160f9
movq -0x78(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x16250
movq %rax, -0xb8(%rbp)
jmp 0x16002
movq -0xb8(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x16096
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x160f9
movq -0x30(%rbp), %rdi
callq 0xe110
movq %rax, -0x68(%rbp)
movq 0xaf6e(%rip), %rdi      # 0x20fb0
leaq 0x46e1(%rip), %rsi      # 0x1a72a
callq 0xe2f0
movq %rax, -0xc0(%rbp)
jmp 0x16057
movq -0x68(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
callq 0xe2f0
movq %rax, -0xc8(%rbp)
jmp 0x1607c
movq -0xc8(%rbp), %rdi
movq 0xaf06(%rip), %rsi      # 0x20f90
callq 0xe340
jmp 0x16091
callq 0xe550
jmp 0x160ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0xe550
jmp 0x160ab
jmp 0x160f9
movq -0x28(%rbp), %rdi
callq 0x17bd0
movq -0x28(%rbp), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, %rax
je 0x160e4
movq -0xd0(%rbp), %rdi
callq 0x17b50
movq -0xd0(%rbp), %rdi
movl $0x40, %esi
callq 0xe310
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0xe5d0
movq %rax, %rdi
callq 0x105e0
nopw (%rax,%rax)