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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, char *, size_t);
void safe_write(int fd, void *buf, size_t len) {
  ssize_t written;
  char *ptr = buf;
  do {
    written = write(fd, ptr, len);
    if (written < 0) {
      if (errno == EAGAIN)
        continue;
      else
        break;
    }
    ptr += written;
    len -= written;
  } while (len > 0);
  if (written < 0)
    perror("write");
}


x86 assembly (clang, -O0):
<safe_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    117e <safe_write+0x5e>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
jne    1179 <safe_write+0x59>
jmp    1199 <safe_write+0x79>
jmp    11a4 <safe_write+0x84>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
ja     113b <safe_write+0x1b>
cmpq   $0x0,-0x20(%rbp)
jge    11bb <safe_write+0x9b>
lea    0xe4a(%rip),%rdi
call   1040 <perror@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2ea9(%rip),%r15
mov    0x2e92(%rip),%r12
jmp    1150 <safe_write+0x30>
add    %rax,%r14
sub    %rax,%rbx
je     1184 <safe_write+0x64>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    1140 <safe_write+0x20>
test   %rbx,%rbx
je     1170 <safe_write+0x50>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1150 <safe_write+0x30>
lea    0xe89(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <perror@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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