#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int write(int, char const *, size_t);
size_t safe_write(int fd, const void *buf, size_t len) {
  size_t ret = 0;
  while (ret < len) {
    ssize_t rem;
    if ((rem = write(fd, (const char *)buf + ret, len - ret)) == -1) {
      if (errno == EINTR)
        continue;
      break;
    }
    ret += rem;
  }
  return ret;
}


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)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1192 <safe_write+0x82>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1181 <safe_write+0x71>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
jne    117c <safe_write+0x6c>
jmp    112b <safe_write+0x1b>
jmp    1192 <safe_write+0x82>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    112b <safe_write+0x1b>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     116c <safe_write+0x5c>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2eaf(%rip),%r12
mov    0x2ea0(%rip),%r13
jmp    114a <safe_write+0x3a>
nopw   0x0(%rax,%rax,1)
cltq
add    %rax,%rbx
cmp    %r14,%rbx
jae    116e <safe_write+0x5e>
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <safe_write+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1145 <safe_write+0x35>
jmp    116e <safe_write+0x5e>
xor    %ebx,%ebx
mov    %rbx,%rax
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
define i64 @safe_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %9
9: ; preds = %29, %27, %3
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %34
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = sub i64 %18, %19
%21 = call i32 @write(i32 %14, i8* %17, i64 %20)
store i32 %21, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %13
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %9
28: ; preds = %23
br label %34
29: ; preds = %13
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = load i64, i64* %7
%33 = add i64 %32, %31
store i64 %33, i64* %7
br label %9
34: ; preds = %28, %9
%35 = load i64, i64* %7
ret i64 %35
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @safe_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %21, label %5
5: ; preds = %3, %18
%6 = phi i64 [ %19, %18 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i32 @write(i32 %0, i8* %7, i64 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %18, label %21
15: ; preds = %5
%16 = sext i32 %9 to i64
%17 = add i64 %6, %16
br label %18
18: ; preds = %11, %15
%19 = phi i64 [ %17, %15 ], [ %6, %11 ]
%20 = icmp ult i64 %19, %2
br i1 %20, label %5, label %21
21: ; preds = %18, %11, %3
%22 = phi i64 [ 0, %3 ], [ %19, %18 ], [ %6, %11 ]
ret i64 %22
}