#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) {
  size_t 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    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <fwrite@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1178 <full_fwrite+0x58>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c1 <full_fwrite+0xa1>
cmpl   $0x0,-0x34(%rbp)
jne    11a0 <full_fwrite+0x80>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11a0 <full_fwrite+0x80>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c1 <full_fwrite+0xa1>
movslq -0x34(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1147 <full_fwrite+0x27>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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,%r13
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebx,%ebx
jmp    114a <full_fwrite+0x2a>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%eax
add    %rax,%rbx
cmp    %r13,%rbx
jae    1177 <full_fwrite+0x57>
mov    %r13,%rdx
sub    %rbx,%rdx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
call   1040 <fwrite@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1179 <full_fwrite+0x59>
jne    1140 <full_fwrite+0x20>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1140 <full_fwrite+0x20>
xor    %ebp,%ebp
jmp    1179 <full_fwrite+0x59>
mov    %ebx,%ebp
mov    %ebp,%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 i64
%11 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i64 0, i64* %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 i64, i64* %10
%17 = sub i64 %15, %16
%18 = load i32*, i32** %9
%19 = call i32 @fwrite(i8* %13, i64 %14, i64 %17, i32* %18)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %12
%23 = load i32, i32* %11
store i32 %23, i32* %5
br label %45
24: ; preds = %12
%25 = load i32, i32* %11
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32*, i32** %9
%29 = call i64 @ferror(i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* %11
store i32 %32, i32* %5
br label %45
33: ; preds = %27, %24
%34 = load i32, i32* %11
%35 = sext i32 %34 to i64
%36 = load i64, i64* %10
%37 = add i64 %36, %35
store i64 %37, i64* %10
br label %38
38: ; preds = %33
%39 = load i64, i64* %10
%40 = load i64, i64* %8
%41 = icmp ult i64 %39, %40
br i1 %41, label %12, label %42
42: ; preds = %38
%43 = load i64, i64* %10
%44 = trunc i64 %43 to i32
store i32 %44, i32* %5
br label %45
45: ; preds = %42, %31, %22
%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 = %15, %4
%6 = phi i64 [ 0, %4 ], [ %17, %15 ]
%7 = sub i64 %2, %6
%8 = tail call i32 @fwrite(i8* %0, i64 %1, i64 %7, i32* %3)
%9 = icmp slt i32 %8, 0
br i1 %9, label %21, label %10
10: ; preds = %5
%11 = icmp eq i32 %8, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = tail call i64 @ferror(i32* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12, %10
%16 = zext i32 %8 to i64
%17 = add i64 %6, %16
%18 = icmp ult i64 %17, %2
br i1 %18, label %5, label %19
19: ; preds = %15
%20 = trunc i64 %17 to i32
br label %21
21: ; preds = %12, %5, %19
%22 = phi i32 [ %20, %19 ], [ 0, %12 ], [ %8, %5 ]
ret i32 %22
}