#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
double **readImages() {
  size_t result;
  double **X = (double **)malloc(784 * sizeof(double *));
  for (int i = 0; i < 784; i++) {
    X[i] = (double *)malloc(5000 * sizeof(double));
  }
  FILE *in;
  in = fopen("train-images.idx3-ubyte", "rb");
  unsigned char buffer[784];
  result = fread(buffer, 1, 16, in);
  for (int i = 0; i < 5000; i++) {
    result = fread(buffer, 1, 784, in);
    for (int j = 0; j < 784; j++) {
      X[j][i] = (double)(buffer[j]);
    }
  }
  fclose(in);
  return X;
}


x86 assembly (clang, -O0):
<readImages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
mov    $0x1880,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x310,-0x14(%rbp)
jge    1194 <readImages+0x54>
mov    $0x9c40,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1160 <readImages+0x20>
lea    0xe65(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x330(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x334(%rbp)
cmpl   $0x1388,-0x334(%rbp)
jge    1276 <readImages+0x136>
lea    -0x330(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x310,%edx
call   1030 <fread@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x338(%rbp)
cmpl   $0x310,-0x338(%rbp)
jge    125d <readImages+0x11d>
movslq -0x338(%rbp),%rax
movzbl -0x330(%rbp,%rax,1),%eax
ff
cvtsi2sd %eax,%xmm0
mov    -0x10(%rbp),%rax
movslq -0x338(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x334(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x338(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x338(%rbp)
jmp    120b <readImages+0xcb>
jmp    1262 <readImages+0x122>
mov    -0x334(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x334(%rbp)
jmp    11d3 <readImages+0x93>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readImages>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    $0x1880,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
xor    %ebx,%ebx
nopl   (%rax)
mov    $0x9c40,%edi
call   1050 <malloc@plt>
mov    %rax,(%r12,%rbx,8)
add    $0x1,%rbx
cmp    $0x310,%rbx
jne    1160 <readImages+0x20>
lea    0xe7e(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x310,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    $0x3,%eax
nopw   0x0(%rax,%rax,1)
movzbl -0x3(%rsp,%rax,1),%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
mov    -0x18(%r12,%rax,8),%rcx
movsd  %xmm0,(%rcx,%rbx,8)
movzbl -0x2(%rsp,%rax,1),%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
mov    -0x10(%r12,%rax,8),%rcx
movsd  %xmm0,(%rcx,%rbx,8)
movzbl -0x1(%rsp,%rax,1),%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
mov    -0x8(%r12,%rax,8),%rcx
movsd  %xmm0,(%rcx,%rbx,8)
movzbl (%rsp,%rax,1),%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
mov    (%r12,%rax,8),%rcx
movsd  %xmm0,(%rcx,%rbx,8)
add    $0x4,%rax
cmp    $0x313,%rax
jne    11d0 <readImages+0x90>
add    $0x1,%rbx
cmp    $0x1388,%rbx
jne    11b0 <readImages+0x70>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12,%rax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"train-images.idx3-ubyte\00"
@.str.1 = [3 x i8] c"rb\00"
define double** @readImages() {
%1 = alloca i64
%2 = alloca double**
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [784 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = call i64 @malloc(i32 6272)
%9 = inttoptr i64 %8 to double**
store double** %9, double*** %2
store i32 0, i32* %3
br label %10
10: ; preds = %20, %0
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 784
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = call i64 @malloc(i32 40000)
%15 = inttoptr i64 %14 to double*
%16 = load double**, double*** %2
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds double*, double** %16, i64 %18
store double* %15, double** %19
br label %20
20: ; preds = %13
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %10
23: ; preds = %10
%24 = call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %4
%25 = getelementptr inbounds [784 x i8], [784 x i8]* %5, i64 0, i64 0
%26 = load i32*, i32** %4
%27 = call i64 @fread(i8* %25, i32 1, i32 16, i32* %26)
store i64 %27, i64* %1
store i32 0, i32* %6
br label %28
28: ; preds = %56, %23
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 5000
br i1 %30, label %31, label %59
31: ; preds = %28
%32 = getelementptr inbounds [784 x i8], [784 x i8]* %5, i64 0, i64 0
%33 = load i32*, i32** %4
%34 = call i64 @fread(i8* %32, i32 1, i32 784, i32* %33)
store i64 %34, i64* %1
store i32 0, i32* %7
br label %35
35: ; preds = %52, %31
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 784
br i1 %37, label %38, label %55
38: ; preds = %35
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [784 x i8], [784 x i8]* %5, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = uitofp i8 %42 to double
%44 = load double**, double*** %2
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double*, double** %44, i64 %46
%48 = load double*, double** %47
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds double, double* %48, i64 %50
store double %43, double* %51
br label %52
52: ; preds = %38
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
store i32 %54, i32* %7
br label %35
55: ; preds = %35
br label %56
56: ; preds = %55
%57 = load i32, i32* %6
%58 = add nsw i32 %57, 1
store i32 %58, i32* %6
br label %28
59: ; preds = %28
%60 = load i32*, i32** %4
%61 = call i32 @fclose(i32* %60)
%62 = load double**, double*** %2
ret double** %62
}

LLVM IR (-O2):
@.str = [24 x i8] c"train-images.idx3-ubyte\00"
@.str.1 = [3 x i8] c"rb\00"
define double** @readImages() {
%1 = alloca [784 x i8]
%2 = tail call i64 @malloc(i32 6272)
%3 = inttoptr i64 %2 to double**
br label %8
4: ; preds = %8
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = getelementptr inbounds [784 x i8], [784 x i8]* %1, i64 0, i64 0
%7 = call i64 @fread(i8* %6, i32 1, i32 16, i32* %5)
br label %17
8: ; preds = %0, %8
%9 = phi i64 [ 0, %0 ], [ %13, %8 ]
%10 = tail call i64 @malloc(i32 40000)
%11 = inttoptr i64 %10 to double*
%12 = getelementptr inbounds double*, double** %3, i64 %9
store double* %11, double** %12
%13 = add nuw nsw i64 %9, 1
%14 = icmp eq i64 %13, 784
br i1 %14, label %4, label %8
15: ; preds = %20
%16 = tail call i32 @fclose(i32* %5)
ret double** %3
17: ; preds = %4, %20
%18 = phi i64 [ 0, %4 ], [ %21, %20 ]
%19 = call i64 @fread(i8* %6, i32 1, i32 784, i32* %5)
br label %23
20: ; preds = %23
%21 = add nuw nsw i64 %18, 1
%22 = icmp eq i64 %21, 5000
br i1 %22, label %15, label %17
23: ; preds = %23, %17
%24 = phi i64 [ 0, %17 ], [ %52, %23 ]
%25 = getelementptr inbounds [784 x i8], [784 x i8]* %1, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = uitofp i8 %26 to double
%28 = getelementptr inbounds double*, double** %3, i64 %24
%29 = load double*, double** %28
%30 = getelementptr inbounds double, double* %29, i64 %18
store double %27, double* %30
%31 = or i64 %24, 1
%32 = getelementptr inbounds [784 x i8], [784 x i8]* %1, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = uitofp i8 %33 to double
%35 = getelementptr inbounds double*, double** %3, i64 %31
%36 = load double*, double** %35
%37 = getelementptr inbounds double, double* %36, i64 %18
store double %34, double* %37
%38 = or i64 %24, 2
%39 = getelementptr inbounds [784 x i8], [784 x i8]* %1, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = uitofp i8 %40 to double
%42 = getelementptr inbounds double*, double** %3, i64 %38
%43 = load double*, double** %42
%44 = getelementptr inbounds double, double* %43, i64 %18
store double %41, double* %44
%45 = or i64 %24, 3
%46 = getelementptr inbounds [784 x i8], [784 x i8]* %1, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = uitofp i8 %47 to double
%49 = getelementptr inbounds double*, double** %3, i64 %45
%50 = load double*, double** %49
%51 = getelementptr inbounds double, double* %50, i64 %18
store double %48, double* %51
%52 = add nuw nsw i64 %24, 4
%53 = icmp eq i64 %52, 784
br i1 %53, label %20, label %23
}