#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(unsigned int);
int /*<<< orphan*/ printf(char *, char *);
char *open_file(char *name, unsigned *psize) {
  unsigned size = 0;
  char *buffer = NULL;
  FILE *file = fopen(name, "rb");
  if (file) {
    fseek(file, 0, SEEK_END);
    size = ftell(file);
    if (size && (buffer = (char *)malloc(size))) {
      fseek(file, 0, SEEK_SET);
      fread(buffer, 1, size, file);
    }
    fclose(file);
  } else {
    printf("failed to open input file '%s'\n", name);
  }
  *psize = size;
  return buffer;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1225 <open_file+0xb5>
mov    -0x28(%rbp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1217 <open_file+0xa7>
mov    -0x14(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1217 <open_file+0xa7>
mov    -0x28(%rbp),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1235 <open_file+0xc5>
mov    -0x8(%rbp),%rsi
lea    0xdd3(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe7c(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11eb <open_file+0x7b>
mov    %rax,%rbx
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1204 <open_file+0x94>
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1201 <open_file+0x91>
mov    %rax,%r15
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
jmp    1204 <open_file+0x94>
lea    0xe11(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
xor    %r15d,%r15d
xor    %ebp,%ebp
jmp    120c <open_file+0x9c>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,(%r14)
mov    %r15,%rax
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
@.str.1 = [32 x i8] c"failed to open input file '%s'\0A\00"
define i8* @open_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %5
store i8* null, i8** %6
%8 = load i8*, i8** %3
%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 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %36
12: ; preds = %2
%13 = load i32*, i32** %7
%14 = load i32, i32* @SEEK_END
%15 = call i32 @fseek(i32* %13, i32 0, i32 %14)
%16 = load i32*, i32** %7
%17 = call i32 @ftell(i32* %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %12
%21 = load i32, i32* %5
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i32*, i32** %7
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @fseek(i32* %26, i32 0, i32 %27)
%29 = load i8*, i8** %6
%30 = load i32, i32* %5
%31 = load i32*, i32** %7
%32 = call i32 @fread(i8* %29, i32 1, i32 %30, i32* %31)
br label %33
33: ; preds = %25, %20, %12
%34 = load i32*, i32** %7
%35 = call i32 @fclose(i32* %34)
br label %39
36: ; preds = %2
%37 = load i8*, i8** %3
%38 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %36, %33
%40 = load i32, i32* %5
%41 = load i32*, i32** %4
store i32 %40, i32* %41
%42 = load i8*, i8** %6
ret i8* %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [32 x i8] c"failed to open input file '%s'\0A\00"
define i8* @open_file(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %21, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i32 @ftell(i32* %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %5
%11 = tail call i64 @malloc(i32 %8)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %3, i32 0, i32 %15)
%17 = tail call i32 @fread(i8* %12, i32 1, i32 %8, i32* %3)
br label %18
18: ; preds = %14, %10, %5
%19 = phi i8* [ %12, %14 ], [ null, %10 ], [ null, %5 ]
%20 = tail call i32 @fclose(i32* %3)
br label %23
21: ; preds = %2
%22 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %21, %18
%24 = phi i8* [ %19, %18 ], [ null, %21 ]
%25 = phi i32 [ %8, %18 ], [ 0, %21 ]
store i32 %25, i32* %1
ret i8* %24
}