#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 */
double *calloc(int, int);
int /*<<< orphan*/ exit(int);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, double *);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ perror(char *);
double *readdoubles(FILE *fp, int *number) {
  struct stat info;
  double *x;
  int limit;
  int n = 0, i;
  fstat(fileno(fp), &info);
  if (info.st_size > 0) {
    limit = 2 * info.st_size / sizeof(double);
  } else {
    limit = 10000;
  }
  x = calloc(limit, sizeof(double));
  if (!x) {
    perror("double alloc");
    exit(3);
  }
  for (i = 0; i < limit; ++i) {
    fscanf(fp, "%lf", &x[i]);
    if (feof(fp))
      break;
    ++n;
  }
  *number = n;
  return x;
}


x86 assembly (clang, -O0):
<readdoubles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmpl   $0x0,-0x18(%rbp)
jle    11b9 <readdoubles+0x49>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
cltq
shr    $0x3,%rax
mov    %eax,-0x24(%rbp)
jmp    11c0 <readdoubles+0x50>
movl   $0x2710,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f2 <readdoubles+0x82>
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1253 <readdoubles+0xe3>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
lea    0xdee(%rip),%rsi
call   1030 <fscanf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     123c <readdoubles+0xcc>
jmp    1253 <readdoubles+0xe3>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11f9 <readdoubles+0x89>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readdoubles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1060 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    0x8(%rsp),%eax
mov    %eax,%ecx
sar    $0x2,%ecx
test   %eax,%eax
mov    $0x2710,%r14d
cmovg  %ecx,%r14d
mov    %r14d,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     122b <readdoubles+0xbb>
mov    %rax,%r15
test   %r14d,%r14d
jle    1212 <readdoubles+0xa2>
mov    %rbp,0x10(%rsp)
xor    %r12d,%r12d
lea    0xe3a(%rip),%r13
mov    %r15,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <fscanf@plt>
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    120b <readdoubles+0x9b>
add    $0x1,%r12
add    $0x8,%rbp
cmp    %r12,%r14
jne    11e0 <readdoubles+0x70>
mov    %r14d,%r12d
mov    0x10(%rsp),%rbp
jmp    1215 <readdoubles+0xa5>
xor    %r12d,%r12d
mov    %r12d,0x0(%rbp)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [13 x i8] c"double alloc\00"
@.str.1 = [4 x i8] c"%lf\00"
define double* @readdoubles(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.stat
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 0, i32* %8
%10 = load i32*, i32** %3
%11 = call i32 @fileno(i32* %10)
%12 = call i32 @fstat(i32 %11, %struct.stat* %5)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%14 = load i32, i32* %13
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%18 = load i32, i32* %17
%19 = mul nsw i32 2, %18
%20 = sext i32 %19 to i64
%21 = udiv i64 %20, 8
%22 = trunc i64 %21 to i32
store i32 %22, i32* %7
br label %24
23: ; preds = %2
store i32 10000, i32* %7
br label %24
24: ; preds = %23, %16
%25 = load i32, i32* %7
%26 = call double* @calloc(i32 %25, i32 8)
store double* %26, double** %6
%27 = load double*, double** %6
%28 = icmp ne double* %27, null
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 3)
unreachable
32: ; preds = %24
store i32 0, i32* %9
br label %33
33: ; preds = %51, %32
%34 = load i32, i32* %9
%35 = load i32, i32* %7
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %54
37: ; preds = %33
%38 = load i32*, i32** %3
%39 = load double*, double** %6
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %39, i64 %41
%43 = call i32 @fscanf(i32* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %42)
%44 = load i32*, i32** %3
%45 = call i64 @feof(i32* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %37
br label %54
48: ; preds = %37
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %51
51: ; preds = %48
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 1
store i32 %53, i32* %9
br label %33
54: ; preds = %47, %33
%55 = load i32, i32* %8
%56 = load i32*, i32** %4
store i32 %55, i32* %56
%57 = load double*, double** %6
ret double* %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [13 x i8] c"double alloc\00"
@.str.1 = [4 x i8] c"%lf\00"
define double* @readdoubles(i32* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @fileno(i32* %0)
%6 = call i32 @fstat(i32 %5, %struct.stat* %3)
%7 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%8 = load i32, i32* %7
%9 = icmp sgt i32 %8, 0
%10 = ashr i32 %8, 2
%11 = select i1 %9, i32 %10, i32 10000
%12 = tail call double* @calloc(i32 %11, i32 8)
%13 = icmp eq double* %12, null
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = icmp sgt i32 %11, 0
br i1 %15, label %16, label %32
16: ; preds = %14
%17 = zext i32 %11 to i64
br label %21
18: ; preds = %2
%19 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 3)
unreachable
21: ; preds = %16, %27
%22 = phi i64 [ 0, %16 ], [ %28, %27 ]
%23 = getelementptr inbounds double, double* %12, i64 %22
%24 = tail call i32 @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %23)
%25 = tail call i64 @feof(i32* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = add nuw nsw i64 %22, 1
%29 = icmp eq i64 %28, %17
br i1 %29, label %32, label %21
30: ; preds = %21
%31 = trunc i64 %22 to i32
br label %32
32: ; preds = %27, %30, %14
%33 = phi i32 [ 0, %14 ], [ %31, %30 ], [ %11, %27 ]
store i32 %33, i32* %1
ret double* %12
}