// File: JohnPapad[P]Mini-Search-Engine/minisearch.cpp
// Function: check_program_args(char**, int, unsigned int&)
char * check_program_args(char ** argv, int argc, unsigned int & K)
{
    if ((argc < 3) || (argc > 5))
    {
        cout<<"-! ERROR - Wrong number of arguments is given !-"<<endl;
        exit(1);
    }

    char * docfile = NULL;
    int kappa;
    bool k_found = 0;

    for(int i = 1; i < argc; i++)
    {
        if(strcmp(argv[i], "-i") == 0)
        {// if -i parameter found the next argument must be docfile's name
            if((i + 1) == argc)
            {
                cout<<"-! ERROR - Docfile's name not found !-"<<endl;
                exit(1);
            }
            else
            {
                docfile = argv[i + 1];
                i++;
            }
        }
        else if(strcmp(argv[i], "-k") == 0)
        {
            if((i + 1) == argc)
            {
                cout<<"-! ERROR - K's value not found !-"<<endl;
                exit(1);
            }
            else
            {
                if(!is_number(argv[i + 1]))
                {
                    cout<<"-> WARNING - K's value is not a number. K will be set by default with 10 <-"<<endl;
                    kappa = 10;
                }
                else
                {
                    kappa = atoi(argv[i + 1]);
                }

                k_found = 1;
                i++;
            }
        }
        else
        {
            cout<<"-! ERROR - invalid parameters given !-"<<endl;
            exit(1);
        }
    }

    if(docfile == NULL)
    {
        cout<<"-! ERROR - Docfile's -i parameter not found !-"<<endl;
        exit(1);
    }

    if(k_found == 0)
    {
        cout<<"-> WARNING - K's -k parameter not found. K will be set by default with 10 <-"<<endl;
        K = 10;
    }
    else
    {
        if (kappa < 1)
        {
            cout<<"-> WARNING - K's value is less than 1. K will be set by default with 10 <-"<<endl;
            K = 10;
        }
        else
        {
            K = kappa;
        }
    }

    return docfile;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
cmpl $0x3, -0xc(%rbp)
jl 0x37df
cmpl $0x5, -0xc(%rbp)
jle 0x380b
movq 0x57ea(%rip), %rdi      # 0x8fd0
leaq 0x2ab0(%rip), %rsi      # 0x629d
callq 0x20c0
movq %rax, %rdi
movq 0x57c4(%rip), %rsi      # 0x8fc0
callq 0x2100
movl $0x1, %edi
callq 0x2140
movq $0x0, -0x20(%rbp)
movb $0x0, -0x25(%rbp)
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x39a4
movq -0x8(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2a91(%rip), %rsi      # 0x62ce
callq 0x2190
cmpl $0x0, %eax
jne 0x38a1
movl -0x2c(%rbp), %eax
addl $0x1, %eax
cmpl -0xc(%rbp), %eax
jne 0x387e
movq 0x5777(%rip), %rdi      # 0x8fd0
leaq 0x2a71(%rip), %rsi      # 0x62d1
callq 0x20c0
movq %rax, %rdi
movq 0x5751(%rip), %rsi      # 0x8fc0
callq 0x2100
movl $0x1, %edi
callq 0x2140
movq -0x8(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3994
movq -0x8(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2a44(%rip), %rsi      # 0x62f8
callq 0x2190
cmpl $0x0, %eax
jne 0x3966
movl -0x2c(%rbp), %eax
addl $0x1, %eax
cmpl -0xc(%rbp), %eax
jne 0x38f9
movq 0x56fc(%rip), %rdi      # 0x8fd0
leaq 0x2a20(%rip), %rsi      # 0x62fb
callq 0x20c0
movq %rax, %rdi
movq 0x56d6(%rip), %rsi      # 0x8fc0
callq 0x2100
movl $0x1, %edi
callq 0x2140
movq -0x8(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2d00
testb $0x1, %al
jne 0x393e
movq 0x56b6(%rip), %rdi      # 0x8fd0
leaq 0x29fc(%rip), %rsi      # 0x631d
callq 0x20c0
movq %rax, %rdi
movq 0x5690(%rip), %rsi      # 0x8fc0
callq 0x2100
movl $0xa, -0x24(%rbp)
jmp 0x3957
movq -0x8(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x21f0
movl %eax, -0x24(%rbp)
movb $0x1, -0x25(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3992
movq 0x5663(%rip), %rdi      # 0x8fd0
leaq 0x29f5(%rip), %rsi      # 0x6369
callq 0x20c0
movq %rax, %rdi
movq 0x563d(%rip), %rsi      # 0x8fc0
callq 0x2100
movl $0x1, %edi
callq 0x2140
jmp 0x3994
jmp 0x3996
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x381e
cmpq $0x0, -0x20(%rbp)
jne 0x39d7
movq 0x561e(%rip), %rdi      # 0x8fd0
leaq 0x29d7(%rip), %rsi      # 0x6390
callq 0x20c0
movq %rax, %rdi
movq 0x55f8(%rip), %rsi      # 0x8fc0
callq 0x2100
movl $0x1, %edi
callq 0x2140
movb -0x25(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x3a12
movq 0x55e5(%rip), %rdi      # 0x8fd0
leaq 0x29cd(%rip), %rsi      # 0x63bf
callq 0x20c0
movq %rax, %rdi
movq 0x55bf(%rip), %rsi      # 0x8fc0
callq 0x2100
movq -0x18(%rbp), %rax
movl $0xa, (%rax)
jmp 0x3a51
cmpl $0x1, -0x24(%rbp)
jge 0x3a46
movq 0x55b1(%rip), %rdi      # 0x8fd0
leaq 0x29e6(%rip), %rsi      # 0x640c
callq 0x20c0
movq %rax, %rdi
movq 0x558b(%rip), %rsi      # 0x8fc0
callq 0x2100
movq -0x18(%rbp), %rax
movl $0xa, (%rax)
jmp 0x3a4f
movl -0x24(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0x3a51
movq -0x20(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)