#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fwrite(char *, size_t, size_t, int /*<<< orphan*/ *);
int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
  size_t ret = 0;
  do {
    clearerr(stream);
    ret += fwrite((char *)ptr + (ret * size), size, nmemb - ret, stream);
  } while (ret < nmemb && ferror(stream) && errno == EINTR);
  return ret;
}


x86 assembly (clang, -O0):
<safe_fwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <clearerr@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rax
imul   -0x10(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <fwrite@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    -0x18(%rbp),%rcx
mov    %al,-0x29(%rbp)
jae    11ce <safe_fwrite+0x9e>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
je     11ce <safe_fwrite+0x9e>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1150 <safe_fwrite+0x20>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %rbx,%rbp
mov    %r13,%rdi
call   1040 <clearerr@plt>
mov    %rbx,%rdi
imul   %r15,%rdi
add    %r12,%rdi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %r15,%rsi
mov    %r13,%rcx
call   1050 <fwrite@plt>
mov    %rax,%rbx
add    %rbp,%rbx
cmp    %r14,%rbx
jae    11a4 <safe_fwrite+0x74>
mov    %r13,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11a4 <safe_fwrite+0x74>
mov    0x2e4b(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
je     1150 <safe_fwrite+0x20>
mov    %ebx,%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 @safe_fwrite(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
store i64 0, i64* %9
br label %10
10: ; preds = %38, %4
%11 = load i32*, i32** %8
%12 = call i32 @clearerr(i32* %11)
%13 = load i8*, i8** %5
%14 = load i64, i64* %9
%15 = load i64, i64* %6
%16 = mul i64 %14, %15
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = load i64, i64* %9
%21 = sub i64 %19, %20
%22 = load i32*, i32** %8
%23 = call i64 @fwrite(i8* %17, i64 %18, i64 %21, i32* %22)
%24 = load i64, i64* %9
%25 = add i64 %24, %23
store i64 %25, i64* %9
br label %26
26: ; preds = %10
%27 = load i64, i64* %9
%28 = load i64, i64* %7
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %38
30: ; preds = %26
%31 = load i32*, i32** %8
%32 = call i64 @ferror(i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br label %38
38: ; preds = %34, %30, %26
%39 = phi i1 [ false, %30 ], [ false, %26 ], [ %37, %34 ]
br i1 %39, label %10, label %40
40: ; preds = %38
%41 = load i64, i64* %9
%42 = trunc i64 %41 to i32
ret i32 %42
}

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