#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ strchr(char *, char const);
int escape_filename(char *dst, int len, const char *src) {
  static char *naughty_chars = " \\\"'@<>=;|&()#$`?*[!:{";
  if (len > 0) {
    while (len > 1 && *src) {
      if (strchr(naughty_chars, *src)) {
        if (len == 2)
          break;
        *dst++ = '\\';
        len--;
      }
      *dst++ = *src++;
      len--;
    }
    *dst = 0;
  }
  return (*src == 0) ? 0 : -1;
}


x86 assembly (clang, -O0):
<escape_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11df <escape_filename+0xcf>
jmp    1132 <escape_filename+0x22>
xor    %eax,%eax
cmpl   $0x1,-0xc(%rbp)
mov    %al,-0x19(%rbp)
jle    1151 <escape_filename+0x41>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1161 <escape_filename+0x51>
jmp    11d8 <escape_filename+0xc8>
mov    0x2ec0(%rip),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11a8 <escape_filename+0x98>
cmpl   $0x2,-0xc(%rbp)
jne    118d <escape_filename+0x7d>
jmp    11d8 <escape_filename+0xc8>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1132 <escape_filename+0x22>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_filename>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %esi,%esi
jle    112d <escape_filename+0x1d>
mov    %esi,%r15d
mov    %rdi,%rbx
cmp    $0x1,%esi
jne    1140 <escape_filename+0x30>
movb   $0x0,(%rbx)
xor    %eax,%eax
cmp    (%r14),%al
sbb    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xeb9(%rip),%r12
jmp    116b <escape_filename+0x5b>
nopl   0x0(%rax)
movzbl (%r14),%eax
add    $0x1,%r14
mov    %al,(%rbx)
add    $0x1,%rbx
lea    -0x1(%r15),%eax
cmp    $0x2,%r15d
mov    %eax,%r15d
jle    112a <escape_filename+0x1a>
movzbl (%r14),%eax
test   %al,%al
je     112a <escape_filename+0x1a>
movsbl %al,%esi
mov    %r12,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     1150 <escape_filename+0x40>
cmp    $0x2,%r15d
je     112a <escape_filename+0x1a>
movb   $0x5c,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%r15d
jmp    1150 <escape_filename+0x40>

LLVM IR (-O0):
@escape_filename.naughty_chars = internal global i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i32 0, i32 0)
@.str = [23 x i8] c" \\\22'@<>=;|&()#$`?*[!:{\00"
define i32 @escape_filename(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%7 = load i32, i32* %5
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %45
9: ; preds = %3
br label %10
10: ; preds = %35, %9
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i8*, i8** %6
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br label %18
18: ; preds = %13, %10
%19 = phi i1 [ false, %10 ], [ %17, %13 ]
br i1 %19, label %20, label %43
20: ; preds = %18
%21 = load i8*, i8** @escape_filename.naughty_chars
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = call i64 @strchr(i8* %21, i8 signext %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %20
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, 2
br i1 %28, label %29, label %30
29: ; preds = %26
br label %43
30: ; preds = %26
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
store i8 92, i8* %31
%33 = load i32, i32* %5
%34 = add nsw i32 %33, -1
store i32 %34, i32* %5
br label %35
35: ; preds = %30, %20
%36 = load i8*, i8** %6
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %6
%38 = load i8, i8* %36
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %4
store i8 %38, i8* %39
%41 = load i32, i32* %5
%42 = add nsw i32 %41, -1
store i32 %42, i32* %5
br label %10
43: ; preds = %29, %18
%44 = load i8*, i8** %4
store i8 0, i8* %44
br label %45
45: ; preds = %43, %3
%46 = load i8*, i8** %6
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 0
%50 = zext i1 %49 to i64
%51 = select i1 %49, i32 0, i32 -1
ret i32 %51
}

LLVM IR (-O2):
@.str = [23 x i8] c" \\\22'@<>=;|&()#$`?*[!:{\00"
define i32 @escape_filename(i8* nocapture writeonly %0, i32 %1, i8* nocapture %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %32
5: ; preds = %3
%6 = icmp eq i32 %1, 1
br i1 %6, label %29, label %7
7: ; preds = %5, %21
%8 = phi i8* [ %24, %21 ], [ %2, %5 ]
%9 = phi i32 [ %27, %21 ], [ %1, %5 ]
%10 = phi i8* [ %26, %21 ], [ %0, %5 ]
%11 = load i8, i8* %8
%12 = icmp eq i8 %11, 0
br i1 %12, label %29, label %13
13: ; preds = %7
%14 = tail call i64 @strchr(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 signext %11)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = icmp eq i32 %9, 2
br i1 %17, label %29, label %18
18: ; preds = %16
%19 = getelementptr inbounds i8, i8* %10, i64 1
store i8 92, i8* %10
%20 = add nsw i32 %9, -1
br label %21
21: ; preds = %18, %13
%22 = phi i8* [ %19, %18 ], [ %10, %13 ]
%23 = phi i32 [ %20, %18 ], [ %9, %13 ]
%24 = getelementptr inbounds i8, i8* %8, i64 1
%25 = load i8, i8* %8
%26 = getelementptr inbounds i8, i8* %22, i64 1
store i8 %25, i8* %22
%27 = add nsw i32 %23, -1
%28 = icmp sgt i32 %23, 2
br i1 %28, label %7, label %29
29: ; preds = %7, %16, %21, %5
%30 = phi i8* [ %0, %5 ], [ %26, %21 ], [ %10, %16 ], [ %10, %7 ]
%31 = phi i8* [ %2, %5 ], [ %24, %21 ], [ %8, %16 ], [ %8, %7 ]
store i8 0, i8* %30
br label %32
32: ; preds = %29, %3
%33 = phi i8* [ %31, %29 ], [ %2, %3 ]
%34 = load i8, i8* %33
%35 = icmp ne i8 %34, 0
%36 = sext i1 %35 to i32
ret i32 %36
}