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


x86 assembly (clang, -O0):
<writepbm>:
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
lea    0xebd(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    122a <writepbm+0xfa>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1217 <writepbm+0xe7>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jl     11f2 <writepbm+0xc2>
mov    -0x30(%rbp),%rdi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1204 <writepbm+0xd4>
mov    -0x30(%rbp),%rdi
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1209 <writepbm+0xd9>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b3 <writepbm+0x83>
jmp    121c <writepbm+0xec>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11a0 <writepbm+0x70>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writepbm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%r13
lea    0xea0(%rip),%rsi
mov    %r8,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
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    %rbx,%rcx
call   1060 <fwrite@plt>
lea    0xeab(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,0xc(%rsp)
mov    %ebp,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %r12d,%r12d
jle    1226 <writepbm+0xe6>
mov    %r12d,%eax
mov    %rax,0x10(%rsp)
mov    0xc(%rsp),%ebp
xor    %r14d,%r14d
jmp    11db <writepbm+0x9b>
nopl   (%rax)
add    $0x1,%r14
cmp    0x10(%rsp),%r14
je     1226 <writepbm+0xe6>
cmpl   $0x0,0xc(%rsp)
jle    11d0 <writepbm+0x90>
xor    %r12d,%r12d
jmp    120b <writepbm+0xcb>
nopw   0x0(%rax,%rax,1)
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
add    $0x1,%r12
cmp    %r12,%rbp
je     11d0 <writepbm+0x90>
mov    0x0(%r13,%r12,8),%rax
lea    0xe3f(%rip),%rdi
cmp    %r15d,(%rax,%r14,4)
jl     11f0 <writepbm+0xb0>
lea    0xddf(%rip),%rdi
jmp    11f0 <writepbm+0xb0>
mov    %rbx,%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"P1\0A\00"
@.str.2 = [73 x i8] c"# Written by pnmmodules (https://github.com/owainkenwayucl/pnmmodules).\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [3 x i8] c"1\0A\00"
@.str.5 = [3 x i8] c"0\0A\00"
define void @writepbm(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 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %22)
store i32 0, i32* %12
br label %24
24: ; preds = %56, %5
%25 = load i32, i32* %12
%26 = load i32, i32* %8
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %59
28: ; preds = %24
store i32 0, i32* %11
br label %29
29: ; preds = %52, %28
%30 = load i32, i32* %11
%31 = load i32, i32* %7
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %55
33: ; preds = %29
%34 = load i32**, i32*** %6
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32*, i32** %34, i64 %36
%38 = load i32*, i32** %37
%39 = load i32, i32* %12
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = load i32, i32* %9
%44 = icmp sge i32 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %33
%46 = load i32*, i32** %13
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %51
48: ; preds = %33
%49 = load i32*, i32** %13
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %48, %45
br label %52
52: ; preds = %51
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
br label %29
55: ; preds = %29
br label %56
56: ; preds = %55
%57 = load i32, i32* %12
%58 = add nsw i32 %57, 1
store i32 %58, i32* %12
br label %24
59: ; preds = %24
%60 = load i32*, i32** %13
%61 = call i32 @fclose(i32* %60)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P1\0A\00"
@.str.2 = [73 x i8] c"# Written by pnmmodules (https://github.com/owainkenwayucl/pnmmodules).\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [3 x i8] c"1\0A\00"
@.str.5 = [3 x i8] c"0\0A\00"
define void @writepbm(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 ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %1, i32 %2)
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %31
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, %28
%16 = phi i64 [ 0, %11 ], [ %29, %28 ]
br i1 %12, label %17, label %28
17: ; preds = %15, %17
%18 = phi i64 [ %26, %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 = icmp slt i32 %22, %3
%24 = select i1 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0)
%25 = tail call i64 @fwrite(i8* %24, i64 2, i64 1, i32* %6)
%26 = add nuw nsw i64 %18, 1
%27 = icmp eq i64 %26, %14
br i1 %27, label %28, label %17
28: ; preds = %17, %15
%29 = add nuw nsw i64 %16, 1
%30 = icmp eq i64 %29, %13
br i1 %30, label %31, label %15
31: ; preds = %28, %5
%32 = tail call i32 @fclose(i32* %6)
ret void
}