#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 */
struct stat {
  int st_size;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
void *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, void *, int);
void *read_file(const char *filename, ssize_t *_size) {
  int ret, fd;
  struct stat sb;
  ssize_t size;
  void *buffer = NULL;
  fd = open(filename, O_RDONLY);
  if (fd < 0)
    return NULL;
  if (fstat(fd, &sb) < 0)
    goto bail;
  size = sb.st_size;
  buffer = malloc(size);
  if (!buffer)
    goto bail;
  ret = read(fd, buffer, size);
  if (ret != size)
    goto bail;
  *_size = size;
bail:
  close(fd);
  return buffer;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1194 <read_file+0x44>
movq   $0x0,-0x8(%rbp)
jmp    120d <read_file+0xbd>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jge    11af <read_file+0x5f>
jmp    11fd <read_file+0xad>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d1 <read_file+0x81>
jmp    11fd <read_file+0xad>
mov    -0x20(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     11f4 <read_file+0xa4>
jmp    11fd <read_file+0xad>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11a4 <read_file+0x54>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
js     11a9 <read_file+0x59>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11a9 <read_file+0x59>
mov    %rax,%r15
mov    %ebx,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
cmp    %ebp,%eax
jne    11ac <read_file+0x5c>
mov    %ebp,(%r14)
jmp    11ac <read_file+0x5c>
xor    %r15d,%r15d
jmp    11b3 <read_file+0x63>
xor    %r15d,%r15d
mov    %ebx,%edi
call   1030 <close@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define noalias i8* @read_file(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %2
%9 = call i64 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = tail call i32 @read(i32 %6, i8* %14, i32 %13)
%18 = icmp eq i32 %17, %13
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 %13, i32* %1
br label %20
20: ; preds = %16, %11, %8, %19
%21 = phi i8* [ null, %8 ], [ %14, %16 ], [ %14, %19 ], [ null, %11 ]
%22 = tail call i32 @close(i32 %6)
br label %23
23: ; preds = %2, %20
%24 = phi i8* [ %21, %20 ], [ null, %2 ]
ret i8* %24
}