#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 */
unsigned char *malloc(int);
int strlen(unsigned char *);
unsigned char *string_escape(unsigned char *in) {
  unsigned char *c;
  unsigned char *out;
  if (!in)
    return NULL;
  out = malloc(strlen(in) * 2 + 2);
  for (c = out; *in; in++) {
    switch (*in) {
    case '\\':
      *c++ = '\\';
      *c++ = '\\';
      break;
    case '\n':
      *c++ = '\\';
      *c++ = 'n';
      break;
    case '\"':
    case '\'':
      *c++ = '\\';
      *c++ = *in;
      break;
    default:
      *c++ = *in;
    }
  }
  *c++ = '\0';
  return out;
}


x86 assembly (clang, -O0):
<string_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1144 <string_escape+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1285 <string_escape+0x165>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
shl    $0x1,%edi
add    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     126b <string_escape+0x14b>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0xa,%eax
je     11e7 <string_escape+0xc7>
jmp    118b <string_escape+0x6b>
mov    -0x24(%rbp),%eax
sub    $0x22,%eax
je     1210 <string_escape+0xf0>
jmp    119c <string_escape+0x7c>
mov    -0x24(%rbp),%eax
sub    $0x27,%eax
je     1210 <string_escape+0xf0>
jmp    11ad <string_escape+0x8d>
mov    -0x24(%rbp),%eax
sub    $0x5c,%eax
jne    123e <string_escape+0x11e>
jmp    11be <string_escape+0x9e>
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   $0x5c,(%rax)
jmp    1255 <string_escape+0x135>
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    1255 <string_escape+0x135>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1255 <string_escape+0x135>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    125a <string_escape+0x13a>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1166 <string_escape+0x46>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_escape>:
test   %rdi,%rdi
je     11a2 <string_escape+0x82>
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    (%rax,%rax,1),%edi
add    $0x2,%edi
call   1040 <malloc@plt>
lea    0xec0(%rip),%rcx
mov    %rax,%rdx
jmp    115f <string_escape+0x3f>
cs nopw 0x0(%rax,%rax,1)
nop
movb   $0x5c,(%rdx)
mov    %sil,0x1(%rdx)
add    $0x2,%rdx
add    $0x1,%rbx
movzbl (%rbx),%esi
cmp    $0x27,%rsi
ja     1180 <string_escape+0x60>
movslq (%rcx,%rsi,4),%rdi
add    %rcx,%rdi
jmp    *%rdi
movw   $0x6e5c,(%rdx)
jmp    1157 <string_escape+0x37>
nopl   0x0(%rax,%rax,1)
cmp    $0x5c,%sil
jne    1190 <string_escape+0x70>
movw   $0x5c5c,(%rdx)
jmp    1157 <string_escape+0x37>
nopl   (%rax)
mov    %sil,(%rdx)
add    $0x1,%rdx
add    $0x1,%rbx
jmp    115f <string_escape+0x3f>
movb   $0x0,(%rdx)
pop    %rbx
ret
xor    %eax,%eax
ret

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

LLVM IR (-O2):
define noalias i8* @string_escape(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %27, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = shl nsw i32 %4, 1
%6 = add nsw i32 %5, 2
%7 = tail call i8* @malloc(i32 %6)
br label %8
8: ; preds = %23, %3
%9 = phi i8* [ %0, %3 ], [ %25, %23 ]
%10 = phi i8* [ %7, %3 ], [ %24, %23 ]
%11 = load i8, i8* %9
switch i8 %11, label %21 [
i8 0, label %26
i8 92, label %12
i8 10, label %15
i8 34, label %18
i8 39, label %18
]
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %10, i64 1
store i8 92, i8* %10
%14 = getelementptr inbounds i8, i8* %10, i64 2
store i8 92, i8* %13
br label %23
15: ; preds = %8
%16 = getelementptr inbounds i8, i8* %10, i64 1
store i8 92, i8* %10
%17 = getelementptr inbounds i8, i8* %10, i64 2
store i8 110, i8* %16
br label %23
18: ; preds = %8, %8
%19 = getelementptr inbounds i8, i8* %10, i64 1
store i8 92, i8* %10
%20 = getelementptr inbounds i8, i8* %10, i64 2
store i8 %11, i8* %19
br label %23
21: ; preds = %8
%22 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %11, i8* %10
br label %23
23: ; preds = %12, %15, %18, %21
%24 = phi i8* [ %22, %21 ], [ %20, %18 ], [ %17, %15 ], [ %14, %12 ]
%25 = getelementptr inbounds i8, i8* %9, i64 1
br label %8
26: ; preds = %8
store i8 0, i8* %10
br label %27
27: ; preds = %1, %26
%28 = phi i8* [ %7, %26 ], [ null, %1 ]
ret i8* %28
}