#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fwrite(int /*<<< orphan*/ const *, int, size_t, int /*<<< orphan*/ *);
int safe_fwrite(const uint8_t *buffer, size_t len, FILE *f) {
  size_t i, amt;
  for (i = 0; i < len; i += amt) {
    amt = fwrite(buffer + i, 1, len - i, f);
    if (ferror(f))
      return -1;
  }
  return 0;
}


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)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11a8 <safe_fwrite+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1192 <safe_fwrite+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <safe_fwrite+0x8f>
jmp    1197 <safe_fwrite+0x77>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    113c <safe_fwrite+0x1c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fwrite>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rsi,%rsi
je     116f <safe_fwrite+0x4f>
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
lea    (%r15,%rbx,4),%rdi
mov    %r12,%rdx
sub    %rbx,%rdx
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fwrite@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1173 <safe_fwrite+0x53>
add    %r13,%rbx
cmp    %r12,%rbx
jb     1140 <safe_fwrite+0x20>
xor    %eax,%eax
jmp    1178 <safe_fwrite+0x58>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @safe_fwrite(i32* %0, i64 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
store i64 0, i64* %8
br label %10
10: ; preds = %28, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %6
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %32
14: ; preds = %10
%15 = load i32*, i32** %5
%16 = load i64, i64* %8
%17 = getelementptr inbounds i32, i32* %15, i64 %16
%18 = load i64, i64* %6
%19 = load i64, i64* %8
%20 = sub i64 %18, %19
%21 = load i32*, i32** %7
%22 = call i64 @fwrite(i32* %17, i32 1, i64 %20, i32* %21)
store i64 %22, i64* %9
%23 = load i32*, i32** %7
%24 = call i64 @ferror(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %14
store i32 -1, i32* %4
br label %33
27: ; preds = %14
br label %28
28: ; preds = %27
%29 = load i64, i64* %9
%30 = load i64, i64* %8
%31 = add i64 %30, %29
store i64 %31, i64* %8
br label %10
32: ; preds = %10
store i32 0, i32* %4
br label %33
33: ; preds = %32, %26
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
define i32 @safe_fwrite(i32* nocapture %0, i64 %1, i32* nocapture %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %15, label %7
5: ; preds = %7
%6 = icmp ult i64 %14, %1
br i1 %6, label %7, label %15
7: ; preds = %3, %5
%8 = phi i64 [ %14, %5 ], [ 0, %3 ]
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = sub i64 %1, %8
%11 = tail call i64 @fwrite(i32* %9, i32 1, i64 %10, i32* %2)
%12 = tail call i64 @ferror(i32* %2)
%13 = icmp eq i64 %12, 0
%14 = add i64 %11, %8
br i1 %13, label %5, label %15
15: ; preds = %7, %5, %3
%16 = phi i32 [ 0, %3 ], [ 0, %5 ], [ -1, %7 ]
ret i32 %16
}