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

/* Variables and functions */
int /*<<< orphan*/ checked_write(int, int *, size_t);
void _write_escaped(int fd, char *buf, ssize_t n) {
  uint8_t out[64];
  size_t escaped = 0;
  size_t buffered = 0;
  while (n--) {
    unsigned char c = *buf++;
    if (c == (0x7E) || c == (0x7D)) {
      out[buffered++] = (0x7D);
      c ^= 0x20;
      ++escaped;
      if (buffered >= 64) {
        checked_write(fd, out, buffered);
        buffered = 0;
      }
    }
    out[buffered++] = c;
    if (buffered >= 64) {
      checked_write(fd, out, buffered);
      buffered = 0;
    }
  }
  if (buffered) {
    checked_write(fd, out, buffered);
  }
}


x86 assembly (clang, -O0):
<_write_escaped>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x128(%rbp)
movq   $0x0,-0x130(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     1253 <_write_escaped+0x143>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x131(%rbp)
movzbl -0x131(%rbp),%eax
cmp    $0x7e,%eax
je     1186 <_write_escaped+0x76>
movzbl -0x131(%rbp),%eax
cmp    $0x7d,%eax
jne    11fc <_write_escaped+0xec>
mov    -0x130(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x130(%rbp)
movl   $0x7d,-0x120(%rbp,%rax,4)
movzbl -0x131(%rbp),%eax
xor    $0x20,%eax
mov    %al,-0x131(%rbp)
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
cmpq   $0x40,-0x130(%rbp)
40
jb     11f7 <_write_escaped+0xe7>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x130(%rbp),%rdx
call   1030 <checked_write@plt>
movq   $0x0,-0x130(%rbp)
jmp    11fc <_write_escaped+0xec>
movzbl -0x131(%rbp),%ecx
mov    -0x130(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x130(%rbp)
mov    %ecx,-0x120(%rbp,%rax,4)
cmpq   $0x40,-0x130(%rbp)
40
jb     124e <_write_escaped+0x13e>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x130(%rbp),%rdx
call   1030 <checked_write@plt>
movq   $0x0,-0x130(%rbp)
jmp    113b <_write_escaped+0x2b>
cmpq   $0x0,-0x130(%rbp)
je     1277 <_write_escaped+0x167>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x130(%rbp),%rdx
call   1030 <checked_write@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write_escaped>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
test   %edx,%edx
je     11bf <_write_escaped+0xaf>
mov    %rsi,%r12
mov    %edi,%r15d
mov    %edx,%r13d
xor    %ebx,%ebx
mov    %rsp,%r14
xor    %edx,%edx
jmp    115d <_write_escaped+0x4d>
nopl   0x0(%rax,%rax,1)
mov    %rdx,%rax
movzbl %bpl,%ecx
mov    %ecx,(%rsp,%rdx,4)
add    $0x1,%rdx
cmp    $0x3f,%rax
jae    11a0 <_write_escaped+0x90>
add    $0x1,%rbx
cmp    %ebx,%r13d
je     11af <_write_escaped+0x9f>
movzbl (%r12,%rbx,1),%ebp
lea    -0x7d(%rbp),%eax
cmp    $0x1,%al
ja     1140 <_write_escaped+0x30>
movzbl %bpl,%ebp
movl   $0x7d,(%rsp,%rdx,4)
add    $0x1,%rdx
xor    $0x20,%ebp
cmp    $0x40,%rdx
jb     1140 <_write_escaped+0x30>
mov    %r15d,%edi
mov    %r14,%rsi
call   1030 <checked_write@plt>
mov    %ebp,(%rsp)
mov    $0x1,%edx
jmp    1154 <_write_escaped+0x44>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
call   1030 <checked_write@plt>
xor    %edx,%edx
jmp    1154 <_write_escaped+0x44>
test   %rdx,%rdx
je     11bf <_write_escaped+0xaf>
mov    %rsp,%rsi
mov    %r15d,%edi
call   1030 <checked_write@plt>
add    $0x108,%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 [64 x i32]
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i64 0, i64* %8
store i64 0, i64* %9
br label %11
11: ; preds = %57, %3
%12 = load i32, i32* %6
%13 = add nsw i32 %12, -1
store i32 %13, i32* %6
%14 = icmp ne i32 %12, 0
br i1 %14, label %15, label %58
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %5
%18 = load i8, i8* %16
store i8 %18, i8* %10
%19 = load i8, i8* %10
%20 = zext i8 %19 to i32
%21 = icmp eq i32 %20, 126
br i1 %21, label %26, label %22
22: ; preds = %15
%23 = load i8, i8* %10
%24 = zext i8 %23 to i32
%25 = icmp eq i32 %24, 125
br i1 %25, label %26, label %44
26: ; preds = %22, %15
%27 = load i64, i64* %9
%28 = add i64 %27, 1
store i64 %28, i64* %9
%29 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 %27
store i32 125, i32* %29
%30 = load i8, i8* %10
%31 = zext i8 %30 to i32
%32 = xor i32 %31, 32
%33 = trunc i32 %32 to i8
store i8 %33, i8* %10
%34 = load i64, i64* %8
%35 = add i64 %34, 1
store i64 %35, i64* %8
%36 = load i64, i64* %9
%37 = icmp uge i64 %36, 64
br i1 %37, label %38, label %43
38: ; preds = %26
%39 = load i32, i32* %4
%40 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 0
%41 = load i64, i64* %9
%42 = call i32 @checked_write(i32 %39, i32* %40, i64 %41)
store i64 0, i64* %9
br label %43
43: ; preds = %38, %26
br label %44
44: ; preds = %43, %22
%45 = load i8, i8* %10
%46 = zext i8 %45 to i32
%47 = load i64, i64* %9
%48 = add i64 %47, 1
store i64 %48, i64* %9
%49 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 %47
store i32 %46, i32* %49
%50 = load i64, i64* %9
%51 = icmp uge i64 %50, 64
br i1 %51, label %52, label %57
52: ; preds = %44
%53 = load i32, i32* %4
%54 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 0
%55 = load i64, i64* %9
%56 = call i32 @checked_write(i32 %53, i32* %54, i64 %55)
store i64 0, i64* %9
br label %57
57: ; preds = %52, %44
br label %11
58: ; preds = %11
%59 = load i64, i64* %9
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load i32, i32* %4
%63 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 0
%64 = load i64, i64* %9
%65 = call i32 @checked_write(i32 %62, i32* %63, i64 %64)
br label %66
66: ; preds = %61, %58
ret void
}

LLVM IR (-O2):
define void @_write_escaped(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca [64 x i32]
%5 = bitcast [64 x i32]* %4 to i8*
%6 = icmp eq i32 %2, 0
br i1 %6, label %43, label %7
7: ; preds = %3
%8 = getelementptr inbounds [64 x i32], [64 x i32]* %4, i64 0, i64 0
br label %9
9: ; preds = %7, %35
%10 = phi i32 [ %2, %7 ], [ %13, %35 ]
%11 = phi i64 [ 0, %7 ], [ %36, %35 ]
%12 = phi i8* [ %1, %7 ], [ %14, %35 ]
%13 = add nsw i32 %10, -1
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = load i8, i8* %12
%16 = add i8 %15, -125
%17 = icmp ult i8 %16, 2
br i1 %17, label %18, label %26
18: ; preds = %9
%19 = add i64 %11, 1
%20 = getelementptr inbounds [64 x i32], [64 x i32]* %4, i64 0, i64 %11
store i32 125, i32* %20
%21 = xor i8 %15, 32
%22 = icmp ugt i64 %19, 63
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @checked_write(i32 %0, i32* %8, i64 %19)
%25 = zext i8 %21 to i32
store i32 %25, i32* %8
br label %35
26: ; preds = %18, %9
%27 = phi i64 [ %19, %18 ], [ %11, %9 ]
%28 = phi i8 [ %21, %18 ], [ %15, %9 ]
%29 = zext i8 %28 to i32
%30 = add nuw nsw i64 %27, 1
%31 = getelementptr inbounds [64 x i32], [64 x i32]* %4, i64 0, i64 %27
store i32 %29, i32* %31
%32 = icmp ugt i64 %27, 62
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @checked_write(i32 %0, i32* %8, i64 %30)
br label %35
35: ; preds = %23, %33, %26
%36 = phi i64 [ 0, %33 ], [ %30, %26 ], [ 1, %23 ]
%37 = icmp eq i32 %13, 0
br i1 %37, label %38, label %9
38: ; preds = %35
%39 = icmp eq i64 %36, 0
br i1 %39, label %43, label %40
40: ; preds = %38
%41 = getelementptr inbounds [64 x i32], [64 x i32]* %4, i64 0, i64 0
%42 = call i32 @checked_write(i32 %0, i32* %41, i64 %36)
br label %43
43: ; preds = %3, %40, %38
ret void
}