#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;
int /*<<< orphan*/ error_printf(char *, char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ write(int, void const *, scalar_t__);
ssize_t safe_write(int fd, const void *buf, size_t count, char *errstr) {
  ssize_t ret = 0;
  ssize_t len = count;
  while (len != 0 && (ret = write(fd, buf, len)) != 0) {
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      error_printf("%s (%s) (fd=%d)\r\n", errstr, strerror(errno), fd);
      break;
    }
    len -= ret;
    buf += ret;
  }
  if (ret == -1)
    return ret;
  else
    return count - len;
}


x86 assembly (clang, -O0):
<safe_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x38(%rbp)
mov    %al,-0x39(%rbp)
je     1185 <safe_write+0x55>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1195 <safe_write+0x65>
jmp    1210 <safe_write+0xe0>
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    11f0 <safe_write+0xc0>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <safe_write+0x8f>
jmp    1157 <safe_write+0x27>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
mov    -0xc(%rbp),%ecx
lea    0xe1a(%rip),%rdi
call   1040 <error_printf@plt>
jmp    1210 <safe_write+0xe0>
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1157 <safe_write+0x27>
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1228 <safe_write+0xf8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <safe_write+0x104>
mov    -0x20(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%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     118e <safe_write+0x5e>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r12d
mov    %rcx,(%rsp)
mov    0x2e8c(%rip),%r13
mov    0x2e7d(%rip),%r14
mov    %rdx,%rbp
xchg   %ax,%ax
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
jne    117f <safe_write+0x4f>
mov    0x0(%r13),%rdi
cmp    (%r14),%rdi
je     1160 <safe_write+0x30>
jmp    1193 <safe_write+0x63>
test   %rax,%rax
je     11b6 <safe_write+0x86>
add    %rax,%rbx
sub    %rax,%rbp
jne    1160 <safe_write+0x30>
jmp    11b9 <safe_write+0x89>
xor    %r15d,%r15d
jmp    11b9 <safe_write+0x89>
call   1050 <strerror@plt>
lea    0xe61(%rip),%rdi
mov    (%rsp),%rsi
mov    %eax,%edx
mov    %r12d,%ecx
call   1040 <error_printf@plt>
mov    $0xffffffffffffffff,%r15
jmp    11b9 <safe_write+0x89>
sub    %rbp,%r15
mov    %r15,%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
@.str = [18 x i8] c"%s (%s) (fd=%d)\0D\0A\00"
define i64 @safe_write(i32 %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i64 0, i64* %10
%12 = load i64, i64* %8
store i64 %12, i64* %11
br label %13
13: ; preds = %38, %31, %4
%14 = load i64, i64* %11
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* %6
%18 = load i8*, i8** %7
%19 = load i64, i64* %11
%20 = call i64 @write(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %10
%21 = icmp ne i64 %20, 0
br label %22
22: ; preds = %16, %13
%23 = phi i1 [ false, %13 ], [ %21, %16 ]
br i1 %23, label %24, label %45
24: ; preds = %22
%25 = load i64, i64* %10
%26 = icmp eq i64 %25, -1
br i1 %26, label %27, label %38
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %13
32: ; preds = %27
%33 = load i8*, i8** %9
%34 = load i64, i64* @errno
%35 = call i32 @strerror(i64 %34)
%36 = load i32, i32* %6
%37 = call i32 @error_printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %33, i32 %35, i32 %36)
br label %45
38: ; preds = %24
%39 = load i64, i64* %10
%40 = load i64, i64* %11
%41 = sub nsw i64 %40, %39
store i64 %41, i64* %11
%42 = load i64, i64* %10
%43 = load i8*, i8** %7
%44 = getelementptr i8, i8* %43, i64 %42
store i8* %44, i8** %7
br label %13
45: ; preds = %32, %22
%46 = load i64, i64* %10
%47 = icmp eq i64 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = load i64, i64* %10
store i64 %49, i64* %5
br label %54
50: ; preds = %45
%51 = load i64, i64* %8
%52 = load i64, i64* %11
%53 = sub i64 %51, %52
store i64 %53, i64* %5
br label %54
54: ; preds = %50, %48
%55 = load i64, i64* %5
ret i64 %55
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [18 x i8] c"%s (%s) (fd=%d)\0D\0A\00"
define i64 @safe_write(i32 %0, i8* nocapture %1, i64 %2, i8* %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %24, label %6
6: ; preds = %4, %17
%7 = phi i64 [ %18, %17 ], [ %2, %4 ]
%8 = phi i8* [ %19, %17 ], [ %1, %4 ]
br label %9
9: ; preds = %13, %6
%10 = tail call i64 @write(i32 %0, i8* %8, i64 %7)
switch i64 %10, label %17 [
i64 0, label %11
i64 -1, label %13
]
11: ; preds = %9
%12 = sub i64 %2, %7
br label %24
13: ; preds = %9
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %9, label %21
17: ; preds = %9
%18 = sub nsw i64 %7, %10
%19 = getelementptr i8, i8* %8, i64 %10
%20 = icmp eq i64 %18, 0
br i1 %20, label %24, label %6
21: ; preds = %13
%22 = tail call i32 @strerror(i64 %14)
%23 = tail call i32 @error_printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %3, i32 %22, i32 %0)
br label %24
24: ; preds = %17, %4, %11, %21
%25 = phi i64 [ -1, %21 ], [ %12, %11 ], [ 0, %4 ], [ %2, %17 ]
ret i64 %25
}