#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 ssize_t;

/* Variables and functions */
int EAGAIN;
int EINTR;
int EPIPE;
int EWOULDBLOCK;
int errno;
int write(int, char const *, size_t);
size_t Write(int fd, const char *data, size_t len) {
  ssize_t n = 0;
  size_t t = 0;
  int safe_errno = errno;
  errno = 0;
  while ((n = write(fd, data + t, len))) {
    if (-1 == n) {
      if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK ||
          errno == EPIPE) {
        errno = safe_errno;
        continue;
      }
      return 0;
    }
    t += n;
    len -= n;
  }
  errno = safe_errno;
  return t;
}


x86 assembly (clang, -O0):
<Write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e93(%rip),%rax
movl   $0x0,(%rax)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     1217 <Write+0x107>
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    11f7 <Write+0xe7>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%eax
je     11d9 <Write+0xc9>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%eax
je     11d9 <Write+0xc9>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%eax
je     11d9 <Write+0xc9>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%eax
jne    11ea <Write+0xda>
mov    -0x34(%rbp),%ecx
mov    0x2df5(%rip),%rax
mov    %ecx,(%rax)
jmp    114b <Write+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    122b <Write+0x11b>
movslq -0x24(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
movslq -0x24(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    114b <Write+0x3b>
mov    -0x34(%rbp),%ecx
mov    0x2db7(%rip),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %edi,%r12d
mov    0x2ead(%rip),%r13
mov    0x0(%r13),%r15d
movl   $0x0,0x0(%r13)
xor    %ecx,%ecx
mov    0x2e88(%rip),%r14
mov    %rsi,0x10(%rsp)
mov    %rcx,0x8(%rsp)
lea    (%rsi,%rcx,1),%rbp
jmp    1154 <Write+0x44>
mov    %r15d,0x0(%r13)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11a0 <Write+0x90>
mov    0x0(%r13),%eax
cmp    (%r14),%eax
je     1150 <Write+0x40>
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%eax
je     1150 <Write+0x40>
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%eax
je     1150 <Write+0x40>
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%eax
je     1150 <Write+0x40>
jmp    11b8 <Write+0xa8>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %eax,%eax
je     11bc <Write+0xac>
cltq
mov    0x8(%rsp),%rcx
add    %rax,%rcx
sub    %rax,%rbx
mov    0x10(%rsp),%rsi
jmp    1145 <Write+0x35>
xor    %eax,%eax
jmp    11c5 <Write+0xb5>
mov    %r15d,0x0(%r13)
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
@EPIPE = global i32 0
define i64 @Write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
store i64 0, i64* %9
%11 = load i32, i32* @errno
store i32 %11, i32* %10
store i32 0, i32* @errno
br label %12
12: ; preds = %42, %39, %3
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %9
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = load i64, i64* %7
%18 = call i32 @write(i32 %13, i8* %16, i64 %17)
store i32 %18, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %51
20: ; preds = %12
%21 = load i32, i32* %8
%22 = icmp eq i32 -1, %21
br i1 %22, label %23, label %42
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = load i32, i32* @EINTR
%26 = icmp eq i32 %24, %25
br i1 %26, label %39, label %27
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = load i32, i32* @EAGAIN
%30 = icmp eq i32 %28, %29
br i1 %30, label %39, label %31
31: ; preds = %27
%32 = load i32, i32* @errno
%33 = load i32, i32* @EWOULDBLOCK
%34 = icmp eq i32 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i32, i32* @errno
%37 = load i32, i32* @EPIPE
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35, %31, %27, %23
%40 = load i32, i32* %10
store i32 %40, i32* @errno
br label %12
41: ; preds = %35
store i64 0, i64* %4
br label %54
42: ; preds = %20
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = load i64, i64* %9
%46 = add i64 %45, %44
store i64 %46, i64* %9
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = load i64, i64* %7
%50 = sub i64 %49, %48
store i64 %50, i64* %7
br label %12
51: ; preds = %12
%52 = load i32, i32* %10
store i32 %52, i32* @errno
%53 = load i64, i64* %9
store i64 %53, i64* %4
br label %54
54: ; preds = %51, %41
%55 = load i64, i64* %4
ret i64 %55
}

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
@EPIPE = global i32 0
define i64 @Write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @errno
store i32 0, i32* @errno
br label %5
5: ; preds = %25, %3
%6 = phi i64 [ %28, %25 ], [ %2, %3 ]
%7 = phi i64 [ %27, %25 ], [ 0, %3 ]
%8 = getelementptr inbounds i8, i8* %1, i64 %7
br label %9
9: ; preds = %5, %24
%10 = tail call i32 @write(i32 %0, i8* %8, i64 %6)
switch i32 %10, label %25 [
i32 0, label %29
i32 -1, label %11
]
11: ; preds = %9
%12 = load i32, i32* @errno
%13 = load i32, i32* @EINTR
%14 = icmp eq i32 %12, %13
%15 = load i32, i32* @EAGAIN
%16 = icmp eq i32 %12, %15
%17 = select i1 %14, i1 true, i1 %16
%18 = load i32, i32* @EWOULDBLOCK
%19 = icmp eq i32 %12, %18
%20 = select i1 %17, i1 true, i1 %19
%21 = load i32, i32* @EPIPE
%22 = icmp eq i32 %12, %21
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %24, label %30
24: ; preds = %11
store i32 %4, i32* @errno
br label %9
25: ; preds = %9
%26 = sext i32 %10 to i64
%27 = add i64 %7, %26
%28 = sub i64 %6, %26
br label %5
29: ; preds = %9
store i32 %4, i32* @errno
br label %30
30: ; preds = %11, %29
%31 = phi i64 [ %7, %29 ], [ 0, %11 ]
ret i64 %31
}