#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 *, int, int);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
void write_pnm(unsigned char *rgb, int width, int height, char *filename) {
  FILE *fp = fopen(filename, "w");
  fprintf(fp, "P6\n%d %d\n255\n", width, height);
  fwrite(rgb, 1, 3 * width * height, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_pnm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe87(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
imul   $0x3,-0xc(%rbp),%edx
imul   -0x10(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pnm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xeaa(%rip),%rsi
mov    %rcx,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbp
lea    0xe9a(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
mov    %r15d,%ecx
call   1040 <fprintf@plt>
imul   %r15d,%ebx
lea    (%rbx,%rbx,2),%edx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbp,%rcx
call   1060 <fwrite@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
define void @write_pnm(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%10 = load i8*, i8** %8
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = load i32, i32* %6
%14 = load i32, i32* %7
%15 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
%16 = load i8*, i8** %5
%17 = load i32, i32* %6
%18 = mul nsw i32 3, %17
%19 = load i32, i32* %7
%20 = mul nsw i32 %18, %19
%21 = load i32*, i32** %9
%22 = call i32 @fwrite(i8* %16, i32 1, i32 %20, i32* %21)
%23 = load i32*, i32** %9
%24 = call i32 @fclose(i32* %23)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
define void @write_pnm(i8* nocapture %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %2)
%7 = mul nsw i32 %1, 3
%8 = mul nsw i32 %7, %2
%9 = tail call i32 @fwrite(i8* %0, i32 1, i32 %8, i32* %5)
%10 = tail call i32 @fclose(i32* %5)
ret void
}