#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 buffered = 0;
  while (n--) {
    unsigned char c = *buf++;
    if (c == (0x7E) || c == (0x7D)) {
      out[buffered++] = (0x7D);
      c ^= 0x20;
      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    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x128(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     1236 <_write_escaped+0x126>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x129(%rbp)
movzbl -0x129(%rbp),%eax
cmp    $0x7e,%eax
je     117b <_write_escaped+0x6b>
movzbl -0x129(%rbp),%eax
cmp    $0x7d,%eax
jne    11df <_write_escaped+0xcf>
mov    -0x128(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x128(%rbp)
movl   $0x7d,-0x120(%rbp,%rax,4)
movzbl -0x129(%rbp),%eax
xor    $0x20,%eax
mov    %al,-0x129(%rbp)
cmpq   $0x40,-0x128(%rbp)
40
jb     11da <_write_escaped+0xca>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%rdx
call   1030 <checked_write@plt>
movq   $0x0,-0x128(%rbp)
jmp    11df <_write_escaped+0xcf>
movzbl -0x129(%rbp),%ecx
mov    -0x128(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x128(%rbp)
mov    %ecx,-0x120(%rbp,%rax,4)
cmpq   $0x40,-0x128(%rbp)
40
jb     1231 <_write_escaped+0x121>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%rdx
call   1030 <checked_write@plt>
movq   $0x0,-0x128(%rbp)
jmp    1130 <_write_escaped+0x20>
cmpq   $0x0,-0x128(%rbp)
je     125a <_write_escaped+0x14a>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%rdx
call   1030 <checked_write@plt>
add    $0x130,%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 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 = %54, %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 %55
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 = zext i8 %18 to i32
%20 = icmp eq i32 %19, 126
br i1 %20, label %25, label %21
21: ; preds = %14
%22 = load i8, i8* %9
%23 = zext i8 %22 to i32
%24 = icmp eq i32 %23, 125
br i1 %24, label %25, label %41
25: ; preds = %21, %14
%26 = load i64, i64* %8
%27 = add i64 %26, 1
store i64 %27, i64* %8
%28 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 %26
store i32 125, i32* %28
%29 = load i8, i8* %9
%30 = zext i8 %29 to i32
%31 = xor i32 %30, 32
%32 = trunc i32 %31 to i8
store i8 %32, i8* %9
%33 = load i64, i64* %8
%34 = icmp uge i64 %33, 64
br i1 %34, label %35, label %40
35: ; preds = %25
%36 = load i32, i32* %4
%37 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 0
%38 = load i64, i64* %8
%39 = call i32 @checked_write(i32 %36, i32* %37, i64 %38)
store i64 0, i64* %8
br label %40
40: ; preds = %35, %25
br label %41
41: ; preds = %40, %21
%42 = load i8, i8* %9
%43 = zext i8 %42 to i32
%44 = load i64, i64* %8
%45 = add i64 %44, 1
store i64 %45, i64* %8
%46 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 %44
store i32 %43, i32* %46
%47 = load i64, i64* %8
%48 = icmp uge i64 %47, 64
br i1 %48, label %49, label %54
49: ; preds = %41
%50 = load i32, i32* %4
%51 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 0
%52 = load i64, i64* %8
%53 = call i32 @checked_write(i32 %50, i32* %51, i64 %52)
store i64 0, i64* %8
br label %54
54: ; preds = %49, %41
br label %10
55: ; preds = %10
%56 = load i64, i64* %8
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %55
%59 = load i32, i32* %4
%60 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 0
%61 = load i64, i64* %8
%62 = call i32 @checked_write(i32 %59, i32* %60, i64 %61)
br label %63
63: ; preds = %58, %55
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
}