#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 scalar_t__ ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ file_size(char const *);
char *malloc(scalar_t__);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, scalar_t__);
char *file_read(const char *filename) {
  off_t len = file_size(filename);
  if (len < 0)
    return NULL;
  char *buf = malloc(len + 1);
  if (!buf)
    return NULL;
  int fd = open(filename, O_RDONLY);
  if (fd < 0)
    return NULL;
  ssize_t size = read(fd, buf, len);
  close(fd);
  if (size != len)
    return NULL;
  return buf;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <file_size@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1181 <file_read+0x31>
movq   $0x0,-0x8(%rbp)
jmp    1215 <file_read+0xc5>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11aa <file_read+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    1215 <file_read+0xc5>
mov    -0x10(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d6 <file_read+0x86>
movq   $0x0,-0x8(%rbp)
jmp    1215 <file_read+0xc5>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     120d <file_read+0xbd>
movq   $0x0,-0x8(%rbp)
jmp    1215 <file_read+0xc5>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <file_size@plt>
test   %rax,%rax
js     11b2 <file_read+0x62>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b2 <file_read+0x62>
mov    %rax,%r14
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11b2 <file_read+0x62>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <read@plt>
mov    %rax,%r12
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
cmp    %r15,%r12
cmove  %r14,%rax
jmp    11b4 <file_read+0x64>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
define noalias i8* @file_read(i8* %0) {
%2 = tail call i64 @file_size(i8* %0)
%3 = icmp slt i64 %2, 0
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = add nuw nsw i64 %2, 1
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %17, label %8
8: ; preds = %4
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %8
%13 = tail call i64 @read(i32 %10, i8* %6, i64 %2)
%14 = tail call i32 @close(i32 %10)
%15 = icmp eq i64 %13, %2
%16 = select i1 %15, i8* %6, i8* null
br label %17
17: ; preds = %4, %8, %12, %1
%18 = phi i8* [ null, %1 ], [ null, %4 ], [ %16, %12 ], [ null, %8 ]
ret i8* %18
}