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


x86 assembly (clang, -O0):
<WritePPM3>:
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 <WritePPM3+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1246 <WritePPM3+0x116>
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    1236 <WritePPM3+0x106>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
imul   $0x3,%rcx,%rcx
add    %rcx,%rax
movzbl (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
imul   $0x3,%rcx,%rcx
add    %rcx,%rax
movzbl 0x1(%rax),%ecx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rdx
imul   $0x3,%rdx,%rdx
add    %rdx,%rax
movzbl 0x2(%rax),%r8d
mov    -0x2c(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
mov    %edx,%esi
mov    -0x30(%rbp),%edx
mov    -0x1c(%rbp),%r10d
sub    $0x1,%r10d
mov    $0x9,%r9d
mov    $0xa,%eax
cmp    %r10d,%esi
cmove  %eax,%r9d
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a2 <WritePPM3+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):
<WritePPM3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %rsi,%rbx
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11fe <WritePPM3+0xbe>
mov    %rax,%r15
lea    0xe90(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
lea    0xe7b(%rip),%rsi
mov    %r15,%rdi
mov    %r14d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
imul   %r14d,%ebp
test   %ebp,%ebp
jle    11f2 <WritePPM3+0xb2>
lea    -0x1(%r14),%r12d
mov    %ebp,%r13d
add    $0x2,%rbx
xor    %ebp,%ebp
nop
movzbl -0x2(%rbx),%r10d
movzbl -0x1(%rbx),%ecx
movzbl (%rbx),%r8d
mov    %ebp,%eax
cltd
idiv   %r14d
xor    %r9d,%r9d
cmp    %r12d,%edx
sete   %r9b
add    $0x9,%r9d
mov    %r15,%rdi
lea    0xe36(%rip),%rsi
mov    %r10d,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%rbp
add    $0x3,%rbx
cmp    %rbp,%r13
jne    11b0 <WritePPM3+0x70>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1203 <WritePPM3+0xc3>
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"P3\0A\00"
@.str.2 = [11 x i8] c"%d %d 255\0A\00"
@.str.3 = [11 x i8] c"%d %d %d%c\00"
define i32 @WritePPM3(i8* %0, [3 x i8]* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [3 x i8]*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %6
store [3 x i8]* %1, [3 x 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 %68
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 = %62, %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 %65
30: ; preds = %24
%31 = load i32*, i32** %10
%32 = load [3 x i8]*, [3 x i8]** %7
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [3 x i8], [3 x i8]* %32, i64 %34
%36 = getelementptr inbounds [3 x i8], [3 x i8]* %35, i64 0, i64 0
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = load [3 x i8]*, [3 x i8]** %7
%40 = load i32, i32* %11
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %39, i64 %41
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %42, i64 0, i64 1
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = load [3 x i8]*, [3 x i8]** %7
%47 = load i32, i32* %11
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [3 x i8], [3 x i8]* %46, i64 %48
%50 = getelementptr inbounds [3 x i8], [3 x i8]* %49, i64 0, i64 2
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = load i32, i32* %11
%54 = load i32, i32* %8
%55 = srem i32 %53, %54
%56 = load i32, i32* %8
%57 = sub nsw i32 %56, 1
%58 = icmp eq i32 %55, %57
%59 = zext i1 %58 to i64
%60 = select i1 %58, i32 10, i32 9
%61 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %38, i32 %45, i32 %52, i32 %60)
br label %62
62: ; preds = %30
%63 = load i32, i32* %11
%64 = add nsw i32 %63, 1
store i32 %64, i32* %11
br label %24
65: ; preds = %24
%66 = load i32*, i32** %10
%67 = call i32 @fclose(i32* %66)
store i32 0, i32* %5
br label %68
68: ; preds = %65, %16
%69 = load i32, i32* %5
ret i32 %69
}

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