#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*/ pnm_write(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                             int, int, char const *);
int pnm_save(const char *name, const uint8_t *buf, int w, int h,
             const char *comment) {
  FILE *fp = fopen(name, "w");
  if (fp) {
    pnm_write(fp, buf, w, h, comment);
    fclose(fp);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<pnm_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeab(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1195 <pnm_save+0x65>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%r8
call   1030 <pnm_write@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    119c <pnm_save+0x6c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pnm_save>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r14
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %rsi,%rbp
lea    0xeb5(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1178 <pnm_save+0x48>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
mov    %r15d,%ecx
mov    %r14,%r8
call   1030 <pnm_write@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    117d <pnm_save+0x4d>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
define i32 @pnm_save(i8* %0, i32* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32*
store i8* %0, i8** %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%13 = load i8*, i8** %7
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %12
%15 = load i32*, i32** %12
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %26
17: ; preds = %5
%18 = load i32*, i32** %12
%19 = load i32*, i32** %8
%20 = load i32, i32* %9
%21 = load i32, i32* %10
%22 = load i8*, i8** %11
%23 = call i32 @pnm_write(i32* %18, i32* %19, i32 %20, i32 %21, i8* %22)
%24 = load i32*, i32** %12
%25 = call i32 @fclose(i32* %24)
store i32 0, i32* %6
br label %27
26: ; preds = %5
store i32 -1, i32* %6
br label %27
27: ; preds = %26, %17
%28 = load i32, i32* %6
ret i32 %28
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
define i32 @pnm_save(i8* nocapture %0, i32* %1, i32 %2, i32 %3, i8* %4) {
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i32 @pnm_write(i32* %6, i32* %1, i32 %2, i32 %3, i8* %4)
%10 = tail call i32 @fclose(i32* %6)
br label %11
11: ; preds = %5, %8
%12 = phi i32 [ 0, %8 ], [ -1, %5 ]
ret i32 %12
}