#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*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t const fread(void *, size_t const, size_t const, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int ReadData(const char *filename, void *data, const size_t size,
             const size_t n) {
  FILE *fd = fopen(filename, "rb");
  if (fd == NULL) {
    fprintf(stderr, "'fopen()' failed during call of 'ReadData()'.\n");
    return -1;
  }
  fseek(fd, 0, SEEK_END);
  long filesize = ftell(fd);
  rewind(fd);
  if ((size_t)filesize != n * size) {
    fprintf(stderr,
            "'ReadData()' failed: expected file size does not match.\n");
    return -2;
  }
  if (fread(data, size, n, fd) != n) {
    fprintf(stderr, "'fread()' failed during call of 'ReadData()'.\n");
    return -3;
  }
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<ReadData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c8 <ReadData+0x58>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <ReadData+0x109>
mov    -0x30(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
imul   -0x20(%rbp),%rcx
cmp    %rcx,%rax
je     1229 <ReadData+0xb9>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1279 <ReadData+0x109>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1030 <fread@plt>
cmp    -0x28(%rbp),%rax
je     1269 <ReadData+0xf9>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1279 <ReadData+0x109>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadData>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
lea    0xe77(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11ea <ReadData+0x7a>
mov    %rax,%rbx
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r14,%rax
imul   %r15,%rax
cmp    %rax,%r13
jne    1206 <ReadData+0x96>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r14,%rax
jne    1222 <ReadData+0xb2>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    123c <ReadData+0xcc>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    123c <ReadData+0xcc>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xfffffffe,%eax
jmp    123c <ReadData+0xcc>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xfffffffd,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [47 x i8] c"'fopen()' failed during call of 'ReadData()'.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [57 x i8] c"'ReadData()' failed: expected file size does not match.\0A\00"
@.str.3 = [47 x i8] c"'fread()' failed during call of 'ReadData()'.\0A\00"
define i32 @ReadData(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %10
%14 = load i32*, i32** %10
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %49
19: ; preds = %4
%20 = load i32*, i32** %10
%21 = load i32, i32* @SEEK_END
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i32*, i32** %10
%24 = call i64 @ftell(i32* %23)
store i64 %24, i64* %11
%25 = load i32*, i32** %10
%26 = call i32 @rewind(i32* %25)
%27 = load i64, i64* %11
%28 = load i64, i64* %9
%29 = load i64, i64* %8
%30 = mul i64 %28, %29
%31 = icmp ne i64 %27, %30
br i1 %31, label %32, label %35
32: ; preds = %19
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
store i32 -2, i32* %5
br label %49
35: ; preds = %19
%36 = load i8*, i8** %7
%37 = load i64, i64* %8
%38 = load i64, i64* %9
%39 = load i32*, i32** %10
%40 = call i64 @fread(i8* %36, i64 %37, i64 %38, i32* %39)
%41 = load i64, i64* %9
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
store i32 -3, i32* %5
br label %49
46: ; preds = %35
%47 = load i32*, i32** %10
%48 = call i32 @fclose(i32* %47)
store i32 0, i32* %5
br label %49
49: ; preds = %46, %43, %32, %16
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [47 x i8] c"'fopen()' failed during call of 'ReadData()'.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [57 x i8] c"'ReadData()' failed: expected file size does not match.\0A\00"
@.str.3 = [47 x i8] c"'fread()' failed during call of 'ReadData()'.\0A\00"
define i32 @ReadData(i8* nocapture %0, i8* nocapture %1, i64 %2, i64 %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %28
10: ; preds = %4
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %5, i32 0, i32 %11)
%13 = tail call i64 @ftell(i32* %5)
%14 = tail call i32 @rewind(i32* %5)
%15 = mul i64 %3, %2
%16 = icmp eq i64 %13, %15
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0))
br label %28
20: ; preds = %10
%21 = tail call i64 @fread(i8* %1, i64 %2, i64 %3, i32* %5)
%22 = icmp eq i64 %21, %3
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %28
26: ; preds = %20
%27 = tail call i32 @fclose(i32* %5)
br label %28
28: ; preds = %17, %23, %26, %7
%29 = phi i32 [ -1, %7 ], [ -2, %17 ], [ -3, %23 ], [ 0, %26 ]
ret i32 %29
}