#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(void *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int gu_decompress_mem(void *, long, void **, long *);
void *malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int gu_decompress_file_to_mem(const char *filename, void **dst,
                              long *dst_size) {
  FILE *f = NULL;
  void *src = NULL;
  long src_size = 0;
  if (NULL == (f = fopen(filename, "rb"))) {
    return 1;
  }
  fseek(f, 0, SEEK_END);
  src_size = ftell(f);
  rewind(f);
  src = malloc(src_size);
  fread(src, 1, src_size, f);
  fclose(f);
  return gu_decompress_mem(src, src_size, dst, dst_size);
}


x86 assembly (clang, -O0):
<gu_decompress_file_to_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11da <gu_decompress_file_to_mem+0x5a>
movl   $0x1,-0x4(%rbp)
jmp    1248 <gu_decompress_file_to_mem+0xc8>
mov    -0x28(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x38(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <gu_decompress_mem@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gu_decompress_file_to_mem>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe6a(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1209 <gu_decompress_file_to_mem+0x89>
mov    %rax,%rbx
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r12,%rdi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1070 <gu_decompress_mem@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @gu_decompress_file_to_mem(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
store i32* null, i32** %8
store i8* null, i8** %9
store i64 0, i64* %10
%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** %8
%13 = icmp eq i32* null, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 1, i32* %4
br label %36
15: ; preds = %3
%16 = load i32*, i32** %8
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %8
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %10
%21 = load i32*, i32** %8
%22 = call i32 @rewind(i32* %21)
%23 = load i64, i64* %10
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = load i64, i64* %10
%27 = load i32*, i32** %8
%28 = call i32 @fread(i8* %25, i32 1, i64 %26, i32* %27)
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
%31 = load i8*, i8** %9
%32 = load i64, i64* %10
%33 = load i8**, i8*** %6
%34 = load i64*, i64** %7
%35 = call i32 @gu_decompress_mem(i8* %31, i64 %32, i8** %33, i64* %34)
store i32 %35, i32* %4
br label %36
36: ; preds = %15, %14
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @gu_decompress_file_to_mem(i8* nocapture %0, i8** %1, i64* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %15, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i32 0, i32 %7)
%9 = tail call i64 @ftell(i32* %4)
%10 = tail call i32 @rewind(i32* %4)
%11 = tail call i8* @malloc(i64 %9)
%12 = tail call i32 @fread(i8* %11, i32 1, i64 %9, i32* %4)
%13 = tail call i32 @fclose(i32* %4)
%14 = tail call i32 @gu_decompress_mem(i8* %11, i64 %9, i8** %1, i64* %2)
br label %15
15: ; preds = %3, %6
%16 = phi i32 [ %14, %6 ], [ 1, %3 ]
ret i32 %16
}