const char *toshort(char buf[3], unsigned char chr) {
  static const char map[][3] = {
      [0x00] = "nul", [0x01] = "soh", [0x02] = "stx", [0x03] = "etx",
      [0x04] = "eot", [0x05] = "enq", [0x06] = "ack", [0x07] = "bel",
      [0x08] = "bs",  [0x09] = "ht",  [0x0a] = "lf",  [0x0b] = "vt",
      [0x0c] = "ff",  [0x0d] = "cr",  [0x0e] = "so",  [0x0f] = "si",
      [0x10] = "dle", [0x11] = "dc1", [0x12] = "dc2", [0x13] = "dc3",
      [0x14] = "dc4", [0x15] = "nak", [0x16] = "syn", [0x17] = "etb",
      [0x18] = "can", [0x19] = "em",  [0x1a] = "sub", [0x1b] = "esc",
      [0x1c] = "fs",  [0x1d] = "gs",  [0x1e] = "rs",  [0x1f] = "us",
      [0x7f] = "del",
  };
  if (chr <= 0x1f || chr == 0x7f) {
    memcpy(buf, map[chr], 3);
  } else if (chr < 0x7f) {
    buf[0] = chr;
    buf[1] = '\0';
  } else {
    buf[0] = "0123456789abcdef"[0x0f & (chr >> 4)];
    buf[1] = "0123456789abcdef"[0x0f & chr];
    buf[2] = '\0';
  }
  return buf;
}


