#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 */
int /*<<< orphan*/ printf(char *, char *);
int escape_single_quotes(char *to, char *from, int max) {
  int l = 0;
  printf("original : %s\n", from);
  for (; *from; from++) {
    switch (*from) {
    case '\'':
      if (l >= max - 4)
        return 0;
      *(to++) = '\'';
      *(to++) = '\\';
      *(to++) = '\'';
      l += 3;
    default:
      if (l >= max - 1)
        return 0;
      *(to++) = *from;
      l++;
    }
  }
  *to = 0;
  return 1;
}


x86 assembly (clang, -O0):
<escape_single_quotes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xecb(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1209 <escape_single_quotes+0xf9>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
sub    $0x27,%al
jne    11b6 <escape_single_quotes+0xa6>
jmp    115a <escape_single_quotes+0x4a>
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x4,%ecx
cmp    %ecx,%eax
jl     1177 <escape_single_quotes+0x67>
movl   $0x0,-0x4(%rbp)
jmp    1217 <escape_single_quotes+0x107>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x27,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x5c,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x27,(%rax)
mov    -0x20(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     11d3 <escape_single_quotes+0xc3>
movl   $0x0,-0x4(%rbp)
jmp    1217 <escape_single_quotes+0x107>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11f8 <escape_single_quotes+0xe8>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    113a <escape_single_quotes+0x2a>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_single_quotes>:
push   %r15
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xedb(%rip),%rdi
call   1030 <printf@plt>
lea    -0x4(%r14),%eax
add    $0xffffffff,%r14d
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%r15),%edx
cmp    $0x27,%dl
je     1160 <escape_single_quotes+0x50>
test   %dl,%dl
je     1190 <escape_single_quotes+0x80>
cmp    %r14d,%ecx
jl     1179 <escape_single_quotes+0x69>
jmp    118c <escape_single_quotes+0x7c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %eax,%ecx
jge    118c <escape_single_quotes+0x7c>
movw   $0x5c27,(%rbx)
movb   $0x27,0x2(%rbx)
add    $0x3,%rbx
add    $0x3,%ecx
cmp    %r14d,%ecx
jge    118c <escape_single_quotes+0x7c>
movzbl (%r15),%edx
mov    %dl,(%rbx)
add    $0x1,%rbx
add    $0x1,%ecx
add    $0x1,%r15
jmp    1140 <escape_single_quotes+0x30>
xor    %eax,%eax
jmp    1198 <escape_single_quotes+0x88>
movb   $0x0,(%rbx)
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [15 x i8] c"original : %s\0A\00"
define i32 @escape_single_quotes(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%9 = load i8*, i8** %6
%10 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %11
11: ; preds = %48, %3
%12 = load i8*, i8** %6
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %51
15: ; preds = %11
%16 = load i8*, i8** %6
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
switch i32 %18, label %34 [
i32 39, label %19
]
19: ; preds = %15
%20 = load i32, i32* %8
%21 = load i32, i32* %7
%22 = sub nsw i32 %21, 4
%23 = icmp sge i32 %20, %22
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 0, i32* %4
br label %53
25: ; preds = %19
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
store i8 39, i8* %26
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
store i8 92, i8* %28
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %5
store i8 39, i8* %30
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 3
store i32 %33, i32* %8
br label %34
34: ; preds = %15, %25
%35 = load i32, i32* %8
%36 = load i32, i32* %7
%37 = sub nsw i32 %36, 1
%38 = icmp sge i32 %35, %37
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 0, i32* %4
br label %53
40: ; preds = %34
%41 = load i8*, i8** %6
%42 = load i8, i8* %41
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %5
store i8 %42, i8* %43
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %47
47: ; preds = %40
br label %48
48: ; preds = %47
%49 = load i8*, i8** %6
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %6
br label %11
51: ; preds = %11
%52 = load i8*, i8** %5
store i8 0, i8* %52
store i32 1, i32* %4
br label %53
53: ; preds = %51, %39, %24
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@.str = [15 x i8] c"original : %s\0A\00"
define i32 @escape_single_quotes(i8* nocapture writeonly %0, i8* %1, i32 %2) {
%4 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %1)
%5 = add nsw i32 %2, -4
%6 = add nsw i32 %2, -1
br label %7
7: ; preds = %23, %3
%8 = phi i8* [ %0, %3 ], [ %25, %23 ]
%9 = phi i8* [ %1, %3 ], [ %27, %23 ]
%10 = phi i32 [ 0, %3 ], [ %26, %23 ]
%11 = load i8, i8* %9
switch i8 %11, label %19 [
i8 0, label %28
i8 39, label %12
]
12: ; preds = %7
%13 = icmp slt i32 %10, %5
br i1 %13, label %14, label %29
14: ; preds = %12
%15 = getelementptr inbounds i8, i8* %8, i64 1
store i8 39, i8* %8
%16 = getelementptr inbounds i8, i8* %8, i64 2
store i8 92, i8* %15
%17 = getelementptr inbounds i8, i8* %8, i64 3
store i8 39, i8* %16
%18 = add nsw i32 %10, 3
br label %19
19: ; preds = %7, %14
%20 = phi i8* [ %17, %14 ], [ %8, %7 ]
%21 = phi i32 [ %18, %14 ], [ %10, %7 ]
%22 = icmp slt i32 %21, %6
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i8, i8* %9
%25 = getelementptr inbounds i8, i8* %20, i64 1
store i8 %24, i8* %20
%26 = add nsw i32 %21, 1
%27 = getelementptr inbounds i8, i8* %9, i64 1
br label %7
28: ; preds = %7
store i8 0, i8* %8
br label %29
29: ; preds = %19, %12, %28
%30 = phi i32 [ 1, %28 ], [ 0, %12 ], [ 0, %19 ]
ret i32 %30
}