#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__ ferror(int /*<<< orphan*/ *);
int fwrite(char *, size_t, size_t, int /*<<< orphan*/ *);
int full_fwrite(char *buf, size_t size, size_t nmemb, FILE *fp) {
  int items_wrote = 0;
  int this_write = 0;
  do {
    this_write = fwrite(buf, size, nmemb - items_wrote, fp);
    if (this_write < 0) {
      return this_write;
    }
    if (this_write == 0 && ferror(fp)) {
      return this_write;
    }
    items_wrote += this_write;
  } while (items_wrote < nmemb);
  return items_wrote;
}


x86 assembly (clang, -O0):
<full_fwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
movslq -0x2c(%rbp),%rax
sub    %rax,%rdx
mov    -0x28(%rbp),%rcx
call   1040 <fwrite@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    117a <full_fwrite+0x5a>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <full_fwrite+0x9f>
cmpl   $0x0,-0x30(%rbp)
jne    11a2 <full_fwrite+0x82>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11a2 <full_fwrite+0x82>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <full_fwrite+0x9f>
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1146 <full_fwrite+0x26>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<full_fwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
xor    %ebx,%ebx
jmp    114a <full_fwrite+0x2a>
nopl   0x0(%rax,%rax,1)
add    %ebp,%ebx
movslq %ebx,%rax
cmp    %r15,%rax
jae    117c <full_fwrite+0x5c>
movslq %ebx,%rax
mov    %r15,%rdx
sub    %rax,%rdx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14,%rcx
call   1040 <fwrite@plt>
mov    %eax,%ebp
test   %eax,%eax
js     117a <full_fwrite+0x5a>
jne    1140 <full_fwrite+0x20>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1140 <full_fwrite+0x20>
xor    %ebx,%ebx
jmp    117c <full_fwrite+0x5c>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @full_fwrite(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i32 0, i32* %10
store i32 0, i32* %11
br label %12
12: ; preds = %38, %4
%13 = load i8*, i8** %6
%14 = load i64, i64* %7
%15 = load i64, i64* %8
%16 = load i32, i32* %10
%17 = sext i32 %16 to i64
%18 = sub i64 %15, %17
%19 = load i32*, i32** %9
%20 = call i32 @fwrite(i8* %13, i64 %14, i64 %18, i32* %19)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = load i32, i32* %11
store i32 %24, i32* %5
br label %45
25: ; preds = %12
%26 = load i32, i32* %11
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32*, i32** %9
%30 = call i64 @ferror(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = load i32, i32* %11
store i32 %33, i32* %5
br label %45
34: ; preds = %28, %25
%35 = load i32, i32* %11
%36 = load i32, i32* %10
%37 = add nsw i32 %36, %35
store i32 %37, i32* %10
br label %38
38: ; preds = %34
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = load i64, i64* %8
%42 = icmp ult i64 %40, %41
br i1 %42, label %12, label %43
43: ; preds = %38
%44 = load i32, i32* %10
store i32 %44, i32* %5
br label %45
45: ; preds = %43, %32, %23
%46 = load i32, i32* %5
ret i32 %46
}

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