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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fstat(int, struct stat *);
scalar_t__ malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *file_read(const char *filename) {
  int fd;
  char *buffer;
  struct stat file_info;
  size_t length;
  fd = open(filename, O_RDONLY);
  fstat(fd, &file_info);
  length = file_info.st_size + 1;
  if (!S_ISREG(file_info.st_mode)) {
    close(fd);
    fprintf(stderr, "%s: File not found.\n", filename);
    exit(1);
  }
  buffer = (char *)malloc(length);
  if (buffer == NULL) {
    fprintf(stderr, "Not enough memory\n");
    exit(1);
  }
  read(fd, buffer, length);
  close(fd);
  buffer[length] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10a0 <fstat@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11f7 <file_read+0x77>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1230 <file_read+0xb0>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
movslq (%rsp),%rbx
mov    0x4(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     11f3 <file_read+0x73>
add    $0x1,%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     121f <file_read+0x9f>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
mov    %r15d,%edi
call   1030 <close@plt>
movb   $0x0,0x0(%rbp,%rbx,1)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
call   1030 <close@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: File not found.\0A\00"
@.str.1 = [19 x i8] c"Not enough memory\0A\00"
define i8* @file_read(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = call i32 @fstat(i32 %10, %struct.stat* %5)
%12 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%13 = load i32, i32* %12
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
store i64 %15, i64* %6
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 @S_ISREG(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %1
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %2
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %1
%28 = load i64, i64* %6
%29 = call i64 @malloc(i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27
%38 = load i32, i32* %3
%39 = load i8*, i8** %4
%40 = load i64, i64* %6
%41 = call i32 @read(i32 %38, i8* %39, i64 %40)
%42 = load i32, i32* %3
%43 = call i32 @close(i32 %42)
%44 = load i8*, i8** %4
%45 = load i64, i64* %6
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %4
ret i8* %47
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: File not found.\0A\00"
@.str.1 = [19 x i8] c"Not enough memory\0A\00"
define i8* @file_read(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = call i32 @fstat(i32 %5, %struct.stat* %2)
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%12 = load i32, i32* %11
%13 = tail call i32 @S_ISREG(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = tail call i32 @close(i32 %5)
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = tail call i64 @malloc(i64 %10)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = tail call i32 @read(i32 %5, i8* %22, i64 %10)
%30 = tail call i32 @close(i32 %5)
%31 = getelementptr inbounds i8, i8* %22, i64 %10
store i8 0, i8* %31
ret i8* %22
}