#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(size_t);
size_t strlen(char *);
char *escape_str(char *str) {
  size_t len = strlen(str);
  char *buf = malloc(len * 2 + 1);
  char *p = buf;
  char c;
  while ((c = *(str++)) != '\0') {
    switch (c) {
    case '\n':
      *(p++) = '\\';
      *(p++) = 'n';
      break;
    case '\r':
      *(p++) = '\\';
      *(p++) = 'r';
      break;
    case '\a':
      *(p++) = '\\';
      *(p++) = 'a';
      break;
    case '\v':
      *(p++) = '\\';
      *(p++) = 'v';
      break;
    case '\t':
      *(p++) = '\\';
      *(p++) = 't';
      break;
    case '\f':
      *(p++) = '\\';
      *(p++) = 'f';
      break;
    case '\b':
      *(p++) = '\\';
      *(p++) = 'b';
      break;
    default:
      *(p++) = c;
      break;
    }
  }
  *p = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<escape_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x1,%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     12d8 <escape_str+0x1b8>
movsbl -0x21(%rbp),%eax
add    $0xfffffff9,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x6,%eax
ja     12bf <escape_str+0x19f>
mov    -0x30(%rbp),%rax
lea    0xe69(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x6e,(%rax)
jmp    12d3 <escape_str+0x1b3>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x72,(%rax)
jmp    12d3 <escape_str+0x1b3>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x61,(%rax)
jmp    12d3 <escape_str+0x1b3>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x76,(%rax)
jmp    12d3 <escape_str+0x1b3>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x74,(%rax)
jmp    12d3 <escape_str+0x1b3>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x66,(%rax)
jmp    12d3 <escape_str+0x1b3>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x62,(%rax)
jmp    12d3 <escape_str+0x1b3>
mov    -0x21(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    1156 <escape_str+0x36>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_str>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    (%rbx),%dl
mov    %rax,%rcx
test   %dl,%dl
je     11b5 <escape_str+0x95>
add    $0x1,%rbx
lea    0xeb6(%rip),%rsi
mov    %rax,%rcx
jmp    116f <escape_str+0x4f>
movw   $0x745c,(%rcx)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x2,%rcx
movzbl (%rbx),%edx
add    $0x1,%rbx
test   %dl,%dl
je     11b5 <escape_str+0x95>
movsbl %dl,%edi
add    $0xfffffff9,%edi
cmp    $0x6,%edi
ja     1198 <escape_str+0x78>
movslq (%rsi,%rdi,4),%rdx
add    %rsi,%rdx
jmp    *%rdx
movw   $0x615c,(%rcx)
jmp    1160 <escape_str+0x40>
movw   $0x6e5c,(%rcx)
jmp    1160 <escape_str+0x40>
movw   $0x765c,(%rcx)
jmp    1160 <escape_str+0x40>
mov    %dl,(%rcx)
add    $0x1,%rcx
jmp    1164 <escape_str+0x44>
movw   $0x625c,(%rcx)
jmp    1160 <escape_str+0x40>
movw   $0x665c,(%rcx)
jmp    1160 <escape_str+0x40>
movw   $0x725c,(%rcx)
jmp    1160 <escape_str+0x40>
movb   $0x0,(%rcx)
pop    %rbx
ret

LLVM IR (-O0):
define i8* @escape_str(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = mul i64 %9, 2
%11 = add i64 %10, 1
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
store i8* %13, i8** %5
br label %14
14: ; preds = %62, %1
%15 = load i8*, i8** %2
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %2
%17 = load i8, i8* %15
store i8 %17, i8* %6
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %63
20: ; preds = %14
%21 = load i8, i8* %6
%22 = sext i8 %21 to i32
switch i32 %22, label %58 [
i32 10, label %23
i32 13, label %28
i32 7, label %33
i32 11, label %38
i32 9, label %43
i32 12, label %48
i32 8, label %53
]
23: ; preds = %20
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %5
store i8 92, i8* %24
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
store i8 110, i8* %26
br label %62
28: ; preds = %20
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %5
store i8 92, i8* %29
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %5
store i8 114, i8* %31
br label %62
33: ; preds = %20
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %5
store i8 92, i8* %34
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %5
store i8 97, i8* %36
br label %62
38: ; preds = %20
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %5
store i8 92, i8* %39
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %5
store i8 118, i8* %41
br label %62
43: ; preds = %20
%44 = load i8*, i8** %5
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %5
store i8 92, i8* %44
%46 = load i8*, i8** %5
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %5
store i8 116, i8* %46
br label %62
48: ; preds = %20
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %5
store i8 92, i8* %49
%51 = load i8*, i8** %5
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %5
store i8 102, i8* %51
br label %62
53: ; preds = %20
%54 = load i8*, i8** %5
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %5
store i8 92, i8* %54
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %5
store i8 98, i8* %56
br label %62
58: ; preds = %20
%59 = load i8, i8* %6
%60 = load i8*, i8** %5
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %5
store i8 %59, i8* %60
br label %62
62: ; preds = %58, %53, %48, %43, %38, %33, %28, %23
br label %14
63: ; preds = %14
%64 = load i8*, i8** %5
store i8 0, i8* %64
%65 = load i8*, i8** %4
ret i8* %65
}

LLVM IR (-O2):
define i8* @escape_str(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = shl i64 %2, 1
%4 = or i64 %3, 1
%5 = tail call i8* @malloc(i64 %4)
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %41, label %8
8: ; preds = %1, %37
%9 = phi i8 [ %39, %37 ], [ %6, %1 ]
%10 = phi i8* [ %12, %37 ], [ %0, %1 ]
%11 = phi i8* [ %38, %37 ], [ %5, %1 ]
%12 = getelementptr inbounds i8, i8* %10, i64 1
%13 = sext i8 %9 to i32
switch i32 %13, label %35 [
i32 10, label %14
i32 13, label %17
i32 7, label %20
i32 11, label %23
i32 9, label %26
i32 12, label %29
i32 8, label %32
]
14: ; preds = %8
%15 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%16 = getelementptr inbounds i8, i8* %11, i64 2
store i8 110, i8* %15
br label %37
17: ; preds = %8
%18 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%19 = getelementptr inbounds i8, i8* %11, i64 2
store i8 114, i8* %18
br label %37
20: ; preds = %8
%21 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%22 = getelementptr inbounds i8, i8* %11, i64 2
store i8 97, i8* %21
br label %37
23: ; preds = %8
%24 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%25 = getelementptr inbounds i8, i8* %11, i64 2
store i8 118, i8* %24
br label %37
26: ; preds = %8
%27 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%28 = getelementptr inbounds i8, i8* %11, i64 2
store i8 116, i8* %27
br label %37
29: ; preds = %8
%30 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%31 = getelementptr inbounds i8, i8* %11, i64 2
store i8 102, i8* %30
br label %37
32: ; preds = %8
%33 = getelementptr inbounds i8, i8* %11, i64 1
store i8 92, i8* %11
%34 = getelementptr inbounds i8, i8* %11, i64 2
store i8 98, i8* %33
br label %37
35: ; preds = %8
%36 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %9, i8* %11
br label %37
37: ; preds = %35, %32, %29, %26, %23, %20, %17, %14
%38 = phi i8* [ %36, %35 ], [ %34, %32 ], [ %31, %29 ], [ %28, %26 ], [ %25, %23 ], [ %22, %20 ], [ %19, %17 ], [ %16, %14 ]
%39 = load i8, i8* %12
%40 = icmp eq i8 %39, 0
br i1 %40, label %41, label %8
41: ; preds = %37, %1
%42 = phi i8* [ %5, %1 ], [ %38, %37 ]
store i8 0, i8* %42
ret i8* %5
}