#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 */
scalar_t__ EOF;
int countLines(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ malloc(int);
int readArray(char *filename, int **array) {
  FILE *file = fopen(filename, "r");
  int lines = countLines(file);
  *array = (int *)malloc(lines * sizeof(int));
  int line = 0;
  while (fscanf(file, "%d\n", (*array) + line) != EOF) {
    line++;
  }
  fclose(file);
  return lines;
}


x86 assembly (clang, -O0):
<readArray>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <countLines@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe45(%rip),%rsi
call   1040 <fscanf@plt>
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
je     11e0 <readArray+0x90>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a0 <readArray+0x50>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readArray>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe9b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <countLines@plt>
mov    %eax,%r15d
lea    0x0(,%r15,4),%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1040 <fscanf@plt>
mov    0x2e47(%rip),%r13
cmp    0x0(%r13),%rax
je     11db <readArray+0x8b>
mov    $0x4,%ebp
lea    0xe4f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r14),%rdx
add    %rbp,%rdx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <fscanf@plt>
add    $0x4,%rbp
cmp    0x0(%r13),%rax
jne    11c0 <readArray+0x70>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
define i32 @readArray(i8* %0, i32** %1) {
%3 = alloca i8*
%4 = alloca i32**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32** %1, i32*** %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = call i32 @countLines(i32* %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 4
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
%18 = load i32**, i32*** %4
store i32* %17, i32** %18
store i32 0, i32* %7
br label %19
19: ; preds = %29, %2
%20 = load i32*, i32** %5
%21 = load i32**, i32*** %4
%22 = load i32*, i32** %21
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
%26 = call i64 @fscanf(i32* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %25)
%27 = load i64, i64* @EOF
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %19
32: ; preds = %19
%33 = load i32*, i32** %5
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %6
ret i32 %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
define i32 @readArray(i8* nocapture %0, i32** nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @countLines(i32* %3)
%5 = shl i32 %4, 2
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i32*
store i32* %7, i32** %1
%8 = tail call i64 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %7)
%9 = load i64, i64* @EOF
%10 = icmp eq i64 %8, %9
br i1 %10, label %19, label %11
11: ; preds = %2, %11
%12 = phi i64 [ %13, %11 ], [ 0, %2 ]
%13 = add nuw i64 %12, 1
%14 = load i32*, i32** %1
%15 = getelementptr inbounds i32, i32* %14, i64 %13
%16 = tail call i64 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %15)
%17 = load i64, i64* @EOF
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %11
19: ; preds = %11, %2
%20 = tail call i32 @fclose(i32* %3)
ret i32 %4
}