int escape_copy_katcl(char *dst, char *src, unsigned int want) {
  int i, j;
  char v;
  j = 0;
  for (j = 0, i = 0; i < want; i++) {
    switch (src[i]) {
    case 27:
      v = 'e';
      break;
    case '\n':
      v = 'n';
      break;
    case '\r':
      v = 'r';
      break;
    case '\0':
      v = '0';
      break;
    case '\\':
      v = '\\';
      break;
    case ' ':
      v = '_';
      break;
    case '\t':
      v = 't';
      break;
    default:
      dst[j++] = src[i];
      continue;
    }
    dst[j++] = '\\';
    dst[j++] = v;
  }
  return j;
}


x86 assembly (clang, -O0):
<escape_copy_katcl>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1252 <escape_copy_katcl+0x152>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x24(%rbp)
test   %eax,%eax
je     11cd <escape_copy_katcl+0xcd>
jmp    114c <escape_copy_katcl+0x4c>
mov    -0x24(%rbp),%eax
sub    $0x9,%eax
je     11e8 <escape_copy_katcl+0xe8>
jmp    115d <escape_copy_katcl+0x5d>
mov    -0x24(%rbp),%eax
sub    $0xa,%eax
je     11bb <escape_copy_katcl+0xbb>
jmp    116e <escape_copy_katcl+0x6e>
mov    -0x24(%rbp),%eax
sub    $0xd,%eax
je     11c4 <escape_copy_katcl+0xc4>
jmp    117f <escape_copy_katcl+0x7f>
mov    -0x24(%rbp),%eax
sub    $0x1b,%eax
je     11b2 <escape_copy_katcl+0xb2>
jmp    1190 <escape_copy_katcl+0x90>
mov    -0x24(%rbp),%eax
sub    $0x20,%eax
je     11df <escape_copy_katcl+0xdf>
jmp    11a1 <escape_copy_katcl+0xa1>
mov    -0x24(%rbp),%eax
sub    $0x5c,%eax
je     11d6 <escape_copy_katcl+0xd6>
jmp    11f1 <escape_copy_katcl+0xf1>
movb   $0x65,-0x1d(%rbp)
jmp    1216 <escape_copy_katcl+0x116>
movb   $0x6e,-0x1d(%rbp)
jmp    1216 <escape_copy_katcl+0x116>
movb   $0x72,-0x1d(%rbp)
jmp    1216 <escape_copy_katcl+0x116>
movb   $0x30,-0x1d(%rbp)
jmp    1216 <escape_copy_katcl+0x116>
movb   $0x5c,-0x1d(%rbp)
jmp    1216 <escape_copy_katcl+0x116>
movb   $0x5f,-0x1d(%rbp)
jmp    1216 <escape_copy_katcl+0x116>
movb   $0x74,-0x1d(%rbp)
jmp    1216 <escape_copy_katcl+0x116>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1244 <escape_copy_katcl+0x144>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x5c,(%rax,%rcx,1)
mov    -0x1d(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1124 <escape_copy_katcl+0x24>
mov    -0x1c(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_copy_katcl>:
push   %rbp
push   %rbx
test   %edx,%edx
je     1184 <escape_copy_katcl+0x84>
mov    %edx,%r9d
xor    %edx,%edx
lea    0xeee(%rip),%r8
xor    %eax,%eax
jmp    1144 <escape_copy_katcl+0x44>
mov    $0x74,%r10b
nopl   0x0(%rax)
lea    0x1(%rax),%ebp
mov    %eax,%ecx
movb   $0x5c,(%rdi,%rcx,1)
mov    $0x2,%r11d
mov    %r10d,%ecx
add    %r11d,%eax
movslq %ebp,%rbp
mov    %cl,(%rdi,%rbp,1)
add    $0x1,%rdx
cmp    %rdx,%r9
je     1186 <escape_copy_katcl+0x86>
movzbl (%rsi,%rdx,1),%ecx
movsbl %cl,%ebx
mov    $0x1,%r11d
cmp    $0x20,%ebx
ja     1169 <escape_copy_katcl+0x69>
mov    $0x65,%r10b
movslq (%r8,%rbx,4),%rbx
add    %r8,%rbx
mov    %eax,%ebp
jmp    *%rbx
mov    $0x30,%r10b
jmp    1120 <escape_copy_katcl+0x20>
mov    %eax,%ebp
cmp    $0x5c,%ebx
jne    1132 <escape_copy_katcl+0x32>
mov    $0x5c,%r10b
jmp    1120 <escape_copy_katcl+0x20>
mov    $0x6e,%r10b
jmp    1120 <escape_copy_katcl+0x20>
mov    $0x72,%r10b
jmp    1120 <escape_copy_katcl+0x20>
mov    $0x5f,%r10b
jmp    1120 <escape_copy_katcl+0x20>
xor    %eax,%eax
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @escape_copy_katcl(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
store i32 0, i32* %8
store i32 0, i32* %7
br label %10
10: ; preds = %51, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %6
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %54
14: ; preds = %10
%15 = load i8*, i8** %5
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
switch i32 %20, label %28 [
i32 27, label %21
i32 10, label %22
i32 13, label %23
i32 0, label %24
i32 92, label %25
i32 32, label %26
i32 9, label %27
]
21: ; preds = %14
store i8 101, i8* %9
br label %39
22: ; preds = %14
store i8 110, i8* %9
br label %39
23: ; preds = %14
store i8 114, i8* %9
br label %39
24: ; preds = %14
store i8 48, i8* %9
br label %39
25: ; preds = %14
store i8 92, i8* %9
br label %39
26: ; preds = %14
store i8 95, i8* %9
br label %39
27: ; preds = %14
store i8 116, i8* %9
br label %39
28: ; preds = %14
%29 = load i8*, i8** %5
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = load i8*, i8** %4
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 %33, i8* %38
br label %51
39: ; preds = %27, %26, %25, %24, %23, %22, %21
%40 = load i8*, i8** %4
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8 92, i8* %44
%45 = load i8, i8* %9
%46 = load i8*, i8** %4
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 %45, i8* %50
br label %51
51: ; preds = %39, %28
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %10
54: ; preds = %10
%55 = load i32, i32* %8
ret i32 %55
}

LLVM IR (-O2):
define i32 @escape_copy_katcl(i8* nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %33, label %5
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %24
%8 = phi i64 [ 0, %5 ], [ %31, %24 ]
%9 = phi i32 [ 0, %5 ], [ %28, %24 ]
%10 = getelementptr inbounds i8, i8* %1, i64 %8
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
switch i32 %12, label %24 [
i32 27, label %19
i32 10, label %13
i32 13, label %14
i32 0, label %15
i32 92, label %16
i32 32, label %17
i32 9, label %18
]
13: ; preds = %7
br label %19
14: ; preds = %7
br label %19
15: ; preds = %7
br label %19
16: ; preds = %7
br label %19
17: ; preds = %7
br label %19
18: ; preds = %7
br label %19
19: ; preds = %7, %18, %17, %16, %15, %14, %13
%20 = phi i8 [ 116, %18 ], [ 95, %17 ], [ 92, %16 ], [ 48, %15 ], [ 114, %14 ], [ 110, %13 ], [ 101, %7 ]
%21 = add nuw nsw i32 %9, 1
%22 = zext i32 %9 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 92, i8* %23
br label %24
24: ; preds = %7, %19
%25 = phi i32 [ 2, %19 ], [ 1, %7 ]
%26 = phi i32 [ %21, %19 ], [ %9, %7 ]
%27 = phi i8 [ %20, %19 ], [ %11, %7 ]
%28 = add nuw nsw i32 %9, %25
%29 = sext i32 %26 to i64
%30 = getelementptr inbounds i8, i8* %0, i64 %29
store i8 %27, i8* %30
%31 = add nuw nsw i64 %8, 1
%32 = icmp eq i64 %31, %6
br i1 %32, label %33, label %7
33: ; preds = %24, %3
%34 = phi i32 [ 0, %3 ], [ %28, %24 ]
ret i32 %34
}