#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__ 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 || errno == EAGAIN)
        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    11d6 <safe_write+0xc6>
cmpq   $0x0,-0x28(%rbp)
jge    11c5 <safe_write+0xb5>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
je     11b3 <safe_write+0xa3>
mov    0x2e40(%rip),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11b8 <safe_write+0xa8>
jmp    112b <safe_write+0x1b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11de <safe_write+0xce>
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     1193 <safe_write+0x83>
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     119f <safe_write+0x8f>
jns    117b <safe_write+0x6b>
mov    (%r15),%rax
cmp    (%r14),%rax
je     1150 <safe_write+0x40>
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%rax
je     1150 <safe_write+0x40>
jmp    1198 <safe_write+0x88>
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    119f <safe_write+0x8f>
xor    %r12d,%r12d
jmp    119f <safe_write+0x8f>
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
@EAGAIN = 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 = %39, %37, %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 %43
26: ; preds = %24
%27 = load i64, i64* %8
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %39
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i64, i64* @errno
%35 = load i64, i64* @EAGAIN
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33, %29
br label %10
38: ; preds = %33
store i64 -1, i64* %4
br label %45
39: ; preds = %26
%40 = load i64, i64* %8
%41 = load i64, i64* %9
%42 = add i64 %41, %40
store i64 %42, i64* %9
br label %10
43: ; preds = %24
%44 = load i64, i64* %9
store i64 %44, i64* %4
br label %45
45: ; preds = %43, %38
%46 = load i64, i64* %4
ret i64 %46
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @safe_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %26, label %5
5: ; preds = %3, %21
%6 = phi i64 [ %25, %21 ], [ %2, %3 ]
%7 = phi i8* [ %24, %21 ], [ %1, %3 ]
%8 = phi i64 [ %22, %21 ], [ 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 %26, label %12
12: ; preds = %9
%13 = icmp slt i64 %10, 0
br i1 %13, label %14, label %21
14: ; preds = %12
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %15, %18
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %9, label %26
21: ; preds = %12
%22 = add i64 %10, %8
%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 %26
26: ; preds = %21, %14, %9, %3
%27 = phi i64 [ 0, %3 ], [ -1, %14 ], [ %8, %9 ], [ %22, %21 ]
ret i64 %27
}