#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 image {
  int dummy;
};

/* Variables and functions */
long filesize(char const *);
int /*<<< orphan*/ free(unsigned char *);
char *get_filename_ext(char const *);
struct image *image_from_mem_buf(unsigned char *, long, char const *);
unsigned char *malloc(long);
int /*<<< orphan*/ read_file_to_mem(char const *, unsigned char *, long);
struct image *image_from_file(const char *fpath) {
  long filesz = filesize(fpath);
  if (filesz == -1)
    return 0;
  unsigned char *data_buf = malloc(filesz);
  read_file_to_mem(fpath, data_buf, filesz);
  const char *ext = get_filename_ext(fpath);
  struct image *im = image_from_mem_buf(data_buf, filesz, ext);
  free(data_buf);
  return im;
}


x86 assembly (clang, -O0):
<image_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <filesize@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    1191 <image_from_file+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <image_from_file+0x82>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read_file_to_mem@plt>
mov    -0x10(%rbp),%rdi
call   1080 <get_filename_ext@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <image_from_mem_buf@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<image_from_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1070 <filesize@plt>
cmp    $0xffffffffffffffff,%rax
je     11b5 <image_from_file+0x55>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1030 <read_file_to_mem@plt>
mov    %r14,%rdi
call   1080 <get_filename_ext@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <image_from_mem_buf@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
jmp    11b7 <image_from_file+0x57>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.image = type { i32 }
define %struct.image* @image_from_file(i8* %0) {
%2 = alloca %struct.image*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.image*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @filesize(i8* %8)
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store %struct.image* null, %struct.image** %2
br label %29
13: ; preds = %1
%14 = load i64, i64* %4
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %5
%16 = load i8*, i8** %3
%17 = load i8*, i8** %5
%18 = load i64, i64* %4
%19 = call i32 @read_file_to_mem(i8* %16, i8* %17, i64 %18)
%20 = load i8*, i8** %3
%21 = call i8* @get_filename_ext(i8* %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %5
%23 = load i64, i64* %4
%24 = load i8*, i8** %6
%25 = call %struct.image* @image_from_mem_buf(i8* %22, i64 %23, i8* %24)
store %struct.image* %25, %struct.image** %7
%26 = load i8*, i8** %5
%27 = call i32 @free(i8* %26)
%28 = load %struct.image*, %struct.image** %7
store %struct.image* %28, %struct.image** %2
br label %29
29: ; preds = %13, %12
%30 = load %struct.image*, %struct.image** %2
ret %struct.image* %30
}

LLVM IR (-O2):
%struct.image = type { i32 }
define %struct.image* @image_from_file(i8* %0) {
%2 = tail call i64 @filesize(i8* %0)
%3 = icmp eq i64 %2, -1
br i1 %3, label %10, label %4
4: ; preds = %1
%5 = tail call i8* @malloc(i64 %2)
%6 = tail call i32 @read_file_to_mem(i8* %0, i8* %5, i64 %2)
%7 = tail call i8* @get_filename_ext(i8* %0)
%8 = tail call %struct.image* @image_from_mem_buf(i8* %5, i64 %2, i8* %7)
%9 = tail call i32 @free(i8* %5)
br label %10
10: ; preds = %1, %4
%11 = phi %struct.image* [ %8, %4 ], [ null, %1 ]
ret %struct.image* %11
}