#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 img_read_file(struct img_pixmap *, int /*<<< orphan*/ *);
int img_load(struct img_pixmap *img, const char *fname) {
  int res;
  FILE *fp;
  if (!(fp = fopen(fname, "rb"))) {
    return -1;
  }
  res = img_read_file(img, fp);
  fclose(fp);
  return res;
}


x86 assembly (clang, -O0):
<img_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    116a <img_load+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1189 <img_load+0x59>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <img_read_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_load>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xebf(%rip),%rsi
mov    %rax,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
je     116a <img_load+0x3a>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <img_read_file@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
jmp    116f <img_load+0x3f>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.img_pixmap = type { i32 }
@.str = [3 x i8] c"rb\00"
define i32 @img_load(%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 i8*, i8** %5
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 -1, i32* %3
br label %19
12: ; preds = %2
%13 = load %struct.img_pixmap*, %struct.img_pixmap** %4
%14 = load i32*, i32** %7
%15 = call i32 @img_read_file(%struct.img_pixmap* %13, i32* %14)
store i32 %15, i32* %6
%16 = load i32*, i32** %7
%17 = call i32 @fclose(i32* %16)
%18 = load i32, i32* %6
store i32 %18, i32* %3
br label %19
19: ; preds = %12, %11
%20 = load i32, i32* %3
ret i32 %20
}

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