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


x86 assembly (clang, -O0):
<pgm_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea7(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     119d <pgm_save+0x6d>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
call   1040 <pgm_write@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    11a4 <pgm_save+0x74>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgm_save>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8,%r15
mov    %ecx,%r12d
mov    %edx,%r13d
mov    %rsi,%rbp
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1181 <pgm_save+0x51>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
mov    %r12d,%ecx
mov    %r15,%r8
mov    %r14d,%r9d
call   1040 <pgm_write@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1186 <pgm_save+0x56>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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