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


x86 assembly (clang, -O0):
<escape_spaces>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
lea    0x2f21(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     117d <escape_spaces+0x7d>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
je     1143 <escape_spaces+0x43>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    1155 <escape_spaces+0x55>
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    1113 <escape_spaces+0x13>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2ea5(%rip),%rax
pop    %rbp
ret

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

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

LLVM IR (-O2):
@escape_spaces.buf = internal global [4096 x i8] zeroinitializer
define i8* @escape_spaces(i8* nocapture %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]* @escape_spaces.buf, i64 0, i64 0), %1 ], [ %12, %9 ]
%5 = load i8, i8* %3
switch i8 %5, label %9 [
i8 0, label %14
i8 92, label %6
i8 32, 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
store i8 0, i8* %4
ret i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @escape_spaces.buf, i64 0, i64 0)
}