#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 */

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ report(char *, char *);
int write(int, char const *, int);
int safer_write(int fd, const void *buf, int size) {
  int cc, sz = size;
  const char *bp = buf;
  do {
    cc = write(fd, bp, sz);
    if ((cc < 0) && (errno != EINTR)) {
      report("safer_write() err!", "");
      return -1;
    }
    if (cc > 0) {
      bp += cc;
      sz -= cc;
    }
  } while (sz > 0);
  return 0;
}


x86 assembly (clang, -O0):
<safer_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1195 <safer_write+0x75>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
je     1195 <safer_write+0x75>
lea    0xe83(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1040 <report@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <safer_write+0xb1>
cmpl   $0x0,-0x18(%rbp)
jle    11bb <safer_write+0x9b>
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c0 <safer_write+0xa0>
cmpl   $0x0,-0x1c(%rbp)
jg     1140 <safer_write+0x20>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safer_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2ea9(%rip),%r15
mov    0x2e9a(%rip),%r12
jmp    114e <safer_write+0x2e>
xor    %eax,%eax
test   %ecx,%ecx
cmovs  %eax,%ecx
add    %rcx,%rbx
sub    %ecx,%ebp
jle    1182 <safer_write+0x62>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %eax,%ecx
test   %eax,%eax
jns    1140 <safer_write+0x20>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1140 <safer_write+0x20>
lea    0xe8f(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1040 <report@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [19 x i8] c"safer_write() err!\00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @safer_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
store i32 %11, i32* %9
%12 = load i8*, i8** %6
store i8* %12, i8** %10
br label %13
13: ; preds = %38, %3
%14 = load i32, i32* %5
%15 = load i8*, i8** %10
%16 = load i32, i32* %9
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @report(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %42
26: ; preds = %20, %13
%27 = load i32, i32* %8
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i32, i32* %8
%31 = load i8*, i8** %10
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8* %33, i8** %10
%34 = load i32, i32* %8
%35 = load i32, i32* %9
%36 = sub nsw i32 %35, %34
store i32 %36, i32* %9
br label %37
37: ; preds = %29, %26
br label %38
38: ; preds = %37
%39 = load i32, i32* %9
%40 = icmp sgt i32 %39, 0
br i1 %40, label %13, label %41
41: ; preds = %38
store i32 0, i32* %4
br label %42
42: ; preds = %41, %24
%43 = load i32, i32* %4
ret i32 %43
}

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