#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 ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int write(int, void const *, size_t);
ssize_t safewrite(int fd, const void *buf, size_t count) {
  int written = 0, result;
  while (count) {
    result = write(fd, buf, count);
    if (result == -1) {
      if (errno == EINTR)
        continue;
      else
        return result;
    }
    written += result;
    buf += result;
    count -= result;
  }
  return written;
}


x86 assembly (clang, -O0):
<safewrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11aa <safewrite+0x9a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    117c <safewrite+0x6c>
mov    0x2e87(%rip),%rax
mov    (%rax),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
jne    1171 <safewrite+0x61>
jmp    112a <safewrite+0x1a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b0 <safewrite+0xa0>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    112a <safewrite+0x1a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safewrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1176 <safewrite+0x66>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %r14d,%r14d
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>
cmp    $0xffffffff,%eax
jne    115f <safewrite+0x4f>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1140 <safewrite+0x30>
jmp    116e <safewrite+0x5e>
add    %eax,%r14d
cltq
add    %rax,%rbp
sub    %rax,%rbx
jne    1140 <safewrite+0x30>
jmp    1179 <safewrite+0x69>
mov    $0xffffffff,%r14d
jmp    1179 <safewrite+0x69>
xor    %r14d,%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 i64 0
@EINTR = global i64 0
define i32 @safewrite(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
br label %10
10: ; preds = %27, %24, %3
%11 = load i64, i64* %7
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %39
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i32 @write(i32 %14, i8* %15, i64 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %27
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %10
25: ; preds = %20
%26 = load i32, i32* %9
store i32 %26, i32* %4
br label %41
27: ; preds = %13
%28 = load i32, i32* %9
%29 = load i32, i32* %8
%30 = add nsw i32 %29, %28
store i32 %30, i32* %8
%31 = load i32, i32* %9
%32 = load i8*, i8** %6
%33 = sext i32 %31 to i64
%34 = getelementptr i8, i8* %32, i64 %33
store i8* %34, i8** %6
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = load i64, i64* %7
%38 = sub i64 %37, %36
store i64 %38, i64* %7
br label %10
39: ; preds = %10
%40 = load i32, i32* %8
store i32 %40, i32* %4
br label %41
41: ; preds = %39, %25
%42 = load i32, i32* %4
ret i32 %42
}

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