#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*/ EXIT_FAILURE;
char *GetNextLine(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ realloc(int *, int);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int *ReadIntArray(FILE *f, int *arrlen) {
  int *array = NULL, len = 0;
  while (1) {
    char *nextline;
    int nextint, res;
    nextline = GetNextLine(f);
    if (nextline == NULL) {
      break;
    }
    res = sscanf(nextline, "%d", &nextint);
    if (res == 0) {
      fprintf(stderr, "file doesn't contain integers; quitting.\n");
      exit(EXIT_FAILURE);
    }
    array = (int *)realloc(array, (len + 1) * sizeof(int));
    array[len] = nextint;
    len++;
    free(nextline);
  }
  *arrlen = len;
  return array;
}


x86 assembly (clang, -O0):
<ReadIntArray>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <GetNextLine@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119c <ReadIntArray+0x3c>
jmp    1220 <ReadIntArray+0xc0>
mov    -0x28(%rbp),%rdi
lea    0xe59(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <sscanf@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    11e0 <ReadIntArray+0x80>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    117f <ReadIntArray+0x1f>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadIntArray>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %rdi,%r15
call   1070 <GetNextLine@plt>
test   %rax,%rax
je     11f6 <ReadIntArray+0x96>
mov    %rax,%rbx
mov    %rbp,0x10(%rsp)
mov    $0x4,%ebp
xor    %r13d,%r13d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
lea    0xe56(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1050 <sscanf@plt>
test   %eax,%eax
je     1212 <ReadIntArray+0xb2>
lea    0x1(%r13),%r14
mov    %r12,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%r12
mov    0xc(%rsp),%eax
mov    %eax,(%r12,%r13,4)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1070 <GetNextLine@plt>
mov    %rax,%rbx
add    $0x4,%ebp
mov    %r14,%r13
test   %rax,%rax
jne    11a0 <ReadIntArray+0x40>
mov    0x10(%rsp),%rbp
jmp    11fc <ReadIntArray+0x9c>
xor    %r14d,%r14d
xor    %r12d,%r12d
mov    %r14d,0x0(%rbp)
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"file doesn't contain integers; quitting.\0A\00"
@EXIT_FAILURE = global i32 0
define i32* @ReadIntArray(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32* null, i32** %5
store i32 0, i32* %6
br label %10
10: ; preds = %2, %26
%11 = load i32*, i32** %3
%12 = call i8* @GetNextLine(i32* %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %10
br label %44
16: ; preds = %10
%17 = load i8*, i8** %7
%18 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %8)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %16
%27 = load i32*, i32** %5
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = mul i64 %30, 4
%32 = trunc i64 %31 to i32
%33 = call i64 @realloc(i32* %27, i32 %32)
%34 = inttoptr i64 %33 to i32*
store i32* %34, i32** %5
%35 = load i32, i32* %8
%36 = load i32*, i32** %5
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
store i32 %35, i32* %39
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
%42 = load i8*, i8** %7
%43 = call i32 @free(i8* %42)
br label %10
44: ; preds = %15
%45 = load i32, i32* %6
%46 = load i32*, i32** %4
store i32 %45, i32* %46
%47 = load i32*, i32** %5
ret i32* %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"file doesn't contain integers; quitting.\0A\00"
@EXIT_FAILURE = global i32 0
define i32* @ReadIntArray(i32* %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i8* @GetNextLine(i32* %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %32, label %7
7: ; preds = %2, %18
%8 = phi i64 [ %19, %18 ], [ 0, %2 ]
%9 = phi i8* [ %27, %18 ], [ %5, %2 ]
%10 = phi i32* [ %23, %18 ], [ null, %2 ]
%11 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %7
%19 = add nuw i64 %8, 1
%20 = trunc i64 %19 to i32
%21 = shl i32 %20, 2
%22 = call i64 @realloc(i32* %10, i32 %21)
%23 = inttoptr i64 %22 to i32*
%24 = load i32, i32* %3
%25 = getelementptr inbounds i32, i32* %23, i64 %8
store i32 %24, i32* %25
%26 = call i32 @free(i8* %9)
%27 = call i8* @GetNextLine(i32* %0)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %7
29: ; preds = %18
%30 = trunc i64 %19 to i32
%31 = inttoptr i64 %22 to i32*
br label %32
32: ; preds = %29, %2
%33 = phi i32 [ 0, %2 ], [ %30, %29 ]
%34 = phi i32* [ null, %2 ], [ %31, %29 ]
store i32 %33, i32* %1
ret i32* %34
}