char *char2ascii(char *string) {
  static char buffer[4096];
  unsigned int bufIdx = 0;
  char *ptr = string;
  while (*ptr) {
    if (*ptr == '\\') {
      switch (*(ptr + 1)) {
      case 'r':
        buffer[bufIdx++] = '\r';
        break;
      case 't':
        buffer[bufIdx++] = '\t';
        break;
      default:
        buffer[bufIdx++] = '\n';
        break;
      }
      ++ptr;
    } else
      buffer[bufIdx++] = *ptr;
    ++ptr;
    if (bufIdx > (sizeof(buffer) - 20))
      break;
  }
  buffer[bufIdx] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<char2ascii>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     120b <char2ascii+0x10b>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    11c7 <char2ascii+0xc7>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x72,%eax
je     115e <char2ascii+0x5e>
jmp    114d <char2ascii+0x4d>
mov    -0x1c(%rbp),%eax
sub    $0x74,%eax
je     117d <char2ascii+0x7d>
jmp    119c <char2ascii+0x9c>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2ebc(%rip),%rax
movb   $0xd,(%rax,%rcx,1)
jmp    11b6 <char2ascii+0xb6>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2e9d(%rip),%rax
movb   $0x9,(%rax,%rcx,1)
jmp    11b6 <char2ascii+0xb6>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2e7e(%rip),%rax
movb   $0xa,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11e6 <char2ascii+0xe6>
mov    -0x18(%rbp),%rax
mov    (%rax),%dl
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2e4d(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
cmp    $0xfec,%rax
jbe    1206 <char2ascii+0x106>
jmp    120b <char2ascii+0x10b>
jmp    1117 <char2ascii+0x17>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
lea    0x2e19(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e0e(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<char2ascii>:
mov    $0x1,%esi
lea    0x2f24(%rip),%rax
jmp    112b <char2ascii+0x2b>
xchg   %ax,%ax
mov    $0x1,%edx
test   %cl,%cl
je     11a0 <char2ascii+0xa0>
mov    %cl,(%rsi,%rax,1)
add    %rdx,%rdi
add    $0x1,%rdi
add    $0x2,%rsi
movzbl (%rdi),%ecx
cmp    $0x5c,%cl
je     1140 <char2ascii+0x40>
test   %cl,%cl
jne    1160 <char2ascii+0x60>
jmp    119c <char2ascii+0x9c>
nopl   0x0(%rax)
movsbl 0x1(%rdi),%ecx
add    $0x1,%rdi
cmp    $0x72,%ecx
je     1156 <char2ascii+0x56>
cmp    $0x74,%ecx
jne    115a <char2ascii+0x5a>
mov    $0x9,%cl
jmp    1160 <char2ascii+0x60>
mov    $0xd,%cl
jmp    1160 <char2ascii+0x60>
mov    $0xa,%cl
nopl   0x0(%rax)
mov    %cl,-0x1(%rsi,%rax,1)
cmp    $0xfed,%rsi
je     1195 <char2ascii+0x95>
movzbl 0x1(%rdi),%ecx
cmp    $0x5c,%cl
jne    1110 <char2ascii+0x10>
movsbl 0x2(%rdi),%ecx
mov    $0x2,%edx
cmp    $0x72,%ecx
je     118d <char2ascii+0x8d>
cmp    $0x74,%ecx
jne    1191 <char2ascii+0x91>
mov    $0x9,%cl
jmp    111d <char2ascii+0x1d>
mov    $0xd,%cl
jmp    111d <char2ascii+0x1d>
mov    $0xa,%cl
jmp    111d <char2ascii+0x1d>
mov    $0xfed,%esi
jmp    11a0 <char2ascii+0xa0>
add    $0xffffffffffffffff,%rsi
mov    %esi,%ecx
movb   $0x0,(%rcx,%rax,1)
ret

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

LLVM IR (-O2):
@char2ascii.buffer = internal global [4096 x i8] zeroinitializer
define i8* @char2ascii(i8* nocapture %0) {
br label %2
2: ; preds = %27, %1
%3 = phi i64 [ 0, %1 ], [ %32, %27 ]
%4 = phi i8* [ %0, %1 ], [ %33, %27 ]
%5 = load i8, i8* %4
switch i8 %5, label %12 [
i8 0, label %34
i8 92, label %6
]
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %4, i64 1
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
switch i32 %9, label %11 [
i32 114, label %12
i32 116, label %10
]
10: ; preds = %6
br label %12
11: ; preds = %6
br label %12
12: ; preds = %2, %6, %10, %11
%13 = phi i8 [ 9, %10 ], [ 10, %11 ], [ 13, %6 ], [ %5, %2 ]
%14 = phi i8* [ %7, %10 ], [ %7, %11 ], [ %7, %6 ], [ %4, %2 ]
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* @char2ascii.buffer, i64 0, i64 %3
store i8 %13, i8* %15
%16 = or i64 %3, 1
%17 = icmp eq i64 %16, 4077
br i1 %17, label %34, label %18
18: ; preds = %12
%19 = getelementptr inbounds i8, i8* %14, i64 1
%20 = load i8, i8* %19
switch i8 %20, label %27 [
i8 0, label %34
i8 92, label %21
]
21: ; preds = %18
%22 = getelementptr inbounds i8, i8* %14, i64 2
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
switch i32 %24, label %26 [
i32 114, label %27
i32 116, label %25
]
25: ; preds = %21
br label %27
26: ; preds = %21
br label %27
27: ; preds = %26, %25, %21, %18
%28 = phi i8 [ 9, %25 ], [ 10, %26 ], [ 13, %21 ], [ %20, %18 ]
%29 = phi i64 [ 2, %25 ], [ 2, %26 ], [ 2, %21 ], [ 1, %18 ]
%30 = getelementptr inbounds i8, i8* %14, i64 1
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* @char2ascii.buffer, i64 0, i64 %16
store i8 %28, i8* %31
%32 = add nuw nsw i64 %3, 2
%33 = getelementptr inbounds i8, i8* %30, i64 %29
br label %2
34: ; preds = %18, %2, %12
%35 = phi i64 [ 4077, %12 ], [ %3, %2 ], [ %16, %18 ]
%36 = and i64 %35, 4294967295
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* @char2ascii.buffer, i64 0, i64 %36
store i8 0, i8* %37
ret i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @char2ascii.buffer, i64 0, i64 0)
}