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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
size_t fwrite(void const *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t safe_fwrite(const void *buf, size_t len, FILE *file) {
  size_t wr = fwrite(buf, 1, len, file);
  if (wr != len) {
    fprintf(stderr, "Error writing file: %s\n", strerror(errno));
    return 0;
  }
  return wr;
}


x86 assembly (clang, -O0):
<safe_fwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11a5 <safe_fwrite+0x75>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe6d(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ad <safe_fwrite+0x7d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fwrite>:
push   %rbx
mov    %rdx,%rcx
mov    %rsi,%rbx
mov    $0x1,%esi
mov    %rbx,%rdx
call   1040 <fwrite@plt>
cmp    %rbx,%rax
je     1173 <safe_fwrite+0x43>
mov    0x2e90(%rip),%rax
mov    (%rax),%ebx
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe99(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Error writing file: %s\0A\00"
@errno = global i32 0
define i64 @safe_fwrite(i8* %0, i64 %1, i32* %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = load i32*, i32** %7
%12 = call i64 @fwrite(i8* %9, i32 1, i64 %10, i32* %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = load i64, i64* %6
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %21
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %19)
store i64 0, i64* %4
br label %23
21: ; preds = %3
%22 = load i64, i64* %8
store i64 %22, i64* %4
br label %23
23: ; preds = %21, %16
%24 = load i64, i64* %4
ret i64 %24
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Error writing file: %s\0A\00"
@errno = global i32 0
define i64 @safe_fwrite(i8* nocapture %0, i64 %1, i32* nocapture %2) {
%4 = tail call i64 @fwrite(i8* %0, i32 1, i64 %1, i32* %2)
%5 = icmp eq i64 %4, %1
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %11
11: ; preds = %3, %6
%12 = phi i64 [ 0, %6 ], [ %1, %3 ]
ret i64 %12
}