#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 fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int WritePGM5(const char *fname, unsigned char *data, int w, int h) {
  FILE *fp = fopen(fname, "wb");
  int i;
  if (!fp)
    return -1;
  fprintf(fp, "P5\n");
  fprintf(fp, "%d %d 255\n", w, h);
  for (i = 0; i < w * h; ++i) {
    if (fwrite(&data[i], 1, 1, fp) != 1) {
      fclose(fp);
      return -1;
    }
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<WritePGM5>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1181 <WritePGM5+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <WritePGM5+0xe0>
mov    -0x28(%rbp),%rdi
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x1c(%rbp),%ecx
imul   -0x20(%rbp),%ecx
cmp    %ecx,%eax
jge    1210 <WritePGM5+0xd0>
mov    -0x18(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <fwrite@plt>
cmp    $0x1,%eax
je     11fd <WritePGM5+0xbd>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <WritePGM5+0xe0>
jmp    1202 <WritePGM5+0xc2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b2 <WritePGM5+0x72>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WritePGM5>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %edx,%r12d
mov    %rsi,%rbx
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c6 <WritePGM5+0x86>
mov    %rax,%r14
lea    0xe98(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
lea    0xe83(%rip),%rsi
xor    %r15d,%r15d
mov    %r14,%rdi
mov    %r12d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
imul   %r12d,%ebp
test   %ebp,%ebp
jle    11d4 <WritePGM5+0x94>
mov    %ebp,%ebp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
cmp    $0x1,%eax
jne    11ce <WritePGM5+0x8e>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    11a0 <WritePGM5+0x60>
jmp    11d4 <WritePGM5+0x94>
mov    $0xffffffff,%r15d
jmp    11dc <WritePGM5+0x9c>
mov    $0xffffffff,%r15d
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [4 x i8] c"P5\0A\00"
@.str.2 = [11 x i8] c"%d %d 255\0A\00"
define i32 @WritePGM5(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %10
%14 = load i32*, i32** %10
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %4
store i32 -1, i32* %5
br label %48
17: ; preds = %4
%18 = load i32*, i32** %10
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32*, i32** %10
%21 = load i32, i32* %8
%22 = load i32, i32* %9
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22)
store i32 0, i32* %11
br label %24
24: ; preds = %42, %17
%25 = load i32, i32* %11
%26 = load i32, i32* %8
%27 = load i32, i32* %9
%28 = mul nsw i32 %26, %27
%29 = icmp slt i32 %25, %28
br i1 %29, label %30, label %45
30: ; preds = %24
%31 = load i8*, i8** %7
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i32*, i32** %10
%36 = call i32 @fwrite(i8* %34, i32 1, i32 1, i32* %35)
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = load i32*, i32** %10
%40 = call i32 @fclose(i32* %39)
store i32 -1, i32* %5
br label %48
41: ; preds = %30
br label %42
42: ; preds = %41
%43 = load i32, i32* %11
%44 = add nsw i32 %43, 1
store i32 %44, i32* %11
br label %24
45: ; preds = %24
%46 = load i32*, i32** %10
%47 = call i32 @fclose(i32* %46)
store i32 0, i32* %5
br label %48
48: ; preds = %45, %38, %16
%49 = load i32, i32* %5
ret i32 %49
}

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