#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 */
typedef char uint8_t;
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ write(int, char *, int);
void _write_escaped(int fd, char *buf, ssize_t n) {
  uint8_t out[2];
  size_t escaped = 0;
  while (n--) {
    char c = *buf++;
    switch (c) {
    case (0x7E):
      out[0] = (0x7D);
      out[1] = ((0x7E) ^ 0x20);
      write(fd, out, 2);
      escaped++;
      break;
    case (0x7D):
      out[0] = (0x7D);
      out[1] = ((0x7D) ^ 0x20);
      write(fd, out, 2);
      escaped++;
      break;
    default:
      write(fd, &c, 1);
    }
  }
}


x86 assembly (clang, -O0):
<_write_escaped>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     11e2 <_write_escaped+0xd2>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x7d,%eax
je     11a2 <_write_escaped+0x92>
jmp    1167 <_write_escaped+0x57>
mov    -0x28(%rbp),%eax
sub    $0x7e,%eax
jne    11cc <_write_escaped+0xbc>
jmp    1178 <_write_escaped+0x68>
movb   $0x7d,-0x16(%rbp)
movb   $0x5e,-0x15(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11dd <_write_escaped+0xcd>
movb   $0x7d,-0x16(%rbp)
movb   $0x5d,-0x15(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11dd <_write_escaped+0xcd>
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    112a <_write_escaped+0x1a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_escaped>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     1194 <_write_escaped+0x84>
mov    %rsi,%r12
mov    %edi,%r13d
mov    %edx,%ebx
xor    %ebp,%ebp
lea    0x6(%rsp),%r14
lea    0x5(%rsp),%r15
jmp    1158 <_write_escaped+0x48>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0x1,%rbp
cmp    %ebp,%ebx
je     1194 <_write_escaped+0x84>
movsbl (%r12,%rbp,1),%eax
mov    %al,0x5(%rsp)
cmp    $0x7d,%eax
je     1180 <_write_escaped+0x70>
cmp    $0x7e,%eax
jne    1140 <_write_escaped+0x30>
movw   $0x5e7d,0x6(%rsp)
jmp    1187 <_write_escaped+0x77>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movw   $0x5d7d,0x6(%rsp)
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x2,%edx
jmp    114b <_write_escaped+0x3b>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @_write_escaped(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [2 x i8]
%8 = alloca i64
%9 = alloca i8
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i64 0, i64* %8
br label %10
10: ; preds = %39, %3
%11 = load i32, i32* %6
%12 = add nsw i32 %11, -1
store i32 %12, i32* %6
%13 = icmp ne i32 %11, 0
br i1 %13, label %14, label %40
14: ; preds = %10
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %5
%17 = load i8, i8* %15
store i8 %17, i8* %9
%18 = load i8, i8* %9
%19 = sext i8 %18 to i32
switch i32 %19, label %36 [
i32 126, label %20
i32 125, label %28
]
20: ; preds = %14
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
store i8 125, i8* %21
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
store i8 94, i8* %22
%23 = load i32, i32* %4
%24 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%25 = call i32 @write(i32 %23, i8* %24, i32 2)
%26 = load i64, i64* %8
%27 = add i64 %26, 1
store i64 %27, i64* %8
br label %39
28: ; preds = %14
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
store i8 125, i8* %29
%30 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
store i8 93, i8* %30
%31 = load i32, i32* %4
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%33 = call i32 @write(i32 %31, i8* %32, i32 2)
%34 = load i64, i64* %8
%35 = add i64 %34, 1
store i64 %35, i64* %8
br label %39
36: ; preds = %14
%37 = load i32, i32* %4
%38 = call i32 @write(i32 %37, i8* %9, i32 1)
br label %39
39: ; preds = %36, %28, %20
br label %10
40: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @_write_escaped(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca [2 x i8]
%5 = alloca i8
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %2, 0
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
br label %10
10: ; preds = %8, %23
%11 = phi i32 [ %2, %8 ], [ %13, %23 ]
%12 = phi i8* [ %1, %8 ], [ %14, %23 ]
%13 = add nsw i32 %11, -1
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = load i8, i8* %12
store i8 %15, i8* %5
%16 = sext i8 %15 to i32
switch i32 %16, label %21 [
i32 126, label %17
i32 125, label %19
]
17: ; preds = %10
store i8 125, i8* %6
store i8 94, i8* %9
%18 = call i32 @write(i32 %0, i8* %6, i32 2)
br label %23
19: ; preds = %10
store i8 125, i8* %6
store i8 93, i8* %9
%20 = call i32 @write(i32 %0, i8* %6, i32 2)
br label %23
21: ; preds = %10
%22 = call i32 @write(i32 %0, i8* %5, i32 1)
br label %23
23: ; preds = %21, %19, %17
%24 = icmp eq i32 %13, 0
br i1 %24, label %25, label %10
25: ; preds = %23, %3
ret void
}