x86 assembly (clang, -O0):
<toshort>:
push   %rbp
mov    %rsp,%rbp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movzbl -0x9(%rbp),%eax
cmp    $0x1f,%eax
jle    1128 <toshort+0x28>
movzbl -0x9(%rbp),%eax
cmp    $0x7f,%eax
jne    1151 <toshort+0x51>
mov    -0x8(%rbp),%rax
movzbl -0x9(%rbp),%ecx
mov    %ecx,%edx
lea    0xec7(%rip),%rcx
imul   $0x3,%rdx,%rdx
add    %rdx,%rcx
mov    (%rcx),%dx
mov    %dx,(%rax)
mov    0x2(%rcx),%cl
mov    %cl,0x2(%rax)
jmp    11b9 <toshort+0xb9>
movzbl -0x9(%rbp),%eax
cmp    $0x7f,%eax
jge    1174 <toshort+0x74>
mov    -0x9(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    11b4 <toshort+0xb4>
movzbl -0x9(%rbp),%eax
sar    $0x4,%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xff8(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
movzbl -0x9(%rbp),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xfde(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    11b9 <toshort+0xb9>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<toshort>:
mov    %rdi,%rax
cmp    $0x20,%sil
jb     1119 <toshort+0x19>
cmp    $0x7f,%sil
je     1119 <toshort+0x19>
jae    1137 <toshort+0x37>
mov    %sil,(%rax)
movb   $0x0,0x1(%rax)
ret
movzbl %sil,%ecx
lea    (%rcx,%rcx,2),%rcx
lea    0xed8(%rip),%rsi
mov    0x2(%rsi,%rcx,1),%dl
mov    %dl,0x2(%rax)
movzwl (%rsi,%rcx,1),%ecx
mov    %cx,(%rax)
ret
mov    %esi,%ecx
mov    %rcx,%rdx
shr    $0x4,%rdx
lea    0x1039(%rip),%rsi
mov    (%rdx,%rsi,1),%dl
mov    %dl,(%rax)
and    $0xf,%ecx
mov    (%rcx,%rsi,1),%cl
mov    %cl,0x1(%rax)
movb   $0x0,0x2(%rax)
ret

LLVM IR (-O0):
@toshort.map = internal constant [128 x [3 x i8]] [[3 x i8] c"nul", [3 x i8] c"soh", [3 x i8] c"stx", [3 x i8] c"etx", [3 x i8] c"eot", [3 x i8] c"enq", [3 x i8] c"ack", [3 x i8] c"bel", [3 x i8] c"bs\00", [3 x i8] c"ht\00", [3 x i8] c"lf\00", [3 x i8] c"vt\00", [3 x i8] c"ff\00", [3 x i8] c"cr\00", [3 x i8] c"so\00", [3 x i8] c"si\00", [3 x i8] c"dle", [3 x i8] c"dc1", [3 x i8] c"dc2", [3 x i8] c"dc3", [3 x i8] c"dc4", [3 x i8] c"nak", [3 x i8] c"syn", [3 x i8] c"etb", [3 x i8] c"can", [3 x i8] c"em\00", [3 x i8] c"sub", [3 x i8] c"esc", [3 x i8] c"fs\00", [3 x i8] c"gs\00", [3 x i8] c"rs\00", [3 x i8] c"us\00", [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] c"del"]
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @toshort(i8* %0, i8 zeroext %1) {
%3 = alloca i8*
%4 = alloca i8
store i8* %0, i8** %3
store i8 %1, i8* %4
%5 = load i8, i8* %4
%6 = zext i8 %5 to i32
%7 = icmp sle i32 %6, 31
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i8, i8* %4
%10 = zext i8 %9 to i32
%11 = icmp eq i32 %10, 127
br i1 %11, label %12, label %18
12: ; preds = %8, %2
%13 = load i8*, i8** %3
%14 = load i8, i8* %4
%15 = zext i8 %14 to i64
%16 = getelementptr inbounds [128 x [3 x i8]], [128 x [3 x i8]]* @toshort.map, i64 0, i64 %15
%17 = getelementptr inbounds [3 x i8], [3 x i8]* %16, i64 0, i64 0
br label %49
18: ; preds = %8
%19 = load i8, i8* %4
%20 = zext i8 %19 to i32
%21 = icmp slt i32 %20, 127
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i8, i8* %4
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 0
store i8 %23, i8* %25
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i64 1
store i8 0, i8* %27
br label %48
28: ; preds = %18
%29 = load i8, i8* %4
%30 = zext i8 %29 to i32
%31 = ashr i32 %30, 4
%32 = and i32 15, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = load i8*, i8** %3
%37 = getelementptr inbounds i8, i8* %36, i64 0
store i8 %35, i8* %37
%38 = load i8, i8* %4
%39 = zext i8 %38 to i32
%40 = and i32 15, %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i64 1
store i8 %43, i8* %45
%46 = load i8*, i8** %3
%47 = getelementptr inbounds i8, i8* %46, i64 2
store i8 0, i8* %47
br label %48
48: ; preds = %28, %22
br label %49
49: ; preds = %48, %12
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@toshort.map = internal unnamed_addr constant [128 x [3 x i8]] [[3 x i8] c"nul", [3 x i8] c"soh", [3 x i8] c"stx", [3 x i8] c"etx", [3 x i8] c"eot", [3 x i8] c"enq", [3 x i8] c"ack", [3 x i8] c"bel", [3 x i8] c"bs\00", [3 x i8] c"ht\00", [3 x i8] c"lf\00", [3 x i8] c"vt\00", [3 x i8] c"ff\00", [3 x i8] c"cr\00", [3 x i8] c"so\00", [3 x i8] c"si\00", [3 x i8] c"dle", [3 x i8] c"dc1", [3 x i8] c"dc2", [3 x i8] c"dc3", [3 x i8] c"dc4", [3 x i8] c"nak", [3 x i8] c"syn", [3 x i8] c"etb", [3 x i8] c"can", [3 x i8] c"em\00", [3 x i8] c"sub", [3 x i8] c"esc", [3 x i8] c"fs\00", [3 x i8] c"gs\00", [3 x i8] c"rs\00", [3 x i8] c"us\00", [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] zeroinitializer, [3 x i8] c"del"]
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @toshort(i8* returned writeonly %0, i8 zeroext %1) {
%3 = zext i8 %1 to i32
%4 = icmp ult i8 %1, 32
%5 = icmp eq i8 %1, 127
%6 = or i1 %4, %5
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = zext i8 %1 to i64
%9 = getelementptr inbounds [128 x [3 x i8]], [128 x [3 x i8]]* @toshort.map, i64 0, i64 %8, i64 0
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %0, i8* align 1 dereferenceable(3) %9, i64 3, i1 false)
br label %25
10: ; preds = %2
%11 = icmp ult i8 %1, 127
br i1 %11, label %12, label %14
12: ; preds = %10
store i8 %1, i8* %0
%13 = getelementptr inbounds i8, i8* %0, i64 1
store i8 0, i8* %13
br label %25
14: ; preds = %10
%15 = lshr i32 %3, 4
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %16
%18 = load i8, i8* %17
store i8 %18, i8* %0
%19 = and i32 %3, 15
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %22, i8* %23
%24 = getelementptr inbounds i8, i8* %0, i64 2
store i8 0, i8* %24
br label %25
25: ; preds = %12, %14, %7
ret i8* %0
}