char *sh_single_quote(string)
char *string;
{
  register int c;
  char *result, *r, *s;
  result = xmalloc(3 + (4 * strlen(string)));
  r = result;
  *r++ = '\'';
  for (s = string; s && (c = *s); s++) {
    *r++ = c;
    if (c == '\'') {
      *r++ = '\\';
      *r++ = '\'';
      *r++ = '\'';
    }
  }
  *r++ = '\'';
  *r = '\0';
  return (result);
}


x86 assembly (clang, -O0):
<sh_single_quote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
shl    $0x2,%rdi
add    $0x3,%rdi
mov    $0x0,%al
call   1040 <xmalloc@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x27,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x29(%rbp)
je     1192 <sh_single_quote+0x72>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11a2 <sh_single_quote+0x82>
jmp    120e <sh_single_quote+0xee>
mov    -0xc(%rbp),%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
cmpl   $0x27,-0xc(%rbp)
jne    11f8 <sh_single_quote+0xd8>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x5c,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x27,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x27,(%rax)
jmp    11fd <sh_single_quote+0xdd>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116f <sh_single_quote+0x4f>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x27,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sh_single_quote>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x3(,%rax,4),%rdi
xor    %eax,%eax
call   1040 <xmalloc@plt>
cltq
lea    0x1(%rax),%rcx
movb   $0x27,(%rax)
test   %rbx,%rbx
je     1186 <sh_single_quote+0x66>
mov    (%rbx),%dl
test   %dl,%dl
je     1186 <sh_single_quote+0x66>
add    $0x1,%rbx
jmp    1179 <sh_single_quote+0x59>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movw   $0x275c,0x1(%rcx)
movb   $0x27,0x3(%rcx)
add    $0x4,%rcx
movzbl (%rbx),%edx
add    $0x1,%rbx
test   %dl,%dl
je     1186 <sh_single_quote+0x66>
mov    %dl,(%rcx)
cmp    $0x27,%dl
je     1160 <sh_single_quote+0x40>
add    $0x1,%rcx
jmp    116e <sh_single_quote+0x4e>
movw   $0x27,(%rcx)
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @sh_single_quote(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = shl i64 %2, 2
%4 = or i64 %3, 3
%5 = tail call i32 (i64, ...) bitcast (i32 (...)* @xmalloc to i32 (i64, ...)*)(i64 %4)
%6 = sext i32 %5 to i64
%7 = inttoptr i64 %6 to i8*
%8 = getelementptr inbounds i8, i8* %7, i64 1
store i8 39, i8* %7
%9 = icmp eq i8* %0, null
br i1 %9, label %28, label %10
10: ; preds = %1
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %28, label %13
13: ; preds = %10, %23
%14 = phi i8 [ %26, %23 ], [ %11, %10 ]
%15 = phi i8* [ %24, %23 ], [ %8, %10 ]
%16 = phi i8* [ %25, %23 ], [ %0, %10 ]
%17 = getelementptr inbounds i8, i8* %15, i64 1
store i8 %14, i8* %15
%18 = icmp eq i8 %14, 39
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = getelementptr inbounds i8, i8* %15, i64 2
store i8 92, i8* %17
%21 = getelementptr inbounds i8, i8* %15, i64 3
store i8 39, i8* %20
%22 = getelementptr inbounds i8, i8* %15, i64 4
store i8 39, i8* %21
br label %23
23: ; preds = %13, %19
%24 = phi i8* [ %22, %19 ], [ %17, %13 ]
%25 = getelementptr inbounds i8, i8* %16, i64 1
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 0
br i1 %27, label %28, label %13
28: ; preds = %23, %10, %1
%29 = phi i8* [ %8, %1 ], [ %8, %10 ], [ %24, %23 ]
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8 39, i8* %29
store i8 0, i8* %30
ret i8* %7
}