#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*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void writePPM(const char *filename, unsigned char *myimg, int w, int h) {
  FILE *fp;
  fp = fopen(filename, "wb");
  if (!fp) {
    fprintf(stderr, "Unable to open file '%s'\n", filename);
  }
  fprintf(fp, "P6\n");
  fprintf(fp, "%d %d\n", w, h);
  fprintf(fp, "%d\n", 255);
  fwrite(myimg, 3, w * h, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<writePPM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1191 <writePPM+0x51>
mov    0x2e64(%rip),%rax
movslq (%rax),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe6d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe5e(%rip),%rsi
mov    $0xff,%edx
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
imul   -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x3,%esi
call   1060 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writePPM>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1182 <writePPM+0x42>
mov    0x2e72(%rip),%rax
movslq (%rax),%rdi
lea    0xe8b(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe94(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
lea    0xe7f(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe6c(%rip),%rsi
mov    %rbx,%rdi
mov    $0xff,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
imul   %r15d,%ebp
mov    %r14,%rdi
mov    $0x3,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"Unable to open file '%s'\0A\00"
@.str.2 = [4 x i8] c"P6\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define void @writePPM(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp ne i32* %12, null
br i1 %13, label %20, label %14
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i32*
%18 = load i8*, i8** %5
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %14, %4
%21 = load i32*, i32** %9
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32*, i32** %9
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32*, i32** %9
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 255)
%29 = load i8*, i8** %6
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = mul nsw i32 %30, %31
%33 = load i32*, i32** %9
%34 = call i32 @fwrite(i8* %29, i32 3, i32 %32, i32* %33)
%35 = load i32*, i32** %9
%36 = call i32 @fclose(i32* %35)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"Unable to open file '%s'\0A\00"
@.str.2 = [4 x i8] c"P6\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define void @writePPM(i8* %0, 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 %7, label %12
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i32*
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %7, %4
%13 = 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.2, i64 0, i64 0), i64 3, i64 1, i32* %5)
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %2, i32 %3)
%15 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 255)
%16 = mul nsw i32 %3, %2
%17 = tail call i32 @fwrite(i8* %1, i32 3, i32 %16, i32* %5)
%18 = tail call i32 @fclose(i32* %5)
ret void
}