#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 */
typedef int /*<<< orphan*/ escape_chars;

/* Variables and functions */
scalar_t__ isprint(char const);
scalar_t__ malloc(int);
scalar_t__ sprintf(char *, char *, ...);
int strlen(char const *);
char *escape(const char *str) {
  static const char escape_chars[] = {'\0', '\a', '\b', '\t', '\n',
                                      '\v', '\f', '\r', '\\'};
  static const char *escape_strs[] = {"\\0", "\\a", "\\b", "\\t", "\\n",
                                      "\\v", "\\f", "\\r", "\\\\"};
  char *escaped;
  size_t size;
  int written;
  unsigned i, found;
  size = strlen(str) * 4;
  escaped = (char *)malloc(sizeof(char) * (size + 1));
  written = 0;
  for (; *str; ++str) {
    if (isprint(*str))
      written += sprintf(escaped + written, "%c", *str);
    else {
      found = 0;
      for (i = 0; i < sizeof(escape_chars) / sizeof(char); ++i) {
        if (*str == escape_chars[i]) {
          written += sprintf(escaped + written, "%s", escape_strs[i]);
          found = 1;
          break;
        }
      }
      if (!found)
        written += sprintf(escaped + written, "\\%o", (unsigned char)*str);
    }
  }
  escaped[written] = '\0';
  return (escaped);
}


