#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 /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ EPIPE;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char *, size_t);
void gt_xwrite(int d, const void *buf, size_t nbytes) {
  size_t pos = 0;
  ssize_t rval;
  int error = 0;
  while (nbytes > pos) {
    rval = write(d, (char *)buf + pos, nbytes - pos);
    switch (rval) {
    case -1:
      if (errno == EINTR || errno == EAGAIN)
        continue;
      error = 1;
      break;
    case 0:
      errno = EPIPE;
      error = 1;
      break;
    default:
      pos += rval;
    }
    if (error)
      break;
  }
  if (error) {
    perror("cannot write to file descriptor");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<gt_xwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1220 <gt_xwrite+0xf0>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    %rax,%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     11a1 <gt_xwrite+0x71>
jmp    1191 <gt_xwrite+0x61>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     11e2 <gt_xwrite+0xb2>
jmp    1200 <gt_xwrite+0xd0>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%eax
je     11d1 <gt_xwrite+0xa1>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%eax
jne    11d6 <gt_xwrite+0xa6>
jmp    1152 <gt_xwrite+0x22>
movl   $0x1,-0x28(%rbp)
jmp    120c <gt_xwrite+0xdc>
mov    0x2df7(%rip),%rax
mov    (%rax),%ecx
mov    0x2de6(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x28(%rbp)
jmp    120c <gt_xwrite+0xdc>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     121b <gt_xwrite+0xeb>
jmp    1220 <gt_xwrite+0xf0>
jmp    1152 <gt_xwrite+0x22>
cmpl   $0x0,-0x28(%rbp)
je     1244 <gt_xwrite+0x114>
lea    0xdcf(%rip),%rdi
call   1040 <perror@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gt_xwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,(%rsp)
mov    %rdx,0x8(%rsp)
test   %rdx,%rdx
je     11e0 <gt_xwrite+0xb0>
mov    %edi,%r12d
xor    %ecx,%ecx
mov    0x2e7c(%rip),%r13
mov    0x2e65(%rip),%r15
mov    0x2e4e(%rip),%r14
mov    0x8(%rsp),%rbx
mov    (%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rcx,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11c0 <gt_xwrite+0x90>
mov    0x0(%r13),%eax
cmp    (%r15),%eax
je     1190 <gt_xwrite+0x60>
cmp    (%r14),%eax
je     1190 <gt_xwrite+0x60>
jmp    11fc <gt_xwrite+0xcc>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %eax,%eax
mov    0x10(%rsp),%rcx
je     11ef <gt_xwrite+0xbf>
cltq
add    %rax,%rcx
mov    (%rsp),%rax
lea    (%rax,%rcx,1),%rbp
mov    0x8(%rsp),%rbx
sub    %rcx,%rbx
ja     1180 <gt_xwrite+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x0(%r13)
lea    0xdfd(%rip),%rdi
call   1040 <perror@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@EPIPE = global i32 0
@.str = [32 x i8] c"cannot write to file descriptor\00"
@EXIT_FAILURE = global i32 0
define void @gt_xwrite(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %30, label %5
5: ; preds = %3, %20
%6 = phi i64 [ %25, %20 ], [ %2, %3 ]
%7 = phi i8* [ %24, %20 ], [ %1, %3 ]
%8 = phi i64 [ %22, %20 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %11
%10 = tail call i32 @write(i32 %0, i8* %7, i64 %6)
switch i32 %10, label %20 [
i32 -1, label %11
i32 0, label %18
]
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
br i1 %17, label %9, label %26
18: ; preds = %9
%19 = load i32, i32* @EPIPE
store i32 %19, i32* @errno
br label %26
20: ; preds = %9
%21 = sext i32 %10 to i64
%22 = add i64 %8, %21
%23 = icmp ult i64 %22, %2
%24 = getelementptr inbounds i8, i8* %1, i64 %22
%25 = sub i64 %2, %22
br i1 %23, label %5, label %30
26: ; preds = %11, %18
%27 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %20, %3
ret void
}