char *charesc(int ch) {
  static char buf[128];
  if (ch > 127)
    return "";
  if (ch == '\n')
    return "\02";
  if (ch == '\b' || ch >= ' ') {
    buf[1] = '\0';
    buf[0] = ch;
    return buf;
  }
  return "";
}


x86 assembly (clang, -O0):
<charesc>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
cmpl   $0x7f,-0xc(%rbp)
jle    1121 <charesc+0x21>
lea    0xee9(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    117a <charesc+0x7a>
cmpl   $0xa,-0xc(%rbp)
jne    113b <charesc+0x3b>
lea    0xece(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    117a <charesc+0x7a>
cmpl   $0x8,-0xc(%rbp)
je     114f <charesc+0x4f>
cmpl   $0x20,-0xc(%rbp)
jl     116f <charesc+0x6f>
movb   $0x0,0x2edb(%rip)
mov    -0xc(%rbp),%eax
mov    %al,0x2ed1(%rip)
lea    0x2eca(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    117a <charesc+0x7a>
lea    0xe8b(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<charesc>:
lea    0xefa(%rip),%rax
cmp    $0x7f,%edi
jg     1123 <charesc+0x23>
cmp    $0xa,%edi
jne    1119 <charesc+0x19>
lea    0xee8(%rip),%rax
ret
cmp    $0x8,%edi
je     1124 <charesc+0x24>
cmp    $0x20,%edi
jge    1124 <charesc+0x24>
ret
movb   $0x0,0x2f06(%rip)
lea    0x2efe(%rip),%rax
mov    %dil,0x2ef7(%rip)
ret

LLVM IR (-O0):
@charesc.buf = internal global [128 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"\02\00"
define i8* @charesc(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp sgt i32 %4, 127
br i1 %5, label %6, label %7
6: ; preds = %1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %2
br label %21
7: ; preds = %1
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 10
br i1 %9, label %10, label %11
10: ; preds = %7
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %21
11: ; preds = %7
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 8
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* %3
%16 = icmp sge i32 %15, 32
br i1 %16, label %17, label %20
17: ; preds = %14, %11
store i8 0, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @charesc.buf, i64 0, i64 1)
%18 = load i32, i32* %3
%19 = trunc i32 %18 to i8
store i8 %19, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @charesc.buf, i64 0, i64 0)
store i8* getelementptr inbounds ([128 x i8], [128 x i8]* @charesc.buf, i64 0, i64 0), i8** %2
br label %21
20: ; preds = %14
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %2
br label %21
21: ; preds = %20, %17, %10, %6
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
@charesc.buf = internal global [128 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"\02\00"
define i8* @charesc(i32 %0) {
%2 = icmp sgt i32 %0, 127
br i1 %2, label %11, label %3
3: ; preds = %1
%4 = icmp eq i32 %0, 10
br i1 %4, label %11, label %5
5: ; preds = %3
%6 = icmp eq i32 %0, 8
%7 = icmp sgt i32 %0, 31
%8 = or i1 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %5
store i8 0, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @charesc.buf, i64 0, i64 1)
%10 = trunc i32 %0 to i8
store i8 %10, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @charesc.buf, i64 0, i64 0)
br label %11
11: ; preds = %5, %3, %1, %9
%12 = phi i8* [ getelementptr inbounds ([128 x i8], [128 x i8]* @charesc.buf, i64 0, i64 0), %9 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %3 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %5 ]
ret i8* %12
}