#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int /*<<< orphan*/ isalpha(int);
int next_symbol(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char tolower(int);
int read_puzzle(char board[40][40], char *file, int *nrows_out,
                int *ncols_out) {
  FILE *fptr = fopen(file, "r");
  int nrows, ncols;
  int i, j;
  int ch;
  if (fptr == NULL) {
    fprintf(stderr, "ERR:  failed to open file %s\n", file);
    return 0;
  }
  if (fscanf(fptr, "%i %i\n", &nrows, &ncols) != 2 || nrows <= 0 ||
      ncols <= 0 || nrows > 40 || ncols > 40) {
    fprintf(stderr, "ERR:  bad board dimensions\n");
    fclose(fptr);
    return 0;
  }
  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 (!isalpha(ch)) {
        fprintf(stderr, "ERR:  bad char in input (%c)\n", ch);
        fclose(fptr);
        return 0;
      }
      board[i][j] = tolower(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_puzzle>:
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    0xe6d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ce <read_puzzle+0x5e>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_puzzle+0x22c>
mov    -0x30(%rbp),%rdi
lea    0xe47(%rip),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1040 <fscanf@plt>
cmp    $0x2,%eax
jne    1217 <read_puzzle+0xa7>
cmpl   $0x0,-0x34(%rbp)
jle    1217 <read_puzzle+0xa7>
cmpl   $0x0,-0x38(%rbp)
jle    1217 <read_puzzle+0xa7>
cmpl   $0x28,-0x34(%rbp)
jg     1217 <read_puzzle+0xa7>
cmpl   $0x28,-0x38(%rbp)
jle    1243 <read_puzzle+0xd3>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_puzzle+0x22c>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1336 <read_puzzle+0x1c6>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    1323 <read_puzzle+0x1b3>
mov    -0x30(%rbp),%rdi
call   1090 <next_symbol@plt>
mov    %eax,-0x44(%rbp)
mov    0x2d6c(%rip),%rcx
cmp    (%rcx),%eax
jne    12b0 <read_puzzle+0x140>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_puzzle+0x22c>
mov    -0x44(%rbp),%edi
call   1070 <isalpha@plt>
cmp    $0x0,%eax
jne    12f0 <read_puzzle+0x180>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%edx
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_puzzle+0x22c>
jmp    12f5 <read_puzzle+0x185>
mov    -0x44(%rbp),%edi
call   1060 <tolower@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
imul   $0x28,%rcx,%rcx
add    %rcx,%rax
movslq -0x40(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    125d <read_puzzle+0xed>
jmp    1328 <read_puzzle+0x1b8>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    124a <read_puzzle+0xda>
mov    -0x30(%rbp),%rdi
call   1090 <next_symbol@plt>
mov    0x2ca2(%rip),%rcx
cmp    (%rcx),%eax
je     137a <read_puzzle+0x20a>
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
lea    0xd19(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    139c <read_puzzle+0x22c>
mov    -0x30(%rbp),%rdi
call   1030 <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_puzzle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe6f(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1269 <read_puzzle+0xf9>
mov    %rax,%r12
lea    0xe74(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x2,%eax
jne    1287 <read_puzzle+0x117>
mov    0x4(%rsp),%ecx
test   %ecx,%ecx
jle    1287 <read_puzzle+0x117>
mov    (%rsp),%eax
test   %eax,%eax
jle    1287 <read_puzzle+0x117>
cmp    $0x28,%ecx
jg     1287 <read_puzzle+0x117>
cmp    $0x28,%eax
jg     1287 <read_puzzle+0x117>
mov    %r15,0x8(%rsp)
mov    %r14,0x10(%rsp)
xor    %r14d,%r14d
mov    0x2de6(%rip),%r13
jmp    1224 <read_puzzle+0xb4>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r14
movslq %ecx,%rdx
add    $0x28,%rbx
cmp    %rdx,%r14
jge    12e8 <read_puzzle+0x178>
test   %eax,%eax
jle    1210 <read_puzzle+0xa0>
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1090 <next_symbol@plt>
cmp    0x0(%r13),%eax
je     1299 <read_puzzle+0x129>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <isalpha@plt>
test   %eax,%eax
je     12b4 <read_puzzle+0x144>
mov    %ebp,%edi
call   1060 <tolower@plt>
mov    %al,(%rbx,%r15,1)
add    $0x1,%r15
movslq (%rsp),%rax
cmp    %rax,%r15
jl     1230 <read_puzzle+0xc0>
mov    0x4(%rsp),%ecx
jmp    1210 <read_puzzle+0xa0>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xd89(%rip),%rsi
xor    %ebx,%ebx
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12d7 <read_puzzle+0x167>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
jmp    12a9 <read_puzzle+0x139>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12cf <read_puzzle+0x15f>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xd95(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1090 <next_symbol@plt>
cmp    0x0(%r13),%eax
jne    131a <read_puzzle+0x1aa>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    0x4(%rsp),%eax
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
mov    (%rsp),%eax
mov    0x10(%rsp),%rcx
mov    %eax,(%rcx)
mov    $0x1,%ebx
jmp    12d7 <read_puzzle+0x167>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
lea    0xd4d(%rip),%rsi
jmp    12a9 <read_puzzle+0x139>

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

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