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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *realloc(char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *readMap(FILE *fp) {
  if (!fp)
    return NULL;
  char buffer[128] = {0};
  char *line = NULL;
  if (fgets(buffer, sizeof(buffer), fp) != NULL) {
    size_t len = strlen(buffer);
    if (buffer[len - 1] == '\n') {
      buffer[len - 1] = '\0';
      line = malloc(len);
      if (!line) {
        fclose(fp);
        fprintf(stderr, "Could not allocate memory!\n");
        exit(-1);
      }
      strcpy(line, buffer);
      return line;
    }
    line = malloc(strlen(buffer) + 1);
    strcpy(line, buffer);
    int done = 0;
    while (!done && fgets(buffer, sizeof(buffer), fp) != NULL) {
      len = strlen(buffer);
      if (buffer[len - 1] == '\n') {
        buffer[len - 1] = '\0';
        done = 1;
      }
      char *temp = realloc(line, strlen(line) + len + 1);
      if (!temp) {
        free(line);
        fclose(fp);
        fprintf(stderr, "Could not reallocate memory!\n");
        exit(-1);
      }
      line = temp;
      strcat(line, buffer);
    }
  }
  return line;
}


x86 assembly (clang, -O0):
<readMap>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d7 <readMap+0x27>
movq   $0x0,-0x8(%rbp)
jmp    1448 <readMap+0x298>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
movq   $0x0,-0x98(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     143d <readMap+0x28d>
lea    -0x90(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rax
sub    $0x1,%rax
movsbl -0x90(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    12c4 <readMap+0x114>
mov    -0xa0(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x90(%rbp,%rax,1)
mov    -0xa0(%rbp),%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    12a1 <readMap+0xf1>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x98(%rbp),%rdi
lea    -0x90(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x98(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1448 <readMap+0x298>
lea    -0x90(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rdi
lea    -0x90(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0xa4(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xa4(%rbp)
mov    %al,-0xb1(%rbp)
jne    1336 <readMap+0x186>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xb1(%rbp)
mov    -0xb1(%rbp),%al
test   $0x1,%al
jne    1349 <readMap+0x199>
jmp    1438 <readMap+0x288>
lea    -0x90(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rax
sub    $0x1,%rax
movsbl -0x90(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1395 <readMap+0x1e5>
mov    -0xa0(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x90(%rbp,%rax,1)
movl   $0x1,-0xa4(%rbp)
mov    -0x98(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    -0x98(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xc0(%rbp),%rdi
mov    %rax,%rsi
add    -0xa0(%rbp),%rsi
add    $0x1,%rsi
call   10b0 <realloc@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
jne    1412 <readMap+0x262>
mov    -0x98(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2be6(%rip),%rax
mov    (%rax),%edi
lea    0xc19(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0xb0(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rdi
lea    -0x90(%rbp),%rsi
call   10c0 <strcat@plt>
jmp    12ff <readMap+0x14f>
jmp    143d <readMap+0x28d>
mov    -0x98(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMap>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
test   %rdi,%rdi
je     122e <readMap+0x8e>
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     122e <readMap+0x8e>
mov    %r15,%rdi
call   1060 <strlen@plt>
cmpb   $0xa,-0x1(%rax,%r15,1)
jne    1243 <readMap+0xa3>
movb   $0x0,-0x1(%rsp,%rax,1)
mov    %eax,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1301 <readMap+0x161>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
jmp    1230 <readMap+0x90>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12ca <readMap+0x12a>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
movzbl -0x1(%rax,%r15,1),%ebp
cmp    $0xa,%bpl
jne    1291 <readMap+0xf1>
movb   $0x0,-0x1(%rsp,%rbx,1)
mov    %r12,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   10a0 <realloc@plt>
test   %rax,%rax
je     12d2 <readMap+0x132>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   10b0 <strcat@plt>
mov    %rbx,%r12
cmp    $0xa,%bpl
jne    1260 <readMap+0xc0>
jmp    1230 <readMap+0x90>
mov    %r12,%rbx
jmp    1230 <readMap+0x90>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd2a(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
lea    0xce7(%rip),%rsi
jmp    12f2 <readMap+0x152>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Could not allocate memory!\0A\00"
@.str.1 = [30 x i8] c"Could not reallocate memory!\0A\00"
define i8* @readMap(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [128 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %3
%9 = load i32*, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i8* null, i8** %2
br label %105
12: ; preds = %1
%13 = bitcast [128 x i8]* %4 to i8*
store i8* null, i8** %5
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%15 = load i32*, i32** %3
%16 = call i32* @fgets(i8* %14, i32 128, i32* %15)
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %103
18: ; preds = %12
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %6
%21 = load i64, i64* %6
%22 = sub i64 %21, 1
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %47
27: ; preds = %18
%28 = load i64, i64* %6
%29 = sub i64 %28, 1
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %29
store i8 0, i8* %30
%31 = load i64, i64* %6
%32 = trunc i64 %31 to i32
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %5
%34 = load i8*, i8** %5
%35 = icmp ne i8* %34, null
br i1 %35, label %42, label %36
36: ; preds = %27
%37 = load i32*, i32** %3
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %27
%43 = load i8*, i8** %5
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%45 = call i32 @strcpy(i8* %43, i8* %44)
%46 = load i8*, i8** %5
store i8* %46, i8** %2
br label %105
47: ; preds = %18
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%49 = call i64 @strlen(i8* %48)
%50 = add i64 %49, 1
%51 = trunc i64 %50 to i32
%52 = call i8* @malloc(i32 %51)
store i8* %52, i8** %5
%53 = load i8*, i8** %5
%54 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%55 = call i32 @strcpy(i8* %53, i8* %54)
store i32 0, i32* %7
br label %56
56: ; preds = %97, %47
%57 = load i32, i32* %7
%58 = icmp ne i32 %57, 0
br i1 %58, label %64, label %59
59: ; preds = %56
%60 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%61 = load i32*, i32** %3
%62 = call i32* @fgets(i8* %60, i32 128, i32* %61)
%63 = icmp ne i32* %62, null
br label %64
64: ; preds = %59, %56
%65 = phi i1 [ false, %56 ], [ %63, %59 ]
br i1 %65, label %66, label %102
66: ; preds = %64
%67 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%68 = call i64 @strlen(i8* %67)
store i64 %68, i64* %6
%69 = load i64, i64* %6
%70 = sub i64 %69, 1
%71 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %70
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp eq i32 %73, 10
br i1 %74, label %75, label %79
75: ; preds = %66
%76 = load i64, i64* %6
%77 = sub i64 %76, 1
%78 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %77
store i8 0, i8* %78
store i32 1, i32* %7
br label %79
79: ; preds = %75, %66
%80 = load i8*, i8** %5
%81 = load i8*, i8** %5
%82 = call i64 @strlen(i8* %81)
%83 = load i64, i64* %6
%84 = add i64 %82, %83
%85 = add i64 %84, 1
%86 = call i8* @realloc(i8* %80, i64 %85)
store i8* %86, i8** %8
%87 = load i8*, i8** %8
%88 = icmp ne i8* %87, null
br i1 %88, label %97, label %89
89: ; preds = %79
%90 = load i8*, i8** %5
%91 = call i32 @free(i8* %90)
%92 = load i32*, i32** %3
%93 = call i32 @fclose(i32* %92)
%94 = load i32, i32* @stderr
%95 = call i32 @fprintf(i32 %94, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%96 = call i32 @exit(i32 -1)
unreachable
97: ; preds = %79
%98 = load i8*, i8** %8
store i8* %98, i8** %5
%99 = load i8*, i8** %5
%100 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%101 = call i32 @strcat(i8* %99, i8* %100)
br label %56
102: ; preds = %64
br label %103
103: ; preds = %102, %12
%104 = load i8*, i8** %5
store i8* %104, i8** %2
br label %105
105: ; preds = %103, %42, %11
%106 = load i8*, i8** %2
ret i8* %106
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Could not allocate memory!\0A\00"
@.str.1 = [30 x i8] c"Could not reallocate memory!\0A\00"
define i8* @readMap(i32* %0) {
%2 = alloca [128 x i8]
%3 = icmp eq i32* %0, null
br i1 %3, label %57, label %4
4: ; preds = %1
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%6 = call i32* @fgets(i8* %5, i32 128, i32* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %55, label %8
8: ; preds = %4
%9 = call i64 @strlen(i8* %5)
%10 = add i64 %9, -1
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %25
14: ; preds = %8
store i8 0, i8* %11
%15 = trunc i64 %9 to i32
%16 = call i8* @malloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = call i32 @fclose(i32* %0)
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %14
%24 = call i32 @strcpy(i8* %16, i8* %5)
br label %55
25: ; preds = %8
%26 = trunc i64 %9 to i32
%27 = add i32 %26, 1
%28 = call i8* @malloc(i32 %27)
%29 = call i32 @strcpy(i8* %28, i8* %5)
br label %30
30: ; preds = %25, %53
%31 = phi i8* [ %28, %25 ], [ %45, %53 ]
%32 = call i32* @fgets(i8* %5, i32 128, i32* %0)
%33 = icmp eq i32* %32, null
br i1 %33, label %55, label %34
34: ; preds = %30
%35 = call i64 @strlen(i8* %5)
%36 = add i64 %35, -1
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 10
br i1 %39, label %40, label %41
40: ; preds = %34
store i8 0, i8* %37
br label %41
41: ; preds = %40, %34
%42 = call i64 @strlen(i8* dereferenceable(1) %31)
%43 = add i64 %35, 1
%44 = add i64 %43, %42
%45 = call i8* @realloc(i8* %31, i64 %44)
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %53
47: ; preds = %41
%48 = call i32 @free(i8* %31)
%49 = call i32 @fclose(i32* %0)
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %41
%54 = call i32 @strcat(i8* %45, i8* %5)
br i1 %39, label %55, label %30
55: ; preds = %53, %30, %4, %23
%56 = phi i8* [ %16, %23 ], [ null, %4 ], [ %31, %30 ], [ %45, %53 ]
br label %57
57: ; preds = %1, %55
%58 = phi i8* [ %56, %55 ], [ null, %1 ]
ret i8* %58
}