#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 */
struct stream {
  int dummy;
};
typedef int ssize_t;

/* Variables and functions */
size_t strcspn(char const *, char *);
int stream_write(struct stream *, char const *, int);
int proto_escape(struct stream *wr, const char *s) {
  size_t len;
  ssize_t n;
  char c;
  do {
    len = strcspn(s, " \t\r\n\\");
    n = stream_write(wr, s, len);
    if (n == -1)
      return (-1);
    c = s[len];
    switch (c) {
    case ' ':
      n = stream_write(wr, "\\_", 2);
      break;
    case '\t':
      n = stream_write(wr, "\\t", 2);
      break;
    case '\r':
      n = stream_write(wr, "\\r", 2);
      break;
    case '\n':
      n = stream_write(wr, "\\n", 2);
      break;
    case '\\':
      n = stream_write(wr, "\\\\", 2);
      break;
    }
    if (n == -1)
      return (-1);
    s += len + 1;
  } while (c != '\0');
  return (0);
}


x86 assembly (clang, -O0):
<proto_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xec5(%rip),%rsi
call   1030 <strcspn@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
call   1040 <stream_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1170 <proto_escape+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <proto_escape+0x17d>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x25(%rbp)
movsbl -0x25(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x9,%eax
je     11f4 <proto_escape+0xd4>
jmp    1193 <proto_escape+0x73>
mov    -0x2c(%rbp),%eax
sub    $0xa,%eax
je     122e <proto_escape+0x10e>
jmp    11a4 <proto_escape+0x84>
mov    -0x2c(%rbp),%eax
sub    $0xd,%eax
je     1211 <proto_escape+0xf1>
jmp    11b5 <proto_escape+0x95>
mov    -0x2c(%rbp),%eax
sub    $0x20,%eax
je     11d7 <proto_escape+0xb7>
jmp    11c6 <proto_escape+0xa6>
mov    -0x2c(%rbp),%eax
sub    $0x5c,%eax
je     124b <proto_escape+0x12b>
jmp    1263 <proto_escape+0x143>
mov    -0x10(%rbp),%rdi
lea    0xe24(%rip),%rsi
mov    $0x2,%edx
call   1040 <stream_write@plt>
mov    %eax,-0x24(%rbp)
jmp    1263 <proto_escape+0x143>
mov    -0x10(%rbp),%rdi
lea    0xe0a(%rip),%rsi
mov    $0x2,%edx
call   1040 <stream_write@plt>
mov    %eax,-0x24(%rbp)
jmp    1263 <proto_escape+0x143>
mov    -0x10(%rbp),%rdi
lea    0xdf0(%rip),%rsi
mov    $0x2,%edx
call   1040 <stream_write@plt>
mov    %eax,-0x24(%rbp)
jmp    1263 <proto_escape+0x143>
mov    -0x10(%rbp),%rdi
lea    0xdd6(%rip),%rsi
mov    $0x2,%edx
call   1040 <stream_write@plt>
mov    %eax,-0x24(%rbp)
jmp    1263 <proto_escape+0x143>
mov    -0x10(%rbp),%rdi
lea    0xdbc(%rip),%rsi
mov    $0x2,%edx
call   1040 <stream_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1279 <proto_escape+0x159>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <proto_escape+0x17d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
movsbl -0x25(%rbp),%eax
cmp    $0x0,%eax
jne    1130 <proto_escape+0x10>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proto_escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xec8(%rip),%r13
mov    $0xffffffff,%ebp
jmp    1168 <proto_escape+0x48>
lea    0xf29(%rip),%rsi
mov    %r14,%rdi
mov    $0x2,%edx
call   1040 <stream_write@plt>
cmp    $0xffffffff,%eax
je     11df <proto_escape+0xbf>
add    %r12,%rbx
add    $0x1,%rbx
test   %r15b,%r15b
je     11dd <proto_escape+0xbd>
mov    %rbx,%rdi
lea    0xeee(%rip),%rsi
call   1030 <strcspn@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <stream_write@plt>
cmp    $0xffffffff,%eax
je     11df <proto_escape+0xbf>
movzbl (%rbx,%r12,1),%r15d
movsbl %r15b,%eax
lea    -0x9(%rax),%ecx
cmp    $0x17,%ecx
ja     11c0 <proto_escape+0xa0>
movslq 0x0(%r13,%rcx,4),%rax
add    %r13,%rax
lea    0xeb9(%rip),%rsi
jmp    *%rax
lea    0xeb3(%rip),%rsi
jmp    1146 <proto_escape+0x26>
nopl   0x0(%rax,%rax,1)
cmp    $0x5c,%eax
jne    115c <proto_escape+0x3c>
lea    0xea6(%rip),%rsi
jmp    1146 <proto_escape+0x26>
lea    0xe94(%rip),%rsi
jmp    1146 <proto_escape+0x26>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stream = type { i32 }
@.str = [6 x i8] c" \09\0D\0A\\\00"
@.str.1 = [3 x i8] c"\\_\00"
@.str.2 = [3 x i8] c"\\t\00"
@.str.3 = [3 x i8] c"\\r\00"
@.str.4 = [3 x i8] c"\\n\00"
@.str.5 = [3 x i8] c"\\\\\00"
define i32 @proto_escape(%struct.stream* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.stream*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8
store %struct.stream* %0, %struct.stream** %4
store i8* %1, i8** %5
br label %9
9: ; preds = %51, %2
%10 = load i8*, i8** %5
%11 = call i64 @strcspn(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i64 %11, i64* %6
%12 = load %struct.stream*, %struct.stream** %4
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = trunc i64 %14 to i32
%16 = call i32 @stream_write(%struct.stream* %12, i8* %13, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %9
store i32 -1, i32* %3
br label %56
20: ; preds = %9
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i8, i8* %23
store i8 %24, i8* %8
%25 = load i8, i8* %8
%26 = sext i8 %25 to i32
switch i32 %26, label %42 [
i32 32, label %27
i32 9, label %30
i32 13, label %33
i32 10, label %36
i32 92, label %39
]
27: ; preds = %20
%28 = load %struct.stream*, %struct.stream** %4
%29 = call i32 @stream_write(%struct.stream* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
store i32 %29, i32* %7
br label %42
30: ; preds = %20
%31 = load %struct.stream*, %struct.stream** %4
%32 = call i32 @stream_write(%struct.stream* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
store i32 %32, i32* %7
br label %42
33: ; preds = %20
%34 = load %struct.stream*, %struct.stream** %4
%35 = call i32 @stream_write(%struct.stream* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
store i32 %35, i32* %7
br label %42
36: ; preds = %20
%37 = load %struct.stream*, %struct.stream** %4
%38 = call i32 @stream_write(%struct.stream* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
store i32 %38, i32* %7
br label %42
39: ; preds = %20
%40 = load %struct.stream*, %struct.stream** %4
%41 = call i32 @stream_write(%struct.stream* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
store i32 %41, i32* %7
br label %42
42: ; preds = %20, %39, %36, %33, %30, %27
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 -1, i32* %3
br label %56
46: ; preds = %42
%47 = load i64, i64* %6
%48 = add i64 %47, 1
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i64 %48
store i8* %50, i8** %5
br label %51
51: ; preds = %46
%52 = load i8, i8* %8
%53 = sext i8 %52 to i32
%54 = icmp ne i32 %53, 0
br i1 %54, label %9, label %55
55: ; preds = %51
store i32 0, i32* %3
br label %56
56: ; preds = %55, %45, %19
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
%struct.stream = type { i32 }
@.str = [6 x i8] c" \09\0D\0A\\\00"
@.str.1 = [3 x i8] c"\\_\00"
@.str.2 = [3 x i8] c"\\t\00"
@.str.3 = [3 x i8] c"\\r\00"
@.str.4 = [3 x i8] c"\\n\00"
@.str.5 = [3 x i8] c"\\\\\00"
define i32 @proto_escape(%struct.stream* %0, i8* %1) {
br label %3
3: ; preds = %21, %2
%4 = phi i8* [ %1, %2 ], [ %23, %21 ]
%5 = tail call i64 @strcspn(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = trunc i64 %5 to i32
%7 = tail call i32 @stream_write(%struct.stream* %0, i8* %4, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %25, label %9
9: ; preds = %3
%10 = getelementptr inbounds i8, i8* %4, i64 %5
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
switch i32 %12, label %21 [
i32 32, label %17
i32 9, label %13
i32 13, label %14
i32 10, label %15
i32 92, label %16
]
13: ; preds = %9
br label %17
14: ; preds = %9
br label %17
15: ; preds = %9
br label %17
16: ; preds = %9
br label %17
17: ; preds = %9, %16, %15, %14, %13
%18 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), %16 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), %15 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), %14 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %13 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %9 ]
%19 = tail call i32 @stream_write(%struct.stream* %0, i8* %18, i32 2)
%20 = icmp eq i32 %19, -1
br i1 %20, label %25, label %21
21: ; preds = %9, %17
%22 = add i64 %5, 1
%23 = getelementptr inbounds i8, i8* %4, i64 %22
%24 = icmp eq i8 %11, 0
br i1 %24, label %25, label %3
25: ; preds = %21, %17, %3
%26 = phi i32 [ -1, %3 ], [ -1, %17 ], [ 0, %21 ]
ret i32 %26
}