#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 *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void writepgm(int **d, int xres, int yres, int white, char filename[]) {
  int i, j;
  FILE *outfile;
  outfile = fopen(filename, "w");
  fprintf(outfile, "P2\n");
  fprintf(outfile, "# Written by pnmmodules "
                   "(https://github.com/owainkenwayucl/pnmmodules).\n");
  fprintf(outfile, "%d %d\n%d\n", xres, yres, white);
  for (j = 0; j < yres; j++) {
    for (i = 0; i < xres; i++) {
      fprintf(outfile, "%d\n", d[i][j]);
    }
  }
  fclose(outfile);
}


x86 assembly (clang, -O0):
<writepgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xeac(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe9a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rdi
lea    0xe8c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x14(%rbp),%r8d
lea    0xeb9(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1209 <writepgm+0xd9>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f6 <writepgm+0xc6>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe74(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b7 <writepgm+0x87>
jmp    11fb <writepgm+0xcb>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11a4 <writepgm+0x74>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writepgm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r13
lea    0xea0(%rip),%rsi
mov    %r8,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbp
lea    0xe90(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
lea    0xe7b(%rip),%rdi
mov    $0x48,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1060 <fwrite@plt>
lea    0xeab(%rip),%rsi
mov    %rbp,%rdi
mov    %ebx,0xc(%rsp)
mov    %ebx,%edx
mov    %r15d,%ecx
mov    %r12d,%r8d
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %r15d,%r15d
jle    1221 <writepgm+0xe1>
mov    %r15d,%eax
mov    %rax,0x10(%rsp)
mov    0xc(%rsp),%ebx
lea    0xe83(%rip),%r15
xor    %r14d,%r14d
jmp    11eb <writepgm+0xab>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14
cmp    0x10(%rsp),%r14
je     1221 <writepgm+0xe1>
cmpl   $0x0,0xc(%rsp)
jle    11e0 <writepgm+0xa0>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13,%r12,8),%rax
mov    (%rax,%r14,4),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%rbx
jne    1200 <writepgm+0xc0>
jmp    11e0 <writepgm+0xa0>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P2\0A\00"
@.str.2 = [73 x i8] c"# Written by pnmmodules (https://github.com/owainkenwayucl/pnmmodules).\0A\00"
@.str.3 = [10 x i8] c"%d %d\0A%d\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define void @writepgm(i32** %0, i32 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
store i32** %0, i32*** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%14 = load i8*, i8** %10
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %13
%16 = load i32*, i32** %13
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32*, i32** %13
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32*, i32** %13
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = load i32, i32* %9
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %22, i32 %23)
store i32 0, i32* %12
br label %25
25: ; preds = %50, %5
%26 = load i32, i32* %12
%27 = load i32, i32* %8
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %53
29: ; preds = %25
store i32 0, i32* %11
br label %30
30: ; preds = %46, %29
%31 = load i32, i32* %11
%32 = load i32, i32* %7
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %49
34: ; preds = %30
%35 = load i32*, i32** %13
%36 = load i32**, i32*** %6
%37 = load i32, i32* %11
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32*, i32** %36, i64 %38
%40 = load i32*, i32** %39
%41 = load i32, i32* %12
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = load i32, i32* %43
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %34
%47 = load i32, i32* %11
%48 = add nsw i32 %47, 1
store i32 %48, i32* %11
br label %30
49: ; preds = %30
br label %50
50: ; preds = %49
%51 = load i32, i32* %12
%52 = add nsw i32 %51, 1
store i32 %52, i32* %12
br label %25
53: ; preds = %25
%54 = load i32*, i32** %13
%55 = call i32 @fclose(i32* %54)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P2\0A\00"
@.str.2 = [73 x i8] c"# Written by pnmmodules (https://github.com/owainkenwayucl/pnmmodules).\0A\00"
@.str.3 = [10 x i8] c"%d %d\0A%d\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define void @writepgm(i32** nocapture %0, i32 %1, i32 %2, i32 %3, i8* nocapture %4) {
%6 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 3, i64 1, i32* %6)
%8 = tail call i64 @fwrite(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.2, i64 0, i64 0), i64 72, i64 1, i32* %6)
%9 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %1, i32 %2, i32 %3)
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %29
11: ; preds = %5
%12 = icmp sgt i32 %1, 0
%13 = zext i32 %2 to i64
%14 = zext i32 %1 to i64
br label %15
15: ; preds = %11, %26
%16 = phi i64 [ 0, %11 ], [ %27, %26 ]
br i1 %12, label %17, label %26
17: ; preds = %15, %17
%18 = phi i64 [ %24, %17 ], [ 0, %15 ]
%19 = getelementptr inbounds i32*, i32** %0, i64 %18
%20 = load i32*, i32** %19
%21 = getelementptr inbounds i32, i32* %20, i64 %16
%22 = load i32, i32* %21
%23 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = add nuw nsw i64 %18, 1
%25 = icmp eq i64 %24, %14
br i1 %25, label %26, label %17
26: ; preds = %17, %15
%27 = add nuw nsw i64 %16, 1
%28 = icmp eq i64 %27, %13
br i1 %28, label %29, label %15
29: ; preds = %26, %5
%30 = tail call i32 @fclose(i32* %6)
ret void
}