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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
int load_data(char *filename, int *buf) {
  FILE *dataf = NULL;
  char *ret;
  char linebuf[5];
  dataf = fopen(filename, "r");
  if (!dataf) {
    fprintf(stderr, "Cannot open data file: %s\n", filename);
    perror("fopen");
    return (-1);
  }
  while (*buf == -1) {
    ret = fgets(linebuf, sizeof(linebuf) - 1, dataf);
    if (!ret) {
      fclose(dataf);
      return (0);
    }
    *buf = (int)strtol(linebuf, NULL, 10);
    buf++;
  }
  fclose(dataf);
  return (0);
}


x86 assembly (clang, -O0):
<load_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c8 <load_data+0x68>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe57(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe66(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <load_data+0xeb>
jmp    11cd <load_data+0x6d>
mov    -0x18(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    123b <load_data+0xdb>
lea    -0x2d(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x4,%esi
call   1040 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1210 <load_data+0xb0>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    124b <load_data+0xeb>
lea    -0x2d(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
jmp    11cd <load_data+0x6d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_data>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e4 <load_data+0x84>
mov    %rax,%r14
cmpl   $0xffffffff,(%rbx)
jne    11d0 <load_data+0x70>
add    $0x4,%rbx
lea    0xb(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x4,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11d0 <load_data+0x70>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x4(%rbx)
cmpl   $0xffffffff,(%rbx)
lea    0x4(%rbx),%rbx
je     11a0 <load_data+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    11da <load_data+0x7a>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Cannot open data file: %s\0A\00"
@.str.2 = [6 x i8] c"fopen\00"
define i32 @load_data(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [5 x i8]
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* null, i32** %6
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
18: ; preds = %2
br label %19
19: ; preds = %32, %18
%20 = load i32*, i32** %5
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %39
23: ; preds = %19
%24 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%25 = load i32*, i32** %6
%26 = call i8* @fgets(i8* %24, i32 4, i32* %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = icmp ne i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %23
%30 = load i32*, i32** %6
%31 = call i32 @fclose(i32* %30)
store i32 0, i32* %3
br label %42
32: ; preds = %23
%33 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%34 = call i64 @strtol(i8* %33, i32* null, i32 10)
%35 = trunc i64 %34 to i32
%36 = load i32*, i32** %5
store i32 %35, i32* %36
%37 = load i32*, i32** %5
%38 = getelementptr inbounds i32, i32* %37, i32 1
store i32* %38, i32** %5
br label %19
39: ; preds = %19
%40 = load i32*, i32** %6
%41 = call i32 @fclose(i32* %40)
store i32 0, i32* %3
br label %42
42: ; preds = %39, %29, %13
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Cannot open data file: %s\0A\00"
@.str.2 = [6 x i8] c"fopen\00"
define i32 @load_data(i8* %0, i32* nocapture %1) {
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %14, label %26
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %28
14: ; preds = %7, %20
%15 = phi i32* [ %23, %20 ], [ %1, %7 ]
%16 = call i8* @fgets(i8* %4, i32 4, i32* %5)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @fclose(i32* %5)
br label %28
20: ; preds = %14
%21 = call i64 @strtol(i8* nocapture %4, i32* null, i32 10)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %15
%23 = getelementptr inbounds i32, i32* %15, i64 1
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, -1
br i1 %25, label %14, label %26
26: ; preds = %20, %7
%27 = call i32 @fclose(i32* %5)
br label %28
28: ; preds = %26, %18, %10
%29 = phi i32 [ 0, %18 ], [ 0, %26 ], [ -1, %10 ]
ret i32 %29
}