int escape_str(char *restrict dst, const char *restrict src, int bufsize,
               int maxglyphs) {
  unsigned char c;
  int my_glyphs = 0;
  int my_bytes = 0;
  const char codes[] = "Z-------------------------------"
                       "********************************"
                       "********************************"
                       "*******************************-"
                       "--------------------------------"
                       "********************************"
                       "********************************"
                       "********************************";
  if (bufsize > maxglyphs + 1)
    bufsize = maxglyphs + 1;
  for (;;) {
    if (my_glyphs >= maxglyphs)
      break;
    if (my_bytes + 1 >= bufsize)
      break;
    c = (unsigned char)*(src++);
    if (!c)
      break;
    if (codes[c] == '-')
      c = '?';
    my_glyphs++;
    my_bytes++;
    *(dst++) = c;
  }
  *(dst++) = '\0';
  return my_bytes;
}


x86 assembly (clang, -O0):
<escape_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x130(%rbp),%rdi
lea    0xebb(%rip),%rsi
mov    $0x101,%edx
call   1030 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jle    1169 <escape_str+0x59>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116e <escape_str+0x5e>
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     117f <escape_str+0x6f>
jmp    11fa <escape_str+0xea>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cmp    -0x14(%rbp),%eax
jl     1193 <escape_str+0x83>
jmp    11fa <escape_str+0xea>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x19(%rbp)
cmpb   $0x0,-0x19(%rbp)
jne    11b6 <escape_str+0xa6>
jmp    11fa <escape_str+0xea>
movzbl -0x19(%rbp),%eax
movsbl -0x130(%rbp,%rax,1),%eax
ff
cmp    $0x2d,%eax
jne    11cf <escape_str+0xbf>
movb   $0x3f,-0x19(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x19(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    116e <escape_str+0x5e>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
mov    -0x24(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_str>:
push   %rbx
test   %ecx,%ecx
jle    116f <escape_str+0x6f>
mov    %ecx,%r8d
add    $0x1,%ecx
cmp    %edx,%ecx
cmovge %edx,%ecx
cmp    $0x2,%ecx
mov    $0x1,%r10d
cmovge %ecx,%r10d
lea    -0x1(%r10),%eax
add    $0xffffffffffffffff,%r10
xor    %ecx,%ecx
lea    0xed2(%rip),%r9
mov    $0x3f,%r11d
mov    %rdi,%rdx
nopw   0x0(%rax,%rax,1)
cmp    %ecx,%r10d
je     1173 <escape_str+0x73>
movzbl (%rsi,%rcx,1),%ebx
test   %rbx,%rbx
je     1178 <escape_str+0x78>
cmpb   $0x2d,(%rbx,%r9,1)
cmove  %r11d,%ebx
add    $0x1,%rdx
mov    %bl,(%rdi,%rcx,1)
add    $0x1,%rcx
cmp    %ecx,%r8d
jne    1140 <escape_str+0x40>
mov    %rdx,%rdi
mov    %r8d,%eax
jmp    117d <escape_str+0x7d>
xor    %eax,%eax
jmp    117d <escape_str+0x7d>
mov    %rdx,%rdi
jmp    117d <escape_str+0x7d>
add    %rcx,%rdi
mov    %ecx,%eax
movb   $0x0,(%rdi)
pop    %rbx
ret

LLVM IR (-O0):
@__const.escape_str.codes = [257 x i8] c"Z-------------------------------***********************************************************************************************---------------------------------************************************************************************************************\00"
define i32 @escape_str(i8* noalias %0, i8* noalias %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca [257 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %10
store i32 0, i32* %11
%13 = bitcast [257 x i8]* %12 to i8*
%14 = load i32, i32* %7
%15 = load i32, i32* %8
%16 = add nsw i32 %15, 1
%17 = icmp sgt i32 %14, %16
br i1 %17, label %18, label %21
18: ; preds = %4
%19 = load i32, i32* %8
%20 = add nsw i32 %19, 1
store i32 %20, i32* %7
br label %21
21: ; preds = %18, %4
br label %22
22: ; preds = %48, %21
%23 = load i32, i32* %10
%24 = load i32, i32* %8
%25 = icmp sge i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %56
27: ; preds = %22
%28 = load i32, i32* %11
%29 = add nsw i32 %28, 1
%30 = load i32, i32* %7
%31 = icmp sge i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %27
br label %56
33: ; preds = %27
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
%36 = load i8, i8* %34
store i8 %36, i8* %9
%37 = load i8, i8* %9
%38 = icmp ne i8 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %33
br label %56
40: ; preds = %33
%41 = load i8, i8* %9
%42 = zext i8 %41 to i64
%43 = getelementptr inbounds [257 x i8], [257 x i8]* %12, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 45
br i1 %46, label %47, label %48
47: ; preds = %40
store i8 63, i8* %9
br label %48
48: ; preds = %47, %40
%49 = load i32, i32* %10
%50 = add nsw i32 %49, 1
store i32 %50, i32* %10
%51 = load i32, i32* %11
%52 = add nsw i32 %51, 1
store i32 %52, i32* %11
%53 = load i8, i8* %9
%54 = load i8*, i8** %5
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %5
store i8 %53, i8* %54
br label %22
56: ; preds = %39, %32, %26
%57 = load i8*, i8** %5
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %5
store i8 0, i8* %57
%59 = load i32, i32* %11
ret i32 %59
}

LLVM IR (-O2):
@__const.escape_str.codes = [257 x i8] c"Z-------------------------------***********************************************************************************************---------------------------------************************************************************************************************\00"
define i32 @escape_str(i8* noalias nocapture writeonly %0, i8* noalias nocapture %1, i32 %2, i32 %3) {
%5 = add nsw i32 %3, 1
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %30
7: ; preds = %4
%8 = icmp slt i32 %5, %2
%9 = select i1 %8, i32 %5, i32 %2
%10 = call i32 @llvm.smax.i32(i32 %9, i32 1)
%11 = add nsw i32 %10, -1
br label %12
12: ; preds = %7, %21
%13 = phi i32 [ %16, %21 ], [ 0, %7 ]
%14 = phi i8* [ %28, %21 ], [ %0, %7 ]
%15 = phi i8* [ %22, %21 ], [ %1, %7 ]
%16 = add nuw nsw i32 %13, 1
%17 = icmp eq i32 %13, %11
br i1 %17, label %30, label %18
18: ; preds = %12
%19 = load i8, i8* %15
%20 = icmp eq i8 %19, 0
br i1 %20, label %30, label %21
21: ; preds = %18
%22 = getelementptr inbounds i8, i8* %15, i64 1
%23 = zext i8 %19 to i64
%24 = getelementptr inbounds [257 x i8], [257 x i8]* @__const.escape_str.codes, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 45
%27 = select i1 %26, i8 63, i8 %19
%28 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %27, i8* %14
%29 = icmp eq i32 %16, %3
br i1 %29, label %30, label %12
30: ; preds = %21, %12, %18, %4
%31 = phi i8* [ %0, %4 ], [ %14, %18 ], [ %14, %12 ], [ %28, %21 ]
%32 = phi i32 [ 0, %4 ], [ %13, %18 ], [ %11, %12 ], [ %3, %21 ]
store i8 0, i8* %31
ret i32 %32
}