#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 WritePGM2(const char *fname, unsigned char *data, int w, int h) {
  FILE *fp = fopen(fname, "w");
  int i;
  if (!fp)
    return -1;
  fprintf(fp, "P2\n");
  fprintf(fp, "%d %d 255\n", w, h);
  for (i = 0; i < w * h; ++i) {
    fprintf(fp, "%d%c", data[i], (i % w == w - 1) ? '\n' : ' ');
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<WritePGM2>:
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    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1171 <WritePGM2+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <WritePGM2+0xe7>
mov    -0x28(%rbp),%rdi
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe72(%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    1207 <WritePGM2+0xd7>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
mov    %edx,%esi
mov    -0x30(%rbp),%edx
mov    -0x1c(%rbp),%r8d
sub    $0x1,%r8d
mov    $0x20,%ecx
mov    $0xa,%eax
cmp    %r8d,%esi
cmove  %eax,%ecx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a2 <WritePGM2+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):
<WritePGM2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11f2 <WritePGM2+0xb2>
mov    %rax,%rbx
lea    0xe90(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
lea    0xe7b(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
imul   %r15d,%ebp
test   %ebp,%ebp
jle    11e6 <WritePGM2+0xa6>
lea    -0x1(%r15),%r12d
mov    %ebp,%r13d
xor    %ebp,%ebp
jmp    11cf <WritePGM2+0x8f>
nopl   (%rax)
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
lea    0xe52(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
je     11e6 <WritePGM2+0xa6>
mov    %ebp,%eax
cltd
idiv   %r15d
mov    $0xa,%ecx
cmp    %r12d,%edx
je     11b0 <WritePGM2+0x70>
mov    $0x20,%ecx
jmp    11b0 <WritePGM2+0x70>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11f7 <WritePGM2+0xb7>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P2\0A\00"
@.str.2 = [11 x i8] c"%d %d 255\0A\00"
@.str.3 = [5 x i8] c"%d%c\00"
define i32 @WritePGM2(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 ([2 x i8], [2 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 %53
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 = %47, %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 %50
30: ; preds = %24
%31 = load i32*, i32** %10
%32 = load i8*, i8** %7
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = load i32, i32* %11
%39 = load i32, i32* %8
%40 = srem i32 %38, %39
%41 = load i32, i32* %8
%42 = sub nsw i32 %41, 1
%43 = icmp eq i32 %40, %42
%44 = zext i1 %43 to i64
%45 = select i1 %43, i32 10, i32 32
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %45)
br label %47
47: ; preds = %30
%48 = load i32, i32* %11
%49 = add nsw i32 %48, 1
store i32 %49, i32* %11
br label %24
50: ; preds = %24
%51 = load i32*, i32** %10
%52 = call i32 @fclose(i32* %51)
store i32 0, i32* %5
br label %53
53: ; preds = %50, %16
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P2\0A\00"
@.str.2 = [11 x i8] c"%d %d 255\0A\00"
@.str.3 = [5 x i8] c"%d%c\00"
define i32 @WritePGM2(i8* nocapture %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %29, label %7
7: ; preds = %4
%8 = tail call i64 @fwrite(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 %27
12: ; preds = %7
%13 = add nsw i32 %2, -1
%14 = zext i32 %10 to i64
br label %15
15: ; preds = %12, %15
%16 = phi i64 [ 0, %12 ], [ %25, %15 ]
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = trunc i64 %16 to i32
%21 = srem i32 %20, %2
%22 = icmp eq i32 %21, %13
%23 = select i1 %22, i32 10, i32 32
%24 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %19, i32 %23)
%25 = add nuw nsw i64 %16, 1
%26 = icmp eq i64 %25, %14
br i1 %26, label %27, label %15
27: ; preds = %15, %7
%28 = tail call i32 @fclose(i32* %5)
br label %29
29: ; preds = %4, %27
%30 = phi i32 [ 0, %27 ], [ -1, %4 ]
ret i32 %30
}