#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*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *file_open(const char *filename) {
  char *buffer = NULL;
  int string_size, read_size;
  FILE *handler = fopen(filename, "r");
  if (handler) {
    fseek(handler, 0, SEEK_END);
    string_size = ftell(handler);
    rewind(handler);
    buffer = (char *)malloc(sizeof(char) * (string_size + 1));
    read_size = fread(buffer, sizeof(char), string_size, handler);
    buffer[string_size + 1] = '\0';
    if (string_size != read_size) {
      free(buffer);
      buffer = NULL;
    }
  }
  return buffer;
}


x86 assembly (clang, -O0):
<file_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     122e <file_open+0xbe>
mov    -0x20(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1229 <file_open+0xb9>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    122e <file_open+0xbe>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_open>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11e3 <file_open+0x73>
mov    %rax,%r14
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
call   1060 <ftell@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <rewind@plt>
movslq %ebx,%r15
lea    0x1(%r15),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
movb   $0x0,0x1(%rbx,%r15,1)
cmp    %eax,%r15d
je     11e5 <file_open+0x75>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define i8* @file_open(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %2
store i8* null, i8** %3
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %42
11: ; preds = %1
%12 = load i32*, i32** %6
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %12, i32 0, i32 %13)
%15 = load i32*, i32** %6
%16 = call i32 @ftell(i32* %15)
store i32 %16, i32* %4
%17 = load i32*, i32** %6
%18 = call i32 @rewind(i32* %17)
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %3
%26 = load i8*, i8** %3
%27 = load i32, i32* %4
%28 = load i32*, i32** %6
%29 = call i32 @fread(i8* %26, i32 1, i32 %27, i32* %28)
store i32 %29, i32* %5
%30 = load i8*, i8** %3
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %4
%36 = load i32, i32* %5
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %11
%39 = load i8*, i8** %3
%40 = call i32 @free(i8* %39)
store i8* null, i8** %3
br label %41
41: ; preds = %38, %11
br label %42
42: ; preds = %41, %1
%43 = load i8*, i8** %3
ret i8* %43
}

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