// File: /workspace/github_cpp_makefile_star2_O2/MITRECND[P]snugglefish/src/snugglefish.cpp
// Function: main
int main(int argc, char *argv[]){
    int c, option_index = 0;
    uint32_t ngramSize = 3;
    uint32_t max_files = 0;
    uint64_t max_buffer = 0;
    uint32_t threads = 0;

    signal(SIGSEGV, handler); 

    string commandString, indexFileName = "", searchString;
    vector <string> fileList;
    static struct option long_options[] ={
        // Ngramsize defaults to 3 if not present
        {INDEX_OPTION_LONG, no_argument, 0, INDEX_OPTION_SHORT},
        {SEARCH_OPTION_LONG, no_argument, 0, SEARCH_OPTION_SHORT},
        {FILE_OPTION_LONG, required_argument, 0, FILE_OPTION_SHORT},
        {OUTPUT_OPTION_LONG, required_argument, 0, OUTPUT_OPTION_SHORT},
        {NGRAM_SIZE_OPTION_LONG, required_argument, 0, NGRAM_SIZE_OPTION_SHORT},
        {HELP_OPTION_LONG, no_argument, 0, HELP_OPTION_SHORT},
        {THREADS_OPTION_LONG, required_argument, 0, THREADS_OPTION_SHORT},
        {0,0,0,0}
    };
    bool indexFlag = false, searchFlag = false;

    // Default threads to the number of cpus
    threads = cpu_count();

    // loop over all of the options
    while ((c = getopt_long(argc, argv, SHORT_OPTIONS_STRING, long_options, &option_index)) != -1){
        // check to see if a single character or long option came through
        switch (c){
            // short option 't'
            case THREADS_OPTION_SHORT:
                {
                    istringstream tss(optarg);
                    uint32_t thr = 0;

                    if(!(tss >> thr)){
                        cout << "Invalid number of threads, please enter an integer" << endl;
                        return 0;
                    }else{
                        threads = thr;
                    }

                    break;
                }
            case SEARCH_OPTION_SHORT:
                searchFlag = true;
                break;
            case INDEX_OPTION_SHORT:
                indexFlag = true;
                break;
                // Outpur and file options are the same
            case OUTPUT_OPTION_SHORT:
            case FILE_OPTION_SHORT:
                indexFileName = optarg;
                break;
            case HELP_OPTION_SHORT:
                printHelp();
                return 0;
                // short option 'a'
            case NGRAM_SIZE_OPTION_SHORT:
                {
                    //string ngramSizeString(optarg);
                    istringstream ss(optarg);
                    //stringstream ss;
                    //ss << ngramSizeString;
                    if(!(ss >> ngramSize)){
                        cout << "Invalid ngram size, please enter an integer" << endl;
                        return 0;
                    }
                    if(ngramSize < 3 || ngramSize > 4){
                        cout << "Ngram size must be 3 or 4. Ngram size is " << ngramSize << endl;
                        return 0;
                    }
                    break;
                }

            case NODE_BUFFER_OPTION_SHORT:
                {
                    istringstream ss(optarg);
                    uint64_t tmax_buffer = 0;
                    if(!(ss >> tmax_buffer)){
                        cout << "Invalid maxmimum node buffer, please enter an integer" << endl;
                        return 0;
                    }

                    if(tmax_buffer < 4){
                        cout << "Node buffer size must be at least 4GB in size" << endl;
                        return 0;
                    }
            
                    max_buffer = tmax_buffer * 1073741824; //in Gigabytes 
                    break;
                }
                    
        }
    }
    /* the rest of the command line arguments 
     * each option needs at least an aditional parameter*/
    if((!indexFlag && !searchFlag)){
        cout << "Must specify -i for index or -s for search" << endl;
        return 0;
    }

    if((indexFlag && searchFlag)){
        cout << "Must specify only one of -i or -s" << endl;
        return 0;

    }
    if(indexFileName.empty()){
        cout << "Index file necessary" << endl;
        return 0;
    }


    if(indexFlag){
        // get the list of files to add

        for(; optind < argc; optind++){
            fileList.push_back(argv[optind]);
        }
        // if fileList is empty, it means we need to get the
        // filenames from stdin
        if(fileList.empty()){
            string fileName;
            while(cin){
                getline(cin,fileName);
                if(fileName.empty())
                    break;
                fileList.push_back(fileName);
            }
        }
        //Eventually options should be sent as a structure
        make_index(indexFileName, fileList, ngramSize, max_files, max_buffer, threads);
    } else if(searchFlag) {
        // Get the string to search for
		if (optind < argc) {
			searchString = argv[optind];
		} else {
			cin >> searchString;
		}
	    if (searchString.size() < ngramSize){
	        cout << "Search string size is smaller than Ngram size, the search string must be greater than or equal to the ngram size" << endl;
	    } else {
			vector<string>* found = search(indexFileName, searchString, ngramSize, threads);
			for(uint32_t i = 0; i < found->size(); i++){
       			cout << (*found)[i] << endl;
   			}
            delete found;
		}
    } else{
        printHelp();
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rsi, %r15
movl %edi, %r12d
andl $0x0, -0x68(%rbp)
movl $0x3, -0x30(%rbp)
leaq -0x7d(%rip), %rsi       # 0x67ee
pushq $0xb
popq %rdi
callq 0x62d0
leaq -0x158(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x4839(%rip), %rsi      # 0xb0c6
leaq -0xb0(%rbp), %rdi
leaq -0x2e8(%rbp), %rdx
callq 0x7684
leaq -0xc0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
andq $0x0, -0x70(%rbp)
callq 0x679c
movl %eax, -0x38(%rbp)
movq $0x0, -0x88(%rbp)
leaq 0x89d7(%rip), %rbx      # 0xf2b0
leaq -0x68(%rbp), %r14
leaq 0x4720(%rip), %r13      # 0xb004
movq $0x0, -0x90(%rbp)
movq $0x0, -0x60(%rbp)
movl %r12d, -0x64(%rbp)
movl %r12d, %edi
movq %r15, %rsi
leaq 0x47dd(%rip), %rdx      # 0xb0e5
movq %rbx, %rcx
movq %r14, %r8
callq 0x6440
leal -0x62(%rax), %ecx
cmpl $0x12, %ecx
ja 0x69d3
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq 0x8668(%rip), %rax      # 0xef98
movq (%rax), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x6520
jmp 0x68fb
movq %r15, %r12
movq %r14, %r15
movq %rbx, %r14
movq 0x8647(%rip), %rax      # 0xef98
movq (%rax), %rsi
leaq -0x58(%rbp), %rbx
movq %rbx, %rdi
leaq -0x29(%rbp), %rdx
callq 0x7684
leaq -0x2e8(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
pushq $0x8
popq %rdx
callq 0x6460
movq %rbx, %rdi
callq 0x60f0
movq %r13, %rdi
leaq -0x30(%rbp), %rsi
callq 0x64a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x6b52
movl -0x30(%rbp), %eax
addl $-0x5, %eax
cmpl $-0x3, %eax
jbe 0x6b67
movq %r14, %rbx
leaq -0x2e8(%rbp), %rdi
callq 0x6100
movq %r15, %r14
movq %r12, %r15
movl -0x64(%rbp), %r12d
leaq 0x4636(%rip), %r13      # 0xb004
jmp 0x68fb
cmpl $-0x1, %eax
jne 0x68fb
jmp 0x6bda
movq 0x85b0(%rip), %rax      # 0xef98
movq (%rax), %rsi
leaq -0x58(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x7684
leaq -0x2e8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x6460
leaq -0x58(%rbp), %rdi
callq 0x60f0
andl $0x0, -0x58(%rbp)
leaq -0x2e8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x64a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x20(%rax,%rcx), %eax
movl %eax, -0x34(%rbp)
testb $0x5, %al
je 0x6b1b
movq 0x856b(%rip), %rdi      # 0xefb0
leaq 0x46a7(%rip), %rsi      # 0xb0f3
callq 0x6270
movq %rax, %rdi
callq 0x6110
jmp 0x6b21
movq 0x8533(%rip), %rax      # 0xef98
movq (%rax), %rsi
leaq -0x58(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x7684
leaq -0x2e8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x6460
leaq -0x58(%rbp), %rdi
callq 0x60f0
andq $0x0, -0x58(%rbp)
leaq -0x2e8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x6160
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
leaq 0x46c3(%rip), %rsi      # 0xb17c
jne 0x6acc
movq -0x58(%rbp), %rax
leaq 0x46ec(%rip), %rsi      # 0xb1b2
cmpq $0x3, %rax
ja 0x6b39
movq 0x84dd(%rip), %rdi      # 0xefb0
callq 0x6270
movq %rax, %rdi
callq 0x6110
movl $0x0, -0x34(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0x6100
cmpb $0x0, -0x34(%rbp)
jne 0x68fb
jmp 0x6b99
movb $0x1, %al
movq %rax, -0x90(%rbp)
jmp 0x68fb
movb $0x1, %al
movq %rax, -0x60(%rbp)
jmp 0x68fb
movl -0x58(%rbp), %eax
movl %eax, -0x38(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0x6100
testb $0x5, -0x34(%rbp)
je 0x68fb
jmp 0x6b99
shlq $0x1e, %rax
movb $0x1, %cl
movl %ecx, -0x34(%rbp)
movq %rax, -0x88(%rbp)
jmp 0x6ae7
callq 0x6f24
jmp 0x6b99
movq 0x8457(%rip), %rdi      # 0xefb0
leaq 0x45c6(%rip), %rsi      # 0xb126
callq 0x6270
jmp 0x6b85
movq 0x8442(%rip), %rdi      # 0xefb0
leaq 0x45dd(%rip), %rsi      # 0xb152
callq 0x6270
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x61a0
movq %rax, %rdi
callq 0x6110
leaq -0x2e8(%rbp), %rdi
callq 0x6100
leaq -0x80(%rbp), %rdi
callq 0x7750
leaq -0xd0(%rbp), %rdi
callq 0x60f0
leaq -0xb0(%rbp), %rdi
callq 0x60f0
leaq -0x168(%rbp), %rdi
callq 0x60f0
xorl %eax, %eax
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, -0x90(%rbp)
je 0x6bf5
testb $0x1, -0x60(%rbp)
je 0x6c35
leaq 0x461b(%rip), %rsi      # 0xb20b
jmp 0x6d31
testb $0x1, -0x60(%rbp)
je 0x6c88
cmpq $0x0, -0xa8(%rbp)
je 0x6c94
movq 0x83ac(%rip), %rax      # 0xefc0
movslq (%rax), %rax
cmpl %r12d, %eax
jge 0x6d0b
movq (%r15,%rax,8), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x6520
jmp 0x6d1e
cmpq $0x0, -0xa8(%rbp)
je 0x6c94
movq 0x837a(%rip), %r14      # 0xefc0
movl (%r14), %eax
leaq -0x2e8(%rbp), %rbx
leaq -0x80(%rbp), %r13
cmpl %r12d, %eax
jge 0x6ca0
cltq
movq (%r15,%rax,8), %rsi
movq %rbx, %rdi
leaq -0x58(%rbp), %rdx
callq 0x7684
movq %r13, %rdi
movq %rbx, %rsi
callq 0x78e4
movq %rbx, %rdi
callq 0x60f0
movl (%r14), %eax
incl %eax
movl %eax, (%r14)
jmp 0x6c54
leaq 0x4551(%rip), %rsi      # 0xb1e0
jmp 0x6d31
leaq 0x4592(%rip), %rsi      # 0xb22d
jmp 0x6d31
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jne 0x6e07
leaq -0x2d8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x8308(%rip), %rbx      # 0xefd0
leaq -0x2e8(%rbp), %r14
leaq -0x80(%rbp), %r15
movq (%rbx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rbx,%rax)
jne 0x6dfb
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6070
cmpq $0x0, -0x2e0(%rbp)
je 0x6dfb
movq %r15, %rdi
movq %r14, %rsi
callq 0x76c0
jmp 0x6cd3
movq 0x82be(%rip), %rdi      # 0xefd0
leaq -0xd0(%rbp), %rsi
callq 0x6320
movl -0x30(%rbp), %eax
cmpq %rax, -0xc8(%rbp)
jae 0x6d4a
leaq 0x4511(%rip), %rsi      # 0xb242
movq 0x8278(%rip), %rdi      # 0xefb0
callq 0x6270
movq %rax, %rdi
callq 0x6110
jmp 0x6b99
leaq -0x128(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x6370
leaq -0x108(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x6370
movl -0x30(%rbp), %edx
leaq -0x128(%rbp), %rdi
leaq -0x108(%rbp), %rsi
movl -0x38(%rbp), %ecx
callq 0x72f9
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
callq 0x60f0
leaq -0x128(%rbp), %rdi
callq 0x60f0
xorl %r15d, %r15d
movq 0x8202(%rip), %r14      # 0xefb0
movl %r15d, %r15d
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r15, %rax
jbe 0x6de3
movq %r15, %rax
shlq $0x5, %rax
addq %rax, %rsi
movq %r14, %rdi
callq 0x6240
movq %rax, %rdi
callq 0x6110
incl %r15d
jmp 0x6dae
movq %rbx, %rdi
callq 0x7750
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0x6290
jmp 0x6b99
leaq -0x2e8(%rbp), %rdi
callq 0x60f0
leaq -0x148(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x6370
leaq -0xe8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x76fc
movl -0x30(%rbp), %edx
leaq -0x148(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
movq -0x88(%rbp), %r8
movl -0x38(%rbp), %r9d
callq 0x7090
leaq -0xe8(%rbp), %rdi
callq 0x7750
leaq -0x148(%rbp), %rdi
callq 0x60f0
jmp 0x6b99
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
callq 0x60f0
jmp 0x6e7c
movq %rax, %rbx
leaq -0x128(%rbp), %rdi
jmp 0x6ee5
jmp 0x6eec
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x7750
jmp 0x6e9b
movq %rax, %rbx
leaq -0x148(%rbp), %rdi
jmp 0x6ee5
jmp 0x6eb2
jmp 0x6ede
jmp 0x6eec
jmp 0x6ede
jmp 0x6eec
jmp 0x6ecd
jmp 0x6eec
movq %rax, %rbx
leaq -0x2e8(%rbp), %rdi
jmp 0x6ee5
jmp 0x6eec
jmp 0x6ecd
movq %rax, %rbx
jmp 0x6f10
jmp 0x6ecd
jmp 0x6eec
jmp 0x6eec
movq %rax, %rbx
leaq -0x2e8(%rbp), %rdi
callq 0x6100
jmp 0x6eef
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x60f0
jmp 0x6eef
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x7750
leaq -0xd0(%rbp), %rdi
callq 0x60f0
leaq -0xb0(%rbp), %rdi
callq 0x60f0
leaq -0x168(%rbp), %rdi
callq 0x60f0
movq %rbx, %rdi
callq 0x64b0