#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 */
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)
      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     118b <safewrite+0x7b>
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    115d <safewrite+0x4d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1191 <safewrite+0x81>
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   %rbx
push   %rax
test   %rdx,%rdx
je     1152 <safewrite+0x42>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1157 <safewrite+0x47>
add    %eax,%r14d
cltq
add    %rax,%rbp
sub    %rax,%rbx
jne    1130 <safewrite+0x20>
jmp    115d <safewrite+0x4d>
xor    %r14d,%r14d
jmp    115d <safewrite+0x4d>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
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 = %22, %3
%11 = load i64, i64* %7
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %34
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 %22
20: ; preds = %13
%21 = load i32, i32* %9
store i32 %21, i32* %4
br label %36
22: ; preds = %13
%23 = load i32, i32* %9
%24 = load i32, i32* %8
%25 = add nsw i32 %24, %23
store i32 %25, i32* %8
%26 = load i32, i32* %9
%27 = load i8*, i8** %6
%28 = sext i32 %26 to i64
%29 = getelementptr i8, i8* %27, i64 %28
store i8* %29, i8** %6
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = load i64, i64* %7
%33 = sub i64 %32, %31
store i64 %33, i64* %7
br label %10
34: ; preds = %10
%35 = load i32, i32* %8
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %20
%37 = load i32, i32* %4
ret i32 %37
}

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