#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 const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fwrite(unsigned char const *, int, int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ sarerror_perror();
int pgmwrite(const unsigned char *buffer_gray, int M, int N,
             const char *filename) {
  FILE *f = fopen(filename, "w");
  if (!f) {
    sarerror_perror();
    return 0;
  }
  fprintf(f, "P5\n\n");
  fprintf(f, "%d %d\n", N, M);
  fprintf(f, "255\n");
  fwrite(buffer_gray, sizeof(unsigned char), M * N, f);
  fclose(f);
  return 1;
}


x86 assembly (clang, -O0):
<pgmwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1198 <pgmwrite+0x48>
mov    $0x0,%al
call   1060 <sarerror_perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fd <pgmwrite+0xad>
mov    -0x28(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
imul   -0x18(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgmwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xe9a(%rip),%rsi
mov    %rcx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e4 <pgmwrite+0x94>
mov    %rax,%rbx
lea    0xe85(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe62(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
imul   %r15d,%ebp
mov    $0x1,%r15d
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11ee <pgmwrite+0x9e>
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <sarerror_perror@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"P5\0A\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [5 x i8] c"255\0A\00"
define i32 @pgmwrite(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%11 = load i8*, i8** %9
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %10
%13 = load i32*, i32** %10
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %4
%16 = call i32 (...) @sarerror_perror()
store i32 0, i32* %5
br label %34
17: ; preds = %4
%18 = load i32*, i32** %10
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32*, i32** %10
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22)
%24 = load i32*, i32** %10
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = load i32, i32* %8
%29 = mul nsw i32 %27, %28
%30 = load i32*, i32** %10
%31 = call i32 @fwrite(i8* %26, i32 1, i32 %29, i32* %30)
%32 = load i32*, i32** %10
%33 = call i32 @fclose(i32* %32)
store i32 1, i32* %5
br label %34
34: ; preds = %17, %15
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"P5\0A\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [5 x i8] c"255\0A\00"
define i32 @pgmwrite(i8* nocapture %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 (...) @sarerror_perror()
br label %16
9: ; preds = %4
%10 = tail call i64 bitcast (i32 (i8*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 4, i64 1, i32* %5)
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %2, i32 %1)
%12 = tail call i64 bitcast (i32 (i8*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 4, i64 1, i32* %5)
%13 = mul nsw i32 %2, %1
%14 = tail call i32 @fwrite(i8* %0, i32 1, i32 %13, i32* %5)
%15 = tail call i32 @fclose(i32* %5)
br label %16
16: ; preds = %9, %7
%17 = phi i32 [ 1, %9 ], [ 0, %7 ]
ret i32 %17
}