char *quote(char *s) {
  static char buf[128];
  char *p;
  p = buf;
  while (*s) {
    if (*s == '\\' || *s == '\"')
      *p++ = '\\';
    *p++ = *s++;
  }
  *p = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<quote>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
lea    0x2f21(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1179 <quote+0x79>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
je     1140 <quote+0x40>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    1152 <quote+0x52>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x5c,(%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 <quote+0x13>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2ea9(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<quote>:
lea    0x2f29(%rip),%rax
jmp    1124 <quote+0x24>
nopl   0x0(%rax)
movb   $0x5c,(%rax)
add    $0x1,%rax
movzbl (%rdi),%ecx
add    $0x1,%rdi
mov    %cl,(%rax)
add    $0x1,%rax
movzbl (%rdi),%ecx
cmp    $0x22,%cl
je     1110 <quote+0x10>
cmp    $0x5c,%cl
je     1110 <quote+0x10>
test   %cl,%cl
jne    111a <quote+0x1a>
movb   $0x0,(%rax)
lea    0x2ef1(%rip),%rax
ret

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

LLVM IR (-O2):
@quote.buf = internal global [128 x i8] zeroinitializer
define i8* @quote(i8* nocapture %0) {
br label %2
2: ; preds = %9, %1
%3 = phi i8* [ %0, %1 ], [ %12, %9 ]
%4 = phi i8* [ getelementptr inbounds ([128 x i8], [128 x i8]* @quote.buf, i64 0, i64 0), %1 ], [ %13, %9 ]
%5 = load i8, i8* %3
switch i8 %5, label %9 [
i8 0, label %14
i8 92, label %6
i8 34, label %6
]
6: ; preds = %2, %2
%7 = getelementptr inbounds i8, i8* %4, i64 1
store i8 92, i8* %4
%8 = load i8, i8* %3
br label %9
9: ; preds = %2, %6
%10 = phi i8 [ %8, %6 ], [ %5, %2 ]
%11 = phi i8* [ %7, %6 ], [ %4, %2 ]
%12 = getelementptr inbounds i8, i8* %3, i64 1
%13 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %10, i8* %11
br label %2
14: ; preds = %2
store i8 0, i8* %4
ret i8* getelementptr inbounds ([128 x i8], [128 x i8]* @quote.buf, i64 0, i64 0)
}