#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 scalar_t__ time_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(double *, int, int const, int /*<<< orphan*/ *);
scalar_t__ time(int /*<<< orphan*/ *);
int read_bin_chunky(const char *fname, double *data, const int ndata) {
  FILE *fp;
  time_t start, end;
  const int CHUNKSIZE = 1024;
  int nchunks = 1024 * 1024 * 128 / CHUNKSIZE;
  fp = fopen(fname, "rb");
  assert(fp);
  start = time(NULL);
  for (int i = 0; i < nchunks; i++) {
    int start = i * CHUNKSIZE;
    fread(&(data[start]), sizeof(double), CHUNKSIZE, fp);
  }
  end = time(NULL);
  fclose(fp);
  return (int)(end - start);
}


x86 assembly (clang, -O0):
<read_bin_chunky>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x400,-0x34(%rbp)
movl   $0x20000,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <assert@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    11e7 <read_bin_chunky+0x97>
mov    -0x3c(%rbp),%eax
shl    $0xa,%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x40(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rcx
mov    $0x8,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11a2 <read_bin_chunky+0x52>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_bin_chunky>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <assert@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <time@plt>
mov    %rax,%r15
xchg   %ax,%ax
lea    (%r14,%rbx,1),%rdi
mov    $0x8,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
add    $0x2000,%rbx
cmp    $0x40000000,%rbx
jne    1180 <read_bin_chunky+0x30>
xor    %edi,%edi
call   1060 <time@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1040 <fclose@plt>
sub    %r15d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i32 @read_bin_chunky(i8* %0, double* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca double*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store double* %1, double** %5
store i32 %2, i32* %6
store i32 1024, i32* %10
store i32 131072, i32* %11
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = call i32 @assert(i32* %16)
%18 = call i64 @time(i32* null)
store i64 %18, i64* %8
store i32 0, i32* %12
br label %19
19: ; preds = %32, %3
%20 = load i32, i32* %12
%21 = load i32, i32* %11
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %35
23: ; preds = %19
%24 = load i32, i32* %12
%25 = mul nsw i32 %24, 1024
store i32 %25, i32* %13
%26 = load double*, double** %5
%27 = load i32, i32* %13
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds double, double* %26, i64 %28
%30 = load i32*, i32** %7
%31 = call i32 @fread(double* %29, i32 8, i32 1024, i32* %30)
br label %32
32: ; preds = %23
%33 = load i32, i32* %12
%34 = add nsw i32 %33, 1
store i32 %34, i32* %12
br label %19
35: ; preds = %19
%36 = call i64 @time(i32* null)
store i64 %36, i64* %9
%37 = load i32*, i32** %7
%38 = call i32 @fclose(i32* %37)
%39 = load i64, i64* %9
%40 = load i64, i64* %8
%41 = sub nsw i64 %39, %40
%42 = trunc i64 %41 to i32
ret i32 %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @read_bin_chunky(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 = tail call i32 @assert(i32* %4)
%6 = tail call i64 @time(i32* null)
br label %12
7: ; preds = %12
%8 = tail call i64 @time(i32* null)
%9 = tail call i32 @fclose(i32* %4)
%10 = sub nsw i64 %8, %6
%11 = trunc i64 %10 to i32
ret i32 %11
12: ; preds = %3, %12
%13 = phi i64 [ 0, %3 ], [ %17, %12 ]
%14 = shl nuw nsw i64 %13, 10
%15 = getelementptr inbounds double, double* %1, i64 %14
%16 = tail call i32 @fread(double* %15, i32 8, i32 1024, i32* %4)
%17 = add nuw nsw i64 %13, 1
%18 = icmp eq i64 %17, 131072
br i1 %18, label %7, label %12
}