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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int get_file_size(int);
char *malloc(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
char *readFile(char *filename) {
  int file, length;
  char *ptr;
  if ((file = open(filename, O_RDONLY, 0)) == -1) {
    return NULL;
  }
  length = get_file_size(file);
  if ((ptr = malloc(length * sizeof(char))) == NULL) {
    return NULL;
  }
  if (read(file, ptr, length) == -1) {
    return NULL;
  }
  close(file);
  return ptr;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1189 <readFile+0x39>
movq   $0x0,-0x8(%rbp)
jmp    11f3 <readFile+0xa3>
mov    -0x14(%rbp),%edi
call   1060 <get_file_size@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11be <readFile+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    11f3 <readFile+0xa3>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11e3 <readFile+0x93>
movq   $0x0,-0x8(%rbp)
jmp    11f3 <readFile+0xa3>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11a3 <readFile+0x53>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <get_file_size@plt>
mov    %eax,%r14d
mov    %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11a1 <readFile+0x51>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11a1 <readFile+0x51>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11a3 <readFile+0x53>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i8* @readFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* null, i8** %2
br label %33
12: ; preds = %1
%13 = load i32, i32* %4
%14 = call i32 @get_file_size(i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = mul i64 %16, 1
%18 = trunc i64 %17 to i32
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %6
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %12
store i8* null, i8** %2
br label %33
22: ; preds = %12
%23 = load i32, i32* %4
%24 = load i8*, i8** %6
%25 = load i32, i32* %5
%26 = call i32 @read(i32 %23, i8* %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %22
store i8* null, i8** %2
br label %33
29: ; preds = %22
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
%32 = load i8*, i8** %6
store i8* %32, i8** %2
br label %33
33: ; preds = %29, %28, %21, %11
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define noalias i8* @readFile(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %14, label %5
5: ; preds = %1
%6 = tail call i32 @get_file_size(i32 %3)
%7 = tail call i8* @malloc(i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = tail call i32 @read(i32 %3, i8* %7, i32 %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @close(i32 %3)
br label %14
14: ; preds = %9, %5, %1, %12
%15 = phi i8* [ %7, %12 ], [ null, %1 ], [ null, %5 ], [ null, %9 ]
ret i8* %15
}