#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ isprint(char const);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char const *);
const char *printable(const char *string) {
  static char *buffer = NULL;
  const char *s;
  char *p;
  for (s = string; *s; ++s) {
    if (!isprint(*s))
      goto need_to_escape;
  }
  return string;
need_to_escape:
  if (buffer)
    free(buffer);
  p = buffer = malloc(3 * strlen(string) + 1);
  for (s = string; *s; ++s) {
    char cc = *s;
    if (isprint(cc)) {
      *p++ = cc;
    } else {
      switch (cc) {
      case '\n':
        strcpy(p, "\\n");
        break;
      case '\r':
        strcpy(p, "\\r");
        break;
      case '\t':
        strcpy(p, "\\t");
        break;
      default:
        sprintf(p, "\\%03o", cc);
      }
    }
  }
  *p = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<printable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11b2 <printable+0x52>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1070 <isprint@plt>
cmp    $0x0,%rax
jne    119c <printable+0x3c>
jmp    11bf <printable+0x5f>
jmp    11a1 <printable+0x41>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1174 <printable+0x14>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f9 <printable+0x199>
cmpq   $0x0,0x2e91(%rip)
je     11d9 <printable+0x79>
mov    0x2e84(%rip),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
imul   $0x3,%eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,0x2e64(%rip)
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     12e7 <printable+0x187>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%edi
call   1070 <isprint@plt>
cmp    $0x0,%rax
je     1242 <printable+0xe2>
mov    -0x21(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    12d1 <printable+0x171>
movsbl -0x21(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x9,%eax
je     12a3 <printable+0x143>
jmp    1257 <printable+0xf7>
mov    -0x28(%rbp),%eax
sub    $0xa,%eax
je     1279 <printable+0x119>
jmp    1268 <printable+0x108>
mov    -0x28(%rbp),%eax
sub    $0xd,%eax
je     128e <printable+0x12e>
jmp    12b8 <printable+0x158>
mov    -0x20(%rbp),%rdi
lea    0xd7c(%rip),%rsi
call   1040 <strcpy@plt>
jmp    12cc <printable+0x16c>
mov    -0x20(%rbp),%rdi
lea    0xd6a(%rip),%rsi
call   1040 <strcpy@plt>
jmp    12cc <printable+0x16c>
mov    -0x20(%rbp),%rdi
lea    0xd58(%rip),%rsi
call   1040 <strcpy@plt>
jmp    12cc <printable+0x16c>
mov    -0x20(%rbp),%rdi
lea    0xd46(%rip),%rsi
movsbl -0x21(%rbp),%edx
call   1080 <sprintf@plt>
jmp    12d1 <printable+0x171>
jmp    12d6 <printable+0x176>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1200 <printable+0xa0>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2d63(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printable>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%al
test   %al,%al
je     126d <printable+0x10d>
lea    0x1(%rbx),%rbp
nopl   0x0(%rax)
movsbl %al,%edi
call   1070 <isprint@plt>
test   %rax,%rax
je     119e <printable+0x3e>
movzbl 0x0(%rbp),%eax
add    $0x1,%rbp
test   %al,%al
jne    1180 <printable+0x20>
jmp    126d <printable+0x10d>
mov    0x2eb3(%rip),%rdi
test   %rdi,%rdi
je     11af <printable+0x4f>
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rax,2),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,0x2e8c(%rip)
mov    (%rbx),%r13b
test   %r13b,%r13b
je     1266 <printable+0x106>
add    $0x1,%rbx
lea    0xe20(%rip),%r12
lea    0xe1f(%rip),%rbp
jmp    1204 <printable+0xa4>
nopl   0x0(%rax)
mov    %r13b,(%r14)
add    $0x1,%r14
movzbl (%rbx),%r13d
add    $0x1,%rbx
test   %r13b,%r13b
je     125d <printable+0xfd>
movsbl %r13b,%r15d
mov    %r15d,%edi
call   1070 <isprint@plt>
test   %rax,%rax
jne    11f0 <printable+0x90>
mov    %r14,%rdi
cmp    $0x9,%r15d
je     1242 <printable+0xe2>
cmp    $0xd,%r15d
je     1238 <printable+0xd8>
cmp    $0xa,%r15d
jne    1250 <printable+0xf0>
lea    0xdcf(%rip),%rsi
call   1040 <strcpy@plt>
jmp    11f7 <printable+0x97>
mov    %r12,%rsi
call   1040 <strcpy@plt>
jmp    11f7 <printable+0x97>
lea    0xdbd(%rip),%rsi
call   1040 <strcpy@plt>
jmp    11f7 <printable+0x97>
mov    %rbp,%rsi
mov    %r15d,%edx
call   1080 <sprintf@plt>
jmp    11f7 <printable+0x97>
mov    0x2df4(%rip),%rbx
jmp    1269 <printable+0x109>
mov    %r14,%rbx
movb   $0x0,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@printable.buffer = internal global i8* null
@.str = [3 x i8] c"\\n\00"
@.str.1 = [3 x i8] c"\\r\00"
@.str.2 = [3 x i8] c"\\t\00"
@.str.3 = [6 x i8] c"\\%03o\00"
define i8* @printable(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
store i8* %0, i8** %3
%7 = load i8*, i8** %3
store i8* %7, i8** %4
br label %8
8: ; preds = %19, %1
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = call i64 @isprint(i8 signext %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %12
br label %24
18: ; preds = %12
br label %19
19: ; preds = %18
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
br label %8
22: ; preds = %8
%23 = load i8*, i8** %3
store i8* %23, i8** %2
br label %75
24: ; preds = %17
%25 = load i8*, i8** @printable.buffer
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** @printable.buffer
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %27, %24
%31 = load i8*, i8** %3
%32 = call i32 @strlen(i8* %31)
%33 = mul nsw i32 3, %32
%34 = add nsw i32 %33, 1
%35 = call i8* @malloc(i32 %34)
store i8* %35, i8** @printable.buffer
store i8* %35, i8** %5
%36 = load i8*, i8** %3
store i8* %36, i8** %4
br label %37
37: ; preds = %69, %30
%38 = load i8*, i8** %4
%39 = load i8, i8* %38
%40 = icmp ne i8 %39, 0
br i1 %40, label %41, label %72
41: ; preds = %37
%42 = load i8*, i8** %4
%43 = load i8, i8* %42
store i8 %43, i8* %6
%44 = load i8, i8* %6
%45 = call i64 @isprint(i8 signext %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %41
%48 = load i8, i8* %6
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %5
store i8 %48, i8* %49
br label %68
51: ; preds = %41
%52 = load i8, i8* %6
%53 = sext i8 %52 to i32
switch i32 %53, label %63 [
i32 10, label %54
i32 13, label %57
i32 9, label %60
]
54: ; preds = %51
%55 = load i8*, i8** %5
%56 = call i32 @strcpy(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %67
57: ; preds = %51
%58 = load i8*, i8** %5
%59 = call i32 @strcpy(i8* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %67
60: ; preds = %51
%61 = load i8*, i8** %5
%62 = call i32 @strcpy(i8* %61, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %67
63: ; preds = %51
%64 = load i8*, i8** %5
%65 = load i8, i8* %6
%66 = call i32 @sprintf(i8* %64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8 signext %65)
br label %67
67: ; preds = %63, %60, %57, %54
br label %68
68: ; preds = %67, %47
br label %69
69: ; preds = %68
%70 = load i8*, i8** %4
%71 = getelementptr inbounds i8, i8* %70, i32 1
store i8* %71, i8** %4
br label %37
72: ; preds = %37
%73 = load i8*, i8** %5
store i8 0, i8* %73
%74 = load i8*, i8** @printable.buffer
store i8* %74, i8** %2
br label %75
75: ; preds = %72, %22
%76 = load i8*, i8** %2
ret i8* %76
}

LLVM IR (-O2):
@printable.buffer = internal unnamed_addr global i8* null
@.str = [3 x i8] c"\\n\00"
@.str.1 = [3 x i8] c"\\r\00"
@.str.2 = [3 x i8] c"\\t\00"
@.str.3 = [6 x i8] c"\\%03o\00"
define i8* @printable(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %53, label %7
4: ; preds = %7
%5 = load i8, i8* %12
%6 = icmp eq i8 %5, 0
br i1 %6, label %53, label %7
7: ; preds = %1, %4
%8 = phi i8 [ %5, %4 ], [ %2, %1 ]
%9 = phi i8* [ %12, %4 ], [ %0, %1 ]
%10 = tail call i64 @isprint(i8 signext %8)
%11 = icmp eq i64 %10, 0
%12 = getelementptr inbounds i8, i8* %9, i64 1
br i1 %11, label %13, label %4
13: ; preds = %7
%14 = load i8*, i8** @printable.buffer
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @free(i8* %14)
br label %18
18: ; preds = %16, %13
%19 = tail call i32 @strlen(i8* %0)
%20 = mul nsw i32 %19, 3
%21 = add nsw i32 %20, 1
%22 = tail call i8* @malloc(i32 %21)
store i8* %22, i8** @printable.buffer
%23 = load i8, i8* %0
%24 = icmp eq i8 %23, 0
br i1 %24, label %50, label %25
25: ; preds = %18, %43
%26 = phi i8 [ %46, %43 ], [ %23, %18 ]
%27 = phi i8* [ %44, %43 ], [ %22, %18 ]
%28 = phi i8* [ %45, %43 ], [ %0, %18 ]
%29 = tail call i64 @isprint(i8 signext %26)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = getelementptr inbounds i8, i8* %27, i64 1
store i8 %26, i8* %27
br label %43
33: ; preds = %25
%34 = sext i8 %26 to i32
switch i32 %34, label %41 [
i32 10, label %35
i32 13, label %37
i32 9, label %39
]
35: ; preds = %33
%36 = tail call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %43
37: ; preds = %33
%38 = tail call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %43
39: ; preds = %33
%40 = tail call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %43
41: ; preds = %33
%42 = tail call i32 @sprintf(i8* dereferenceable(1) %27, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8 signext %26)
br label %43
43: ; preds = %35, %37, %39, %41, %31
%44 = phi i8* [ %32, %31 ], [ %27, %41 ], [ %27, %39 ], [ %27, %37 ], [ %27, %35 ]
%45 = getelementptr inbounds i8, i8* %28, i64 1
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 0
br i1 %47, label %48, label %25
48: ; preds = %43
%49 = load i8*, i8** @printable.buffer
br label %50
50: ; preds = %48, %18
%51 = phi i8* [ %22, %18 ], [ %49, %48 ]
%52 = phi i8* [ %22, %18 ], [ %44, %48 ]
store i8 0, i8* %52
br label %53
53: ; preds = %4, %1, %50
%54 = phi i8* [ %51, %50 ], [ %0, %1 ], [ %0, %4 ]
ret i8* %54
}