#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*/ isprint(unsigned char);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int strlen(char const *);
char *test_escape_string_for_display(const char *string) {
  char *escaped_string, *optr;
  const char *iptr;
  escaped_string = malloc(strlen(string) * 4 + 1);
  optr = escaped_string;
  for (iptr = string; *iptr != '\0'; iptr++) {
    switch (*iptr) {
    case '\a':
      *optr++ = '\\';
      *optr++ = 'a';
      break;
    case '\b':
      *optr++ = '\\';
      *optr++ = 'b';
      break;
    case '\t':
      *optr++ = '\\';
      *optr++ = 't';
      break;
    case '\n':
      *optr++ = '\\';
      *optr++ = 'n';
      break;
    case '\v':
      *optr++ = '\\';
      *optr++ = 'v';
      break;
    case '\f':
      *optr++ = '\\';
      *optr++ = 'f';
      break;
    case '\r':
      *optr++ = '\\';
      *optr++ = 'r';
      break;
    default:
      if (isprint((unsigned char)*iptr)) {
        *optr++ = *iptr;
      } else {
        *optr++ = '\\';
        sprintf(optr, "%03u", (unsigned char)(*iptr));
        optr += 3;
      }
      break;
    }
  }
  *optr++ = '\0';
  return escaped_string;
}


x86 assembly (clang, -O0):
<test_escape_string_for_display>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
shl    $0x2,%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1353 <test_escape_string_for_display+0x213>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
add    $0xfffffff9,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x6,%eax
ja     12d2 <test_escape_string_for_display+0x192>
mov    -0x28(%rbp),%rax
lea    0xe56(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x61,(%rax)
jmp    133d <test_escape_string_for_display+0x1fd>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x62,(%rax)
jmp    133d <test_escape_string_for_display+0x1fd>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x74,(%rax)
jmp    133d <test_escape_string_for_display+0x1fd>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x6e,(%rax)
jmp    133d <test_escape_string_for_display+0x1fd>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x76,(%rax)
jmp    133d <test_escape_string_for_display+0x1fd>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x66,(%rax)
jmp    133d <test_escape_string_for_display+0x1fd>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x72,(%rax)
jmp    133d <test_escape_string_for_display+0x1fd>
mov    -0x20(%rbp),%rax
movzbl (%rax),%edi
call   1050 <isprint@plt>
cmp    $0x0,%eax
je     1303 <test_escape_string_for_display+0x1c3>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1338 <test_escape_string_for_display+0x1f8>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
lea    0xcf8(%rip),%rsi
movzbl (%rax),%edx
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x18(%rbp)
jmp    133d <test_escape_string_for_display+0x1fd>
jmp    1342 <test_escape_string_for_display+0x202>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1176 <test_escape_string_for_display+0x36>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_escape_string_for_display>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x1(,%rax,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
mov    %r14,%r12
test   %al,%al
je     1226 <test_escape_string_for_display+0xe6>
lea    0xea8(%rip),%r15
lea    0xe85(%rip),%r13
mov    %r14,%r12
jmp    11a4 <test_escape_string_for_display+0x64>
movw   $0x765c,(%r12)
nopw   0x0(%rax,%rax,1)
add    $0x2,%r12
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     1226 <test_escape_string_for_display+0xe6>
movsbl %al,%ecx
add    $0xfffffff9,%ecx
cmp    $0x6,%ecx
ja     11d4 <test_escape_string_for_display+0x94>
movslq 0x0(%r13,%rcx,4),%rax
add    %r13,%rax
jmp    *%rax
movw   $0x615c,(%r12)
jmp    1190 <test_escape_string_for_display+0x50>
movw   $0x745c,(%r12)
jmp    1190 <test_escape_string_for_display+0x50>
movw   $0x6e5c,(%r12)
jmp    1190 <test_escape_string_for_display+0x50>
movzbl %al,%edi
call   1050 <isprint@plt>
test   %eax,%eax
je     1208 <test_escape_string_for_display+0xc8>
movzbl (%rbx),%eax
mov    %al,(%r12)
add    $0x1,%r12
jmp    1194 <test_escape_string_for_display+0x54>
movw   $0x625c,(%r12)
jmp    1190 <test_escape_string_for_display+0x50>
movw   $0x665c,(%r12)
jmp    1190 <test_escape_string_for_display+0x50>
movw   $0x725c,(%r12)
jmp    1190 <test_escape_string_for_display+0x50>
lea    0x1(%r12),%rdi
movb   $0x5c,(%r12)
movzbl (%rbx),%edx
mov    %r15,%rsi
call   1060 <sprintf@plt>
add    $0x4,%r12
jmp    1194 <test_escape_string_for_display+0x54>
movb   $0x0,(%r12)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%03u\00"
define i8* @test_escape_string_for_display(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = shl nsw i32 %2, 2
%4 = or i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %50, label %8
8: ; preds = %1, %45
%9 = phi i8 [ %48, %45 ], [ %6, %1 ]
%10 = phi i8* [ %47, %45 ], [ %0, %1 ]
%11 = phi i8* [ %46, %45 ], [ %5, %1 ]
%12 = sext i8 %9 to i32
switch i32 %12, label %34 [
i32 7, label %13
i32 8, label %16
i32 9, label %19
i32 10, label %22
i32 11, label %25
i32 12, label %28
i32 13, label %31
]
13: ; preds = %8
%14 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%15 = getelementptr inbounds i8, i8* %11, i64 2
store i8 97, i8* %14
br label %45
16: ; preds = %8
%17 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%18 = getelementptr inbounds i8, i8* %11, i64 2
store i8 98, i8* %17
br label %45
19: ; preds = %8
%20 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%21 = getelementptr inbounds i8, i8* %11, i64 2
store i8 116, i8* %20
br label %45
22: ; preds = %8
%23 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%24 = getelementptr inbounds i8, i8* %11, i64 2
store i8 110, i8* %23
br label %45
25: ; preds = %8
%26 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%27 = getelementptr inbounds i8, i8* %11, i64 2
store i8 118, i8* %26
br label %45
28: ; preds = %8
%29 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%30 = getelementptr inbounds i8, i8* %11, i64 2
store i8 102, i8* %29
br label %45
31: ; preds = %8
%32 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%33 = getelementptr inbounds i8, i8* %11, i64 2
store i8 114, i8* %32
br label %45
34: ; preds = %8
%35 = tail call i32 @isprint(i8 zeroext %9)
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = load i8, i8* %10
%39 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %38, i8* %11
br label %45
40: ; preds = %34
%41 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%42 = load i8, i8* %10
%43 = tail call i32 @sprintf(i8* %41, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %42)
%44 = getelementptr inbounds i8, i8* %11, i64 4
br label %45
45: ; preds = %13, %16, %19, %22, %25, %28, %31, %40, %37
%46 = phi i8* [ %39, %37 ], [ %44, %40 ], [ %33, %31 ], [ %30, %28 ], [ %27, %25 ], [ %24, %22 ], [ %21, %19 ], [ %18, %16 ], [ %15, %13 ]
%47 = getelementptr inbounds i8, i8* %10, i64 1
%48 = load i8, i8* %47
%49 = icmp eq i8 %48, 0
br i1 %49, label %50, label %8
50: ; preds = %45, %1
%51 = phi i8* [ %5, %1 ], [ %46, %45 ]
store i8 0, i8* %51
ret i8* %5
}