// File: MITRECND[P]snugglefish/src/nGramSearch.cpp
// Function: snugglefish::nGramSearch::orderNGrams[abi:cxx11](snugglefish::indexSet*, std::vector<unsigned long, std::allocator<unsigned long>> const&)
list< pair<uint64_t, size_t> > nGramSearch::orderNGrams(indexSet* index, const vector<uint64_t> & nGramQuery){
    bool nomatch = false;
    list< pair<uint64_t, size_t> > queryList;
    for(uint32_t j = 0; j < nGramQuery.size(); j++){
        size_t numfiles = index->getNGramCount(nGramQuery[j]);
        //index_entry* index_table = (index_entry*) (indexEntries + (nGramQuery[j] * (INDEX_ENTRY_SIZE)));

        if(numfiles == 0){
            nomatch = true;
            break;
            //No Files Match
        }

        if(queryList.empty()){
            queryList.push_back(pair<uint64_t,size_t>(nGramQuery[j], numfiles));
        }else{
            bool placed = false;
            for (list< pair<uint64_t, size_t> >::iterator it = queryList.begin(); 
              it != queryList.end(); it++ ){
                if((*it).second > numfiles){
                    queryList.insert(it, pair<uint64_t, size_t>(nGramQuery[j], numfiles));
                    placed = true;
                    break;
                }
            }
            if (!placed){
                queryList.push_back(pair<uint64_t,size_t>(nGramQuery[j], numfiles));
            }
        }
    }

    if (nomatch){
        queryList.clear();
    }

    return queryList;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0xa8(%rbp)
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movb $0x0, -0x1a(%rbp)
callq 0x138a0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x11c70
movq %rax, %rcx
movq -0xb0(%rbp), %rax
cmpq %rcx, %rax
jae 0x13397
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rdi
movl -0x20(%rbp), %eax
movl %eax, %esi
callq 0x138c0
movq -0xc0(%rbp), %rdi
movq (%rax), %rsi
callq 0x19170
movq %rax, -0xb8(%rbp)
jmp 0x13203
movq -0xb8(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x1323b
movb $0x1, -0x19(%rbp)
jmp 0x13397
movq -0xa8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x13880
jmp 0x133cf
movq -0xa8(%rbp), %rdi
callq 0x138e0
testb $0x1, %al
jne 0x1324d
jmp 0x13284
movq -0x18(%rbp), %rdi
movl -0x20(%rbp), %eax
movl %eax, %esi
callq 0x138c0
movq %rax, %rsi
leaq -0x48(%rbp), %rdi
leaq -0x28(%rbp), %rdx
callq 0x13940
jmp 0x1326d
movq -0xa8(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x13900
jmp 0x1327f
jmp 0x13387
movq -0xa8(%rbp), %rdi
movb $0x0, -0x49(%rbp)
callq 0x13970
movq %rax, -0x58(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x139d0
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x139a0
testb $0x1, %al
jne 0x132be
jmp 0x13345
leaq -0x58(%rbp), %rdi
callq 0x13a00
movq 0x8(%rax), %rax
cmpq -0x28(%rbp), %rax
jbe 0x1332c
leaq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x13a60
movq -0x18(%rbp), %rdi
movl -0x20(%rbp), %eax
movl %eax, %esi
callq 0x138c0
movq %rax, %rsi
leaq -0x78(%rbp), %rdi
leaq -0x28(%rbp), %rdx
callq 0x13940
jmp 0x132fe
movq -0xa8(%rbp), %rdi
movq -0x68(%rbp), %rsi
leaq -0x78(%rbp), %rdx
callq 0x13a20
movq %rax, -0xc8(%rbp)
jmp 0x1331b
movq -0xc8(%rbp), %rax
movq %rax, -0x80(%rbp)
movb $0x1, -0x49(%rbp)
jmp 0x13345
jmp 0x1332e
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x13a80
movq %rax, -0x88(%rbp)
jmp 0x13298
testb $0x1, -0x49(%rbp)
jne 0x13385
movq -0x18(%rbp), %rdi
movl -0x20(%rbp), %eax
movl %eax, %esi
callq 0x138c0
movq %rax, %rsi
leaq -0x98(%rbp), %rdi
leaq -0x28(%rbp), %rdx
callq 0x13940
jmp 0x1336e
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x13900
jmp 0x13383
jmp 0x13385
jmp 0x13387
jmp 0x13389
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x131ac
testb $0x1, -0x19(%rbp)
je 0x133a9
movq -0xa8(%rbp), %rdi
callq 0x13ab0
movb $0x1, -0x1a(%rbp)
testb $0x1, -0x1a(%rbp)
jne 0x133bf
movq -0xa8(%rbp), %rdi
callq 0x13880
movq -0xa0(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0xe5d0
nopl (%rax,%rax)