#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*/ _io_err(int, size_t, size_t, int /*<<< orphan*/ *);
size_t fwrite(void *, size_t, size_t, int /*<<< orphan*/ *);
size_t check_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream) {
  size_t res = 1, nwritten = 0;
  while (nwritten < nitems) {
    res = fwrite(ptr, size, nitems, stream);
    if (res <= 0)
      _io_err(1, size - 1, nitems, stream);
    nwritten += res;
  }
  return nwritten;
}


x86 assembly (clang, -O0):
<check_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   $0x1,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11a5 <check_fwrite+0x85>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <fwrite@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
ja     1194 <check_fwrite+0x74>
mov    -0x10(%rbp),%rsi
sub    $0x1,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%edi
call   1030 <_io_err@plt>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1148 <check_fwrite+0x28>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_fwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1187 <check_fwrite+0x67>
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r13
lea    -0x1(%rsi),%rax
mov    %rax,(%rsp)
xor    %ebx,%ebx
jmp    1158 <check_fwrite+0x38>
nopl   0x0(%rax,%rax,1)
add    %rbp,%rbx
cmp    %r12,%rbx
jae    1189 <check_fwrite+0x69>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
call   1040 <fwrite@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1150 <check_fwrite+0x30>
mov    $0x1,%edi
mov    (%rsp),%rsi
mov    %r12,%rdx
mov    %r14,%rcx
call   1030 <_io_err@plt>
jmp    1150 <check_fwrite+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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