// File: /workspace/github_cpp_makefile_star2_O3/jovermann[P]streplace/src/MiscUtils.cpp
// Function: ut1::expandUnprintable(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, char, char)
std::string expandUnprintable(const std::string& s, char quotes, char addQuotes)
{
    std::string r;
    char        buf[4];

    if (addQuotes)
    {
        r += addQuotes;
    }

    for (const char& c: s)
    {
        if (isprint(c))
        {
            if ((c == '\\') || (quotes && (c == quotes)))
            {
                // Backslashify backslash and quotes.
                r += '\\';
            }
            r += c;
        }
        else
        {
            // Unprintable char.
            r += '\\'; // Leading backslash.
            switch (c)
            {
                // Named control chars.
            case '\a': r += 'a'; break;
            case '\b': r += 'b'; break;
            case '\f': r += 'f'; break;
            case '\n': r += 'n'; break;
            case '\r': r += 'r'; break;
            case '\t': r += 't'; break;
            case '\v': r += 'v'; break;
            default:
                // Hex/octal byte.
                char next = *(&c + 1);
                if (std::isxdigit(unsigned(next)))
                {
                    // Next digit is a valid hex digit:
                    // Use 3-digit octal variant to limit the length of the numeric escape sequence.
                    std::snprintf(buf, sizeof(buf), "%03o", uint8_t(c));
                }
                else
                {
                    // Hex byte.
                    std::snprintf(buf, sizeof(buf), "x%02x", uint8_t(c));
                }
                r += buf;
                break;
            }
        }
    }

    if (addQuotes)
    {
        r += addQuotes;
    }

    return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, 0x14(%rsp)
movq %rsi, %r12
movq %rdi, %r14
leaq 0x10(%rdi), %rax
movq %rax, 0x18(%rsp)
movq %rax, (%rdi)
movq $0x0, 0x8(%rdi)
movb $0x0, 0x10(%rdi)
movsbl %cl, %ebp
movl %ecx, 0x10(%rsp)
testb %cl, %cl
je 0xa4e4
movq %r14, %rdi
movl %ebp, %esi
callq 0x5390
movl %ebp, 0xc(%rsp)
movq 0x8(%r12), %r15
testq %r15, %r15
je 0xa5cd
movq (%r12), %rbp
xorl %r12d, %r12d
movb (%rbp,%r12), %r13b
movzbl %r13b, %edi
callq 0x5730
testl %eax, %eax
je 0xa53c
movl 0x14(%rsp), %ecx
testb %cl, %cl
setne %al
cmpb %cl, %r13b
sete %cl
cmpb $0x5c, %r13b
je 0xa528
andb %cl, %al
je 0xa568
movq %r14, %rdi
movl $0x5c, %esi
callq 0x5390
movb (%rbp,%r12), %r13b
jmp 0xa568
movq %r14, %rdi
movl $0x5c, %esi
callq 0x5390
movzbl (%rbp,%r12), %r13d
leal -0x7(%r13), %ecx
cmpb $0x6, %cl
ja 0xa57e
shlb $0x3, %cl
movabsq $0x7266766e746261, %r13 # imm = 0x7266766E746261
shrq %cl, %r13
movsbl %r13b, %esi
movq %r14, %rdi
callq 0x5390
incq %r12
cmpq %r12, %r15
jne 0xa4fd
jmp 0xa5cd
movsbl 0x1(%rbp,%r12), %edi
callq 0x5090
testl %eax, %eax
leaq 0x16e86(%rip), %rdx     # 0x21418
leaq 0x16e84(%rip), %rax     # 0x2141d
cmoveq %rax, %rdx
movl $0x4, %esi
movq %r14, %rbx
movq %r15, %r14
leaq 0x24(%rsp), %r15
movq %r15, %rdi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x5670
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %r15
movq %rbx, %r14
callq 0x57c0
jmp 0xa574
cmpb $0x0, 0x10(%rsp)
movl 0xc(%rsp), %esi
je 0xa5e0
movq %r14, %rdi
callq 0x5390
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa5f6
jmp 0xa5f9
movq %r14, %rbx
movq %rax, %r14
movq (%rbx), %rdi
cmpq 0x18(%rsp), %rdi
je 0xa616
movq 0x18(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x5410
movq %r14, %rdi
callq 0x5720