#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);
int strlen(char const *);
char *escape(const char *s) {
  char *e = malloc(strlen(s) * 2 + 1);
  const char *p = s;
  char *r = e;
  while (*p) {
    if (*p == '\n') {
      *r = '\\';
      r++;
      *r = 'n';
    } else if (*p == '\r') {
      *r = '\\';
      r++;
      *r = 'r';
    } else if (*p == '\\') {
      *r = '\\';
      r++;
      *r = '\\';
    } else
      *r = *p;
    r++;
    p++;
  }
  *r = 0;
  return e;
}


x86 assembly (clang, -O0):
<escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1223 <escape+0x103>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1192 <escape+0x72>
mov    -0x20(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x6e,(%rax)
jmp    1206 <escape+0xe6>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
jne    11c1 <escape+0xa1>
mov    -0x20(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x72,(%rax)
jmp    1201 <escape+0xe1>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    11f0 <escape+0xd0>
mov    -0x20(%rbp),%rax
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x5c,(%rax)
jmp    11fc <escape+0xdc>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
jmp    1201 <escape+0xe1>
jmp    1206 <escape+0xe6>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1156 <escape+0x36>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
jmp    1151 <escape+0x31>
nopl   0x0(%rax)
movw   $0x725c,(%rcx)
add    $0x1,%rcx
add    $0x1,%rcx
add    $0x1,%rbx
movzbl (%rbx),%edx
cmp    $0xc,%dl
jle    1170 <escape+0x50>
cmp    $0xd,%dl
je     1140 <escape+0x20>
cmp    $0x5c,%dl
jne    1180 <escape+0x60>
movw   $0x5c5c,(%rcx)
jmp    1145 <escape+0x25>
nopw   0x0(%rax,%rax,1)
cmp    $0xa,%dl
jne    117c <escape+0x5c>
movw   $0x6e5c,(%rcx)
jmp    1145 <escape+0x25>
test   %dl,%dl
je     1184 <escape+0x64>
mov    %dl,(%rcx)
jmp    1149 <escape+0x29>
movb   $0x0,(%rcx)
pop    %rbx
ret

LLVM IR (-O0):
define i8* @escape(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, 2
%9 = add nsw i32 %8, 1
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %2
store i8* %11, i8** %4
%12 = load i8*, i8** %3
store i8* %12, i8** %5
br label %13
13: ; preds = %53, %1
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %17, label %58
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 10
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i8*, i8** %5
store i8 92, i8* %23
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %5
%26 = load i8*, i8** %5
store i8 110, i8* %26
br label %53
27: ; preds = %17
%28 = load i8*, i8** %4
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 13
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i8*, i8** %5
store i8 92, i8* %33
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %5
%36 = load i8*, i8** %5
store i8 114, i8* %36
br label %52
37: ; preds = %27
%38 = load i8*, i8** %4
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 92
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i8*, i8** %5
store i8 92, i8* %43
%44 = load i8*, i8** %5
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %5
%46 = load i8*, i8** %5
store i8 92, i8* %46
br label %51
47: ; preds = %37
%48 = load i8*, i8** %4
%49 = load i8, i8* %48
%50 = load i8*, i8** %5
store i8 %49, i8* %50
br label %51
51: ; preds = %47, %42
br label %52
52: ; preds = %51, %32
br label %53
53: ; preds = %52, %22
%54 = load i8*, i8** %5
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %5
%56 = load i8*, i8** %4
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %4
br label %13
58: ; preds = %13
%59 = load i8*, i8** %5
store i8 0, i8* %59
%60 = load i8*, i8** %3
ret i8* %60
}

LLVM IR (-O2):
define noalias i8* @escape(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = shl nsw i32 %2, 1
%4 = or i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
br label %6
6: ; preds = %17, %1
%7 = phi i8* [ %0, %1 ], [ %20, %17 ]
%8 = phi i8* [ %5, %1 ], [ %19, %17 ]
%9 = load i8, i8* %7
switch i8 %9, label %16 [
i8 0, label %21
i8 10, label %10
i8 13, label %12
i8 92, label %14
]
10: ; preds = %6
store i8 92, i8* %8
%11 = getelementptr inbounds i8, i8* %8, i64 1
store i8 110, i8* %11
br label %17
12: ; preds = %6
store i8 92, i8* %8
%13 = getelementptr inbounds i8, i8* %8, i64 1
store i8 114, i8* %13
br label %17
14: ; preds = %6
store i8 92, i8* %8
%15 = getelementptr inbounds i8, i8* %8, i64 1
store i8 92, i8* %15
br label %17
16: ; preds = %6
store i8 %9, i8* %8
br label %17
17: ; preds = %12, %16, %14, %10
%18 = phi i8* [ %11, %10 ], [ %13, %12 ], [ %15, %14 ], [ %8, %16 ]
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = getelementptr inbounds i8, i8* %7, i64 1
br label %6
21: ; preds = %6
store i8 0, i8* %8
ret i8* %5
}