#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*/ uint8_t;
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, int);
int fwrite(int /*<<< orphan*/ ***, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
bool write_pnm(uint8_t data[][320][3], int width, int height,
               const char *filename) {
  FILE *file = fopen(filename, "wb");
  if (file == NULL) {
    perror("Could not open file for writing");
    return false;
  }
  fprintf(file, "P6 %d %d 255\n", width, height);
  if (fwrite(data, width * height * 3, 1, file) != 1) {
    perror("Could not write output data");
    fclose(file);
    return false;
  }
  fclose(file);
  return true;
}


x86 assembly (clang, -O0):
<write_pnm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119d <write_pnm+0x4d>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1209 <write_pnm+0xb9>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe75(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
imul   $0x3,%eax,%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
je     11f9 <write_pnm+0xa9>
lea    0xe52(%rip),%rdi
call   1060 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1209 <write_pnm+0xb9>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pnm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xe9b(%rip),%rsi
mov    %rcx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11bd <write_pnm+0x6d>
mov    %rax,%r15
lea    0xea7(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1040 <fprintf@plt>
imul   %ebp,%ebx
lea    (%rbx,%rbx,2),%esi
mov    $0x1,%ebx
mov    %r14,%rdi
mov    $0x1,%edx
mov    %r15,%rcx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
jne    11cd <write_pnm+0x7d>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    11b0 <write_pnm+0x60>
lea    0xe5d(%rip),%rdi
call   1060 <perror@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
jmp    11b0 <write_pnm+0x60>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [32 x i8] c"Could not open file for writing\00"
@.str.2 = [14 x i8] c"P6 %d %d 255\0A\00"
@.str.3 = [28 x i8] c"Could not write output data\00"
define i32 @write_pnm([320 x [3 x i32]]* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca [320 x [3 x i32]]*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
store [320 x [3 x i32]]* %0, [320 x [3 x i32]]** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%11 = load i8*, i8** %9
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %10
%13 = load i32*, i32** %10
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %38
17: ; preds = %4
%18 = load i32*, i32** %10
%19 = load i32, i32* %7
%20 = load i32, i32* %8
%21 = call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
%22 = load [320 x [3 x i32]]*, [320 x [3 x i32]]** %6
%23 = bitcast [320 x [3 x i32]]* %22 to i32***
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = mul nsw i32 %24, %25
%27 = mul nsw i32 %26, 3
%28 = load i32*, i32** %10
%29 = call i32 @fwrite(i32*** %23, i32 %27, i32 1, i32* %28)
%30 = icmp ne i32 %29, 1
br i1 %30, label %31, label %35
31: ; preds = %17
%32 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32*, i32** %10
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %5
br label %38
35: ; preds = %17
%36 = load i32*, i32** %10
%37 = call i32 @fclose(i32* %36)
store i32 1, i32* %5
br label %38
38: ; preds = %35, %31, %15
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [32 x i8] c"Could not open file for writing\00"
@.str.2 = [14 x i8] c"P6 %d %d 255\0A\00"
@.str.3 = [28 x i8] c"Could not write output data\00"
define i32 @write_pnm([320 x [3 x i32]]* nocapture %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32* @fopen(i8* %3, 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 %9
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %21
9: ; preds = %4
%10 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
%11 = bitcast [320 x [3 x i32]]* %0 to i32***
%12 = mul i32 %1, 3
%13 = mul i32 %12, %2
%14 = tail call i32 @fwrite(i32*** %11, i32 %13, i32 1, i32* %5)
%15 = icmp eq i32 %14, 1
br i1 %15, label %19, label %16
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%18 = tail call i32 @fclose(i32* %5)
br label %21
19: ; preds = %9
%20 = tail call i32 @fclose(i32* %5)
br label %21
21: ; preds = %19, %16, %7
%22 = phi i32 [ 0, %7 ], [ 0, %16 ], [ 1, %19 ]
ret i32 %22
}