#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 *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ perror(char *);
int *realloc(int *, int);
int /*<<< orphan*/ stderr;
int readfile(int *array[], const char *filename) {
  FILE *fp;
  int *A;
  size_t bytes_read = 0;
  int limit, i, d;
  int read;
  fp = fopen(filename, "rb");
  if (fp == NULL) {
    fprintf(stderr, "Failed to open file %s\n", filename);
    return -1;
  }
  limit = 10;
  A = calloc(limit, sizeof(int));
  if (!A) {
    perror("calloc");
    goto err_close;
  }
  i = 0;
  while (!feof(fp)) {
    read = fread(&d, sizeof(int), 1, fp);
    if (read == 0)
      break;
    A[i++] = d;
    if (i == limit) {
      limit *= 2;
      A = realloc(A, limit * sizeof(int));
    }
  }
  A = realloc(A, i * sizeof(int));
  fclose(fp);
  *array = A;
  return i;
err_free:
  if (A)
    free(A);
err_close:
  fclose(fp);
  return -1;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dc <readfile+0x5c>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe38(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <readfile+0x16d>
movl   $0xa,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1210 <readfile+0x90>
lea    0xe15(%rip),%rdi
call   10a0 <perror@plt>
jmp    12dd <readfile+0x15d>
movl   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1235 <readfile+0xb5>
jmp    12a7 <readfile+0x127>
mov    -0x20(%rbp),%rcx
lea    -0x3c(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jne    125e <readfile+0xde>
jmp    12a7 <readfile+0x127>
mov    -0x3c(%rbp),%edx
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x38(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jne    12a2 <readfile+0x122>
mov    -0x34(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
movslq -0x34(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x28(%rbp)
jmp    1217 <readfile+0x97>
mov    -0x28(%rbp),%rdi
movslq -0x38(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ed <readfile+0x16d>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1242 <readfile+0xc2>
mov    %rax,%r15
mov    $0xa,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     1294 <readfile+0x114>
mov    %rax,%r12
mov    %r15,%rdi
call   1070 <feof@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    1266 <readfile+0xe6>
mov    %r14,0x10(%rsp)
mov    $0xa,%ebp
xor    %ebx,%ebx
lea    0xc(%rsp),%r13
jmp    11fc <readfile+0x7c>
nop
mov    %r15,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1261 <readfile+0xe1>
mov    %r13,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     1261 <readfile+0xe1>
mov    0xc(%rsp),%eax
mov    %eax,(%r12,%rbx,4)
add    $0x1,%rbx
mov    %ebp,%eax
cmp    %rax,%rbx
jne    11f0 <readfile+0x70>
lea    0x0(%rbp,%rbp,1),%r14d
shl    $0x3,%ebp
mov    %r12,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,%r12
mov    %r14d,%ebp
jmp    11f0 <readfile+0x70>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1283 <readfile+0x103>
mov    0x10(%rsp),%r14
lea    0x0(,%rbx,4),%esi
mov    %r12,%rdi
call   1080 <realloc@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbp,(%r14)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd80(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%ebx
jmp    1283 <readfile+0x103>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Failed to open file %s\0A\00"
@.str.2 = [7 x i8] c"calloc\00"
define i32 @readfile(i32** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32**
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32** %0, i32*** %4
store i8* %1, i8** %5
store i64 0, i64* %8
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %5
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 -1, i32* %3
br label %82
21: ; preds = %2
store i32 10, i32* %9
%22 = load i32, i32* %9
%23 = call i32* @calloc(i32 %22, i32 4)
store i32* %23, i32** %7
%24 = load i32*, i32** %7
%25 = icmp ne i32* %24, null
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %79
28: ; preds = %21
store i32 0, i32* %10
br label %29
29: ; preds = %59, %28
%30 = load i32*, i32** %6
%31 = call i32 @feof(i32* %30)
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br i1 %33, label %34, label %60
34: ; preds = %29
%35 = load i32*, i32** %6
%36 = call i32 @fread(i32* %11, i32 4, i32 1, i32* %35)
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
br label %60
40: ; preds = %34
%41 = load i32, i32* %11
%42 = load i32*, i32** %7
%43 = load i32, i32* %10
%44 = add nsw i32 %43, 1
store i32 %44, i32* %10
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds i32, i32* %42, i64 %45
store i32 %41, i32* %46
%47 = load i32, i32* %10
%48 = load i32, i32* %9
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %59
50: ; preds = %40
%51 = load i32, i32* %9
%52 = mul nsw i32 %51, 2
store i32 %52, i32* %9
%53 = load i32*, i32** %7
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = mul i64 %55, 4
%57 = trunc i64 %56 to i32
%58 = call i32* @realloc(i32* %53, i32 %57)
store i32* %58, i32** %7
br label %59
59: ; preds = %50, %40
br label %29
60: ; preds = %39, %29
%61 = load i32*, i32** %7
%62 = load i32, i32* %10
%63 = sext i32 %62 to i64
%64 = mul i64 %63, 4
%65 = trunc i64 %64 to i32
%66 = call i32* @realloc(i32* %61, i32 %65)
store i32* %66, i32** %7
%67 = load i32*, i32** %6
%68 = call i32 @fclose(i32* %67)
%69 = load i32*, i32** %7
%70 = load i32**, i32*** %4
store i32* %69, i32** %70
%71 = load i32, i32* %10
store i32 %71, i32* %3
br label %82
72: ; No predecessors!
%73 = load i32*, i32** %7
%74 = icmp ne i32* %73, null
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32*, i32** %7
%77 = call i32 @free(i32* %76)
br label %78
78: ; preds = %75, %72
br label %79
79: ; preds = %78, %26
%80 = load i32*, i32** %6
%81 = call i32 @fclose(i32* %80)
store i32 -1, i32* %3
br label %82
82: ; preds = %79, %60, %17
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Failed to open file %s\0A\00"
@.str.2 = [7 x i8] c"calloc\00"
define i32 @readfile(i32** nocapture writeonly %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %50
10: ; preds = %2
%11 = tail call i32* @calloc(i32 10, i32 4)
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @feof(i32* %5)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %44
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @fclose(i32* %5)
br label %50
19: ; preds = %13, %35
%20 = phi i64 [ %27, %35 ], [ 0, %13 ]
%21 = phi i32 [ %37, %35 ], [ 10, %13 ]
%22 = phi i32* [ %36, %35 ], [ %11, %13 ]
%23 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %5)
%24 = icmp eq i32 %23, 0
br i1 %24, label %40, label %25
25: ; preds = %19
%26 = load i32, i32* %3
%27 = add nuw i64 %20, 1
%28 = getelementptr inbounds i32, i32* %22, i64 %20
store i32 %26, i32* %28
%29 = zext i32 %21 to i64
%30 = icmp eq i64 %27, %29
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = shl nsw i32 %21, 1
%33 = shl i32 %21, 3
%34 = tail call i32* @realloc(i32* %22, i32 %33)
br label %35
35: ; preds = %31, %25
%36 = phi i32* [ %34, %31 ], [ %22, %25 ]
%37 = phi i32 [ %32, %31 ], [ %21, %25 ]
%38 = tail call i32 @feof(i32* %5)
%39 = icmp eq i32 %38, 0
br i1 %39, label %19, label %40
40: ; preds = %19, %35
%41 = phi i32* [ %36, %35 ], [ %22, %19 ]
%42 = phi i64 [ %27, %35 ], [ %20, %19 ]
%43 = trunc i64 %42 to i32
br label %44
44: ; preds = %40, %13
%45 = phi i32* [ %11, %13 ], [ %41, %40 ]
%46 = phi i32 [ 0, %13 ], [ %43, %40 ]
%47 = shl i32 %46, 2
%48 = tail call i32* @realloc(i32* %45, i32 %47)
%49 = tail call i32 @fclose(i32* %5)
store i32* %48, i32** %0
br label %50
50: ; preds = %16, %44, %7
%51 = phi i32 [ -1, %7 ], [ %46, %44 ], [ -1, %16 ]
ret i32 %51
}