const char *tooctal(char buf[3], unsigned char chr) {
  if (chr > 0x1f && chr < 0x7f) {
    buf[0] = chr;
    buf[1] = '\0';
  } else {
    switch (chr) {
    case '\0':
      buf[0] = '\\';
      buf[1] = '0';
      buf[2] = '\0';
      break;
    case '\a':
      buf[0] = '\\';
      buf[1] = 'a';
      buf[2] = '\0';
      break;
    case '\b':
      buf[0] = '\\';
      buf[1] = 'b';
      buf[2] = '\0';
      break;
    case '\f':
      buf[0] = '\\';
      buf[1] = 'f';
      buf[2] = '\0';
      break;
    case '\n':
      buf[0] = '\\';
      buf[1] = 'n';
      buf[2] = '\0';
      break;
    case '\r':
      buf[0] = '\\';
      buf[1] = 'r';
      buf[2] = '\0';
      break;
    case '\t':
      buf[0] = '\\';
      buf[1] = 't';
      buf[2] = '\0';
      break;
    case '\v':
      buf[0] = '\\';
      buf[1] = 'v';
      buf[2] = '\0';
      break;
    default:
      buf[0] = "01234567"[0x7 & (chr >> 6)];
      buf[1] = "01234567"[0x7 & (chr >> 3)];
      buf[2] = "01234567"[0x7 & (chr >> 0)];
      break;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<tooctal>:
push   %rbp
mov    %rsp,%rbp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movzbl -0x9(%rbp),%eax
cmp    $0x1f,%eax
jle    113e <tooctal+0x3e>
movzbl -0x9(%rbp),%eax
cmp    $0x7f,%eax
jge    113e <tooctal+0x3e>
mov    -0x9(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    12a2 <tooctal+0x1a2>
movzbl -0x9(%rbp),%eax
mov    %rax,-0x18(%rbp)
sub    $0xd,%rax
ja     1244 <tooctal+0x144>
mov    -0x18(%rbp),%rax
lea    0xea5(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x8(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x30,0x1(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    129d <tooctal+0x19d>
mov    -0x8(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x61,0x1(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    129d <tooctal+0x19d>
mov    -0x8(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x62,0x1(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    129d <tooctal+0x19d>
mov    -0x8(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x66,0x1(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    129d <tooctal+0x19d>
mov    -0x8(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x6e,0x1(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    129d <tooctal+0x19d>
mov    -0x8(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x72,0x1(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    129d <tooctal+0x19d>
mov    -0x8(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x74,0x1(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    129d <tooctal+0x19d>
mov    -0x8(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x76,0x1(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    129d <tooctal+0x19d>
movzbl -0x9(%rbp),%eax
sar    $0x6,%eax
and    $0x7,%eax
movslq %eax,%rcx
lea    0xde0(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
movzbl -0x9(%rbp),%eax
sar    $0x3,%eax
and    $0x7,%eax
movslq %eax,%rcx
lea    0xdc3(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x1(%rax)
movzbl -0x9(%rbp),%eax
sar    $0x0,%eax
and    $0x7,%eax
movslq %eax,%rcx
lea    0xda5(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x2(%rax)
jmp    12a2 <tooctal+0x1a2>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<tooctal>:
mov    %rdi,%rax
lea    -0x20(%rsi),%ecx
cmp    $0x5e,%cl
ja     1118 <tooctal+0x18>
mov    %sil,(%rax)
mov    $0x1,%ecx
jmp    119d <tooctal+0x9d>
cmp    $0xd,%sil
ja     1139 <tooctal+0x39>
movzbl %sil,%ecx
lea    0xed7(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
movw   $0x305c,(%rax)
jmp    1198 <tooctal+0x98>
mov    %esi,%ecx
mov    %rcx,%rdx
shr    $0x6,%rdx
lea    0xeef(%rip),%rdi
mov    (%rdx,%rdi,1),%dl
mov    %dl,(%rax)
shr    $0x3,%esi
and    $0x7,%esi
mov    (%rsi,%rdi,1),%dl
mov    %dl,0x1(%rax)
and    $0x7,%ecx
mov    (%rcx,%rdi,1),%dl
mov    $0x2,%ecx
mov    %dl,(%rax,%rcx,1)
ret
movw   $0x615c,(%rax)
jmp    1198 <tooctal+0x98>
movw   $0x625c,(%rax)
jmp    1198 <tooctal+0x98>
movw   $0x745c,(%rax)
jmp    1198 <tooctal+0x98>
movw   $0x6e5c,(%rax)
jmp    1198 <tooctal+0x98>
movw   $0x765c,(%rax)
jmp    1198 <tooctal+0x98>
movw   $0x665c,(%rax)
jmp    1198 <tooctal+0x98>
movw   $0x725c,(%rax)
mov    $0x2,%ecx
xor    %edx,%edx
mov    %dl,(%rax,%rcx,1)
ret

LLVM IR (-O0):
@.str = [9 x i8] c"01234567\00"
define i8* @tooctal(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 sgt i32 %6, 31
br i1 %7, label %8, label %18
8: ; preds = %2
%9 = load i8, i8* %4
%10 = zext i8 %9 to i32
%11 = icmp slt i32 %10, 127
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = load i8, i8* %4
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 %13, i8* %15
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8 0, i8* %17
br label %106
18: ; preds = %8, %2
%19 = load i8, i8* %4
%20 = zext i8 %19 to i32
switch i32 %20, label %77 [
i32 0, label %21
i32 7, label %28
i32 8, label %35
i32 12, label %42
i32 10, label %49
i32 13, label %56
i32 9, label %63
i32 11, label %70
]
21: ; preds = %18
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 92, i8* %23
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 1
store i8 48, i8* %25
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i64 2
store i8 0, i8* %27
br label %105
28: ; preds = %18
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 92, i8* %30
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8 97, i8* %32
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 2
store i8 0, i8* %34
br label %105
35: ; preds = %18
%36 = load i8*, i8** %3
%37 = getelementptr inbounds i8, i8* %36, i64 0
store i8 92, i8* %37
%38 = load i8*, i8** %3
%39 = getelementptr inbounds i8, i8* %38, i64 1
store i8 98, i8* %39
%40 = load i8*, i8** %3
%41 = getelementptr inbounds i8, i8* %40, i64 2
store i8 0, i8* %41
br label %105
42: ; preds = %18
%43 = load i8*, i8** %3
%44 = getelementptr inbounds i8, i8* %43, i64 0
store i8 92, i8* %44
%45 = load i8*, i8** %3
%46 = getelementptr inbounds i8, i8* %45, i64 1
store i8 102, i8* %46
%47 = load i8*, i8** %3
%48 = getelementptr inbounds i8, i8* %47, i64 2
store i8 0, i8* %48
br label %105
49: ; preds = %18
%50 = load i8*, i8** %3
%51 = getelementptr inbounds i8, i8* %50, i64 0
store i8 92, i8* %51
%52 = load i8*, i8** %3
%53 = getelementptr inbounds i8, i8* %52, i64 1
store i8 110, i8* %53
%54 = load i8*, i8** %3
%55 = getelementptr inbounds i8, i8* %54, i64 2
store i8 0, i8* %55
br label %105
56: ; preds = %18
%57 = load i8*, i8** %3
%58 = getelementptr inbounds i8, i8* %57, i64 0
store i8 92, i8* %58
%59 = load i8*, i8** %3
%60 = getelementptr inbounds i8, i8* %59, i64 1
store i8 114, i8* %60
%61 = load i8*, i8** %3
%62 = getelementptr inbounds i8, i8* %61, i64 2
store i8 0, i8* %62
br label %105
63: ; preds = %18
%64 = load i8*, i8** %3
%65 = getelementptr inbounds i8, i8* %64, i64 0
store i8 92, i8* %65
%66 = load i8*, i8** %3
%67 = getelementptr inbounds i8, i8* %66, i64 1
store i8 116, i8* %67
%68 = load i8*, i8** %3
%69 = getelementptr inbounds i8, i8* %68, i64 2
store i8 0, i8* %69
br label %105
70: ; preds = %18
%71 = load i8*, i8** %3
%72 = getelementptr inbounds i8, i8* %71, i64 0
store i8 92, i8* %72
%73 = load i8*, i8** %3
%74 = getelementptr inbounds i8, i8* %73, i64 1
store i8 118, i8* %74
%75 = load i8*, i8** %3
%76 = getelementptr inbounds i8, i8* %75, i64 2
store i8 0, i8* %76
br label %105
77: ; preds = %18
%78 = load i8, i8* %4
%79 = zext i8 %78 to i32
%80 = ashr i32 %79, 6
%81 = and i32 7, %80
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [9 x i8], [9 x i8]* @.str, i64 0, i64 %82
%84 = load i8, i8* %83
%85 = load i8*, i8** %3
%86 = getelementptr inbounds i8, i8* %85, i64 0
store i8 %84, i8* %86
%87 = load i8, i8* %4
%88 = zext i8 %87 to i32
%89 = ashr i32 %88, 3
%90 = and i32 7, %89
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [9 x i8], [9 x i8]* @.str, i64 0, i64 %91
%93 = load i8, i8* %92
%94 = load i8*, i8** %3
%95 = getelementptr inbounds i8, i8* %94, i64 1
store i8 %93, i8* %95
%96 = load i8, i8* %4
%97 = zext i8 %96 to i32
%98 = ashr i32 %97, 0
%99 = and i32 7, %98
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds [9 x i8], [9 x i8]* @.str, i64 0, i64 %100
%102 = load i8, i8* %101
%103 = load i8*, i8** %3
%104 = getelementptr inbounds i8, i8* %103, i64 2
store i8 %102, i8* %104
br label %105
105: ; preds = %77, %70, %63, %56, %49, %42, %35, %28, %21
br label %106
106: ; preds = %105, %12
%107 = load i8*, i8** %3
ret i8* %107
}

LLVM IR (-O2):
@.str = [9 x i8] c"01234567\00"
define i8* @tooctal(i8* returned writeonly %0, i8 zeroext %1) {
%3 = zext i8 %1 to i32
%4 = add i8 %1, -32
%5 = icmp ult i8 %4, 95
br i1 %5, label %6, label %7
6: ; preds = %2
store i8 %1, i8* %0
br label %39
7: ; preds = %2
switch i8 %1, label %24 [
i8 0, label %8
i8 7, label %10
i8 8, label %12
i8 12, label %14
i8 10, label %16
i8 13, label %18
i8 9, label %20
i8 11, label %22
]
8: ; preds = %7
store i8 92, i8* %0
%9 = getelementptr inbounds i8, i8* %0, i64 1
store i8 48, i8* %9
br label %39
10: ; preds = %7
store i8 92, i8* %0
%11 = getelementptr inbounds i8, i8* %0, i64 1
store i8 97, i8* %11
br label %39
12: ; preds = %7
store i8 92, i8* %0
%13 = getelementptr inbounds i8, i8* %0, i64 1
store i8 98, i8* %13
br label %39
14: ; preds = %7
store i8 92, i8* %0
%15 = getelementptr inbounds i8, i8* %0, i64 1
store i8 102, i8* %15
br label %39
16: ; preds = %7
store i8 92, i8* %0
%17 = getelementptr inbounds i8, i8* %0, i64 1
store i8 110, i8* %17
br label %39
18: ; preds = %7
store i8 92, i8* %0
%19 = getelementptr inbounds i8, i8* %0, i64 1
store i8 114, i8* %19
br label %39
20: ; preds = %7
store i8 92, i8* %0
%21 = getelementptr inbounds i8, i8* %0, i64 1
store i8 116, i8* %21
br label %39
22: ; preds = %7
store i8 92, i8* %0
%23 = getelementptr inbounds i8, i8* %0, i64 1
store i8 118, i8* %23
br label %39
24: ; preds = %7
%25 = lshr i32 %3, 6
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds [9 x i8], [9 x i8]* @.str, i64 0, i64 %26
%28 = load i8, i8* %27
store i8 %28, i8* %0
%29 = lshr i32 %3, 3
%30 = and i32 %29, 7
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [9 x i8], [9 x i8]* @.str, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %33, i8* %34
%35 = and i32 %3, 7
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds [9 x i8], [9 x i8]* @.str, i64 0, i64 %36
%38 = load i8, i8* %37
br label %39
39: ; preds = %8, %10, %12, %14, %16, %18, %20, %22, %24, %6
%40 = phi i64 [ 2, %8 ], [ 2, %10 ], [ 2, %12 ], [ 2, %14 ], [ 2, %16 ], [ 2, %18 ], [ 2, %20 ], [ 2, %22 ], [ 2, %24 ], [ 1, %6 ]
%41 = phi i8 [ 0, %8 ], [ 0, %10 ], [ 0, %12 ], [ 0, %14 ], [ 0, %16 ], [ 0, %18 ], [ 0, %20 ], [ 0, %22 ], [ %38, %24 ], [ 0, %6 ]
%42 = getelementptr inbounds i8, i8* %0, i64 %40
store i8 %41, i8* %42
ret i8* %0
}