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


x86 assembly (clang, -O0):
<WritePBM1>:
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   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1181 <WritePBM1+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <WritePBM1+0xf2>
mov    -0x28(%rbp),%rdi
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x1c(%rbp),%ecx
imul   -0x20(%rbp),%ecx
cmp    %ecx,%eax
jge    1222 <WritePBM1+0xe2>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
mov    $0x31,%eax
mov    $0x30,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
mov    -0x2c(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    $0x20,%eax
mov    $0xa,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b2 <WritePBM1+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):
<WritePBM1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     120a <WritePBM1+0xba>
mov    %rax,%rbx
lea    0xe80(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe6b(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
imul   %r15d,%ebp
test   %ebp,%ebp
jle    11fe <WritePBM1+0xae>
lea    -0x1(%r15),%r13d
mov    %ebp,%r12d
xor    %ebp,%ebp
jmp    11d1 <WritePBM1+0x81>
nopl   (%rax)
mov    %rbx,%rsi
call   1040 <putc@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
je     11fe <WritePBM1+0xae>
xor    %edi,%edi
cmpb   $0x0,(%r14,%rbp,1)
sete   %dil
or     $0x30,%edi
mov    %rbx,%rsi
call   1040 <putc@plt>
mov    %ebp,%eax
cltd
idiv   %r15d
mov    $0xa,%edi
cmp    %r13d,%edx
je     11c0 <WritePBM1+0x70>
mov    $0x20,%edi
jmp    11c0 <WritePBM1+0x70>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    120f <WritePBM1+0xbf>
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"P1\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
define i32 @WritePBM1(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 %60
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 ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22)
store i32 0, i32* %11
br label %24
24: ; preds = %54, %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 %57
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 i8, i8* %34
%36 = zext i8 %35 to i32
%37 = icmp ne i32 %36, 0
%38 = zext i1 %37 to i64
%39 = select i1 %37, i32 48, i32 49
%40 = trunc i32 %39 to i8
%41 = load i32*, i32** %10
%42 = call i32 @putc(i8 signext %40, i32* %41)
%43 = load i32, i32* %11
%44 = load i32, i32* %8
%45 = srem i32 %43, %44
%46 = load i32, i32* %8
%47 = sub nsw i32 %46, 1
%48 = icmp eq i32 %45, %47
%49 = zext i1 %48 to i64
%50 = select i1 %48, i32 10, i32 32
%51 = trunc i32 %50 to i8
%52 = load i32*, i32** %10
%53 = call i32 @putc(i8 signext %51, i32* %52)
br label %54
54: ; preds = %30
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
br label %24
57: ; preds = %24
%58 = load i32*, i32** %10
%59 = call i32 @fclose(i32* %58)
store i32 0, i32* %5
br label %60
60: ; preds = %57, %16
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P1\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
define i32 @WritePBM1(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 %31, 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 ([7 x i8], [7 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 %29
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 ], [ %27, %15 ]
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
%20 = select i1 %19, i8 49, i8 48
%21 = tail call i32 @putc(i8 signext %20, i32* %5)
%22 = trunc i64 %16 to i32
%23 = srem i32 %22, %2
%24 = icmp eq i32 %23, %13
%25 = select i1 %24, i8 10, i8 32
%26 = tail call i32 @putc(i8 signext %25, i32* %5)
%27 = add nuw nsw i64 %16, 1
%28 = icmp eq i64 %27, %14
br i1 %28, label %29, label %15
29: ; preds = %15, %7
%30 = tail call i32 @fclose(i32* %5)
br label %31
31: ; preds = %4, %29
%32 = phi i32 [ 0, %29 ], [ -1, %4 ]
ret i32 %32
}