#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*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
void write_pgm(const char *filename, int width, int height,
               unsigned char **components) {
  FILE *F;
  char temp[1024];
  snprintf(temp, 1024, "%s", filename);
  F = fopen(temp, "wb");
  fprintf(F, "P5\n%d %d\n255\n", width, height);
  fwrite(components[0], width, height, F);
  fclose(F);
}


x86 assembly (clang, -O0):
<write_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xe80(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1070 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pgm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rcx,%r14
mov    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%rcx
lea    0xe8f(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x400,%esi
call   1040 <snprintf@plt>
lea    0xe7b(%rip),%rsi
mov    %r15,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xe6c(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
mov    %r12d,%ecx
call   1050 <fprintf@plt>
mov    (%r14),%rdi
mov    %ebp,%esi
mov    %r12d,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
define void @write_pgm(i8* %0, i32 %1, i32 %2, i8** nocapture %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
%10 = load i8*, i8** %3
%11 = call i32 @fwrite(i8* %10, i32 %1, i32 %2, i32* %8)
%12 = call i32 @fclose(i32* %8)
ret void
}