#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*/ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
unsigned char *calloc(int /*<<< orphan*/, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int /*<<< orphan*/,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
uint32_t open_file(char *fname, uint32_t *inlen, unsigned char **instring) {
  FILE *fp;
  uint32_t len = 0;
  unsigned char *buf;
  fp = fopen(fname, "rb");
  fseek(fp, 0L, SEEK_END);
  len = ftell(fp);
  fseek(fp, 0L, SEEK_SET);
  buf = calloc(len, sizeof(unsigned char));
  *instring = buf;
  *inlen = fread(*instring, 1, len, fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xe8c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,%ebp
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @open_file(i8* %0, i32* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8** %2, i8*** %6
store i32 0, i32* %8
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %12, i64 0, i32 %13)
%15 = load i32*, i32** %7
%16 = call i32 @ftell(i32* %15)
store i32 %16, i32* %8
%17 = load i32*, i32** %7
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %17, i64 0, i32 %18)
%20 = load i32, i32* %8
%21 = call i8* @calloc(i32 %20, i32 1)
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = load i8**, i8*** %6
store i8* %22, i8** %23
%24 = load i8**, i8*** %6
%25 = load i8*, i8** %24
%26 = load i32, i32* %8
%27 = load i32*, i32** %7
%28 = call i32 @fread(i8* %25, i32 1, i32 %26, i32* %27)
%29 = load i32*, i32** %5
store i32 %28, i32* %29
%30 = load i32*, i32** %7
%31 = call i32 @fclose(i32* %30)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @open_file(i8* nocapture %0, i32* nocapture writeonly %1, i8** nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %4, i64 0, i32 %5)
%7 = tail call i32 @ftell(i32* %4)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %4, i64 0, i32 %8)
%10 = tail call i8* @calloc(i32 %7, i32 1)
store i8* %10, i8** %2
%11 = tail call i32 @fread(i8* %10, i32 1, i32 %7, i32* %4)
store i32 %11, i32* %1
%12 = tail call i32 @fclose(i32* %4)
ret i32 0
}