#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
size_t safe_fread(void *buf, size_t sz, FILE *fp) {
  if (fread(buf, 1, sz, fp) != sz) {
    if (ferror(fp))
      do {
        fprintf(stderr, "Error %d reading from input file", ferror(fp));
        fputc('\n', stderr);
        exit(EXIT_FAILURE);
      } while (0);
    else
      do {
        fprintf(stderr, "Could not read requested amount from input file");
        fputc('\n', stderr);
        exit(EXIT_FAILURE);
      } while (0);
  }
  return sz;
}


x86 assembly (clang, -O0):
<safe_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x10(%rbp),%rax
je     1221 <safe_fread+0xd1>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
je     11e4 <safe_fread+0x94>
jmp    119b <safe_fread+0x4b>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    $0xa,%edi
call   1050 <fputc@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11e9 <safe_fread+0x99>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    $0xa,%edi
call   1050 <fputc@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fread>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %rbx,%rax
jne    117a <safe_fread+0x2a>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
call   1030 <ferror@plt>
mov    0x2e57(%rip),%rbx
mov    (%rbx),%edi
test   %eax,%eax
jne    119f <safe_fread+0x4f>
lea    0xe8b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11af <safe_fread+0x5f>
lea    0xe5a(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%esi
mov    $0xa,%edi
call   1050 <fputc@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"Error %d reading from input file\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [48 x i8] c"Could not read requested amount from input file\00"
define i64 @safe_fread(i8* %0, i64 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
%7 = load i8*, i8** %4
%8 = load i64, i64* %5
%9 = load i32*, i32** %6
%10 = call i64 @fread(i8* %7, i32 1, i64 %8, i32* %9)
%11 = load i64, i64* %5
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %38
13: ; preds = %3
%14 = load i32*, i32** %6
%15 = call i32 @ferror(i32* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %13
br label %18
18: ; preds = %17
%19 = load i32, i32* @stderr
%20 = load i32*, i32** %6
%21 = call i32 @ferror(i32* %20)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @stderr
%24 = call i32 @fputc(i8 signext 10, i32 %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; No predecessors!
br label %37
28: ; preds = %13
br label %29
29: ; preds = %28
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @stderr
%33 = call i32 @fputc(i8 signext 10, i32 %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; No predecessors!
br label %37
37: ; preds = %36, %27
br label %38
38: ; preds = %37, %3
%39 = load i64, i64* %5
ret i64 %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"Error %d reading from input file\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [48 x i8] c"Could not read requested amount from input file\00"
define i64 @safe_fread(i8* nocapture %0, i64 returned %1, i32* nocapture %2) {
%4 = tail call i64 @fread(i8* %0, i32 1, i64 %1, i32* %2)
%5 = icmp eq i64 %4, %1
br i1 %5, label %22, label %6
6: ; preds = %3
%7 = tail call i32 @ferror(i32* %2)
%8 = icmp eq i32 %7, 0
%9 = load i32, i32* @stderr
br i1 %8, label %16, label %10
10: ; preds = %6
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %7)
%12 = load i32, i32* @stderr
%13 = tail call i32 @fputc(i8 signext 10, i32 %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %6
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = tail call i32 @fputc(i8 signext 10, i32 %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %3
ret i64 %1
}