const char *enclose_in_dquotes(const char str[]) {
  static char buf[4096];
  char *p;
  p = buf;
  *p++ = '"';
  while (*str != '\0') {
    if (*str == '\\' || *str == '"')
      *p++ = '\\';
    *p++ = *str;
    str++;
  }
  *p++ = '"';
  *p = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<enclose_in_dquotes>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
lea    0x2f21(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x22,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     118f <enclose_in_dquotes+0x8f>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
je     1155 <enclose_in_dquotes+0x55>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    1167 <enclose_in_dquotes+0x67>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1125 <enclose_in_dquotes+0x25>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x22,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2e81(%rip),%rax
pop    %rbp
ret

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

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

LLVM IR (-O2):
@enclose_in_dquotes.buf = internal global [4096 x i8] zeroinitializer
define i8* @enclose_in_dquotes(i8* nocapture %0) {
store i8 34, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @enclose_in_dquotes.buf, i64 0, i64 0)
br label %2
2: ; preds = %9, %1
%3 = phi i8* [ %0, %1 ], [ %13, %9 ]
%4 = phi i8* [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @enclose_in_dquotes.buf, i64 0, i64 1), %1 ], [ %12, %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* %11, i64 1
store i8 %10, i8* %11
%13 = getelementptr inbounds i8, i8* %3, i64 1
br label %2
14: ; preds = %2
%15 = getelementptr inbounds i8, i8* %4, i64 1
store i8 34, i8* %4
store i8 0, i8* %15
ret i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @enclose_in_dquotes.buf, i64 0, i64 0)
}