#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*/ 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);
  }
  fprintf(fp, "%c%c\n", magic_number[0], magic_number[1] = '5');
  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    0xe85(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a4 <iie_save_PGM+0x44>
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
movsbl -0x1e(%rbp),%edx
movb   $0x35,-0x1d(%rbp)
lea    0xe62(%rip),%rsi
mov    $0x35,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1080 <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
push   %rax
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xe8f(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11df <iie_save_PGM+0x7f>
mov    %rax,%rbx
lea    0xe7b(%rip),%rsi
mov    %rax,%rdi
xor    %edx,%edx
mov    $0x35,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe69(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <fclose@plt>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <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 = [6 x i8] c"%c%c\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [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 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 1
store i8 53, i8* %21
%22 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 53)
%23 = load i32*, i32** %9
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32*, i32** %9
%28 = load i32, i32* %7
%29 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = load [256 x i8]*, [256 x i8]** %3
%31 = bitcast [256 x i8]* %30 to i8**
%32 = load i32*, i32** %9
%33 = call i32 @fwrite(i8** %31, i32 1, i32 65536, i32* %32)
%34 = load i32*, i32** %9
%35 = call i32 @fclose(i32* %34)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.2 = [6 x i8] c"%c%c\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [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 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 53)
%10 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 undef, i32 undef)
%11 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 undef)
%12 = bitcast [256 x i8]* %0 to i8**
%13 = tail call i32 @fwrite(i8** %12, i32 1, i32 65536, i32* %3)
%14 = tail call i32 @fclose(i32* %3)
ret void
}