#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_pgm(const char *filename, int width, int height, unsigned char *data) {
  int bytes2write = width * height;
  FILE *f = fopen(filename, "wb");
  if (!f)
    return 0;
  fprintf(f, "P5\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_pgm>:
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    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    118b <save_pgm+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    11e4 <save_pgm+0xa4>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe67(%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     11d4 <save_pgm+0x94>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e4 <save_pgm+0xa4>
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_pgm>:
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     11a5 <save_pgm+0x65>
mov    %rax,%rbx
mov    %r15d,%r12d
imul   %ebp,%r12d
lea    0xe91(%rip),%rsi
mov    %rax,%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    11a7 <save_pgm+0x67>
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"P5\0A%d %d\0A255\0A\00"
define i32 @save_pgm(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 = load i32, i32* %8
%14 = mul nsw i32 %12, %13
store i32 %14, i32* %10
%15 = load i8*, i8** %6
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %11
%17 = load i32*, i32** %11
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %4
store i32 0, i32* %5
br label %37
20: ; preds = %4
%21 = load i32*, i32** %11
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %23)
%25 = load i8*, i8** %9
%26 = load i32, i32* %10
%27 = load i32*, i32** %11
%28 = call i32 @fwrite(i8* %25, i32 1, i32 %26, i32* %27)
%29 = load i32, i32* %10
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %20
%32 = load i32*, i32** %11
%33 = call i32 @fclose(i32* %32)
store i32 0, i32* %5
br label %37
34: ; preds = %20
%35 = load i32*, i32** %11
%36 = call i32 @fclose(i32* %35)
store i32 1, i32* %5
br label %37
37: ; preds = %34, %31, %19
%38 = load i32, i32* %5
ret i32 %38
}

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