#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(unsigned char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *);
void iie_load_PGM(unsigned char image[][256], const char path[]) {
  int height, width;
  int max_intensity;
  char magic_number[2];
  FILE *fp;
  if ((fp = fopen(path, "rb")) == NULL) {
    printf("InputFile Not Found\n");
    exit(1);
  } else {
    fscanf(fp, "%c%c", &magic_number[0], &magic_number[1]);
    if (magic_number[0] != 'P' || magic_number[1] != '5') {
      printf("UnMatch MagicNumber\n");
      exit(1);
    } else {
      fscanf(fp, "%d %d", &width, &height);
      if (width > 256 || height > 256) {
        printf("Size over\n");
        exit(1);
      } else {
        fscanf(fp, "%d", &max_intensity);
        if (max_intensity != 255) {
          printf("Out of Max Intensity\n");
          exit(1);
        } else {
          fgetc(fp);
          fread(image, sizeof(unsigned char), 256 * 256, fp);
          fclose(fp);
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<iie_load_PGM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b4 <iie_load_PGM+0x44>
lea    0xe5e(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    -0x1e(%rbp),%rdx
lea    -0x1e(%rbp),%rcx
add    $0x1,%rcx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1060 <fscanf@plt>
movsbl -0x1e(%rbp),%eax
cmp    $0x50,%eax
jne    11ec <iie_load_PGM+0x7c>
movsbl -0x1d(%rbp),%eax
cmp    $0x35,%eax
je     1202 <iie_load_PGM+0x92>
lea    0xe2a(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xe25(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x14(%rbp),%rcx
mov    $0x0,%al
call   1060 <fscanf@plt>
cmpl   $0x100,-0x18(%rbp)
jg     1236 <iie_load_PGM+0xc6>
cmpl   $0x100,-0x14(%rbp)
jle    124c <iie_load_PGM+0xdc>
lea    0xdfb(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xdde(%rip),%rsi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1060 <fscanf@plt>
cmpl   $0xff,-0x1c(%rbp)
je     1285 <iie_load_PGM+0x115>
lea    0xdcd(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1070 <fgetc@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12b3 <iie_load_PGM+0x143>
jmp    12b8 <iie_load_PGM+0x148>
jmp    12bd <iie_load_PGM+0x14d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iie_load_PGM>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xe7c(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1245 <iie_load_PGM+0xd5>
mov    %rax,%rbx
lea    0xb(%rsp),%rcx
lea    0xe5f(%rip),%rsi
lea    0xa(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1060 <fscanf@plt>
cmpb   $0x50,0xa(%rsp)
jne    124e <iie_load_PGM+0xde>
cmpb   $0x35,0xb(%rsp)
jne    124e <iie_load_PGM+0xde>
lea    0xe38(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x14(%rsp),%rcx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fscanf@plt>
cmpl   $0x100,0x10(%rsp)
jg     1257 <iie_load_PGM+0xe7>
cmpl   $0x101,0x14(%rsp)
jge    1257 <iie_load_PGM+0xe7>
lea    0xe0c(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fscanf@plt>
cmpl   $0xff,0xc(%rsp)
jne    1260 <iie_load_PGM+0xf0>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf5(%rip),%rdi
jmp    1267 <iie_load_PGM+0xf7>
lea    0xdd8(%rip),%rdi
jmp    1267 <iie_load_PGM+0xf7>
lea    0xdc5(%rip),%rdi
jmp    1267 <iie_load_PGM+0xf7>
lea    0xda7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [21 x i8] c"InputFile Not Found\0A\00"
@.str.2 = [5 x i8] c"%c%c\00"
@.str.3 = [21 x i8] c"UnMatch MagicNumber\0A\00"
@.str.4 = [6 x i8] c"%d %d\00"
@.str.5 = [11 x i8] c"Size over\0A\00"
@.str.6 = [3 x i8] c"%d\00"
@.str.7 = [22 x i8] c"Out of Max Intensity\0A\00"
define void @iie_load_PGM([256 x i8]* %0, i8* %1) {
%3 = alloca [256 x i8]*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i8]
%9 = alloca i32*
store [256 x i8]* %0, [256 x i8]** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32*, i32** %9
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 1
%20 = call i32 (i32*, i8*, ...) @fscanf(i32* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %18, i8* %19)
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 80
br i1 %24, label %30, label %25
25: ; preds = %16
%26 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 1
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 53
br i1 %29, label %30, label %33
30: ; preds = %25, %16
%31 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %25
%34 = load i32*, i32** %9
%35 = call i32 (i32*, i8*, ...) @fscanf(i32* %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %6, i32* %5)
%36 = load i32, i32* %6
%37 = icmp sgt i32 %36, 256
br i1 %37, label %41, label %38
38: ; preds = %33
%39 = load i32, i32* %5
%40 = icmp sgt i32 %39, 256
br i1 %40, label %41, label %44
41: ; preds = %38, %33
%42 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %38
%45 = load i32*, i32** %9
%46 = call i32 (i32*, i8*, ...) @fscanf(i32* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %7)
%47 = load i32, i32* %7
%48 = icmp ne i32 %47, 255
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %44
%53 = load i32*, i32** %9
%54 = call i32 @fgetc(i32* %53)
%55 = load [256 x i8]*, [256 x i8]** %3
%56 = bitcast [256 x i8]* %55 to i8**
%57 = load i32*, i32** %9
%58 = call i32 @fread(i8** %56, i32 1, i32 65536, i32* %57)
%59 = load i32*, i32** %9
%60 = call i32 @fclose(i32* %59)
br label %61
61: ; preds = %52
br label %62
62: ; preds = %61
br label %63
63: ; preds = %62
br label %64
64: ; preds = %63
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.2 = [5 x i8] c"%c%c\00"
@.str.4 = [6 x i8] c"%d %d\00"
@.str.6 = [3 x i8] c"%d\00"
@str = [21 x i8] c"Out of Max Intensity\00"
@str.8 = [10 x i8] c"Size over\00"
@str.9 = [20 x i8] c"UnMatch MagicNumber\00"
@str.10 = [20 x i8] c"InputFile Not Found\00"
define void @iie_load_PGM([256 x i8]* nocapture %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%11 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 1
%18 = call i32 (i32*, i8*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %10, i8* %17)
%19 = load i8, i8* %10
%20 = icmp ne i8 %19, 80
%21 = load i8, i8* %17
%22 = icmp ne i8 %21, 53
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %16
%28 = call i32 (i32*, i8*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %4, i32* %3)
%29 = load i32, i32* %4
%30 = icmp sgt i32 %29, 256
%31 = load i32, i32* %3
%32 = icmp sgt i32 %31, 256
%33 = select i1 %30, i1 true, i1 %32
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.8, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27
%38 = call i32 (i32*, i8*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %5)
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, 255
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37
%45 = call i32 @fgetc(i32* %11)
%46 = bitcast [256 x i8]* %0 to i8**
%47 = call i32 @fread(i8** %46, i32 1, i32 65536, i32* %11)
%48 = call i32 @fclose(i32* %11)
ret void
}