#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *, int *, int *,
                          int *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strcmp(char *, char *);
void load_flag_from_pgm(char **flag, int imax, int jmax, char *filename) {
  char buf[80];
  char *pix;
  int width, height, max, i, j;
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    printf("Couldn't open file '%s'\n", filename);
    return;
  }
  fscanf(fp, "%s %d %d %d", buf, &width, &height, &max);
  if (strcmp("P5", buf) != 0) {
    printf("'%s' is not a PGM file.\n", filename);
    return;
  }
  if (width < 1 || height < 1 || max < 1) {
    printf("'%s' has invalid headers.\n", filename);
    return;
  }
  pix = malloc(width);
  for (j = 1; j < jmax + 2; j++) {
    if (j <= height) {
      fread(pix, width, 1, fp);
    }
    for (i = 1; i < imax + 2; i++) {
      if (j >= height + 1 || i >= width + 1) {
        flag[i][j] = 0x0010;
      } else {
        if (pix[i - 1] == 0) {
          flag[i][j] = 0x0000;
        } else {
          flag[i][j] = 0x0010;
        }
      }
    }
  }
  free(pix);
  fclose(fp);
}


x86 assembly (clang, -O0):
<load_flag_from_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    11d3 <load_flag_from_pgm+0x53>
mov    -0x18(%rbp),%rsi
lea    0xe3a(%rip),%rdi
call   1060 <printf@plt>
jmp    13b6 <load_flag_from_pgm+0x236>
mov    -0x98(%rbp),%rdi
lea    -0x70(%rbp),%rdx
lea    0xe37(%rip),%rsi
lea    -0x7c(%rbp),%rcx
lea    -0x80(%rbp),%r8
lea    -0x84(%rbp),%r9
call   1070 <fscanf@plt>
lea    -0x70(%rbp),%rsi
lea    0xe24(%rip),%rdi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1228 <load_flag_from_pgm+0xa8>
mov    -0x18(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1060 <printf@plt>
jmp    13b6 <load_flag_from_pgm+0x236>
cmpl   $0x1,-0x7c(%rbp)
jl     1249 <load_flag_from_pgm+0xc9>
cmpl   $0x1,-0x80(%rbp)
jl     1249 <load_flag_from_pgm+0xc9>
cmpl   $0x1,-0x84(%rbp)
jge    125e <load_flag_from_pgm+0xde>
mov    -0x18(%rbp),%rsi
lea    0xdf0(%rip),%rdi
call   1060 <printf@plt>
jmp    13b6 <load_flag_from_pgm+0x236>
mov    -0x7c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x78(%rbp)
movl   $0x1,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    13a1 <load_flag_from_pgm+0x221>
mov    -0x8c(%rbp),%eax
cmp    -0x80(%rbp),%eax
jg     12af <load_flag_from_pgm+0x12f>
mov    -0x78(%rbp),%rdi
mov    -0x7c(%rbp),%esi
mov    -0x98(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
movl   $0x1,-0x88(%rbp)
mov    -0x88(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    1388 <load_flag_from_pgm+0x208>
mov    -0x8c(%rbp),%eax
mov    -0x80(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    12f5 <load_flag_from_pgm+0x175>
mov    -0x88(%rbp),%eax
mov    -0x7c(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jl     1314 <load_flag_from_pgm+0x194>
mov    -0x8(%rbp),%rax
movslq -0x88(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x8c(%rbp),%rcx
movb   $0x10,(%rax,%rcx,1)
jmp    136f <load_flag_from_pgm+0x1ef>
mov    -0x78(%rbp),%rax
mov    -0x88(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1350 <load_flag_from_pgm+0x1d0>
mov    -0x8(%rbp),%rax
movslq -0x88(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x8c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    136a <load_flag_from_pgm+0x1ea>
mov    -0x8(%rbp),%rax
movslq -0x88(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x8c(%rbp),%rcx
movb   $0x10,(%rax,%rcx,1)
jmp    136f <load_flag_from_pgm+0x1ef>
jmp    1374 <load_flag_from_pgm+0x1f4>
mov    -0x88(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x88(%rbp)
jmp    12b9 <load_flag_from_pgm+0x139>
jmp    138d <load_flag_from_pgm+0x20d>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    1274 <load_flag_from_pgm+0xf4>
mov    -0x78(%rbp),%rdi
call   1030 <free@plt>
mov    -0x98(%rbp),%rdi
call   1050 <fclose@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_flag_from_pgm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rcx,%rbx
mov    %edx,%r14d
mov    %esi,0x8(%rsp)
mov    %rdi,%rbp
lea    0xe5e(%rip),%rsi
mov    %rcx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11f6 <load_flag_from_pgm+0x76>
mov    %rax,%r12
lea    0xe63(%rip),%rsi
lea    0x10(%rsp),%r13
lea    0x4(%rsp),%rcx
mov    %rsp,%r8
lea    0xc(%rsp),%r9
mov    %rax,%rdi
mov    %r13,%rdx
call   1070 <fscanf@plt>
lea    0xe4b(%rip),%rdi
mov    %r13,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1213 <load_flag_from_pgm+0x93>
lea    0xe3a(%rip),%rdi
jmp    1309 <load_flag_from_pgm+0x189>
lea    0xe06(%rip),%rdi
mov    %rbx,%rsi
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
jle    1302 <load_flag_from_pgm+0x182>
mov    (%rsp),%r15d
test   %r15d,%r15d
jle    1302 <load_flag_from_pgm+0x182>
cmpl   $0x0,0xc(%rsp)
jle    1302 <load_flag_from_pgm+0x182>
call   1090 <malloc@plt>
mov    %rax,%r13
mov    %r14,%rdx
test   %edx,%edx
js     1320 <load_flag_from_pgm+0x1a0>
xor    %eax,%eax
mov    0x8(%rsp),%ecx
test   %ecx,%ecx
cmovg  %ecx,%eax
add    $0x2,%eax
add    $0x2,%edx
mov    $0x2,%ebx
sub    %rax,%rbx
mov    $0x1,%r14d
nopl   0x0(%rax)
movslq %r15d,%rax
cmp    %rax,%r14
jg     1292 <load_flag_from_pgm+0x112>
mov    0x4(%rsp),%esi
mov    %r13,%rdi
mov    %rdx,%r15
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %r15,%rdx
cmpl   $0x0,0x8(%rsp)
js     12f0 <load_flag_from_pgm+0x170>
mov    $0x1,%eax
jmp    12bc <load_flag_from_pgm+0x13c>
mov    0x0(%rbp,%rax,8),%rcx
movb   $0x10,(%rcx,%r14,1)
lea    (%rbx,%rax,1),%rcx
add    $0x1,%rcx
add    $0x1,%rax
cmp    $0x2,%rcx
je     12f0 <load_flag_from_pgm+0x170>
movslq (%rsp),%rcx
cmp    %rcx,%r14
jg     12a0 <load_flag_from_pgm+0x120>
movslq 0x4(%rsp),%rcx
cmp    %rcx,%rax
jg     12a0 <load_flag_from_pgm+0x120>
cmpb   $0x0,-0x1(%r13,%rax,1)
mov    0x0(%rbp,%rax,8),%rcx
jne    12a5 <load_flag_from_pgm+0x125>
movb   $0x0,(%rcx,%r14,1)
jmp    12aa <load_flag_from_pgm+0x12a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r14
cmp    %rdx,%r14
je     1320 <load_flag_from_pgm+0x1a0>
mov    (%rsp),%r15d
jmp    1270 <load_flag_from_pgm+0xf0>
lea    0xd3b(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
jmp    1311 <load_flag_from_pgm+0x191>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"Couldn't open file '%s'\0A\00"
@.str.2 = [12 x i8] c"%s %d %d %d\00"
@.str.3 = [3 x i8] c"P5\00"
@.str.4 = [25 x i8] c"'%s' is not a PGM file.\0A\00"
@.str.5 = [27 x i8] c"'%s' has invalid headers.\0A\00"
define void @load_flag_from_pgm(i8** %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [80 x i8]
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32*
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%17 = load i8*, i8** %8
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %16
%19 = load i32*, i32** %16
%20 = icmp ne i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %4
%22 = load i8*, i8** %8
%23 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %129
24: ; preds = %4
%25 = load i32*, i32** %16
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%27 = call i32 @fscanf(i32* %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %26, i32* %11, i32* %12, i32* %13)
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%29 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i8*, i8** %8
%33 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %129
34: ; preds = %24
%35 = load i32, i32* %11
%36 = icmp slt i32 %35, 1
br i1 %36, label %43, label %37
37: ; preds = %34
%38 = load i32, i32* %12
%39 = icmp slt i32 %38, 1
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = load i32, i32* %13
%42 = icmp slt i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %40, %37, %34
%44 = load i8*, i8** %8
%45 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %44)
br label %129
46: ; preds = %40
%47 = load i32, i32* %11
%48 = call i8* @malloc(i32 %47)
store i8* %48, i8** %10
store i32 1, i32* %15
br label %49
49: ; preds = %121, %46
%50 = load i32, i32* %15
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 2
%53 = icmp slt i32 %50, %52
br i1 %53, label %54, label %124
54: ; preds = %49
%55 = load i32, i32* %15
%56 = load i32, i32* %12
%57 = icmp sle i32 %55, %56
br i1 %57, label %58, label %63
58: ; preds = %54
%59 = load i8*, i8** %10
%60 = load i32, i32* %11
%61 = load i32*, i32** %16
%62 = call i32 @fread(i8* %59, i32 %60, i32 1, i32* %61)
br label %63
63: ; preds = %58, %54
store i32 1, i32* %14
br label %64
64: ; preds = %117, %63
%65 = load i32, i32* %14
%66 = load i32, i32* %6
%67 = add nsw i32 %66, 2
%68 = icmp slt i32 %65, %67
br i1 %68, label %69, label %120
69: ; preds = %64
%70 = load i32, i32* %15
%71 = load i32, i32* %12
%72 = add nsw i32 %71, 1
%73 = icmp sge i32 %70, %72
br i1 %73, label %79, label %74
74: ; preds = %69
%75 = load i32, i32* %14
%76 = load i32, i32* %11
%77 = add nsw i32 %76, 1
%78 = icmp sge i32 %75, %77
br i1 %78, label %79, label %88
79: ; preds = %74, %69
%80 = load i8**, i8*** %5
%81 = load i32, i32* %14
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8*, i8** %80, i64 %82
%84 = load i8*, i8** %83
%85 = load i32, i32* %15
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %84, i64 %86
store i8 16, i8* %87
br label %116
88: ; preds = %74
%89 = load i8*, i8** %10
%90 = load i32, i32* %14
%91 = sub nsw i32 %90, 1
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %89, i64 %92
%94 = load i8, i8* %93
%95 = sext i8 %94 to i32
%96 = icmp eq i32 %95, 0
br i1 %96, label %97, label %106
97: ; preds = %88
%98 = load i8**, i8*** %5
%99 = load i32, i32* %14
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8*, i8** %98, i64 %100
%102 = load i8*, i8** %101
%103 = load i32, i32* %15
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds i8, i8* %102, i64 %104
store i8 0, i8* %105
br label %115
106: ; preds = %88
%107 = load i8**, i8*** %5
%108 = load i32, i32* %14
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds i8*, i8** %107, i64 %109
%111 = load i8*, i8** %110
%112 = load i32, i32* %15
%113 = sext i32 %112 to i64
%114 = getelementptr inbounds i8, i8* %111, i64 %113
store i8 16, i8* %114
br label %115
115: ; preds = %106, %97
br label %116
116: ; preds = %115, %79
br label %117
117: ; preds = %116
%118 = load i32, i32* %14
%119 = add nsw i32 %118, 1
store i32 %119, i32* %14
br label %64
120: ; preds = %64
br label %121
121: ; preds = %120
%122 = load i32, i32* %15
%123 = add nsw i32 %122, 1
store i32 %123, i32* %15
br label %49
124: ; preds = %49
%125 = load i8*, i8** %10
%126 = call i32 @free(i8* %125)
%127 = load i32*, i32** %16
%128 = call i32 @fclose(i32* %127)
br label %129
129: ; preds = %124, %43, %31, %21
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"Couldn't open file '%s'\0A\00"
@.str.2 = [12 x i8] c"%s %d %d %d\00"
@.str.3 = [3 x i8] c"P5\00"
@.str.4 = [25 x i8] c"'%s' is not a PGM file.\0A\00"
@.str.5 = [27 x i8] c"'%s' has invalid headers.\0A\00"
define void @load_flag_from_pgm(i8** nocapture %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca [80 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = bitcast i32* %8 to i8*
%13 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %87
17: ; preds = %4
%18 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %9, i32* %6, i32* %7, i32* %8)
%19 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %3)
br label %87
23: ; preds = %17
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 1
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 1
%28 = select i1 %25, i1 true, i1 %27
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 1
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %3)
br label %87
34: ; preds = %23
%35 = call i8* @malloc(i32 %24)
%36 = icmp sgt i32 %2, -1
br i1 %36, label %37, label %84
37: ; preds = %34
%38 = icmp sgt i32 %1, -1
%39 = call i32 @llvm.smax.i32(i32 %1, i32 0)
%40 = add nuw i32 %39, 2
%41 = add nuw i32 %2, 2
%42 = zext i32 %41 to i64
%43 = zext i32 %40 to i64
br label %44
44: ; preds = %82, %37
%45 = phi i32 [ %26, %37 ], [ %83, %82 ]
%46 = phi i64 [ 1, %37 ], [ %80, %82 ]
%47 = sext i32 %45 to i64
%48 = icmp sgt i64 %46, %47
br i1 %48, label %52, label %49
49: ; preds = %44
%50 = load i32, i32* %6
%51 = call i32 @fread(i8* %35, i32 %50, i32 1, i32* %13)
br label %52
52: ; preds = %49, %44
br i1 %38, label %53, label %79
53: ; preds = %52, %76
%54 = phi i64 [ %77, %76 ], [ 1, %52 ]
%55 = load i32, i32* %7
%56 = sext i32 %55 to i64
%57 = icmp sgt i64 %46, %56
%58 = load i32, i32* %6
%59 = sext i32 %58 to i64
%60 = icmp sgt i64 %54, %59
%61 = select i1 %57, i1 true, i1 %60
br i1 %61, label %62, label %66
62: ; preds = %53
%63 = getelementptr inbounds i8*, i8** %0, i64 %54
%64 = load i8*, i8** %63
%65 = getelementptr inbounds i8, i8* %64, i64 %46
store i8 16, i8* %65
br label %76
66: ; preds = %53
%67 = add nsw i64 %54, -1
%68 = getelementptr inbounds i8, i8* %35, i64 %67
%69 = load i8, i8* %68
%70 = icmp eq i8 %69, 0
%71 = getelementptr inbounds i8*, i8** %0, i64 %54
%72 = load i8*, i8** %71
%73 = getelementptr inbounds i8, i8* %72, i64 %46
br i1 %70, label %74, label %75
74: ; preds = %66
store i8 0, i8* %73
br label %76
75: ; preds = %66
store i8 16, i8* %73
br label %76
76: ; preds = %62, %75, %74
%77 = add nuw nsw i64 %54, 1
%78 = icmp eq i64 %77, %43
br i1 %78, label %79, label %53
79: ; preds = %76, %52
%80 = add nuw nsw i64 %46, 1
%81 = icmp eq i64 %80, %42
br i1 %81, label %84, label %82
82: ; preds = %79
%83 = load i32, i32* %7
br label %44
84: ; preds = %79, %34
%85 = call i32 @free(i8* %35)
%86 = call i32 @fclose(i32* %13)
br label %87
87: ; preds = %84, %32, %21, %15
ret void
}