#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 /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int write(int, void const *, size_t);
ssize_t safe_write(int fd, const void *buf, size_t count) {
  int r;
  while (count > 0) {
    r = write(fd, buf, count);
    if (r < 0) {
      if (errno == EINTR)
        continue;
      return -errno;
    }
    count -= r;
    buf = (char *)buf + r;
  }
  return 0;
}


x86 assembly (clang, -O0):
<safe_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    119e <safe_write+0x8e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117b <safe_write+0x6b>
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    0x2e7d(%rip),%rcx
cmp    (%rcx),%eax
jne    1168 <safe_write+0x58>
jmp    1123 <safe_write+0x13>
xor    %eax,%eax
mov    0x2e6f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11a5 <safe_write+0x95>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <safe_write+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
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
xor    %r14d,%r14d
test   %rdx,%rdx
je     116f <safe_write+0x5f>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2ead(%rip),%r12
mov    0x2e9e(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %eax,%eax
jns    115e <safe_write+0x4e>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
je     1140 <safe_write+0x30>
jmp    116a <safe_write+0x5a>
mov    %eax,%eax
add    %rax,%rbp
sub    %rax,%rbx
jne    1140 <safe_write+0x30>
jmp    116f <safe_write+0x5f>
neg    %eax
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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