#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 EPIPE;
int errno;
int /*<<< orphan*/ printf(char *, int, ...);
char *strerror(int);
int write(int, char *, size_t);
ssize_t xwrite(int fd, const void *buf, size_t count) {
  ssize_t wr;
  ssize_t off = 0;
  while (count > 0) {
    wr = write(fd, (char *)buf + off, count);
    if (wr < 0) {
      if (errno == EPIPE) {
        printf("fd: %d: EPIPE!\n", fd);
        break;
      } else {
        printf("write: %d (%s)\n", errno, strerror(errno));
        return -1;
      }
    }
    off += wr;
    count -= wr;
  }
  return off;
}


x86 assembly (clang, -O0):
<xwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11fe <xwrite+0xce>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e1 <xwrite+0xb1>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%eax
jne    11a7 <xwrite+0x77>
mov    -0x8(%rbp),%esi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11fe <xwrite+0xce>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%esi
mov    %rax,%rdx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <xwrite+0xd4>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    114a <xwrite+0x1a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     1170 <xwrite+0x40>
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %edi,%r14d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movslq %ebx,%rsi
add    %r15,%rsi
mov    %r14d,%edi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %eax,%eax
js     1174 <xwrite+0x44>
add    %eax,%ebx
mov    %eax,%eax
sub    %rax,%rbp
jne    1150 <xwrite+0x20>
jmp    11ba <xwrite+0x8a>
xor    %ebx,%ebx
jmp    11ba <xwrite+0x8a>
mov    0x2e5d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e5c(%rip),%rax
cmp    (%rax),%ebp
jne    119b <xwrite+0x6b>
lea    0xe71(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11ba <xwrite+0x8a>
mov    %ebp,%edi
call   1050 <strerror@plt>
lea    0xe67(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EPIPE = global i32 0
@.str = [16 x i8] c"fd: %d: EPIPE!\0A\00"
@.str.1 = [16 x i8] c"write: %d (%s)\0A\00"
define i32 @xwrite(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %9
br label %10
10: ; preds = %35, %3
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %43
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i32, i32* %9
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i64, i64* %7
%20 = call i32 @write(i32 %14, i8* %18, i64 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %13
%24 = load i32, i32* @errno
%25 = load i32, i32* @EPIPE
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* %5
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %43
30: ; preds = %23
%31 = load i32, i32* @errno
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %31, i8* %33)
store i32 -1, i32* %4
br label %45
35: ; preds = %13
%36 = load i32, i32* %8
%37 = load i32, i32* %9
%38 = add nsw i32 %37, %36
store i32 %38, i32* %9
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = load i64, i64* %7
%42 = sub i64 %41, %40
store i64 %42, i64* %7
br label %10
43: ; preds = %27, %10
%44 = load i32, i32* %9
store i32 %44, i32* %4
br label %45
45: ; preds = %43, %30
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@errno = global i32 0
@EPIPE = global i32 0
@.str = [16 x i8] c"fd: %d: EPIPE!\0A\00"
@.str.1 = [16 x i8] c"write: %d (%s)\0A\00"
define i32 @xwrite(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %26, label %5
5: ; preds = %3, %21
%6 = phi i32 [ %22, %21 ], [ 0, %3 ]
%7 = phi i64 [ %24, %21 ], [ %2, %3 ]
%8 = sext i32 %6 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = tail call i32 @write(i32 %0, i8* %9, i64 %7)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %5
%13 = load i32, i32* @errno
%14 = load i32, i32* @EPIPE
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %26
18: ; preds = %12
%19 = tail call i8* @strerror(i32 %13)
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %19)
br label %26
21: ; preds = %5
%22 = add nsw i32 %10, %6
%23 = zext i32 %10 to i64
%24 = sub i64 %7, %23
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %5
26: ; preds = %21, %3, %16, %18
%27 = phi i32 [ -1, %18 ], [ %6, %16 ], [ 0, %3 ], [ %22, %21 ]
ret i32 %27
}