#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 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;
  if (!fstat(fileno(fp), &info) && 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) {
    if (fscanf(fp, "%lf", &x[i]) != 1 || 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>
cmp    $0x0,%eax
jne    11c2 <readdoubles+0x52>
cmpl   $0x0,-0x18(%rbp)
jle    11c2 <readdoubles+0x52>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
cltq
shr    $0x3,%rax
mov    %eax,-0x24(%rbp)
jmp    11c9 <readdoubles+0x59>
movl   $0x2710,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fb <readdoubles+0x8b>
lea    0xe14(%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    1265 <readdoubles+0xf5>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
lea    0xde5(%rip),%rsi
call   1030 <fscanf@plt>
cmp    $0x1,%eax
jne    1249 <readdoubles+0xd9>
mov    -0x8(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     124e <readdoubles+0xde>
jmp    1265 <readdoubles+0xf5>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1202 <readdoubles+0x92>
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,%r12
call   1060 <fileno@plt>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    0x10(%rsp),%ecx
mov    %ecx,%r14d
sar    $0x2,%r14d
test   %ecx,%ecx
mov    $0x2710,%ecx
cmovle %ecx,%r14d
test   %eax,%eax
cmovne %ecx,%r14d
mov    %r14d,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     122e <readdoubles+0xbe>
mov    %rax,%r15
test   %r14d,%r14d
jle    1217 <readdoubles+0xa7>
mov    %rbp,0x8(%rsp)
xor    %ebx,%ebx
lea    0xe34(%rip),%r13
mov    %r15,%rbp
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <fscanf@plt>
cmp    $0x1,%eax
jne    1210 <readdoubles+0xa0>
mov    %r12,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    1210 <readdoubles+0xa0>
add    $0x1,%rbx
add    $0x8,%rbp
cmp    %rbx,%r14
jne    11e0 <readdoubles+0x70>
mov    %r14d,%ebx
mov    0x8(%rsp),%rbp
jmp    1219 <readdoubles+0xa9>
xor    %ebx,%ebx
mov    %ebx,0x0(%rbp)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcb(%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 = icmp ne i32 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%16 = load i32, i32* %15
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = mul nsw i32 2, %20
%22 = sext i32 %21 to i64
%23 = udiv i64 %22, 8
%24 = trunc i64 %23 to i32
store i32 %24, i32* %7
br label %26
25: ; preds = %14, %2
store i32 10000, i32* %7
br label %26
26: ; preds = %25, %18
%27 = load i32, i32* %7
%28 = call double* @calloc(i32 %27, i32 8)
store double* %28, double** %6
%29 = load double*, double** %6
%30 = icmp ne double* %29, null
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 3)
unreachable
34: ; preds = %26
store i32 0, i32* %9
br label %35
35: ; preds = %55, %34
%36 = load i32, i32* %9
%37 = load i32, i32* %7
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %58
39: ; preds = %35
%40 = load i32*, i32** %3
%41 = load double*, double** %6
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds double, double* %41, i64 %43
%45 = call i32 @fscanf(i32* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %44)
%46 = icmp ne i32 %45, 1
br i1 %46, label %51, label %47
47: ; preds = %39
%48 = load i32*, i32** %3
%49 = call i64 @feof(i32* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %47, %39
br label %58
52: ; preds = %47
%53 = load i32, i32* %8
%54 = add nsw i32 %53, 1
store i32 %54, i32* %8
br label %55
55: ; preds = %52
%56 = load i32, i32* %9
%57 = add nsw i32 %56, 1
store i32 %57, i32* %9
br label %35
58: ; preds = %51, %35
%59 = load i32, i32* %8
%60 = load i32*, i32** %4
store i32 %59, i32* %60
%61 = load double*, double** %6
ret double* %61
}

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 = icmp eq i32 %6, 0
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = icmp sgt i32 %9, 0
%11 = select i1 %7, i1 %10, i1 false
%12 = ashr i32 %9, 2
%13 = select i1 %11, i32 %12, i32 10000
%14 = tail call double* @calloc(i32 %13, i32 8)
%15 = icmp eq double* %14, null
br i1 %15, label %20, label %16
16: ; preds = %2
%17 = icmp sgt i32 %13, 0
br i1 %17, label %18, label %38
18: ; preds = %16
%19 = zext i32 %13 to i64
br label %23
20: ; preds = %2
%21 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 3)
unreachable
23: ; preds = %18, %31
%24 = phi i64 [ 0, %18 ], [ %32, %31 ]
%25 = getelementptr inbounds double, double* %14, i64 %24
%26 = tail call i32 @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double* %25)
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = tail call i64 @feof(i32* %0)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = add nuw nsw i64 %24, 1
%33 = icmp eq i64 %32, %19
br i1 %33, label %38, label %23
34: ; preds = %23
%35 = trunc i64 %24 to i32
br label %38
36: ; preds = %28
%37 = trunc i64 %24 to i32
br label %38
38: ; preds = %31, %34, %36, %16
%39 = phi i32 [ 0, %16 ], [ %35, %34 ], [ %37, %36 ], [ %13, %31 ]
store i32 %39, i32* %1
ret double* %14
}