x86 assembly (clang, -O0):
<escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
shl    $0x2,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     12b2 <escape+0x172>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isprint@plt>
cmp    $0x0,%rax
je     11d1 <escape+0x91>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movsbl (%rax),%edx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
jmp    129c <escape+0x15c>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    $0x9,%rax
jae    1260 <escape+0x120>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
mov    -0x20(%rbp),%ecx
mov    %ecx,%edx
lea    0xe01(%rip),%rcx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    124d <escape+0x10d>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
lea    0x2e2e(%rip),%rax
mov    (%rax,%rcx,8),%rdx
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1260 <escape+0x120>
jmp    1252 <escape+0x112>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11df <escape+0x9f>
cmpl   $0x0,-0x24(%rbp)
jne    1297 <escape+0x157>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movzbl (%rax),%edx
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
jmp    129c <escape+0x15c>
jmp    12a1 <escape+0x161>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    117c <escape+0x3c>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x1(,%rax,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
xor    %ecx,%ecx
test   %al,%al
je     125e <escape+0x11e>
lea    0xec3(%rip),%rbp
lea    0xe84(%rip),%r12
lea    0xef4(%rip),%r13
xor    %r15d,%r15d
jmp    11bf <escape+0x7f>
mov    $0x7,%eax
nopl   (%rax)
movslq %r15d,%rdi
add    %r14,%rdi
movslq 0x0(%rbp,%rax,4),%rdx
add    %rbp,%rdx
lea    0xed5(%rip),%rsi
xor    %eax,%eax
call   1060 <sprintf@plt>
add    %eax,%r15d
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     125b <escape+0x11b>
movsbl %al,%edi
call   1050 <isprint@plt>
test   %rax,%rax
je     11f0 <escape+0xb0>
movslq %r15d,%rdi
add    %r14,%rdi
movsbl (%rbx),%edx
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <sprintf@plt>
jmp    11ac <escape+0x6c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rbx),%edx
cmp    $0xd,%rdx
ja     120b <escape+0xcb>
movslq (%r12,%rdx,4),%rcx
add    %r12,%rcx
xor    %eax,%eax
jmp    *%rcx
mov    $0x1,%eax
jmp    1190 <escape+0x50>
cmp    $0x5c,%dl
jne    121a <escape+0xda>
mov    $0x8,%eax
jmp    1190 <escape+0x50>
movslq %r15d,%rdi
add    %r14,%rdi
lea    0xe56(%rip),%rsi
jmp    11d8 <escape+0x98>
mov    $0x2,%eax
jmp    1190 <escape+0x50>
mov    $0x3,%eax
jmp    1190 <escape+0x50>
mov    $0x4,%eax
jmp    1190 <escape+0x50>
mov    $0x5,%eax
jmp    1190 <escape+0x50>
mov    $0x6,%eax
jmp    1190 <escape+0x50>
movslq %r15d,%rcx
movb   $0x0,(%r14,%rcx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@escape.escape_chars = internal constant [9 x i8] c"\00\07\08\09\0A\0B\0C\0D\\"
@escape.escape_strs = internal global [9 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i32 0, i32 0)]
@.str = [3 x i8] c"\\0\00"
@.str.1 = [3 x i8] c"\\a\00"
@.str.2 = [3 x i8] c"\\b\00"
@.str.3 = [3 x i8] c"\\t\00"
@.str.4 = [3 x i8] c"\\n\00"
@.str.5 = [3 x i8] c"\\v\00"
@.str.6 = [3 x i8] c"\\f\00"
@.str.7 = [3 x i8] c"\\r\00"
@.str.8 = [3 x i8] c"\\\\\00"
@.str.9 = [3 x i8] c"%c\00"
@.str.10 = [3 x i8] c"%s\00"
@.str.11 = [4 x i8] c"\\%o\00"
define i8* @escape(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
%10 = mul nsw i32 %9, 4
%11 = sext i32 %10 to i64
store i64 %11, i64* %4
%12 = load i64, i64* %4
%13 = add i64 %12, 1
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %3
store i32 0, i32* %5
br label %18
18: ; preds = %91, %1
%19 = load i8*, i8** %2
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %94
22: ; preds = %18
%23 = load i8*, i8** %2
%24 = load i8, i8* %23
%25 = call i64 @isprint(i8 signext %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %40
27: ; preds = %22
%28 = load i8*, i8** %3
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8*, i8** %2
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = call i64 (i8*, i8*, ...) @sprintf(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = add nsw i64 %37, %35
%39 = trunc i64 %38 to i32
store i32 %39, i32* %5
br label %90
40: ; preds = %22
store i32 0, i32* %7
store i32 0, i32* %6
br label %41
41: ; preds = %70, %40
%42 = load i32, i32* %6
%43 = zext i32 %42 to i64
%44 = icmp ult i64 %43, 9
br i1 %44, label %45, label %73
45: ; preds = %41
%46 = load i8*, i8** %2
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = load i32, i32* %6
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds [9 x i8], [9 x i8]* @escape.escape_chars, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %48, %53
br i1 %54, label %55, label %69
55: ; preds = %45
%56 = load i8*, i8** %3
%57 = load i32, i32* %5
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i32, i32* %6
%61 = zext i32 %60 to i64
%62 = getelementptr inbounds [9 x i8*], [9 x i8*]* @escape.escape_strs, i64 0, i64 %61
%63 = load i8*, i8** %62
%64 = call i64 (i8*, i8*, ...) @sprintf(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i8* %63)
%65 = load i32, i32* %5
%66 = sext i32 %65 to i64
%67 = add nsw i64 %66, %64
%68 = trunc i64 %67 to i32
store i32 %68, i32* %5
store i32 1, i32* %7
br label %73
69: ; preds = %45
br label %70
70: ; preds = %69
%71 = load i32, i32* %6
%72 = add i32 %71, 1
store i32 %72, i32* %6
br label %41
73: ; preds = %55, %41
%74 = load i32, i32* %7
%75 = icmp ne i32 %74, 0
br i1 %75, label %89, label %76
76: ; preds = %73
%77 = load i8*, i8** %3
%78 = load i32, i32* %5
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %77, i64 %79
%81 = load i8*, i8** %2
%82 = load i8, i8* %81
%83 = zext i8 %82 to i32
%84 = call i64 (i8*, i8*, ...) @sprintf(i8* %80, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i32 %83)
%85 = load i32, i32* %5
%86 = sext i32 %85 to i64
%87 = add nsw i64 %86, %84
%88 = trunc i64 %87 to i32
store i32 %88, i32* %5
br label %89
89: ; preds = %76, %73
br label %90
90: ; preds = %89, %27
br label %91
91: ; preds = %90
%92 = load i8*, i8** %2
%93 = getelementptr inbounds i8, i8* %92, i32 1
store i8* %93, i8** %2
br label %18
94: ; preds = %18
%95 = load i8*, i8** %3
%96 = load i32, i32* %5
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i8, i8* %95, i64 %97
store i8 0, i8* %98
%99 = load i8*, i8** %3
ret i8* %99
}

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