#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 fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int WritePPM6(const char *fname, unsigned char (*data)[3], int w, int h) {
  FILE *fp = fopen(fname, "wb");
  int i;
  if (!fp)
    return -1;
  fprintf(fp, "P6\n");
  fprintf(fp, "%d %d 255\n", w, h);
  for (i = 0; i < w * h; ++i) {
    if (fwrite(data[i], 1, 3, fp) != 3) {
      fclose(fp);
      return -1;
    }
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<WritePPM6>:
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   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1181 <WritePPM6+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <WritePPM6+0xe7>
mov    -0x28(%rbp),%rdi
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe63(%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    1217 <WritePPM6+0xd7>
mov    -0x18(%rbp),%rdi
movslq -0x2c(%rbp),%rax
imul   $0x3,%rax,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1060 <fwrite@plt>
cmp    $0x3,%eax
je     1204 <WritePPM6+0xc4>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <WritePPM6+0xe7>
jmp    1209 <WritePPM6+0xc9>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b2 <WritePPM6+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):
<WritePPM6>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %edx,%r12d
mov    %rsi,%rbx
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c6 <WritePPM6+0x86>
mov    %rax,%r14
lea    0xe98(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
lea    0xe83(%rip),%rsi
xor    %r15d,%r15d
mov    %r14,%rdi
mov    %r12d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
imul   %r12d,%ebp
test   %ebp,%ebp
jle    11d4 <WritePPM6+0x94>
mov    %ebp,%ebp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
cmp    $0x3,%eax
jne    11ce <WritePPM6+0x8e>
add    $0x3,%rbx
add    $0xffffffffffffffff,%rbp
jne    11a0 <WritePPM6+0x60>
jmp    11d4 <WritePPM6+0x94>
mov    $0xffffffff,%r15d
jmp    11dc <WritePPM6+0x9c>
mov    $0xffffffff,%r15d
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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