#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 */
struct img_pixmap {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ img_set_name(struct img_pixmap *, char const *);
int img_write_file(struct img_pixmap *, int /*<<< orphan*/ *);
int img_save(struct img_pixmap *img, const char *fname) {
  int res;
  FILE *fp;
  img_set_name(img, fname);
  if (!(fp = fopen(fname, "wb"))) {
    return -1;
  }
  res = img_write_file(img, fp);
  fclose(fp);
  return res;
}


x86 assembly (clang, -O0):
<img_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <img_set_name@plt>
mov    -0x18(%rbp),%rdi
lea    0xe98(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1187 <img_save+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <img_save+0x66>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <img_write_file@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<img_save>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <img_set_name@plt>
lea    0xeaa(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     117f <img_save+0x3f>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <img_write_file@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
jmp    1184 <img_save+0x44>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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