#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 *);
size_t fread(double *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int ReadDoubleData(const char *filename, double *data, int n) {
  FILE *fd = fopen(filename, "rb");
  if (fd == NULL) {
    perror("'fopen' failed.");
    return -1;
  }
  fseek(fd, 0, SEEK_END);
  long filesize = ftell(fd);
  rewind(fd);
  if (filesize != (long)(n * sizeof(double))) {
    perror("File size does not match.");
    return -2;
  }
  if (fread(data, sizeof(double), n, fd) != (size_t)n) {
    perror("'fread' failed.");
    return -3;
  }
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<ReadDoubleData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ba <ReadDoubleData+0x4a>
lea    0xe5a(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <ReadDoubleData+0xeb>
mov    -0x28(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x3,%rcx
cmp    %rcx,%rax
je     1211 <ReadDoubleData+0xa1>
lea    0xe13(%rip),%rdi
call   1090 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    125b <ReadDoubleData+0xeb>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x8,%esi
call   1030 <fread@plt>
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
je     124b <ReadDoubleData+0xdb>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    125b <ReadDoubleData+0xeb>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadDoubleData>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
lea    0xe7d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f2 <ReadDoubleData+0x82>
mov    %rax,%rbx
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <rewind@plt>
movslq %ebp,%rbp
lea    0x0(,%rbp,8),%rax
cmp    %rax,%r15
jne    1205 <ReadDoubleData+0x95>
mov    %r14,%rdi
mov    $0x8,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %rbp,%rax
jne    1218 <ReadDoubleData+0xa8>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0a(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    11e7 <ReadDoubleData+0x77>
lea    0xe07(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffffe,%eax
jmp    11e7 <ReadDoubleData+0x77>
lea    0xe0e(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffffd,%eax
jmp    11e7 <ReadDoubleData+0x77>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [16 x i8] c"'fopen' failed.\00"
@SEEK_END = global i32 0
@.str.2 = [26 x i8] c"File size does not match.\00"
@.str.3 = [16 x i8] c"'fread' failed.\00"
define i32 @ReadDoubleData(i8* nocapture %0, double* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %25
8: ; preds = %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %4, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %4)
%12 = tail call i32 @rewind(i32* %4)
%13 = sext i32 %2 to i64
%14 = shl nsw i64 %13, 3
%15 = icmp eq i64 %11, %14
br i1 %15, label %18, label %16
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %25
18: ; preds = %8
%19 = tail call i64 @fread(double* %1, i32 8, i32 %2, i32* %4)
%20 = icmp eq i64 %19, %13
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %25
23: ; preds = %18
%24 = tail call i32 @fclose(i32* %4)
br label %25
25: ; preds = %16, %21, %23, %6
%26 = phi i32 [ -1, %6 ], [ -2, %16 ], [ -3, %21 ], [ 0, %23 ]
ret i32 %26
}