int escape_str(char *restrict dst, const char *restrict src, int bufsize,
               int *maxcells) {
  unsigned char c;
  int my_cells = 0;
  int my_bytes = 0;
  const char codes[] = "Z-------------------------------"
                       "********************************"
                       "********************************"
                       "*******************************-"
                       "--------------------------------"
                       "********************************"
                       "********************************"
                       "********************************";
  if (bufsize > *maxcells + 1)
    bufsize = *maxcells + 1;
  for (;;) {
    if (my_cells >= *maxcells || my_bytes + 1 >= bufsize)
      break;
    c = (unsigned char)*(src++);
    if (!c)
      break;
    if (codes[c] == '-')
      c = '?';
    my_cells++;
    my_bytes++;
    *(dst++) = c;
  }
  *(dst++) = '\0';
  *maxcells -= my_cells;
  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    %rcx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x130(%rbp),%rdi
lea    0xeba(%rip),%rsi
mov    $0x101,%edx
call   1030 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jle    1170 <escape_str+0x60>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1175 <escape_str+0x65>
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jge    1193 <escape_str+0x83>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cmp    -0x14(%rbp),%eax
jl     1198 <escape_str+0x88>
jmp    11ff <escape_str+0xef>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
cmpb   $0x0,-0x21(%rbp)
jne    11bb <escape_str+0xab>
jmp    11ff <escape_str+0xef>
movzbl -0x21(%rbp),%eax
movsbl -0x130(%rbp,%rax,1),%eax
ff
cmp    $0x2d,%eax
jne    11d4 <escape_str+0xc4>
movb   $0x3f,-0x21(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x21(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    1175 <escape_str+0x65>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
sub    %edx,%ecx
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_str>:
push   %rbp
push   %rbx
mov    (%rcx),%r8d
test   %r8d,%r8d
jle    116e <escape_str+0x6e>
lea    0x1(%r8),%eax
cmp    %edx,%eax
cmovl  %eax,%edx
cmp    $0x2,%edx
mov    $0x1,%r9d
cmovge %edx,%r9d
lea    -0x1(%r9),%eax
add    $0xffffffffffffffff,%r9
xor    %edx,%edx
lea    0xecf(%rip),%r10
mov    $0x3f,%ebp
mov    %rdi,%r11
nopl   0x0(%rax)
cmp    %edx,%r9d
je     1172 <escape_str+0x72>
movzbl (%rsi,%rdx,1),%ebx
test   %rbx,%rbx
je     1177 <escape_str+0x77>
cmpb   $0x2d,(%rbx,%r10,1)
cmove  %ebp,%ebx
add    $0x1,%r11
mov    %bl,(%rdi,%rdx,1)
add    $0x1,%rdx
cmp    %edx,%r8d
jne    1140 <escape_str+0x40>
mov    %r11,%rdi
mov    %r8d,%eax
jmp    117c <escape_str+0x7c>
xor    %eax,%eax
jmp    117c <escape_str+0x7c>
mov    %r11,%rdi
jmp    117c <escape_str+0x7c>
add    %rdx,%rdi
mov    %edx,%eax
movb   $0x0,(%rdi)
sub    %eax,%r8d
mov    %r8d,(%rcx)
pop    %rbx
pop    %rbp
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 = load i32, i32* %15
%17 = add nsw i32 %16, 1
%18 = icmp sgt i32 %14, %17
br i1 %18, label %19, label %23
19: ; preds = %4
%20 = load i32*, i32** %8
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
store i32 %22, i32* %7
br label %23
23: ; preds = %19, %4
br label %24
24: ; preds = %50, %23
%25 = load i32, i32* %10
%26 = load i32*, i32** %8
%27 = load i32, i32* %26
%28 = icmp sge i32 %25, %27
br i1 %28, label %34, label %29
29: ; preds = %24
%30 = load i32, i32* %11
%31 = add nsw i32 %30, 1
%32 = load i32, i32* %7
%33 = icmp sge i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %29, %24
br label %58
35: ; preds = %29
%36 = load i8*, i8** %6
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %6
%38 = load i8, i8* %36
store i8 %38, i8* %9
%39 = load i8, i8* %9
%40 = icmp ne i8 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %35
br label %58
42: ; preds = %35
%43 = load i8, i8* %9
%44 = zext i8 %43 to i64
%45 = getelementptr inbounds [257 x i8], [257 x i8]* %12, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 45
br i1 %48, label %49, label %50
49: ; preds = %42
store i8 63, i8* %9
br label %50
50: ; preds = %49, %42
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
store i32 %52, i32* %10
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
%55 = load i8, i8* %9
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %5
store i8 %55, i8* %56
br label %24
58: ; preds = %41, %34
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %5
store i8 0, i8* %59
%61 = load i32, i32* %10
%62 = load i32*, i32** %8
%63 = load i32, i32* %62
%64 = sub nsw i32 %63, %61
store i32 %64, i32* %62
%65 = load i32, i32* %11
ret i32 %65
}

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