// File: adamiaonr[P]rid-analytics/src/argvparser.cpp
// Function: CommandLineProcessing::formatString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int, unsigned int)
std::string CommandLineProcessing::formatString(const std::string& _string,
        unsigned int _width,
        unsigned int _indent)
{
    // if insane parameters do nothing
    if (_indent >= _width)
        return(_string);

    // list of lines of the formated string
    list<string> lines;

    // current position in the string
    unsigned int pos = 0;

    // till the end of the string
    while (pos < _string.length())
    {
        // get the next line of the string
        string line = _string.substr(pos, _width - _indent );

#ifdef ARGVPARSER_DEBUG

        cout << "EXTRACT: '" << line << "'" << endl;
#endif

        // check for newlines in the line and break line at first occurence (if any)
        string::size_type first_newline = line.find_first_of("\n");
        if (first_newline != string::npos)
        {
            line = line.substr(0, first_newline);
        }

        // we need to check for possible breaks within words only if the extracted
        // line spans the whole allowed width
        bool check_truncation = true;
        if (line.length() < _width - _indent)
            check_truncation = false;

        // remove unecessary whitespace at front and back
        line = trimmedString(line);

#ifdef ARGVPARSER_DEBUG

        cout << "TRIMMED: '" << line << "'" << endl;
#endif

        // only perform truncation if there was enough data for a full line
        if (!check_truncation)
            pos += line.length() + 1;
        else
        {
            // look for the last whitespace character
            string::size_type last_white_space = line.find_last_of(" \a\b\f\n\r\t\v");

            if (last_white_space != string::npos) // whitespace found!
            {
                // truncated the line at the last whitespace
                line = string(line, 0, last_white_space);
                pos += last_white_space + 1;
            }
            else // no whitespace found
                // rude break! we can leave the line in its current state
                pos += _width - _indent;
        }

        if (!line.empty())
        {
#ifdef ARGVPARSER_DEBUG
            cout << "UNINDEN: '" << line << "'" << endl;
#endif

            if (_indent)
                line.insert(0, _indent, ' ');

#ifdef ARGVPARSER_DEBUG

            cout << "INDENT: '" << line << "'" << endl;
#endif

            lines.push_back(line);
        }
    }

    // concat the formated string
    string formated;
    bool first = true;
    // for all lines
    for (list<string>::iterator it = lines.begin(); it != lines.end(); ++it)
    {
        // prefix with newline if not first
        if (!first)
            formated += "\n";
        else
            first = false;

        formated += *it;
    }
    return(formated);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x108(%rbp)
movq %rdi, %rax
movq %rax, -0x100(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jb 0x41e07
movq -0x108(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x51f0
jmp 0x4211e
leaq -0x30(%rbp), %rdi
callq 0x43c30
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq %rax, -0x110(%rbp)
movq -0x10(%rbp), %rdi
callq 0x57e0
movq %rax, %rcx
movq -0x110(%rbp), %rax
cmpq %rcx, %rax
jae 0x42028
movq -0x10(%rbp), %rsi
movl -0x34(%rbp), %eax
movl %eax, %edx
movl -0x14(%rbp), %eax
movl -0x18(%rbp), %ecx
subl %ecx, %eax
movl %eax, %ecx
leaq -0x58(%rbp), %rdi
callq 0x54d0
jmp 0x41e5b
leaq -0x58(%rbp), %rdi
leaq 0x2165d(%rip), %rsi     # 0x634c3
xorl %eax, %eax
movl %eax, %edx
callq 0x5260
movq %rax, -0x70(%rbp)
cmpq $-0x1, -0x70(%rbp)
je 0x41edd
movq -0x70(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x90(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x54d0
jmp 0x41e94
leaq -0x58(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x5600
leaq -0x90(%rbp), %rdi
callq 0x52f0
jmp 0x41edd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x42113
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x52f0
jmp 0x42113
movb $0x1, -0x91(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x57e0
movl -0x14(%rbp), %ecx
subl -0x18(%rbp), %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x41f01
movb $0x0, -0x91(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x42960
jmp 0x41f13
leaq -0x58(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x5600
leaq -0xb8(%rbp), %rdi
callq 0x52f0
testb $0x1, -0x91(%rbp)
jne 0x41f56
leaq -0x58(%rbp), %rdi
callq 0x57e0
movq %rax, %rcx
addq $0x1, %rcx
movl -0x34(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x34(%rbp)
jmp 0x41fdb
leaq -0x58(%rbp), %rdi
leaq 0x21ed1(%rip), %rsi     # 0x63e32
movq $-0x1, %rdx
callq 0x5450
movq %rax, -0xc0(%rbp)
cmpq $-0x1, -0xc0(%rbp)
je 0x41fcd
movq -0xc0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0xe0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5710
jmp 0x41f9b
leaq -0x58(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x5600
leaq -0xe0(%rbp), %rdi
callq 0x52f0
movq -0xc0(%rbp), %rcx
addq $0x1, %rcx
movl -0x34(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x34(%rbp)
jmp 0x41fd9
movl -0x14(%rbp), %eax
subl -0x18(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x41fdb
leaq -0x58(%rbp), %rdi
callq 0x5880
testb $0x1, %al
jne 0x4201a
cmpl $0x0, -0x18(%rbp)
je 0x42009
movl -0x18(%rbp), %eax
movl %eax, %edx
xorl %eax, %eax
movl %eax, %esi
leaq -0x58(%rbp), %rdi
movl $0x20, %ecx
callq 0x5a50
jmp 0x42007
jmp 0x42009
leaq -0x30(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x43c80
jmp 0x42018
jmp 0x4201a
leaq -0x58(%rbp), %rdi
callq 0x52f0
jmp 0x41e17
movq -0x108(%rbp), %rdi
movb $0x0, -0xe1(%rbp)
callq 0x5730
movb $0x1, -0xe2(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x43420
movq %rax, -0xf0(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x434a0
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x440a0
testb $0x1, %al
jne 0x4207b
jmp 0x420ec
testb $0x1, -0xe2(%rbp)
jne 0x420b5
movq -0x108(%rbp), %rdi
leaq 0x21431(%rip), %rsi     # 0x634c3
callq 0x5460
jmp 0x42099
jmp 0x420bc
movq -0x108(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x52f0
jmp 0x42113
movb $0x0, -0xe2(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x440d0
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x5650
jmp 0x420d9
jmp 0x420db
leaq -0xf0(%rbp), %rdi
callq 0x44020
jmp 0x42052
movb $0x1, -0xe1(%rbp)
testb $0x1, -0xe1(%rbp)
jne 0x42108
movq -0x108(%rbp), %rdi
callq 0x52f0
leaq -0x30(%rbp), %rdi
callq 0x43680
jmp 0x4211e
leaq -0x30(%rbp), %rdi
callq 0x43680
jmp 0x4212e
movq -0x100(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x5920
nopw (%rax,%rax)