#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 const *, char *);
size_t fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int read_content_from_file(const char *filename, char **buffer, size_t *len) {
  if (filename == NULL) {
    return -1;
  }
  FILE *file = fopen(filename, "r");
  if (!file) {
    return -1;
  }
  fseek(file, 0, SEEK_END);
  size_t size = ftell(file);
  if (len) {
    *len = size;
  }
  *buffer = malloc(size + 1);
  fseek(file, 0, SEEK_SET);
  size_t ret = fread(*buffer, size, 1, file);
  if (ret != 1) {
    fclose(file);
    free(*buffer);
    return -1;
  }
  *(*buffer + size) = '\0';
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<read_content_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119b <read_content_from_file+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <read_content_from_file+0x120>
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <read_content_from_file+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <read_content_from_file+0x120>
mov    -0x28(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11fd <read_content_from_file+0x8d>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x1,-0x38(%rbp)
je     1271 <read_content_from_file+0x101>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <read_content_from_file+0x120>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_content_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     122b <read_content_from_file+0xbb>
mov    %rdx,%rbx
mov    %rsi,%r15
lea    0xe6c(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     122b <read_content_from_file+0xbb>
mov    %rax,%rbp
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbp,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
test   %rbx,%rbx
je     11cb <read_content_from_file+0x5b>
mov    %r12,(%rbx)
mov    %r12,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r15)
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    (%r15),%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
cmp    $0x1,%rax
jne    121b <read_content_from_file+0xab>
mov    (%r15),%rax
movb   $0x0,(%rax,%r12,1)
mov    %rbp,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
jmp    122b <read_content_from_file+0xbb>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_content_from_file(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %59
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %14
store i32 -1, i32* %4
br label %59
20: ; preds = %14
%21 = load i32*, i32** %8
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %8
%25 = call i64 @ftell(i32* %24)
store i64 %25, i64* %9
%26 = load i64*, i64** %7
%27 = icmp ne i64* %26, null
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = load i64, i64* %9
%30 = load i64*, i64** %7
store i64 %29, i64* %30
br label %31
31: ; preds = %28, %20
%32 = load i64, i64* %9
%33 = add i64 %32, 1
%34 = call i8* @malloc(i64 %33)
%35 = load i8**, i8*** %6
store i8* %34, i8** %35
%36 = load i32*, i32** %8
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @fseek(i32* %36, i32 0, i32 %37)
%39 = load i8**, i8*** %6
%40 = load i8*, i8** %39
%41 = load i64, i64* %9
%42 = load i32*, i32** %8
%43 = call i64 @fread(i8* %40, i64 %41, i32 1, i32* %42)
store i64 %43, i64* %10
%44 = load i64, i64* %10
%45 = icmp ne i64 %44, 1
br i1 %45, label %46, label %52
46: ; preds = %31
%47 = load i32*, i32** %8
%48 = call i32 @fclose(i32* %47)
%49 = load i8**, i8*** %6
%50 = load i8*, i8** %49
%51 = call i32 @free(i8* %50)
store i32 -1, i32* %4
br label %59
52: ; preds = %31
%53 = load i8**, i8*** %6
%54 = load i8*, i8** %53
%55 = load i64, i64* %9
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 0, i8* %56
%57 = load i32*, i32** %8
%58 = call i32 @fclose(i32* %57)
store i32 0, i32* %4
br label %59
59: ; preds = %52, %46, %19, %13
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_content_from_file(i8* %0, i8** nocapture %1, i64* writeonly %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %30, label %5
5: ; preds = %3
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %30, label %8
8: ; preds = %5
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %6, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %6)
%12 = icmp eq i64* %2, null
br i1 %12, label %14, label %13
13: ; preds = %8
store i64 %11, i64* %2
br label %14
14: ; preds = %13, %8
%15 = add i64 %11, 1
%16 = tail call i8* @malloc(i64 %15)
store i8* %16, i8** %1
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %6, i32 0, i32 %17)
%19 = load i8*, i8** %1
%20 = tail call i64 @fread(i8* %19, i64 %11, i32 1, i32* %6)
%21 = icmp eq i64 %20, 1
br i1 %21, label %26, label %22
22: ; preds = %14
%23 = tail call i32 @fclose(i32* %6)
%24 = load i8*, i8** %1
%25 = tail call i32 @free(i8* %24)
br label %30
26: ; preds = %14
%27 = load i8*, i8** %1
%28 = getelementptr inbounds i8, i8* %27, i64 %11
store i8 0, i8* %28
%29 = tail call i32 @fclose(i32* %6)
br label %30
30: ; preds = %5, %26, %22, %3
%31 = phi i32 [ -1, %3 ], [ -1, %5 ], [ -1, %22 ], [ 0, %26 ]
ret i32 %31
}