#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*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, ...);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fwrite(unsigned char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void iie_save_PGM(unsigned char image[][256], const char path[]) {
  int height, width;
  int max_intensity;
  char magic_number[2];
  FILE *fp;
  if ((fp = fopen(path, "wb")) == NULL) {
    printf("OutputFile Not Found\n");
    exit(1);
  }
  fscanf(fp, "%c%c", &magic_number[0], &magic_number[1]);
  fscanf(fp, "%d %d", &width, &height);
  fscanf(fp, "%d", &max_intensity);
  fprintf(fp, "%c%c\n", magic_number[0], magic_number[1]);
  fprintf(fp, "%d %d\n", width, height);
  fprintf(fp, "%d\n", max_intensity);
  fwrite(image, sizeof(unsigned char), 256 * 256, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<iie_save_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   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b4 <iie_save_PGM+0x44>
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    -0x1e(%rbp),%rdx
lea    -0x1e(%rbp),%rcx
add    $0x1,%rcx
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe41(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x14(%rbp),%rcx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe2a(%rip),%rsi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
movsbl -0x1e(%rbp),%edx
movsbl -0x1d(%rbp),%ecx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1090 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iie_save_PGM>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xe7c(%rip),%rsi
mov    %rax,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1252 <iie_save_PGM+0xe2>
mov    %rax,%rbx
lea    0xb(%rsp),%rcx
lea    0xe5f(%rip),%rsi
lea    0xa(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <fscanf@plt>
lea    0xe4e(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x14(%rsp),%rcx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fscanf@plt>
lea    0xe36(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fscanf@plt>
movsbl 0xa(%rsp),%edx
movsbl 0xb(%rsp),%ecx
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x10(%rsp),%edx
mov    0x14(%rsp),%ecx
lea    0xe06(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0xc(%rsp),%edx
lea    0xdf4(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdc2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [22 x i8] c"OutputFile Not Found\0A\00"
@.str.2 = [5 x i8] c"%c%c\00"
@.str.3 = [6 x i8] c"%d %d\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [6 x i8] c"%c%c\0A\00"
@.str.6 = [7 x i8] c"%d %d\0A\00"
@.str.7 = [4 x i8] c"%d\0A\00"
define void @iie_save_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 ([22 x i8], [22 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 = load i32*, i32** %9
%22 = call i32 (i32*, i8*, ...) @fscanf(i32* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %6, i32* %5)
%23 = load i32*, i32** %9
%24 = call i32 (i32*, i8*, ...) @fscanf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %7)
%25 = load i32*, i32** %9
%26 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 1
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 %31)
%33 = load i32*, i32** %9
%34 = load i32, i32* %6
%35 = load i32, i32* %5
%36 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %34, i32 %35)
%37 = load i32*, i32** %9
%38 = load i32, i32* %7
%39 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %38)
%40 = load [256 x i8]*, [256 x i8]** %3
%41 = bitcast [256 x i8]* %40 to i8**
%42 = load i32*, i32** %9
%43 = call i32 @fwrite(i8** %41, i32 1, i32 65536, i32* %42)
%44 = load i32*, i32** %9
%45 = call i32 @fclose(i32* %44)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.2 = [5 x i8] c"%c%c\00"
@.str.3 = [6 x i8] c"%d %d\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [6 x i8] c"%c%c\0A\00"
@.str.6 = [7 x i8] c"%d %d\0A\00"
@.str.7 = [4 x i8] c"%d\0A\00"
@str = [21 x i8] c"OutputFile Not Found\00"
define void @iie_save_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 ([21 x i8], [21 x i8]* @str, 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 = call i32 (i32*, i8*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %4, i32* %3)
%20 = call i32 (i32*, i8*, ...) @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %5)
%21 = load i8, i8* %10
%22 = sext i8 %21 to i32
%23 = load i8, i8* %17
%24 = sext i8 %23 to i32
%25 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %22, i32 %24)
%26 = load i32, i32* %4
%27 = load i32, i32* %3
%28 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i32, i32* %5
%30 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %29)
%31 = bitcast [256 x i8]* %0 to i8**
%32 = call i32 @fwrite(i8** %31, i32 1, i32 65536, i32* %11)
%33 = call i32 @fclose(i32* %11)
ret void
}