#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(double *, int, int, int /*<<< orphan*/ *);
double *malloc(int);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
double *get_doubles(int ndoubles, const char *filename) {
  struct stat buf;
  if (stat(filename, &buf) < 0) {
    fprintf(stderr, "the file %s was not found\n", filename);
    return NULL;
  }
  if (buf.st_size != ndoubles * sizeof(double)) {
    fprintf(stderr, "unexpected number of bytes ");
    fprintf(stderr, "in the file %s\n", filename);
    return NULL;
  }
  FILE *fin = fopen(filename, "rb");
  if (!fin) {
    fprintf(stderr, "failed to open the file ");
    fprintf(stderr, "%s for reading\n", filename);
    return NULL;
  }
  double *arr = malloc(ndoubles * sizeof(double));
  size_t nbytes = fread(arr, sizeof(double), ndoubles, fin);
  fclose(fin);
  if (!nbytes) {
    fprintf(stderr, "no bytes read from %s\n", filename);
    return NULL;
  }
  return arr;
}


x86 assembly (clang, -O0):
<get_doubles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11ae <get_doubles+0x4e>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <get_doubles+0x170>
movslq -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x3,%rcx
cmp    %rcx,%rax
je     1202 <get_doubles+0xa2>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <get_doubles+0x170>
mov    -0x18(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1260 <get_doubles+0x100>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <get_doubles+0x170>
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x8,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x38(%rbp)
jne    12c8 <get_doubles+0x168>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdbf(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <get_doubles+0x170>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_doubles>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rsi
mov    %r14,%rdi
call   1060 <stat@plt>
test   %rax,%rax
js     11d8 <get_doubles+0x78>
movslq (%rsp),%rbx
movslq %ebp,%rax
shl    $0x3,%rax
cmp    %rbx,%rax
jne    11e1 <get_doubles+0x81>
lea    0xeb4(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f1 <get_doubles+0x91>
mov    %rax,%r15
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x8,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %rbp,%rbp
jne    1227 <get_doubles+0xc7>
lea    0xe9d(%rip),%rbp
jmp    120f <get_doubles+0xaf>
lea    0xe21(%rip),%rbp
jmp    120f <get_doubles+0xaf>
lea    0xe4f(%rip),%rbp
lea    0xe2c(%rip),%rsi
jmp    11ff <get_doubles+0x9f>
lea    0xe6b(%rip),%rbp
lea    0xe4b(%rip),%rsi
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
mov    %rbp,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"the file %s was not found\0A\00"
@.str.1 = [28 x i8] c"unexpected number of bytes \00"
@.str.2 = [16 x i8] c"in the file %s\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [25 x i8] c"failed to open the file \00"
@.str.5 = [16 x i8] c"%s for reading\0A\00"
@.str.6 = [23 x i8] c"no bytes read from %s\0A\00"
define double* @get_doubles(i32 %0, i8* %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32*
%8 = alloca double*
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i64 @stat(i8* %10, %struct.stat* %6)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %5
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %15)
store double* null, double** %3
br label %62
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = sext i32 %19 to i64
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 8
%24 = icmp ne i64 %20, %23
br i1 %24, label %25, label %31
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %5
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %29)
store double* null, double** %3
br label %62
31: ; preds = %17
%32 = load i8*, i8** %5
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %33, i32** %7
%34 = load i32*, i32** %7
%35 = icmp ne i32* %34, null
br i1 %35, label %42, label %36
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %5
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %40)
store double* null, double** %3
br label %62
42: ; preds = %31
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = mul i64 %44, 8
%46 = trunc i64 %45 to i32
%47 = call double* @malloc(i32 %46)
store double* %47, double** %8
%48 = load double*, double** %8
%49 = load i32, i32* %4
%50 = load i32*, i32** %7
%51 = call i64 @fread(double* %48, i32 8, i32 %49, i32* %50)
store i64 %51, i64* %9
%52 = load i32*, i32** %7
%53 = call i32 @fclose(i32* %52)
%54 = load i64, i64* %9
%55 = icmp ne i64 %54, 0
br i1 %55, label %60, label %56
56: ; preds = %42
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %5
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %58)
store double* null, double** %3
br label %62
60: ; preds = %42
%61 = load double*, double** %8
store double* %61, double** %3
br label %62
62: ; preds = %60, %56, %36, %25, %13
%63 = load double*, double** %3
ret double* %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [27 x i8] c"the file %s was not found\0A\00"
@.str.1 = [28 x i8] c"unexpected number of bytes \00"
@.str.2 = [16 x i8] c"in the file %s\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [25 x i8] c"failed to open the file \00"
@.str.5 = [16 x i8] c"%s for reading\0A\00"
@.str.6 = [23 x i8] c"no bytes read from %s\0A\00"
define noalias double* @get_doubles(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %1, %struct.stat* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = sext i32 %9 to i64
%11 = sext i32 %0 to i64
%12 = shl nsw i64 %11, 3
%13 = icmp eq i64 %12, %10
br i1 %13, label %14, label %22
14: ; preds = %7
%15 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = call double* @malloc(i32 %9)
%19 = call i64 @fread(double* %18, i32 8, i32 %0, i32* %15)
%20 = call i32 @fclose(i32* %15)
%21 = icmp eq i64 %19, 0
br i1 %21, label %27, label %31
22: ; preds = %14, %7
%23 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %7 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), %14 ]
%24 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %7 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), %14 ]
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* %23)
br label %27
27: ; preds = %22, %17, %2
%28 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), %17 ], [ %24, %22 ]
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* %28, i8* %1)
br label %31
31: ; preds = %27, %17
%32 = phi double* [ %18, %17 ], [ null, %27 ]
ret double* %32
}