#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 {
  size_t st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
scalar_t__ malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, size_t);
char *read_file(const char *filename, size_t *length) {
  int fd;
  struct stat file_info;
  char *buffer;
  fd = open(filename, O_RDONLY);
  fstat(fd, &file_info);
  *length = file_info.st_size;
  if (!S_ISREG(file_info.st_mode)) {
    close(fd);
    return NULL;
  }
  buffer = (char *)malloc(*length);
  read(fd, buffer, *length);
  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)
mov    -0x10(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11c2 <read_file+0x62>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f5 <read_file+0x95>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
call   1040 <read@plt>
mov    -0x1c(%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   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
mov    (%rsp),%rax
mov    %rax,(%r14)
mov    0x8(%rsp),%edi
call   1050 <S_ISREG@plt>
test   %eax,%eax
je     11b3 <read_file+0x53>
mov    (%r14),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    (%r14),%rdx
mov    %ebp,%edi
mov    %rax,%rsi
call   1040 <read@plt>
jmp    11b5 <read_file+0x55>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
define i8* @read_file(i8* nocapture %0, i64* nocapture %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 = call i32 @fstat(i32 %6, %struct.stat* %3)
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i64, i64* %8
store i64 %9, i64* %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%11 = load i32, i32* %10
%12 = tail call i32 @S_ISREG(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = load i64, i64* %1
%16 = tail call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
%18 = load i64, i64* %1
%19 = tail call i32 @read(i32 %6, i8* %17, i64 %18)
br label %20
20: ; preds = %2, %14
%21 = phi i8* [ %17, %14 ], [ null, %2 ]
%22 = tail call i32 @close(i32 %6)
ret i8* %21
}