#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 */
typedef int ssize_t;

/* Variables and functions */
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memmove(char *, char *, size_t);
char *realloc(char *, size_t);
char *strchr(char *, char);
int strlen(char const *);
char *escape(const char *string) {
  size_t len = strlen(string) + 1;
  char *dup = malloc(len);
  memcpy(dup, string, len);
  char *searchptr = dup;
  while (1) {
    char *badchr = strchr(searchptr, '\'');
    if (badchr == NULL)
      break;
    ssize_t diff = searchptr - dup;
    ssize_t pos = badchr - dup;
    len++;
    dup = realloc(dup, len);
    searchptr = dup + diff;
    badchr = dup + pos;
    memmove(badchr + 1, badchr, len - pos - 1);
    searchptr = badchr + 2;
  }
  return dup;
}


x86 assembly (clang, -O0):
<escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x27,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <escape+0x66>
jmp    124e <escape+0xee>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
movslq -0x30(%rbp),%rax
sub    %rax,%rdx
sub    $0x1,%rdx
call   1080 <memmove@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    11a4 <escape+0x44>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
mov    %r14,%rdi
mov    $0x27,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11fc <escape+0x9c>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
sub    %r14d,%ebx
lea    0x1(%r15),%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <realloc@plt>
mov    %rax,%r14
movslq %ebx,%rbx
lea    (%rax,%rbx,1),%rsi
lea    (%rax,%rbx,1),%rdi
add    $0x1,%rdi
sub    %rbx,%r15
mov    %r15,%rdx
call   1080 <memmove@plt>
lea    (%r14,%rbx,1),%rdi
add    $0x2,%rdi
mov    $0x27,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
mov    %r12,%r15
test   %rax,%rax
jne    11b0 <escape+0x50>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @escape(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = load i8*, i8** %2
%17 = load i64, i64* %3
%18 = call i32 @memcpy(i8* %15, i8* %16, i64 %17)
%19 = load i8*, i8** %4
store i8* %19, i8** %5
br label %20
20: ; preds = %1, %26
%21 = load i8*, i8** %5
%22 = call i8* @strchr(i8* %21, i8 signext 39)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20
br label %63
26: ; preds = %20
%27 = load i8*, i8** %5
%28 = load i8*, i8** %4
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = trunc i64 %31 to i32
store i32 %32, i32* %7
%33 = load i8*, i8** %6
%34 = load i8*, i8** %4
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = trunc i64 %37 to i32
store i32 %38, i32* %8
%39 = load i64, i64* %3
%40 = add i64 %39, 1
store i64 %40, i64* %3
%41 = load i8*, i8** %4
%42 = load i64, i64* %3
%43 = call i8* @realloc(i8* %41, i64 %42)
store i8* %43, i8** %4
%44 = load i8*, i8** %4
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8* %47, i8** %5
%48 = load i8*, i8** %4
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8* %51, i8** %6
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = load i8*, i8** %6
%55 = load i64, i64* %3
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = sub i64 %55, %57
%59 = sub i64 %58, 1
%60 = call i32 @memmove(i8* %53, i8* %54, i64 %59)
%61 = load i8*, i8** %6
%62 = getelementptr inbounds i8, i8* %61, i64 2
store i8* %62, i8** %5
br label %20
63: ; preds = %25
%64 = load i8*, i8** %4
ret i8* %64
}

LLVM IR (-O2):
define i8* @escape(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = sext i32 %3 to i64
%5 = tail call i8* @malloc(i64 %4)
%6 = tail call i32 @memcpy(i8* %5, i8* %0, i64 %4)
%7 = tail call i8* @strchr(i8* dereferenceable(1) %5, i8 signext 39)
%8 = icmp eq i8* %7, null
br i1 %8, label %27, label %9
9: ; preds = %1, %9
%10 = phi i8* [ %25, %9 ], [ %7, %1 ]
%11 = phi i64 [ %16, %9 ], [ %4, %1 ]
%12 = phi i8* [ %17, %9 ], [ %5, %1 ]
%13 = ptrtoint i8* %12 to i64
%14 = ptrtoint i8* %10 to i64
%15 = sub i64 %14, %13
%16 = add i64 %11, 1
%17 = tail call i8* @realloc(i8* %12, i64 %16)
%18 = shl i64 %15, 32
%19 = ashr exact i64 %18, 32
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = sub i64 %11, %19
%23 = tail call i32 @memmove(i8* %21, i8* %20, i64 %22)
%24 = getelementptr inbounds i8, i8* %20, i64 2
%25 = tail call i8* @strchr(i8* dereferenceable(1) %24, i8 signext 39)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %9
27: ; preds = %9, %1
%28 = phi i8* [ %5, %1 ], [ %17, %9 ]
ret i8* %28
}