// File: /workspace/github_cpp_makefile_star2_O1/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, -0x68(%rbp)
movl %edi, %r12d
movl $0x0, -0x34(%rbp)
movl $0x3, -0x30(%rbp)
leaq -0x87(%rip), %rsi       # 0x58e9
movl $0xb, %edi
callq 0x52e0
leaq -0x140(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x78(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x5727(%rip), %rdx      # 0xb0c6
leaq -0x88(%rbp), %rdi
movq %rdx, %rsi
callq 0x712a
leaq -0xc0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
movq %rax, -0x90(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
callq 0x583c
movl %eax, -0x38(%rbp)
movq $0x0, -0xa8(%rbp)
leaq 0x56f8(%rip), %r14      # 0xb0e5
leaq 0x98dc(%rip), %r13      # 0xf2d0
leaq -0x34(%rbp), %rbx
movq $0x0, -0xb0(%rbp)
movq $0x0, -0x60(%rbp)
movl %r12d, %edi
movq -0x68(%rbp), %rsi
movq %r14, %rdx
movq %r13, %rcx
movq %rbx, %r8
callq 0x5470
leal -0x62(%rax), %ecx
cmpl $0x12, %ecx
ja 0x5b18
leaq 0x55d1(%rip), %rdx      # 0xb004
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x954d(%rip), %rax      # 0xef90
movq (%rax), %rbx
movq %r14, %r15
movq -0x80(%rbp), %r14
movq %rbx, %rdi
callq 0x50f0
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r15, %r14
leaq 0x9865(%rip), %r13      # 0xf2d0
movq %rbx, %rcx
leaq -0x34(%rbp), %rbx
movq %rax, %r8
callq 0x5410
jmp 0x5a0b
movq 0x950d(%rip), %rax      # 0xef90
movq (%rax), %rsi
leaq -0x58(%rbp), %r15
movq %r15, %rdi
leaq -0x29(%rbp), %rdx
callq 0x6e7a
leaq -0x2e8(%rbp), %rdi
movq %r15, %rsi
movl $0x8, %edx
callq 0x5490
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x5ac3
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x5290
leaq -0x2e8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x54d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x5d4c
movl -0x30(%rbp), %eax
addl $-0x5, %eax
cmpl $-0x3, %eax
jbe 0x5d7d
leaq -0x2e8(%rbp), %rdi
movq 0x949e(%rip), %rsi      # 0xefa0
callq 0x5390
leaq -0x270(%rbp), %rdi
callq 0x5090
jmp 0x5a0b
cmpl $-0x1, %eax
jne 0x5a0b
jmp 0x5df2
movq 0x9463(%rip), %rax      # 0xef90
movq (%rax), %rsi
leaq -0x58(%rbp), %rbx
movq %rbx, %rdi
leaq -0x29(%rbp), %rdx
callq 0x6e7a
leaq -0x2e8(%rbp), %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x5490
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x5b6d
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x5290
movl $0x0, -0x58(%rbp)
leaq -0x2e8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x54d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x20(%rax,%rcx), %ebx
testb $0x5, %bl
je 0x5d03
movl $0x32, %edx
movq 0x940c(%rip), %rdi      # 0xefb0
leaq 0x5548(%rip), %rsi      # 0xb0f3
callq 0x52c0
movq 0x93f9(%rip), %rcx      # 0xefb0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x5260
movsbl %al, %esi
movq 0x93db(%rip), %rdi      # 0xefb0
callq 0x5040
movq %rax, %rdi
callq 0x51a0
jmp 0x5d09
movb $0x1, %al
movq %rax, -0xb0(%rbp)
jmp 0x5a0b
movq 0x9394(%rip), %rax      # 0xef90
movq (%rax), %rsi
leaq -0x58(%rbp), %rbx
movq %rbx, %rdi
leaq -0x29(%rbp), %rdx
callq 0x6e7a
leaq -0x2e8(%rbp), %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x5490
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x5c3c
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x5290
movq $0x0, -0x58(%rbp)
leaq -0x2e8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5150
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl $0x35, %edx
testb $0x5, 0x20(%rax,%rcx)
leaq 0x5510(%rip), %rsi      # 0xb17c
jne 0x5c88
movq -0x58(%rbp), %rax
movl $0x2d, %edx
leaq 0x5534(%rip), %rsi      # 0xb1b2
cmpq $0x3, %rax
ja 0x5d3a
movq 0x9321(%rip), %rdi      # 0xefb0
callq 0x52c0
movq 0x9315(%rip), %rcx      # 0xefb0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x5260
movsbl %al, %esi
movq 0x92f7(%rip), %rdi      # 0xefb0
callq 0x5040
movq %rax, %rdi
callq 0x51a0
xorl %ebx, %ebx
leaq -0x2e8(%rbp), %rdi
movq 0x92ca(%rip), %rsi      # 0xefa0
callq 0x5390
leaq -0x270(%rbp), %rdi
callq 0x5090
testb %bl, %bl
leaq -0x34(%rbp), %rbx
jne 0x5a0b
jmp 0x5fba
movb $0x1, %al
movq %rax, -0x60(%rbp)
jmp 0x5a0b
movl -0x58(%rbp), %eax
movl %eax, -0x38(%rbp)
leaq -0x2e8(%rbp), %rdi
movq 0x9289(%rip), %rsi      # 0xefa0
callq 0x5390
leaq -0x270(%rbp), %rdi
callq 0x5090
testb $0x5, %bl
leaq -0x34(%rbp), %rbx
je 0x5a0b
jmp 0x5fba
shlq $0x1e, %rax
movb $0x1, %bl
movq %rax, -0xa8(%rbp)
jmp 0x5cc8
movq 0x925d(%rip), %rbx      # 0xefb0
leaq 0x53cc(%rip), %rsi      # 0xb126
movl $0x2b, %edx
movq %rbx, %rdi
callq 0x52c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5260
jmp 0x5dbb
movq 0x922c(%rip), %rdi      # 0xefb0
leaq 0x53c7(%rip), %rsi      # 0xb152
movl $0x29, %edx
callq 0x52c0
movl -0x30(%rbp), %esi
movq 0x9211(%rip), %rdi      # 0xefb0
callq 0x5190
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x51a0
movq 0x91cb(%rip), %rsi      # 0xefa0
leaq -0x2e8(%rbp), %rdi
callq 0x5390
leaq -0x270(%rbp), %rdi
callq 0x5090
jmp 0x5fba
movq -0xb0(%rbp), %rcx
testb $0x1, %cl
je 0x5e17
movq -0x60(%rbp), %rax
testb $0x1, %al
je 0x5e23
movl $0x21, %edx
leaq 0x53f9(%rip), %rsi      # 0xb20b
jmp 0x5f49
movq -0x60(%rbp), %rax
testb $0x1, %al
je 0x5f2f
cmpq $0x0, -0x80(%rbp)
je 0x5f3d
testb $0x1, %cl
je 0x5f85
movq 0x9182(%rip), %r14      # 0xefc0
movl (%r14), %eax
cmpl %r12d, %eax
jge 0x5ea4
leaq -0x2d8(%rbp), %r15
leaq -0x2e8(%rbp), %rbx
leaq -0xa0(%rbp), %r13
cltq
movq -0x68(%rbp), %rcx
movq (%rcx,%rax,8), %rsi
movq %rbx, %rdi
leaq -0x58(%rbp), %rdx
callq 0x6e7a
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7202
movq -0x2e8(%rbp), %rdi
cmpq %r15, %rdi
je 0x5e97
movq -0x2d8(%rbp), %rsi
incq %rsi
callq 0x5290
movl (%r14), %eax
incl %eax
movl %eax, (%r14)
cmpl %r12d, %eax
jl 0x5e5b
movq -0xa0(%rbp), %rax
cmpq -0x98(%rbp), %rax
jne 0x6055
leaq -0x2d8(%rbp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
movq 0x90f7(%rip), %rbx      # 0xefd0
leaq -0x2e8(%rbp), %r14
leaq -0xa0(%rbp), %r15
movq (%rbx), %rax
movq -0x18(%rax), %rdi
testb $0x5, 0x20(%rbx,%rdi)
jne 0x603a
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5260
movsbl %al, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5530
cmpq $0x0, -0x2e0(%rbp)
je 0x603a
movq %r15, %rdi
movq %r14, %rsi
callq 0x6eb8
jmp 0x5ee7
movl $0x2a, %edx
leaq 0x52a5(%rip), %rsi      # 0xb1e0
jmp 0x5f49
movl $0x14, %edx
leaq 0x52e4(%rip), %rsi      # 0xb22d
movq 0x9060(%rip), %rbx      # 0xefb0
movq %rbx, %rdi
callq 0x52c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x5260
movsbl %al, %esi
movq 0x903a(%rip), %rdi      # 0xefb0
callq 0x5040
movq %rax, %rdi
callq 0x51a0
jmp 0x5fba
testb $0x1, %al
je 0x5fb5
movq 0x9030(%rip), %rax      # 0xefc0
movslq (%rax), %rax
cmpl %r12d, %eax
jge 0x60d9
movq -0x68(%rbp), %rcx
movq (%rcx,%rax,8), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x5370
jmp 0x60ec
callq 0x63c4
leaq -0xa0(%rbp), %rdi
callq 0x6f98
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x5fe8
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x6004
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x6026
movq -0x140(%rbp), %rsi
incq %rsi
callq 0x5290
xorl %eax, %eax
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x2e8(%rbp), %rdi
cmpq %r12, %rdi
je 0x6055
movq -0x2d8(%rbp), %rsi
incq %rsi
callq 0x5290
leaq -0x120(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x130(%rbp), %rdi
callq 0x7090
leaq -0x168(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x6f04
movl -0x30(%rbp), %edx
leaq -0x130(%rbp), %rdi
leaq -0x168(%rbp), %rsi
movq -0xa8(%rbp), %r8
movl -0x38(%rbp), %r9d
callq 0x674c
leaq -0x168(%rbp), %rdi
callq 0x6f98
movq -0x130(%rbp), %rdi
cmpq %r14, %rdi
je 0x5fba
movq -0x120(%rbp), %rsi
incq %rsi
jmp 0x6240
movq 0x8ef0(%rip), %rdi      # 0xefd0
leaq -0xd0(%rbp), %rsi
callq 0x5330
movl -0x30(%rbp), %eax
cmpq %rax, -0xc8(%rbp)
jae 0x6121
movq 0x8eb1(%rip), %rdi      # 0xefb0
leaq 0x513c(%rip), %rsi      # 0xb242
movl $0x70, %edx
callq 0x52c0
movq 0x8e99(%rip), %rdi      # 0xefb0
callq 0x5100
jmp 0x5fba
leaq -0x100(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x110(%rbp), %rdi
callq 0x7090
leaq -0xe0(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x7090
movl -0x30(%rbp), %edx
leaq -0x110(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movl -0x38(%rbp), %ecx
callq 0x6a00
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x61a5
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x110(%rbp), %rdi
cmpq %r14, %rdi
je 0x61c0
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x5290
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x6230
movl $0x1, %r12d
xorl %ecx, %ecx
movq 0x8dd8(%rip), %r14      # 0xefb0
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r14, %rdi
callq 0x52c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x5260
movsbl %al, %esi
movq %r15, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x51a0
movl %r12d, %ecx
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
incl %r12d
cmpq %rcx, %rdx
ja 0x61d8
movq %rbx, %rdi
callq 0x6f98
movl $0x18, %esi
movq %rbx, %rdi
callq 0x5290
jmp 0x5fba
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x626d
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x5290
jmp 0x626d
movq %rax, %rbx
movq -0x110(%rbp), %rdi
cmpq %r14, %rdi
je 0x6350
movq -0x100(%rbp), %rsi
jmp 0x6343
jmp 0x634d
movq %rax, %rbx
leaq -0x168(%rbp), %rdi
callq 0x6f98
jmp 0x62a2
movq %rax, %rbx
movq -0x130(%rbp), %rdi
cmpq %r14, %rdi
je 0x6350
movq -0x120(%rbp), %rsi
jmp 0x6343
jmp 0x632f
jmp 0x634d
jmp 0x632f
jmp 0x634d
jmp 0x634d
movq %rax, %rbx
movq -0x2e8(%rbp), %rdi
cmpq %r15, %rdi
jne 0x62ee
jmp 0x6350
movq %rax, %rbx
movq -0x2e8(%rbp), %rdi
cmpq %r12, %rdi
je 0x6350
movq -0x2d8(%rbp), %rsi
jmp 0x6343
jmp 0x634d
movq %rax, %rbx
jmp 0x639a
jmp 0x630b
jmp 0x630b
jmp 0x634d
jmp 0x630b
jmp 0x634d
movq %rax, %rbx
movq 0x8c8b(%rip), %rsi      # 0xefa0
leaq -0x2e8(%rbp), %rdi
callq 0x5390
leaq -0x270(%rbp), %rdi
callq 0x5090
jmp 0x6350
movq %rax, %rbx
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x6350
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x5290
jmp 0x6350
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x6f98
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x637e
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x639a
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x63bc
movq -0x140(%rbp), %rsi
incq %rsi
callq 0x5290
movq %rbx, %rdi
callq 0x54e0