#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 */

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int write(int, void const *, size_t);
size_t safe_write(int fd, const void *buf, size_t count) {
  int r;
  do {
    r = write(fd, buf, count);
  } while (r == -1 && (errno == EINTR || errno == EAGAIN));
  if (r == -1)
    perror("safe_write");
  return r;
}


x86 assembly (clang, -O0):
<safe_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jne    1194 <safe_write+0x74>
mov    0x2e84(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e70(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1e(%rbp)
je     118e <safe_write+0x6e>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1133 <safe_write+0x13>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b5 <safe_write+0x95>
lea    0xe50(%rip),%rdi
call   1040 <perror@plt>
movslq -0x1c(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ea6(%rip),%r15
mov    0x2e97(%rip),%r12
mov    0x2e80(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1186 <safe_write+0x66>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1150 <safe_write+0x30>
cmp    0x0(%r13),%rax
je     1150 <safe_write+0x30>
lea    0xe88(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffffffffffff,%rax
jmp    1188 <safe_write+0x68>
cltq
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [11 x i8] c"safe_write\00"
define i64 @safe_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %8
8: ; preds = %26, %3
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i32 @write(i32 %9, i8* %10, i64 %11)
store i32 %12, i32* %7
br label %13
13: ; preds = %8
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %21, %22
br label %24
24: ; preds = %20, %16
%25 = phi i1 [ true, %16 ], [ %23, %20 ]
br label %26
26: ; preds = %24, %13
%27 = phi i1 [ false, %13 ], [ %25, %24 ]
br i1 %27, label %8, label %28
28: ; preds = %26
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
ret i64 %35
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [11 x i8] c"safe_write\00"
define i64 @safe_write(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %7, %3
%5 = tail call i32 @write(i32 %0, i8* %1, i64 %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %16
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %8, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %4, label %14
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %18
16: ; preds = %4
%17 = sext i32 %5 to i64
br label %18
18: ; preds = %16, %14
%19 = phi i64 [ %17, %16 ], [ -1, %14 ]
ret i64 %19
}