#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__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int populate_array(int array[], char *fileName) {
  int i = 0;
  FILE *fp = fopen(fileName, "r");
  if (fp == NULL) {
    return -1;
  }
  while (1) {
    fscanf(fp, "%d", &array[i++]);
    if (feof(fp)) {
      break;
    }
  }
  return i;
}


x86 assembly (clang, -O0):
<populate_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xeae(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1172 <populate_array+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <populate_array+0x92>
jmp    1177 <populate_array+0x47>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cltq
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe68(%rip),%rsi
call   1030 <fscanf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%rax
je     11b7 <populate_array+0x87>
jmp    11bc <populate_array+0x8c>
jmp    1177 <populate_array+0x47>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<populate_array>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%rbx
lea    0xebc(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1184 <populate_array+0x54>
mov    %rax,%r14
xor    %ebp,%ebp
lea    0xea5(%rip),%r15
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <fscanf@plt>
mov    %r14,%rdi
call   1040 <feof@plt>
add    $0x4,%rbx
add    $0x1,%ebp
test   %rax,%rax
je     1160 <populate_array+0x30>
jmp    1189 <populate_array+0x59>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
define i32 @populate_array(i32* %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %14, label %5
5: ; preds = %2, %5
%6 = phi i64 [ %7, %5 ], [ 0, %2 ]
%7 = add nuw i64 %6, 1
%8 = getelementptr inbounds i32, i32* %0, i64 %6
%9 = tail call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %8)
%10 = tail call i64 @feof(i32* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %5, label %12
12: ; preds = %5
%13 = trunc i64 %7 to i32
br label %14
14: ; preds = %12, %2
%15 = phi i32 [ -1, %2 ], [ %13, %12 ]
ret i32 %15
}