#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, int);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int save_ppm(const char *filename, int width, int height, unsigned char *data) {
  int bytes2write = 3 * width * height;
  FILE *f = fopen(filename, "wb");
  if (!f)
    return 0;
  fprintf(f, "P6\n%d %d\n255\n", width, height);
  if (fwrite(data, 1, bytes2write, f) != bytes2write) {
    fclose(f);
    return 0;
  }
  fclose(f);
  return 1;
}


x86 assembly (clang, -O0):
<save_ppm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
imul   $0x3,-0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    118c <save_ppm+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <save_ppm+0xa5>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
cmp    -0x24(%rbp),%eax
je     11d5 <save_ppm+0x95>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <save_ppm+0xa5>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_ppm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebp
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a8 <save_ppm+0x68>
mov    %rax,%rbx
mov    %ebp,%eax
imul   %r15d,%eax
lea    (%rax,%rax,2),%r12d
lea    0xe8e(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
mov    %r15d,%ecx
call   1040 <fprintf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
xor    %ebp,%ebp
cmp    %r12d,%eax
sete   %bpl
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11aa <save_ppm+0x6a>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
define i32 @save_ppm(i8* nocapture %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = mul nsw i32 %1, 3
%6 = mul nsw i32 %5, %2
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %15, label %9
9: ; preds = %4
%10 = tail call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %2)
%11 = tail call i32 @fwrite(i8* %3, i32 1, i32 %6, i32* %7)
%12 = icmp eq i32 %11, %6
%13 = tail call i32 @fclose(i32* %7)
%14 = zext i1 %12 to i32
br label %15
15: ; preds = %9, %4
%16 = phi i32 [ 0, %4 ], [ %14, %9 ]
ret i32 %16
}