#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 atoi(char *);
char *fgetl(int /*<<< orphan*/ *);
int /*<<< orphan*/ file_error(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int *realloc(int *, int);
int *read_map(char *filename) {
  int n = 0;
  int *map = 0;
  char *str;
  FILE *file = fopen(filename, "r");
  if (!file)
    file_error(filename);
  while ((str = fgetl(file))) {
    ++n;
    map = realloc(map, n * sizeof(int));
    map[n - 1] = atoi(str);
  }
  return map;
}


x86 assembly (clang, -O0):
<read_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1193 <read_map+0x43>
mov    -0x8(%rbp),%rdi
call   1070 <file_error@plt>
jmp    1198 <read_map+0x48>
mov    -0x28(%rbp),%rdi
call   1030 <fgetl@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ef <read_map+0x9f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,%edx
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1198 <read_map+0x48>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_map>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
lea    0xe9d(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1178 <read_map+0x28>
mov    %rbx,%rdi
call   1070 <file_error@plt>
mov    %r14,%rdi
call   1030 <fgetl@plt>
test   %rax,%rax
je     11c3 <read_map+0x73>
mov    %rax,%r13
xor    %ebx,%ebx
xor    %r15d,%r15d
nopl   (%rax)
lea    0x4(%rbx),%r12
mov    %r15,%rdi
mov    %r12d,%esi
call   1040 <realloc@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   1060 <atoi@plt>
mov    %eax,(%r15,%rbx,1)
mov    %r14,%rdi
call   1030 <fgetl@plt>
mov    %rax,%r13
mov    %r12,%rbx
test   %rax,%rax
jne    1190 <read_map+0x40>
jmp    11c6 <read_map+0x76>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32* @read_map(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @file_error(i8* %0)
br label %6
6: ; preds = %4, %1
%7 = tail call i8* @fgetl(i32* %2)
%8 = icmp eq i8* %7, null
br i1 %8, label %21, label %9
9: ; preds = %6, %9
%10 = phi i64 [ %13, %9 ], [ 0, %6 ]
%11 = phi i8* [ %19, %9 ], [ %7, %6 ]
%12 = phi i32* [ %16, %9 ], [ null, %6 ]
%13 = add nuw i64 %10, 1
%14 = trunc i64 %13 to i32
%15 = shl i32 %14, 2
%16 = tail call i32* @realloc(i32* %12, i32 %15)
%17 = tail call i32 @atoi(i8* %11)
%18 = getelementptr inbounds i32, i32* %16, i64 %10
store i32 %17, i32* %18
%19 = tail call i8* @fgetl(i32* %2)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %9
21: ; preds = %9, %6
%22 = phi i32* [ null, %6 ], [ %16, %9 ]
ret i32* %22
}