// File: /workspace/github_cpp_makefile_star2_O2/adamiaonr[P]rid-analytics/src/argvparser.cpp
// Function: CommandLineProcessing::ArgvParser::parse(int, char**)
ArgvParser::ParserResults
ArgvParser::parse(int _argc, char ** _argv)
{
    bool finished_options = false; // flag whether an argument was found (options are passed)

    // loop over all command line arguments
    int i = 1; // argument counter
    while( i< _argc )
    {
        string argument = _argv[i];
        unsigned int key = 0;
        string option; // option name
        string value;  // option value

        // if argument is an option
        if (!isValidOptionString(argument))
        {
            // string is a real argument since values are processed elsewhere
            finished_options=true;
            argument_container.push_back(argument);
        }
        else // can be a long or multiple short options at this point
        {
            // check whether we already found an argument
            if (finished_options)
            {
                error_option = argument;
                return(ParserOptionAfterArgument); // return error code
            }
            // check for long options
            if (isValidLongOptionString(argument))
            {
                // handle long options

                // remove trailing '--'
                argument = argument.substr(2);
                // check for option value assignment 'option=value'
                splitOptionAndValue(argument, option, value);

                if (!isDefinedOption(option)) // is this a known option
                {
                    error_option = option; // store the option that caused the error
                    return(ParserUnknownOption); // return error code if not
                }

                // get the key of this option - now that we know that it is defined
                key = option2key.find(option)->second;
                if (key == help_option) // if help is requested return error code
                    return(ParserHelpRequested);

                // do we need to extract a value
                // AND a value is not already assigned from the previous step
                if ((option2attribute.find(key)->second & OptionRequiresValue) && value.empty())
                {
                    if (i+1 >= _argc) // are there arguments left?
                    {
                        error_option = option; // store the option that caused the error
                        return(ParserMissingValue); // the was no argument left although we need a value
                    }

                    string temp = _argv[i+1]; // get the next element
                    ++i; // increase counter now that we moved forward

                    if (isValidOptionString(temp))
                    {
                        error_option = option; // store the option that caused the error
                        return(ParserMissingValue);  // missing option value
                    }
                    value = temp; // assign value
                }
                // add option-value map entry
                option2value[key] = value;
            }
            else // handle short options
            {
                argument = argument.substr(1);   // remove trailing '-'

                // check for option value assignment 'option=value'
                if (splitOptionAndValue(argument, option, value))
                {
                    // there was an option <- value assignment
                    if (option.length() > 1)
                    {
                        error_option = option; // store the option that caused the error
                        return(ParserMalformedMultipleShortOption); // return error code if option has more than one character
                    }

                    if (!isDefinedOption(option)) // is this a known option
                    {
                        error_option = option; // store the option that caused the error
                        return(ParserUnknownOption); // return error code if not
                    }
                    key = option2key.find(option)->second; // get the key for the extracted option name

                    if (key == help_option) // if help is requested return error code
                        return(ParserHelpRequested);

                    // if value is still empty for some reason: we have an error
                    if ((option2attribute.find(key)->second & OptionRequiresValue) && value.empty())
                    {
                        error_option = option; // store the option that caused the error
                        return(ParserMissingValue);   // missing option value
                    }
                    else
                        // add option-value map entry
                        option2value[key] = value;
                }
                else // no '=' assignment: can be either multiple short options or
                    // something like '-s 4'
                {
                    // handle short options with value like '-s 4'
                    option.clear();
                    value.clear();

                    if (argument.length() == 1) // if a single short option
                    {
                        if (!isDefinedOption(argument)) // is this a known option
                        {
                            error_option = argument; // store the option that caused the error
                            return(ParserUnknownOption); // return error code if not
                        }
                        key = option2key.find(argument)->second; // get the key for the extracted option name

                        if (key == help_option) // if help is requested return error code
                            return(ParserHelpRequested);

                        // check if option needs a value and next arg is not an option
                        if ((option2attribute.find(key)->second & OptionRequiresValue))
                        {
                            if (i+1 >= _argc) // are there arguments left?
                            {
                                error_option = argument; // store the option that caused the error
                                return(ParserMissingValue); // the was no argument left although we need a value
                            }
                            string temp = _argv[i+1]; // get the next element
                            ++i; // increase counter now that we moved forward

                            if (isValidOptionString(temp))
                            {
                                error_option = argument; // store the option that caused the error
                                return(ParserMissingValue);  // missing option value
                            }
                            // add option-value map entry
                            option2value[key] = temp;

                        }
                        else // no value needed
                        {
                            option2value[key] = ""; // assign value
                        }
                    }
                    else // handle multiple short option like '-svxgh'
                    {
                        unsigned int short_option_counter = 0; // position in the multiple short option string
                        while( short_option_counter < argument.length() ) // parse the whole string
                        {
                            option = argument[short_option_counter]; // get the option character

                            if (!isDefinedOption(option)) // is this a known option
                            {
                                error_option = option; // store the option that caused the error
                                return(ParserUnknownOption); // return error code if not
                            }
                            key = option2key.find(option)->second; // get the key for the extracted option name

                            if (key == help_option) // if help is requested return error code
                                return(ParserHelpRequested);

                            option2value[key] = value;

                            ++short_option_counter; // advance one character forward
                        }
                    }
                }
            }
        }
        ++i;
    }

    map<unsigned int, OptionAttributes>::iterator it;
    for( it = option2attribute.begin(); it != option2attribute.end(); it++ )
    {
        // if the current option is required look if we got it
        if (it->second & OptionRequired)
        {
            // is the object missing
            if (option2value.find(it->first) == option2value.end())
            {
                // get the list of alternative names for this option
                list<string> alternatives = getAllOptionAlternatives(it->first);

                unsigned int count = 0;
                for( list<string>::const_iterator alt = alternatives.begin();
                        alt != alternatives.end();
                        ++alt )
                {
                    ++count;
                    // additional '-' for long options
                    if (alt->length() > 1)
                        error_option += "-";

                    error_option += "-" + *alt;

                    // alternatives to come?
                    if (count < alternatives.size())
                        error_option += ", "; // add separator
                }
                return(ParserRequiredOptionMissing);
            }
        }
    }

    return(NoParserError); // everthing went fine -> sucess
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, 0x48(%rsp)
movl %esi, 0x2c(%rsp)
leaq 0xf8(%rdi), %rax
movq %rax, 0x90(%rsp)
leaq 0x8(%rdi), %rax
movq %rax, 0x38(%rsp)
leaq 0x98(%rdi), %rax
movq %rax, 0x30(%rsp)
leaq 0x138(%rdi), %rax
movq %rax, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
leaq 0x38(%rdi), %rax
movq %rax, 0x40(%rsp)
pushq $0x1
popq %rax
leaq 0x98(%rsp), %r13
leaq 0x70(%rsp), %r15
leaq 0xb8(%rsp), %r14
movl %eax, %r12d
xorl %ebp, %ebp
cmpl 0x2c(%rsp), %r12d
jge 0x1b574
movslq %r12d, %rax
movq 0x48(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x16c66
andl $0x0, 0x10(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
andq $0x0, 0x78(%rsp)
xorl %ecx, %ecx
movb %cl, 0x80(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
andq $0x0, 0x58(%rsp)
movb %cl, 0x60(%rsp)
movq %r13, %rdi
callq 0x1b6f9
movl %eax, %ebx
testb %al, %al
je 0x1b116
testb $0x1, %bpl
je 0x1b136
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x4250
movb $0x1, %bpl
xorl %r14d, %r14d
pushq $0x4
jmp 0x1b355
movq 0x90(%rsp), %rdi
movq %r13, %rsi
callq 0x1c7e8
xorb $0x1, %bl
incl %r12d
movb $0x1, %r14b
movl %ebx, %ebp
jmp 0x1b35a
movq %r13, %rdi
callq 0x1b757
testb %al, %al
je 0x1b1bf
movq %r14, %rdi
movq %r13, %rsi
pushq $0x2
popq %rdx
pushq $-0x1
popq %rcx
callq 0x43e0
movq %r13, %rdi
movq %r14, %rsi
callq 0x4460
movq %r14, %rdi
callq 0x41c0
movq %r13, %rdi
movq %r15, %rsi
leaq 0x50(%rsp), %rdx
callq 0x1b77d
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x1af4e
movq %r15, %rsi
testb %al, %al
je 0x1b344
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x1d186
movl 0x40(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x20(%rsp), %rcx
cmpl 0x130(%rcx), %eax
jne 0x1b40a
xorl %r14d, %r14d
xorl %ebp, %ebp
pushq $0x32
jmp 0x1b355
movq %r14, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
pushq $-0x1
popq %rcx
callq 0x43e0
movq %r13, %rdi
movq %r14, %rsi
callq 0x4460
movq %r14, %rdi
callq 0x41c0
movq %r13, %rdi
movq %r15, %rsi
leaq 0x50(%rsp), %rdx
callq 0x1b77d
testb %al, %al
je 0x1b21c
cmpq $0x2, 0x78(%rsp)
jb 0x1b2c6
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x4250
xorl %r14d, %r14d
xorl %ebp, %ebp
pushq $0x8
jmp 0x1b355
andq $0x0, 0x78(%rsp)
movq 0x70(%rsp), %rax
xorl %ecx, %ecx
movb %cl, (%rax)
andq $0x0, 0x58(%rsp)
movq 0x50(%rsp), %rax
movb %cl, (%rax)
movq 0xa0(%rsp), %rax
cmpq $0x1, %rax
movq 0x20(%rsp), %rbp
jne 0x1b38a
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1af4e
movq %r13, %rsi
testb %al, %al
je 0x1b344
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x1d186
movl 0x40(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x20(%rsp), %rcx
cmpl 0x130(%rcx), %eax
je 0x1b1b3
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1d1fc
testb $0x1, 0x24(%rax)
jne 0x1b498
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1c814
movq %rax, %rdi
leaq 0xf3db(%rip), %rsi      # 0x2a697
callq 0x4770
jmp 0x1b126
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x1af4e
movq %r15, %rsi
testb %al, %al
je 0x1b344
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x1d186
movl 0x40(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x20(%rsp), %rcx
cmpl 0x130(%rcx), %eax
je 0x1b1b3
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1d1fc
testb $0x1, 0x24(%rax)
je 0x1b323
cmpq $0x0, 0x58(%rsp)
movq %r15, %rsi
je 0x1b4e4
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1c814
movq %rax, %rdi
leaq 0x50(%rsp), %rsi
callq 0x4250
jmp 0x1b126
movq 0x18(%rsp), %rdi
callq 0x4250
xorl %r14d, %r14d
xorl %ebp, %ebp
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x41c0
movq %r15, %rdi
callq 0x41c0
movq %r13, %rdi
callq 0x41c0
testb %r14b, %r14b
leaq 0xb8(%rsp), %r14
jne 0x1b091
jmp 0x1b661
xorl %r14d, %r14d
movl %r14d, %r14d
cmpq %r14, %rax
jbe 0x1b126
movq 0x98(%rsp), %rax
movsbl (%rax,%r14), %esi
movq %r15, %rdi
callq 0x4670
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1af4e
testb %al, %al
je 0x1b486
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x1d186
movl 0x40(%rax), %eax
movl %eax, 0x10(%rsp)
cmpl 0x130(%rbp), %eax
je 0x1b1b3
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1c814
movq %rax, %rdi
leaq 0x50(%rsp), %rsi
callq 0x4250
incl %r14d
movq 0xa0(%rsp), %rax
jmp 0x1b38d
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1d1fc
testb $0x1, 0x24(%rax)
je 0x1b524
cmpq $0x0, 0x58(%rsp)
jne 0x1b524
leal 0x1(%r12), %ebp
movq %r15, %rsi
cmpl 0x2c(%rsp), %ebp
jge 0x1b4e4
movslq %ebp, %rax
movq 0x48(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %r14, %rdi
leaq 0x17(%rsp), %rdx
callq 0x16c66
movq %r14, %rdi
callq 0x1b6f9
movl %eax, %r12d
testb %al, %al
je 0x1b4fa
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x4250
pushq $0x2
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x1b507
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x4250
jmp 0x1b34e
leal 0x1(%r12), %ebp
movq %r13, %rsi
cmpl 0x2c(%rsp), %ebp
jge 0x1b4e4
movslq %ebp, %rax
movq 0x48(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %r14, %rdi
leaq 0x17(%rsp), %rdx
callq 0x16c66
movq %r14, %rdi
callq 0x1b6f9
movl %eax, %r12d
testb %al, %al
je 0x1b545
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x4250
pushq $0x2
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x1b55f
movq 0x18(%rsp), %rdi
callq 0x4250
xorl %r14d, %r14d
xorl %ebp, %ebp
pushq $0x2
jmp 0x1b355
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0x4250
movq %r14, %rdi
callq 0x41c0
testb %r12b, %r12b
je 0x1b521
xorl %r14d, %r14d
movl %ebp, %r12d
xorl %ebp, %ebp
jmp 0x1b35a
movl %ebp, %r12d
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1c814
movq %rax, %rdi
leaq 0x50(%rsp), %rsi
callq 0x4250
jmp 0x1b126
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1c814
movq %rax, %rdi
movq %r14, %rsi
callq 0x4250
movq %r14, %rdi
callq 0x41c0
testb %r12b, %r12b
jne 0x1b514
movl %ebp, %r12d
jmp 0x1b126
movq 0x20(%rsp), %r13
movq 0x50(%r13), %rbx
leaq 0x40(%r13), %r14
leaq 0xa0(%r13), %r12
cmpq %r14, %rbx
je 0x1b5b6
testb $0x2, 0x24(%rbx)
je 0x1b5a9
leaq 0x20(%rbx), %r15
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x1d4c8
cmpq %r12, %rax
je 0x1b5c3
movq %rbx, %rdi
callq 0x42d0
movq %rax, %rbx
jmp 0x1b588
movl $0x0, 0xc(%rsp)
jmp 0x1b661
movl (%r15), %edx
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1b838
pushq $0x1
popq %rbp
leaq 0xf640(%rip), %r14      # 0x2ac20
leaq 0x98(%rsp), %r15
leaq 0xf0b9(%rip), %r12      # 0x2a6a8
movq %rbx, %r13
movq (%r13), %r13
cmpq %rbx, %r13
je 0x1b650
cmpq $0x2, 0x18(%r13)
jb 0x1b60f
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x4760
leaq 0x10(%r13), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1c862
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x4790
movq %r15, %rdi
callq 0x41c0
movl %ebp, %eax
cmpq %rax, 0x80(%rsp)
jbe 0x1b64c
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x4760
incl %ebp
jmp 0x1b5f2
leaq 0x70(%rsp), %rdi
callq 0x1cd4e
pushq $0x16
popq %rax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1b6cd
jmp 0x1b683
jmp 0x1b6cd
jmp 0x1b6cd
jmp 0x1b6cd
jmp 0x1b6cd
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x41c0
jmp 0x1b6d0
jmp 0x1b6cd
jmp 0x1b6cd
jmp 0x1b6cd
jmp 0x1b6cd
jmp 0x1b6cd
jmp 0x1b6cd
jmp 0x1b6cd
jmp 0x1b6b7
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x41c0
jmp 0x1b6ba
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x1cd4e
jmp 0x1b6f1
movq %rax, %rbx
jmp 0x1b6f1
jmp 0x1b6cd
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x41c0
leaq 0x70(%rsp), %rdi
callq 0x41c0
leaq 0x98(%rsp), %rdi
callq 0x41c0
movq %rbx, %rdi
callq 0x4700