const char *escape_sql_str(const char *pstr) {
  static char buf[2048 * 3];
  char *pbuf = buf;
  while (pstr && *pstr) {
    if (*pstr == '\'') {
      *pbuf++ = '\'';
    }
    *pbuf++ = *pstr++;
  }
  *pbuf = 0;
  return buf;
}


x86 assembly (clang, -O0):
<escape_sql_str>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
lea    0x2f21(%rip),%rax
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x11(%rbp)
je     1133 <escape_sql_str+0x33>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1143 <escape_sql_str+0x43>
jmp    118c <escape_sql_str+0x8c>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x27,%eax
jne    1165 <escape_sql_str+0x65>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x27,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    1113 <escape_sql_str+0x13>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2e96(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_sql_str>:
lea    0x2f29(%rip),%rax
test   %rdi,%rdi
jne    1124 <escape_sql_str+0x24>
jmp    1130 <escape_sql_str+0x30>
xchg   %ax,%ax
movb   $0x27,(%rax)
add    $0x1,%rax
movzbl (%rdi),%ecx
add    $0x1,%rdi
mov    %cl,(%rax)
add    $0x1,%rax
movzbl (%rdi),%ecx
cmp    $0x27,%cl
je     1110 <escape_sql_str+0x10>
test   %cl,%cl
jne    111a <escape_sql_str+0x1a>
movb   $0x0,(%rax)
lea    0x2ef6(%rip),%rax
ret

LLVM IR (-O0):
@escape_sql_str.buf = internal global [6144 x i8] zeroinitializer
define i8* @escape_sql_str(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
store i8* getelementptr inbounds ([6144 x i8], [6144 x i8]* @escape_sql_str.buf, i64 0, i64 0), i8** %3
br label %4
4: ; preds = %22, %1
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br label %12
12: ; preds = %7, %4
%13 = phi i1 [ false, %4 ], [ %11, %7 ]
br i1 %13, label %14, label %28
14: ; preds = %12
%15 = load i8*, i8** %2
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 39
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %3
store i8 39, i8* %20
br label %22
22: ; preds = %19, %14
%23 = load i8*, i8** %2
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %2
%25 = load i8, i8* %23
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %3
store i8 %25, i8* %26
br label %4
28: ; preds = %12
%29 = load i8*, i8** %3
store i8 0, i8* %29
ret i8* getelementptr inbounds ([6144 x i8], [6144 x i8]* @escape_sql_str.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@escape_sql_str.buf = internal global [6144 x i8] zeroinitializer
define i8* @escape_sql_str(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1, %10
%4 = phi i8* [ %14, %10 ], [ getelementptr inbounds ([6144 x i8], [6144 x i8]* @escape_sql_str.buf, i64 0, i64 0), %1 ]
%5 = phi i8* [ %13, %10 ], [ %0, %1 ]
%6 = load i8, i8* %5
switch i8 %6, label %10 [
i8 0, label %15
i8 39, label %7
]
7: ; preds = %3
%8 = getelementptr inbounds i8, i8* %4, i64 1
store i8 39, i8* %4
%9 = load i8, i8* %5
br label %10
10: ; preds = %3, %7
%11 = phi i8 [ %9, %7 ], [ %6, %3 ]
%12 = phi i8* [ %8, %7 ], [ %4, %3 ]
%13 = getelementptr inbounds i8, i8* %5, i64 1
%14 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %11, i8* %12
br label %3
15: ; preds = %3, %1
%16 = phi i8* [ getelementptr inbounds ([6144 x i8], [6144 x i8]* @escape_sql_str.buf, i64 0, i64 0), %1 ], [ %4, %3 ]
store i8 0, i8* %16
ret i8* getelementptr inbounds ([6144 x i8], [6144 x i8]* @escape_sql_str.buf, i64 0, i64 0)
}