#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__ EINTR;
scalar_t__ errno;
scalar_t__ write(int, char *, size_t);
ssize_t safe_write(int fd, const void *buf, size_t count) {
  ssize_t w;
  size_t written = 0;
  while (written < count &&
         (w = write(fd, (char *)buf + written, count - written)) != 0) {
    if (w < 0) {
      if (errno == EINTR)
        continue;
      return -1;
    }
    written += w;
  }
  return written;
}


x86 assembly (clang, -O0):
<safe_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
cmp    -0x20(%rbp),%rcx
mov    %al,-0x31(%rbp)
jae    1164 <safe_write+0x54>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1174 <safe_write+0x64>
jmp    11bc <safe_write+0xac>
cmpq   $0x0,-0x28(%rbp)
jge    11ab <safe_write+0x9b>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
jne    119e <safe_write+0x8e>
jmp    112b <safe_write+0x1b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c4 <safe_write+0xb4>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    112b <safe_write+0x1b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     1187 <safe_write+0x77>
mov    %edi,%r13d
xor    %r12d,%r12d
mov    0x2eb0(%rip),%r15
mov    0x2ea1(%rip),%r14
mov    %rdx,0x10(%rsp)
mov    %rdx,%rbx
mov    %rsi,0x8(%rsp)
mov    %rsi,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
je     1193 <safe_write+0x83>
jns    116f <safe_write+0x5f>
mov    (%r15),%rax
cmp    (%r14),%rax
je     1150 <safe_write+0x40>
jmp    118c <safe_write+0x7c>
add    %rax,%r12
mov    0x8(%rsp),%rax
lea    (%rax,%r12,1),%rbp
mov    0x10(%rsp),%rbx
sub    %r12,%rbx
ja     1150 <safe_write+0x40>
jmp    1193 <safe_write+0x83>
xor    %r12d,%r12d
jmp    1193 <safe_write+0x83>
mov    $0xffffffffffffffff,%r12
mov    %r12,%rax
add    $0x18,%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 i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %9
br label %10
10: ; preds = %35, %33, %3
%11 = load i64, i64* %9
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %24
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %9
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %9
%21 = sub i64 %19, %20
%22 = call i64 @write(i32 %15, i8* %18, i64 %21)
store i64 %22, i64* %8
%23 = icmp ne i64 %22, 0
br label %24
24: ; preds = %14, %10
%25 = phi i1 [ false, %10 ], [ %23, %14 ]
br i1 %25, label %26, label %39
26: ; preds = %24
%27 = load i64, i64* %8
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
br label %10
34: ; preds = %29
store i64 -1, i64* %4
br label %41
35: ; preds = %26
%36 = load i64, i64* %8
%37 = load i64, i64* %9
%38 = add i64 %37, %36
store i64 %38, i64* %9
br label %10
39: ; preds = %24
%40 = load i64, i64* %9
store i64 %40, i64* %4
br label %41
41: ; preds = %39, %34
%42 = load i64, i64* %4
ret i64 %42
}

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 %23, label %5
5: ; preds = %3, %18
%6 = phi i64 [ %22, %18 ], [ %2, %3 ]
%7 = phi i8* [ %21, %18 ], [ %1, %3 ]
%8 = phi i64 [ %19, %18 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %14
%10 = tail call i64 @write(i32 %0, i8* %7, i64 %6)
%11 = icmp eq i64 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %9
%13 = icmp slt i64 %10, 0
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %9, label %23
18: ; preds = %12
%19 = add i64 %10, %8
%20 = icmp ult i64 %19, %2
%21 = getelementptr inbounds i8, i8* %1, i64 %19
%22 = sub i64 %2, %19
br i1 %20, label %5, label %23
23: ; preds = %18, %14, %9, %3
%24 = phi i64 [ 0, %3 ], [ -1, %14 ], [ %8, %9 ], [ %19, %18 ]
ret i64 %24
}