#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 */
char EOF;
char **create_2darr(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char **);
int fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int /*<<< orphan*/ is_balloon(char);
char next_symbol(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int read_grid(char ***grid, char *file, int *nrows_out, int *ncols_out) {
  FILE *fptr;
  int nrows, ncols;
  int i, j;
  char ch;
  fptr = fopen(file, "r");
  if (fptr == NULL) {
    return 0;
  }
  if (fscanf(fptr, "%i %i\n", &nrows, &ncols) != 2 || nrows <= 0 ||
      ncols <= 0) {
    fclose(fptr);
    return 0;
  } else {
    if (*grid != NULL)
      free(*grid);
    *grid = create_2darr(nrows, ncols);
  }
  for (i = 0; i < nrows; i++) {
    for (j = 0; j < ncols; j++) {
      if ((ch = next_symbol(fptr)) == EOF) {
        fprintf(stderr, "ERR:  file too short\n");
        fclose(fptr);
        return 0;
      } else if (!is_balloon(ch)) {
        fprintf(stderr, "ERR:  bad symbol in input (%c)\n", ch);
        fclose(fptr);
        return 0;
      }
      (*grid)[i][j] = ch;
    }
  }
  if (next_symbol(fptr) != EOF) {
    fprintf(stderr, "ERR:  file too long\n");
    fclose(fptr);
    return 0;
  }
  fclose(fptr);
  *nrows_out = nrows;
  *ncols_out = ncols;
  return 1;
}


x86 assembly (clang, -O0):
<read_grid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c3 <read_grid+0x43>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_grid+0x21c>
mov    -0x30(%rbp),%rdi
lea    0xe34(%rip),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1060 <fscanf@plt>
cmp    $0x2,%eax
jne    11f8 <read_grid+0x78>
cmpl   $0x0,-0x34(%rbp)
jle    11f8 <read_grid+0x78>
cmpl   $0x0,-0x38(%rbp)
jg     120d <read_grid+0x8d>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_grid+0x21c>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1227 <read_grid+0xa7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%esi
call   1080 <create_2darr@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1330 <read_grid+0x1b0>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    131d <read_grid+0x19d>
mov    -0x30(%rbp),%rdi
call   10a0 <next_symbol@plt>
mov    %al,-0x41(%rbp)
movsbl %al,%eax
mov    0x2d70(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    12af <read_grid+0x12f>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_grid+0x21c>
movsbl -0x41(%rbp),%edi
call   1040 <is_balloon@plt>
cmp    $0x0,%eax
jne    12f1 <read_grid+0x171>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
movsbl -0x41(%rbp),%edx
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_grid+0x21c>
jmp    12f6 <read_grid+0x176>
mov    -0x41(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x40(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1256 <read_grid+0xd6>
jmp    1322 <read_grid+0x1a2>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1243 <read_grid+0xc3>
mov    -0x30(%rbp),%rdi
call   10a0 <next_symbol@plt>
movsbl %al,%eax
mov    0x2ca5(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     137a <read_grid+0x1fa>
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
lea    0xce1(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_grid+0x21c>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_grid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%rax
mov    %rdi,%r12
lea    0xe5f(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     131b <read_grid+0x19b>
mov    %rax,%r13
lea    0xe44(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
mov    %rax,%rdi
call   1060 <fscanf@plt>
cmp    $0x2,%eax
jne    1313 <read_grid+0x193>
mov    0x4(%rsp),%edi
test   %edi,%edi
jle    1313 <read_grid+0x193>
mov    (%rsp),%esi
test   %esi,%esi
jle    1313 <read_grid+0x193>
mov    (%r12),%rax
test   %rax,%rax
je     1206 <read_grid+0x86>
mov    %rax,%rdi
call   1030 <free@plt>
mov    0x4(%rsp),%edi
mov    (%rsp),%esi
call   1080 <create_2darr@plt>
mov    %rax,(%r12)
mov    0x4(%rsp),%ecx
test   %ecx,%ecx
jle    129c <read_grid+0x11c>
mov    (%rsp),%eax
xor    %r14d,%r14d
mov    %r15,0x10(%rsp)
mov    %rbp,0x8(%rsp)
jmp    123c <read_grid+0xbc>
nopl   (%rax)
add    $0x1,%r14
movslq %ecx,%rdx
cmp    %rdx,%r14
jge    129c <read_grid+0x11c>
test   %eax,%eax
jle    1230 <read_grid+0xb0>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
call   10a0 <next_symbol@plt>
mov    %eax,%ebx
mov    0x2d87(%rip),%rax
cmp    (%rax),%bl
je     12cb <read_grid+0x14b>
movsbl %bl,%ebp
mov    %ebp,%edi
call   1040 <is_balloon@plt>
test   %eax,%eax
je     12dd <read_grid+0x15d>
mov    (%r12),%rax
mov    (%rax,%r14,8),%rax
mov    %bl,(%rax,%r15,1)
add    $0x1,%r15
movslq (%rsp),%rax
cmp    %rax,%r15
jl     1250 <read_grid+0xd0>
mov    0x4(%rsp),%ecx
mov    0x10(%rsp),%r15
mov    0x8(%rsp),%rbp
jmp    1230 <read_grid+0xb0>
mov    %r13,%rdi
call   10a0 <next_symbol@plt>
mov    0x2d3d(%rip),%rcx
cmp    (%rcx),%al
jne    12fa <read_grid+0x17a>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    0x4(%rsp),%eax
mov    %eax,0x0(%rbp)
mov    (%rsp),%eax
mov    %eax,(%r15)
mov    $0x1,%ebx
jmp    131b <read_grid+0x19b>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    0xd2e(%rip),%rsi
jmp    130a <read_grid+0x18a>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
lea    0xd32(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1313 <read_grid+0x193>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
lea    0xd35(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [7 x i8] c"%i %i\0A\00"
@EOF = global i8 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"ERR: file too short\0A\00"
@.str.3 = [32 x i8] c"ERR: bad symbol in input (%c)\0A\00"
@.str.4 = [21 x i8] c"ERR: file too long\0A\00"
define i32 @read_grid(i8*** %0, i8* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8***
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8
store i8*** %0, i8**** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%16 = load i8*, i8** %7
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %10
%18 = load i32*, i32** %10
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 0, i32* %5
br label %118
21: ; preds = %4
%22 = load i32*, i32** %10
%23 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %11, i32* %12)
%24 = icmp ne i32 %23, 2
br i1 %24, label %31, label %25
25: ; preds = %21
%26 = load i32, i32* %11
%27 = icmp sle i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* %12
%30 = icmp sle i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28, %25, %21
%32 = load i32*, i32** %10
%33 = call i32 @fclose(i32* %32)
store i32 0, i32* %5
br label %118
34: ; preds = %28
%35 = load i8***, i8**** %6
%36 = load i8**, i8*** %35
%37 = icmp ne i8** %36, null
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i8***, i8**** %6
%40 = load i8**, i8*** %39
%41 = call i32 @free(i8** %40)
br label %42
42: ; preds = %38, %34
%43 = load i32, i32* %11
%44 = load i32, i32* %12
%45 = call i8** @create_2darr(i32 %43, i32 %44)
%46 = load i8***, i8**** %6
store i8** %45, i8*** %46
br label %47
47: ; preds = %42
store i32 0, i32* %13
br label %48
48: ; preds = %96, %47
%49 = load i32, i32* %13
%50 = load i32, i32* %11
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %99
52: ; preds = %48
store i32 0, i32* %14
br label %53
53: ; preds = %92, %52
%54 = load i32, i32* %14
%55 = load i32, i32* %12
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %95
57: ; preds = %53
%58 = load i32*, i32** %10
%59 = call signext i8 @next_symbol(i32* %58)
store i8 %59, i8* %15
%60 = sext i8 %59 to i32
%61 = load i8, i8* @EOF
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %60, %62
br i1 %63, label %64, label %69
64: ; preds = %57
%65 = load i32, i32* @stderr
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%67 = load i32*, i32** %10
%68 = call i32 @fclose(i32* %67)
store i32 0, i32* %5
br label %118
69: ; preds = %57
%70 = load i8, i8* %15
%71 = call i32 @is_balloon(i8 signext %70)
%72 = icmp ne i32 %71, 0
br i1 %72, label %80, label %73
73: ; preds = %69
%74 = load i32, i32* @stderr
%75 = load i8, i8* %15
%76 = sext i8 %75 to i32
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %76)
%78 = load i32*, i32** %10
%79 = call i32 @fclose(i32* %78)
store i32 0, i32* %5
br label %118
80: ; preds = %69
br label %81
81: ; preds = %80
%82 = load i8, i8* %15
%83 = load i8***, i8**** %6
%84 = load i8**, i8*** %83
%85 = load i32, i32* %13
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8*, i8** %84, i64 %86
%88 = load i8*, i8** %87
%89 = load i32, i32* %14
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i8, i8* %88, i64 %90
store i8 %82, i8* %91
br label %92
92: ; preds = %81
%93 = load i32, i32* %14
%94 = add nsw i32 %93, 1
store i32 %94, i32* %14
br label %53
95: ; preds = %53
br label %96
96: ; preds = %95
%97 = load i32, i32* %13
%98 = add nsw i32 %97, 1
store i32 %98, i32* %13
br label %48
99: ; preds = %48
%100 = load i32*, i32** %10
%101 = call signext i8 @next_symbol(i32* %100)
%102 = sext i8 %101 to i32
%103 = load i8, i8* @EOF
%104 = sext i8 %103 to i32
%105 = icmp ne i32 %102, %104
br i1 %105, label %106, label %111
106: ; preds = %99
%107 = load i32, i32* @stderr
%108 = call i32 (i32, i8*, ...) @fprintf(i32 %107, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%109 = load i32*, i32** %10
%110 = call i32 @fclose(i32* %109)
store i32 0, i32* %5
br label %118
111: ; preds = %99
%112 = load i32*, i32** %10
%113 = call i32 @fclose(i32* %112)
%114 = load i32, i32* %11
%115 = load i32*, i32** %8
store i32 %114, i32* %115
%116 = load i32, i32* %12
%117 = load i32*, i32** %9
store i32 %116, i32* %117
store i32 1, i32* %5
br label %118
118: ; preds = %111, %106, %73, %64, %31, %20
%119 = load i32, i32* %5
ret i32 %119
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [7 x i8] c"%i %i\0A\00"
@EOF = global i8 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"ERR: file too short\0A\00"
@.str.3 = [32 x i8] c"ERR: bad symbol in input (%c)\0A\00"
@.str.4 = [21 x i8] c"ERR: file too long\0A\00"
define i32 @read_grid(i8*** nocapture %0, i8* nocapture %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %88, label %11
11: ; preds = %4
%12 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %5, i32* %6)
%13 = icmp ne i32 %12, 2
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 1
%16 = select i1 %13, i1 true, i1 %15
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 1
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 @fclose(i32* %9)
br label %88
22: ; preds = %11
%23 = load i8**, i8*** %0
%24 = icmp eq i8** %23, null
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = call i32 @free(i8** %23)
%27 = load i32, i32* %5
%28 = load i32, i32* %6
br label %29
29: ; preds = %25, %22
%30 = phi i32 [ %28, %25 ], [ %17, %22 ]
%31 = phi i32 [ %27, %25 ], [ %14, %22 ]
%32 = call i8** @create_2darr(i32 %31, i32 %30)
store i8** %32, i8*** %0
%33 = load i32, i32* %5
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %76
35: ; preds = %29
%36 = load i32, i32* %6
br label %37
37: ; preds = %35, %70
%38 = phi i32 [ %33, %35 ], [ %71, %70 ]
%39 = phi i32 [ %36, %35 ], [ %72, %70 ]
%40 = phi i64 [ 0, %35 ], [ %73, %70 ]
%41 = icmp sgt i32 %39, 0
br i1 %41, label %42, label %70
42: ; preds = %37, %59
%43 = phi i64 [ %64, %59 ], [ 0, %37 ]
%44 = call signext i8 @next_symbol(i32* %9)
%45 = load i8, i8* @EOF
%46 = icmp eq i8 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%50 = call i32 @fclose(i32* %9)
br label %88
51: ; preds = %42
%52 = call i32 @is_balloon(i8 signext %44)
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = sext i8 %44 to i32
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %55)
%58 = call i32 @fclose(i32* %9)
br label %88
59: ; preds = %51
%60 = load i8**, i8*** %0
%61 = getelementptr inbounds i8*, i8** %60, i64 %40
%62 = load i8*, i8** %61
%63 = getelementptr inbounds i8, i8* %62, i64 %43
store i8 %44, i8* %63
%64 = add nuw nsw i64 %43, 1
%65 = load i32, i32* %6
%66 = sext i32 %65 to i64
%67 = icmp slt i64 %64, %66
br i1 %67, label %42, label %68
68: ; preds = %59
%69 = load i32, i32* %5
br label %70
70: ; preds = %68, %37
%71 = phi i32 [ %69, %68 ], [ %38, %37 ]
%72 = phi i32 [ %65, %68 ], [ %39, %37 ]
%73 = add nuw nsw i64 %40, 1
%74 = sext i32 %71 to i64
%75 = icmp slt i64 %73, %74
br i1 %75, label %37, label %76
76: ; preds = %70, %29
%77 = call signext i8 @next_symbol(i32* %9)
%78 = load i8, i8* @EOF
%79 = icmp eq i8 %77, %78
br i1 %79, label %84, label %80
80: ; preds = %76
%81 = load i32, i32* @stderr
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%83 = call i32 @fclose(i32* %9)
br label %88
84: ; preds = %76
%85 = call i32 @fclose(i32* %9)
%86 = load i32, i32* %5
store i32 %86, i32* %2
%87 = load i32, i32* %6
store i32 %87, i32* %3
br label %88
88: ; preds = %4, %84, %80, %54, %47, %20
%89 = phi i32 [ 0, %20 ], [ 0, %47 ], [ 0, %54 ], [ 0, %80 ], [ 1, %84 ], [ 0, %4 ]
ret i32 %89
}