char *quote_string(char *out, char *in) {
  *out++ = '"';
  char c;
  while ((c = *in++) != 0) {
    switch (c) {
    case 0:
      *out++ = '\\';
      *out++ = '0';
      *out++ = '0';
      *out++ = '0';
      break;
    case '"':
      *out++ = '\\';
      *out++ = '"';
      break;
    case '\\':
      *out++ = '\\';
      *out++ = '\\';
      break;
    default:
      *out++ = c;
    }
  }
  *out++ = '"';
  *out = 0;
  return out;
}


x86 assembly (clang, -O0):
<quote_string>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x22,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x11(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     122c <quote_string+0x12c>
movsbl -0x11(%rbp),%eax
mov    %eax,-0x18(%rbp)
test   %eax,%eax
je     1174 <quote_string+0x74>
jmp    1152 <quote_string+0x52>
mov    -0x18(%rbp),%eax
sub    $0x22,%eax
je     11c1 <quote_string+0xc1>
jmp    1163 <quote_string+0x63>
mov    -0x18(%rbp),%eax
sub    $0x5c,%eax
je     11ea <quote_string+0xea>
jmp    1213 <quote_string+0x113>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x30,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x30,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x30,(%rax)
jmp    1227 <quote_string+0x127>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x22,(%rax)
jmp    1227 <quote_string+0x127>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
jmp    1227 <quote_string+0x127>
mov    -0x11(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    111e <quote_string+0x1e>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x22,(%rax)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<quote_string>:
mov    %rdi,%rax
movb   $0x22,(%rdi)
add    $0x1,%rax
mov    (%rsi),%cl
test   %cl,%cl
je     1178 <quote_string+0x78>
add    $0x1,%rsi
jmp    1134 <quote_string+0x34>
cs nopw 0x0(%rax,%rax,1)
movw   $0x225c,(%rax)
add    $0x2,%rax
movzbl (%rsi),%ecx
add    $0x1,%rsi
test   %cl,%cl
je     1178 <quote_string+0x78>
movsbl %cl,%edx
cmp    $0x5c,%edx
je     1160 <quote_string+0x60>
cmp    $0x22,%edx
je     1120 <quote_string+0x20>
test   %edx,%edx
jne    1170 <quote_string+0x70>
movl   $0x3030305c,(%rax)
add    $0x4,%rax
jmp    1129 <quote_string+0x29>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movw   $0x5c5c,(%rax)
jmp    1125 <quote_string+0x25>
nopw   0x0(%rax,%rax,1)
mov    %cl,(%rax)
add    $0x1,%rax
jmp    1129 <quote_string+0x29>
movw   $0x22,(%rax)
add    $0x1,%rax
ret

LLVM IR (-O0):
define i8* @quote_string(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %3
%7 = getelementptr inbounds i8, i8* %6, i32 1
store i8* %7, i8** %3
store i8 34, i8* %6
br label %8
8: ; preds = %40, %2
%9 = load i8*, i8** %4
%10 = getelementptr inbounds i8, i8* %9, i32 1
store i8* %10, i8** %4
%11 = load i8, i8* %9
store i8 %11, i8* %5
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %41
14: ; preds = %8
%15 = load i8, i8* %5
%16 = sext i8 %15 to i32
switch i32 %16, label %36 [
i32 0, label %17
i32 34, label %26
i32 92, label %31
]
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %3
store i8 92, i8* %18
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %3
store i8 48, i8* %20
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %3
store i8 48, i8* %22
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %3
store i8 48, i8* %24
br label %40
26: ; preds = %14
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %3
store i8 92, i8* %27
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %3
store i8 34, i8* %29
br label %40
31: ; preds = %14
%32 = load i8*, i8** %3
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %3
store i8 92, i8* %32
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %3
store i8 92, i8* %34
br label %40
36: ; preds = %14
%37 = load i8, i8* %5
%38 = load i8*, i8** %3
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %3
store i8 %37, i8* %38
br label %40
40: ; preds = %36, %31, %26, %17
br label %8
41: ; preds = %8
%42 = load i8*, i8** %3
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %3
store i8 34, i8* %42
%44 = load i8*, i8** %3
store i8 0, i8* %44
%45 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
define i8* @quote_string(i8* writeonly %0, i8* nocapture %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 1
store i8 34, i8* %0
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %29, label %6
6: ; preds = %2, %25
%7 = phi i8 [ %27, %25 ], [ %4, %2 ]
%8 = phi i8* [ %10, %25 ], [ %1, %2 ]
%9 = phi i8* [ %26, %25 ], [ %3, %2 ]
%10 = getelementptr inbounds i8, i8* %8, i64 1
%11 = sext i8 %7 to i32
switch i32 %11, label %23 [
i32 0, label %12
i32 34, label %17
i32 92, label %20
]
12: ; preds = %6
%13 = getelementptr inbounds i8, i8* %9, i64 1
store i8 92, i8* %9
%14 = getelementptr inbounds i8, i8* %9, i64 2
store i8 48, i8* %13
%15 = getelementptr inbounds i8, i8* %9, i64 3
store i8 48, i8* %14
%16 = getelementptr inbounds i8, i8* %9, i64 4
store i8 48, i8* %15
br label %25
17: ; preds = %6
%18 = getelementptr inbounds i8, i8* %9, i64 1
store i8 92, i8* %9
%19 = getelementptr inbounds i8, i8* %9, i64 2
store i8 34, i8* %18
br label %25
20: ; preds = %6
%21 = getelementptr inbounds i8, i8* %9, i64 1
store i8 92, i8* %9
%22 = getelementptr inbounds i8, i8* %9, i64 2
store i8 92, i8* %21
br label %25
23: ; preds = %6
%24 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %7, i8* %9
br label %25
25: ; preds = %23, %20, %17, %12
%26 = phi i8* [ %24, %23 ], [ %22, %20 ], [ %19, %17 ], [ %16, %12 ]
%27 = load i8, i8* %10
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %6
29: ; preds = %25, %2
%30 = phi i8* [ %3, %2 ], [ %26, %25 ]
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8 34, i8* %30
store i8 0, i8* %31
ret i8* %31
}