#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*/ die(char *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fread(void *, int, size_t, int /*<<< orphan*/ *);
void rdbuf(FILE *fd, void *buf, size_t sz) {
  size_t n;
  n = sz;
  while (n > 0) {
    n -= fread(buf, 1, n, fd);
    if (feof(fd))
      die("Unexpected EOF");
    if (ferror(fd))
      die("Error writing");
  }
}


x86 assembly (clang, -O0):
<rdbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11ce <rdbuf+0x8e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11aa <rdbuf+0x6a>
lea    0xe5b(%rip),%rdi
call   1060 <die@plt>
mov    -0x8(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11c9 <rdbuf+0x89>
lea    0xe4b(%rip),%rdi
call   1060 <die@plt>
jmp    115c <rdbuf+0x1c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdbuf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11ba <rdbuf+0x7a>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xeaf(%rip),%r12
lea    0xe99(%rip),%r13
jmp    1175 <rdbuf+0x35>
nopl   0x0(%rax)
test   %rbx,%rbx
je     11ba <rdbuf+0x7a>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     11a0 <rdbuf+0x60>
mov    %r13,%rdi
call   1060 <die@plt>
sub    %rbp,%rbx
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1170 <rdbuf+0x30>
mov    %r12,%rdi
call   1060 <die@plt>
jmp    1170 <rdbuf+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Unexpected EOF\00"
@.str.1 = [14 x i8] c"Error writing\00"
define void @rdbuf(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i64, i64* %6
store i64 %8, i64* %7
br label %9
9: ; preds = %30, %3
%10 = load i64, i64* %7
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %31
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = load i64, i64* %7
%15 = load i32*, i32** %4
%16 = call i64 @fread(i8* %13, i32 1, i64 %14, i32* %15)
%17 = load i64, i64* %7
%18 = sub i64 %17, %16
store i64 %18, i64* %7
%19 = load i32*, i32** %4
%20 = call i64 @feof(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %12
%23 = call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %12
%25 = load i32*, i32** %4
%26 = call i64 @ferror(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
br label %9
31: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"Unexpected EOF\00"
@.str.1 = [14 x i8] c"Error writing\00"
define void @rdbuf(i32* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %20, label %5
5: ; preds = %3, %18
%6 = phi i64 [ %8, %18 ], [ %2, %3 ]
%7 = tail call i64 @fread(i8* %1, i32 1, i64 %6, i32* %0)
%8 = sub i64 %6, %7
%9 = tail call i64 @feof(i32* %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %5
%12 = tail call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %5
%14 = tail call i64 @ferror(i32* %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = icmp eq i64 %8, 0
br i1 %19, label %20, label %5
20: ; preds = %18, %3
ret void
}