// File: /workspace/github_cpp_makefile_star2_O3/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, %r13d
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 0x7134
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), %r12      # 0xf2d0
leaq -0x34(%rbp), %rbx
movq $0x0, -0xb0(%rbp)
movq $0x0, -0x60(%rbp)
movl %r13d, %edi
movq -0x68(%rbp), %rsi
movq %r14, %rdx
movq %r12, %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), %r12      # 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 0x6e7c
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 0x5d56
movl -0x30(%rbp), %eax
addl $-0x5, %eax
cmpl $-0x3, %eax
jbe 0x5d87
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 0x5e77
movq 0x9463(%rip), %rax      # 0xef90
movq (%rax), %rsi
leaq -0x58(%rbp), %rbx
movq %rbx, %rdi
leaq -0x29(%rbp), %rdx
callq 0x6e7c
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
movq 0x93a2(%rip), %rax      # 0xef90
movq (%rax), %rsi
leaq -0x58(%rbp), %rbx
movq %rbx, %rdi
leaq -0x29(%rbp), %rdx
callq 0x6e7c
leaq -0x2e8(%rbp), %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x5490
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x5c2e
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 0x551e(%rip), %rsi      # 0xb17c
jne 0x5c7a
movq -0x58(%rbp), %rax
movl $0x2d, %edx
leaq 0x5542(%rip), %rsi      # 0xb1b2
cmpq $0x3, %rax
ja 0x5d3a
movq 0x932f(%rip), %rdi      # 0xefb0
callq 0x52c0
movq 0x9323(%rip), %rcx      # 0xefb0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x5260
movsbl %al, %esi
movq 0x9305(%rip), %rdi      # 0xefb0
callq 0x5040
movq %rax, %rdi
callq 0x51a0
xorl %ebx, %ebx
leaq -0x2e8(%rbp), %rdi
movq 0x92d8(%rip), %rsi      # 0xefa0
callq 0x5390
leaq -0x270(%rbp), %rdi
callq 0x5090
testb %bl, %bl
leaq -0x34(%rbp), %rbx
jne 0x5a0b
jmp 0x5df7
movb $0x1, %al
movq %rax, -0xb0(%rbp)
jmp 0x5a0b
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 0x5df7
shlq $0x1e, %rax
movb $0x1, %bl
movq %rax, -0xa8(%rbp)
jmp 0x5cba
callq 0x63be
jmp 0x5df7
movq 0x9253(%rip), %rbx      # 0xefb0
leaq 0x53c2(%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 0x5dc5
movq 0x9222(%rip), %rdi      # 0xefb0
leaq 0x53bd(%rip), %rsi      # 0xb152
movl $0x29, %edx
callq 0x52c0
movl -0x30(%rbp), %esi
movq 0x9207(%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 0x91c1(%rip), %rsi      # 0xefa0
leaq -0x2e8(%rbp), %rdi
callq 0x5390
leaq -0x270(%rbp), %rdi
callq 0x5090
leaq -0xa0(%rbp), %rdi
callq 0x6f9a
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x5e25
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x5e41
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x5e63
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
testb $0x1, -0xb0(%rbp)
je 0x5e97
testb $0x1, -0x60(%rbp)
je 0x5ed8
movl $0x21, %edx
leaq 0x5379(%rip), %rsi      # 0xb20b
jmp 0x5ff5
testb $0x1, -0x60(%rbp)
je 0x5fdb
cmpq $0x0, -0x80(%rbp)
je 0x5fe9
movq 0x910d(%rip), %rax      # 0xefc0
movslq (%rax), %rax
cmpl %r13d, %eax
jge 0x6034
movq -0x68(%rbp), %rcx
movq (%rcx,%rax,8), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x5370
jmp 0x6047
cmpq $0x0, -0x80(%rbp)
je 0x5fe9
movq 0x90d6(%rip), %r14      # 0xefc0
movl (%r14), %eax
cmpl %r13d, %eax
jge 0x5f50
leaq -0x2d8(%rbp), %r15
leaq -0x2e8(%rbp), %rbx
leaq -0xa0(%rbp), %r12
cltq
movq -0x68(%rbp), %rcx
movq (%rcx,%rax,8), %rsi
movq %rbx, %rdi
leaq -0x58(%rbp), %rdx
callq 0x6e7c
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7212
movq -0x2e8(%rbp), %rdi
cmpq %r15, %rdi
je 0x5f43
movq -0x2d8(%rbp), %rsi
incq %rsi
callq 0x5290
movl (%r14), %eax
incl %eax
movl %eax, (%r14)
cmpl %r13d, %eax
jl 0x5f07
movq -0xa0(%rbp), %rax
cmpq -0x98(%rbp), %rax
jne 0x61bb
leaq -0x2d8(%rbp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
movq 0x904b(%rip), %rbx      # 0xefd0
leaq -0x2e8(%rbp), %r14
leaq -0xa0(%rbp), %r15
movq (%rbx), %rax
movq -0x18(%rax), %rdi
testb $0x5, 0x20(%rbx,%rdi)
jne 0x61a0
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5260
movsbl %al, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5530
cmpq $0x0, -0x2e0(%rbp)
je 0x61a0
movq %r15, %rdi
movq %r14, %rsi
callq 0x6eba
jmp 0x5f93
movl $0x2a, %edx
leaq 0x51f9(%rip), %rsi      # 0xb1e0
jmp 0x5ff5
movl $0x14, %edx
leaq 0x5238(%rip), %rsi      # 0xb22d
movq 0x8fb4(%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 0x8f8e(%rip), %rdi      # 0xefb0
callq 0x5040
movq %rax, %rdi
callq 0x51a0
jmp 0x5df7
movq 0x8f95(%rip), %rdi      # 0xefd0
leaq -0xd0(%rbp), %rsi
callq 0x5330
movl -0x30(%rbp), %eax
cmpq %rax, -0xc8(%rbp)
jae 0x607c
movq 0x8f56(%rip), %rdi      # 0xefb0
leaq 0x51e1(%rip), %rsi      # 0xb242
movl $0x70, %edx
callq 0x52c0
movq 0x8f3e(%rip), %rdi      # 0xefb0
callq 0x5100
jmp 0x5df7
leaq -0x100(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x110(%rbp), %rdi
callq 0x7096
leaq -0xe0(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x7096
movl -0x30(%rbp), %edx
leaq -0x110(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movl -0x38(%rbp), %ecx
callq 0x6a02
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x6100
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x110(%rbp), %rdi
cmpq %r14, %rdi
je 0x611b
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x5290
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x618b
movl $0x1, %r12d
xorl %ecx, %ecx
movq 0x8e7d(%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 0x6133
movq %rbx, %rdi
callq 0x6f9a
movl $0x18, %esi
movq %rbx, %rdi
jmp 0x623a
movq -0x2e8(%rbp), %rdi
cmpq %r12, %rdi
je 0x61bb
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 0x7096
leaq -0x168(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x6f06
movl -0x30(%rbp), %edx
leaq -0x130(%rbp), %rdi
leaq -0x168(%rbp), %rsi
movq -0xa8(%rbp), %r8
movl -0x38(%rbp), %r9d
callq 0x6746
leaq -0x168(%rbp), %rdi
callq 0x6f9a
movq -0x130(%rbp), %rdi
cmpq %r14, %rdi
je 0x5df7
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x5290
jmp 0x5df7
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x6267
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x5290
jmp 0x6267
movq %rax, %rbx
movq -0x110(%rbp), %rdi
cmpq %r14, %rdi
je 0x634a
movq -0x100(%rbp), %rsi
jmp 0x633d
jmp 0x6347
movq %rax, %rbx
leaq -0x168(%rbp), %rdi
callq 0x6f9a
jmp 0x629c
movq %rax, %rbx
movq -0x130(%rbp), %rdi
cmpq %r14, %rdi
je 0x634a
movq -0x120(%rbp), %rsi
jmp 0x633d
jmp 0x6329
jmp 0x6347
jmp 0x6329
jmp 0x6347
jmp 0x6347
movq %rax, %rbx
movq -0x2e8(%rbp), %rdi
cmpq %r15, %rdi
jne 0x62e8
jmp 0x634a
movq %rax, %rbx
movq -0x2e8(%rbp), %rdi
cmpq %r12, %rdi
je 0x634a
movq -0x2d8(%rbp), %rsi
jmp 0x633d
jmp 0x6347
movq %rax, %rbx
jmp 0x6394
jmp 0x6305
jmp 0x6305
jmp 0x6347
jmp 0x6305
jmp 0x6347
movq %rax, %rbx
movq 0x8c91(%rip), %rsi      # 0xefa0
leaq -0x2e8(%rbp), %rdi
callq 0x5390
leaq -0x270(%rbp), %rdi
callq 0x5090
jmp 0x634a
movq %rax, %rbx
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x634a
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x5290
jmp 0x634a
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x6f9a
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x6378
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x6394
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x5290
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x63b6
movq -0x140(%rbp), %rsi
incq %rsi
callq 0x5290
movq %rbx, %rdi
callq 0x54e0