// File: MITRECND[P]snugglefish/src/fileIndexer.cpp
// Function: snugglefish::fileIndexer::processNgrams(unsigned char*, unsigned long)
vector<uint32_t>* fileIndexer::processNgrams(unsigned char* buf, uint64_t fileSize){
    map<uint32_t, bool> ngram_map;

    uint32_t nGram = 0;
    uint32_t i, j;

    for(i = 0; ((i + this->ngramLength) - 1) < fileSize; i++){
        nGram = 0;
        for(j = 0; j < this->ngramLength; j++){
            nGram += (unsigned char)buf[i+j] * (1 << (8*j));
        }
        if(nGram >= this->maxNgram){
            throw std::runtime_error("Ngram greater than maxNgram");
        }
        ngram_map.insert(pair<uint32_t,bool>(nGram, true));
    }

    //We should now have a map of ngrams, turn into a sorted vector
    //TODO pre-allocate vector to do this faster
    vector<uint32_t>* ngramVector = new vector<uint32_t>;
    
    for(map<uint32_t,bool>::iterator it = ngram_map.begin(); it != ngram_map.end() ; it++){
        ngramVector->push_back(it->first);
    }

    return ngramVector;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x16540
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movq -0xa8(%rbp), %rcx
movl -0x50(%rbp), %eax
addl 0x8(%rcx), %eax
subl $0x1, %eax
movl %eax, %eax
cmpq -0x18(%rbp), %rax
jae 0x163cc
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x54(%rbp)
movq -0xa8(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x8(%rcx), %eax
jae 0x162f5
movq -0x10(%rbp), %rax
movl -0x50(%rbp), %ecx
addl -0x54(%rbp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl -0x54(%rbp), %ecx
shll $0x3, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
imull %ecx, %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x162b3
movq -0xa8(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpq 0x10(%rcx), %rax
jb 0x16377
movl $0x10, %edi
callq 0xe170
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xb0(%rbp)
leaq 0x4421(%rip), %rsi      # 0x1a744
callq 0xe100
jmp 0x1632a
movq -0xb0(%rbp), %rdi
movq 0xacb8(%rip), %rsi      # 0x20ff0
movq 0xac69(%rip), %rdx      # 0x20fa8
callq 0xe5b0
jmp 0x164af
movq -0xb0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0xe230
jmp 0x1649d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1649d
movb $0x1, -0x6d(%rbp)
leaq -0x6c(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
leaq -0x6d(%rbp), %rdx
callq 0x165a0
jmp 0x1638e
leaq -0x48(%rbp), %rdi
leaq -0x6c(%rbp), %rsi
callq 0x16560
movb %dl, -0xb9(%rbp)
movq %rax, -0xb8(%rbp)
jmp 0x163aa
movb -0xb9(%rbp), %al
movq -0xb8(%rbp), %rcx
movq %rcx, -0x80(%rbp)
movb %al, -0x78(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x16289
movl $0x18, %edi
callq 0xe300
movq %rax, -0xc8(%rbp)
jmp 0x163df
movq -0xc8(%rbp), %rdi
callq 0x164b0
movq -0xc8(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x165d0
movq %rax, -0x90(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x16630
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x16600
testb $0x1, %al
jne 0x16432
jmp 0x16476
movq -0x88(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x16660
movq -0xd0(%rbp), %rdi
movq %rax, %rsi
callq 0x164d0
jmp 0x1645d
jmp 0x1645f
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x16680
movq %rax, -0xa0(%rbp)
jmp 0x16409
movq -0x88(%rbp), %rax
movq %rax, -0xd8(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x166c0
movq -0xd8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x166c0
movq -0x60(%rbp), %rdi
callq 0xe5d0
nop