#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 */
char *malloc(int);
char *realloc(char *, unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, char);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *str_escape(char str[]) {
  char chr[3];
  char *buffer = malloc(sizeof(char));
  unsigned int len = 0, blk_size;
  while (*str != '\0') {
    blk_size = 2;
    switch (*str) {
    case '\n':
      strcpy(chr, "\\n");
      break;
    case '\t':
      strcpy(chr, "\\t");
      break;
    case '\v':
      strcpy(chr, "\\v");
      break;
    case '\f':
      strcpy(chr, "\\f");
      break;
    case '\a':
      strcpy(chr, "\\a");
      break;
    case '\b':
      strcpy(chr, "\\b");
      break;
    case '\r':
      strcpy(chr, "\\r");
      break;
    default:
      sprintf(chr, "%c", *str);
      blk_size = 1;
      break;
    }
    len += blk_size;
    buffer = realloc(buffer, len * sizeof(char));
    strcat(buffer, chr);
    ++str;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<str_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12a3 <str_escape+0x153>
movl   $0x2,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
add    $0xfffffff9,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x6,%eax
ja     1248 <str_escape+0xf8>
mov    -0x28(%rbp),%rax
lea    0xe54(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    -0xb(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1266 <str_escape+0x116>
lea    -0xb(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1266 <str_escape+0x116>
lea    -0xb(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1266 <str_escape+0x116>
lea    -0xb(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1266 <str_escape+0x116>
lea    -0xb(%rbp),%rdi
lea    0xe14(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1266 <str_escape+0x116>
lea    -0xb(%rbp),%rdi
lea    0xe02(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1266 <str_escape+0x116>
lea    -0xb(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1266 <str_escape+0x116>
lea    -0xb(%rbp),%rdi
mov    -0x8(%rbp),%rax
lea    0xdda(%rip),%rsi
movsbl (%rax),%edx
call   1070 <sprintf@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0xb(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1171 <str_escape+0x21>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
test   %al,%al
je     1231 <str_escape+0xf1>
add    $0x1,%rbx
xor    %ebp,%ebp
lea    0x5(%rsp),%r15
lea    0xe89(%rip),%r13
lea    0xeb0(%rip),%r12
jmp    11bf <str_escape+0x7f>
mov    %r15,%rdi
lea    0xe92(%rip),%rsi
nopw   0x0(%rax,%rax,1)
call   1030 <strcpy@plt>
mov    $0x2,%eax
add    %eax,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <strcat@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     1231 <str_escape+0xf1>
movsbl %al,%ecx
add    $0xfffffff9,%ecx
cmp    $0x6,%ecx
ja     11f8 <str_escape+0xb8>
movslq 0x0(%r13,%rcx,4),%rax
add    %r13,%rax
jmp    *%rax
mov    %r15,%rdi
lea    0xe4a(%rip),%rsi
jmp    1190 <str_escape+0x50>
mov    %r15,%rdi
lea    0xe35(%rip),%rsi
jmp    1190 <str_escape+0x50>
mov    %r15,%rdi
lea    0xe2c(%rip),%rsi
jmp    1190 <str_escape+0x50>
mov    %al,0x5(%rsp)
movb   $0x0,0x6(%rsp)
mov    $0x1,%eax
jmp    119a <str_escape+0x5a>
mov    %r15,%rdi
lea    0xe19(%rip),%rsi
jmp    1190 <str_escape+0x50>
mov    %r15,%rdi
lea    0xe04(%rip),%rsi
jmp    1190 <str_escape+0x50>
mov    %r15,%rdi
mov    %r12,%rsi
jmp    1190 <str_escape+0x50>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\\n\00"
@.str.1 = [3 x i8] c"\\t\00"
@.str.2 = [3 x i8] c"\\v\00"
@.str.3 = [3 x i8] c"\\f\00"
@.str.4 = [3 x i8] c"\\a\00"
@.str.5 = [3 x i8] c"\\b\00"
@.str.6 = [3 x i8] c"\\r\00"
@.str.7 = [3 x i8] c"%c\00"
define i8* @str_escape(i8* %0) {
%2 = alloca i8*
%3 = alloca [3 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i8* @malloc(i32 1)
store i8* %7, i8** %4
store i32 0, i32* %5
br label %8
8: ; preds = %43, %1
%9 = load i8*, i8** %2
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %58
13: ; preds = %8
store i32 2, i32* %6
%14 = load i8*, i8** %2
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
switch i32 %16, label %38 [
i32 10, label %17
i32 9, label %20
i32 11, label %23
i32 12, label %26
i32 7, label %29
i32 8, label %32
i32 13, label %35
]
17: ; preds = %13
%18 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%19 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %43
20: ; preds = %13
%21 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %43
23: ; preds = %13
%24 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %43
26: ; preds = %13
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%28 = call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %43
29: ; preds = %13
%30 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%31 = call i32 @strcpy(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %43
32: ; preds = %13
%33 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%34 = call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %43
35: ; preds = %13
%36 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%37 = call i32 @strcpy(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %43
38: ; preds = %13
%39 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%40 = load i8*, i8** %2
%41 = load i8, i8* %40
%42 = call i32 @sprintf(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8 signext %41)
store i32 1, i32* %6
br label %43
43: ; preds = %38, %35, %32, %29, %26, %23, %20, %17
%44 = load i32, i32* %6
%45 = load i32, i32* %5
%46 = add i32 %45, %44
store i32 %46, i32* %5
%47 = load i8*, i8** %4
%48 = load i32, i32* %5
%49 = zext i32 %48 to i64
%50 = mul i64 %49, 1
%51 = trunc i64 %50 to i32
%52 = call i8* @realloc(i8* %47, i32 %51)
store i8* %52, i8** %4
%53 = load i8*, i8** %4
%54 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%55 = call i32 @strcat(i8* %53, i8* %54)
%56 = load i8*, i8** %2
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %2
br label %8
58: ; preds = %8
%59 = load i8*, i8** %4
ret i8* %59
}

LLVM IR (-O2):
@.str = [3 x i8] c"\\n\00"
@.str.1 = [3 x i8] c"\\t\00"
@.str.2 = [3 x i8] c"\\v\00"
@.str.3 = [3 x i8] c"\\f\00"
@.str.4 = [3 x i8] c"\\a\00"
@.str.5 = [3 x i8] c"\\b\00"
@.str.6 = [3 x i8] c"\\r\00"
define i8* @str_escape(i8* nocapture %0) {
%2 = alloca [3 x i8]
%3 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 0
%4 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %38, label %7
7: ; preds = %1
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 1
br label %9
9: ; preds = %7, %30
%10 = phi i8 [ %5, %7 ], [ %36, %30 ]
%11 = phi i32 [ 0, %7 ], [ %32, %30 ]
%12 = phi i8* [ %4, %7 ], [ %33, %30 ]
%13 = phi i8* [ %0, %7 ], [ %35, %30 ]
%14 = sext i8 %10 to i32
switch i32 %14, label %29 [
i32 10, label %15
i32 9, label %17
i32 11, label %19
i32 12, label %21
i32 7, label %23
i32 8, label %25
i32 13, label %27
]
15: ; preds = %9
%16 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %30
17: ; preds = %9
%18 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %30
19: ; preds = %9
%20 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %30
21: ; preds = %9
%22 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %30
23: ; preds = %9
%24 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %30
25: ; preds = %9
%26 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %30
27: ; preds = %9
%28 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %30
29: ; preds = %9
store i8 %10, i8* %3
store i8 0, i8* %8
br label %30
30: ; preds = %29, %27, %25, %23, %21, %19, %17, %15
%31 = phi i32 [ 1, %29 ], [ 2, %27 ], [ 2, %25 ], [ 2, %23 ], [ 2, %21 ], [ 2, %19 ], [ 2, %17 ], [ 2, %15 ]
%32 = add i32 %31, %11
%33 = call i8* @realloc(i8* %12, i32 %32)
%34 = call i32 @strcat(i8* %33, i8* %3)
%35 = getelementptr inbounds i8, i8* %13, i64 1
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %38, label %9
38: ; preds = %30, %1
%39 = phi i8* [ %4, %1 ], [ %33, %30 ]
ret i8* %39
}