#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(long);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, long);
char *get_file_contents(const char *filename) {
  long size;
  int fd = -1;
  char *contents = NULL;
  if (filename == NULL)
    return NULL;
  if ((fd = open(filename, O_RDONLY)) == -1)
    return NULL;
  if ((size = lseek(fd, 0, SEEK_END) + 1) == -1)
    return NULL;
  lseek(fd, 0, SEEK_SET);
  if ((contents = malloc(size * sizeof(char))) == NULL)
    return NULL;
  if (read(fd, contents, size - 1) == -1)
    return NULL;
  close(fd);
  return contents;
}


x86 assembly (clang, -O0):
<get_file_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1183 <get_file_contents+0x33>
movq   $0x0,-0x8(%rbp)
jmp    1256 <get_file_contents+0x106>
mov    -0x10(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11ae <get_file_contents+0x5e>
movq   $0x0,-0x8(%rbp)
jmp    1256 <get_file_contents+0x106>
mov    -0x1c(%rbp),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    11e1 <get_file_contents+0x91>
movq   $0x0,-0x8(%rbp)
jmp    1256 <get_file_contents+0x106>
mov    -0x1c(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x18(%rbp),%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    121c <get_file_contents+0xcc>
movq   $0x0,-0x8(%rbp)
jmp    1256 <get_file_contents+0x106>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    1246 <get_file_contents+0xf6>
movq   $0x0,-0x8(%rbp)
jmp    1256 <get_file_contents+0x106>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_contents>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     11ce <get_file_contents+0x7e>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11ce <get_file_contents+0x7e>
mov    %eax,%ebp
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
add    $0x1,%eax
cmp    $0xffffffff,%eax
je     11d0 <get_file_contents+0x80>
movslq %eax,%r14
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %r14,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ce <get_file_contents+0x7e>
mov    %rax,%rbx
add    $0xffffffffffffffff,%r14
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11ce <get_file_contents+0x7e>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11d0 <get_file_contents+0x80>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @get_file_contents(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 -1, i32* %5
store i8* null, i8** %6
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %45
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %10
store i8* null, i8** %2
br label %45
16: ; preds = %10
%17 = load i32, i32* %5
%18 = load i32, i32* @SEEK_END
%19 = call i32 @lseek(i32 %17, i32 0, i32 %18)
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
store i64 %21, i64* %4
%22 = icmp eq i64 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %16
store i8* null, i8** %2
br label %45
24: ; preds = %16
%25 = load i32, i32* %5
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %25, i32 0, i32 %26)
%28 = load i64, i64* %4
%29 = mul i64 %28, 1
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %6
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %24
store i8* null, i8** %2
br label %45
33: ; preds = %24
%34 = load i32, i32* %5
%35 = load i8*, i8** %6
%36 = load i64, i64* %4
%37 = sub nsw i64 %36, 1
%38 = call i32 @read(i32 %34, i8* %35, i64 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %33
store i8* null, i8** %2
br label %45
41: ; preds = %33
%42 = load i32, i32* %5
%43 = call i32 @close(i32 %42)
%44 = load i8*, i8** %6
store i8* %44, i8** %2
br label %45
45: ; preds = %41, %40, %32, %23, %15, %9
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @get_file_contents(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %24, label %3
3: ; preds = %1
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %24, label %7
7: ; preds = %3
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @lseek(i32 %5, i32 0, i32 %8)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = icmp eq i32 %10, -1
br i1 %12, label %24, label %13
13: ; preds = %7
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %5, i32 0, i32 %14)
%16 = tail call i8* @malloc(i64 %11)
%17 = icmp eq i8* %16, null
br i1 %17, label %24, label %18
18: ; preds = %13
%19 = add nsw i64 %11, -1
%20 = tail call i32 @read(i32 %5, i8* %16, i64 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 @close(i32 %5)
br label %24
24: ; preds = %18, %13, %7, %3, %1, %22
%25 = phi i8* [ %16, %22 ], [ null, %1 ], [ null, %3 ], [ null, %7 ], [ null, %13 ], [ null, %18 ]
ret i8* %25
}