#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 off_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
scalar_t__ malloc(int);
int /*<<< orphan*/ report_errno(char const *);
char *read_file(const char *file_name) {
  FILE *f;
  char *contents = NULL;
  struct stat f_info;
  off_t f_len;
  if ((f = fopen(file_name, "r")) != NULL) {
    if (fstat(fileno(f), &f_info) == 0) {
      f_len = f_info.st_size;
      contents = (char *)malloc((f_len + 1) * sizeof(char));
      if (fread(contents, f_len, 1, f) == 1) {
        contents[f_len] = '\0';
      } else if (ferror(f)) {
        report_errno(file_name);
        free(contents);
        contents = NULL;
      }
    } else {
      report_errno(file_name);
    }
    fclose(f);
  }
  return contents;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1275 <read_file+0xe5>
mov    -0x10(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jne    1263 <read_file+0xd3>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    122c <read_file+0x9c>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    125e <read_file+0xce>
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1259 <read_file+0xc9>
mov    -0x8(%rbp),%rdi
call   1090 <report_errno@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    125e <read_file+0xce>
jmp    126c <read_file+0xdc>
mov    -0x8(%rbp),%rdi
call   1090 <report_errno@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe5e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11db <read_file+0x4b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
je     11ed <read_file+0x5d>
mov    %r14,%rdi
call   1090 <report_errno@plt>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    11de <read_file+0x4e>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%rsp),%r12d
lea    0x1(%r12),%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    1220 <read_file+0x90>
movslq %r12d,%rax
movb   $0x0,(%r15,%rax,1)
jmp    11d1 <read_file+0x41>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11d1 <read_file+0x41>
mov    %r14,%rdi
call   1090 <report_errno@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11ce <read_file+0x3e>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %4
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %52
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = call i32 @fileno(i32* %11)
%13 = call i64 @fstat(i32 %12, %struct.stat* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %46
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%17 = load i32, i32* %16
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 1
%22 = trunc i64 %21 to i32
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = load i32, i32* %6
%27 = load i32*, i32** %3
%28 = call i32 @fread(i8* %25, i32 %26, i32 1, i32* %27)
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %35
30: ; preds = %15
%31 = load i8*, i8** %4
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
br label %45
35: ; preds = %15
%36 = load i32*, i32** %3
%37 = call i64 @ferror(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = load i8*, i8** %2
%41 = call i32 @report_errno(i8* %40)
%42 = load i8*, i8** %4
%43 = call i32 @free(i8* %42)
store i8* null, i8** %4
br label %44
44: ; preds = %39, %35
br label %45
45: ; preds = %44, %30
br label %49
46: ; preds = %10
%47 = load i8*, i8** %2
%48 = call i32 @report_errno(i8* %47)
br label %49
49: ; preds = %46, %45
%50 = load i32*, i32** %3
%51 = call i32 @fclose(i32* %50)
br label %52
52: ; preds = %49, %1
%53 = load i8*, i8** %4
ret i8* %53
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
define i8* @read_file(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %32, label %6
6: ; preds = %1
%7 = tail call i32 @fileno(i32* %4)
%8 = call i64 @fstat(i32 %7, %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %27
10: ; preds = %6
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @fread(i8* %15, i32 %12, i32 1, i32* %4)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = sext i32 %12 to i64
%20 = getelementptr inbounds i8, i8* %15, i64 %19
store i8 0, i8* %20
br label %29
21: ; preds = %10
%22 = tail call i64 @ferror(i32* %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = tail call i32 @report_errno(i8* %0)
%26 = tail call i32 @free(i8* %15)
br label %29
27: ; preds = %6
%28 = tail call i32 @report_errno(i8* %0)
br label %29
29: ; preds = %18, %24, %21, %27
%30 = phi i8* [ %15, %18 ], [ null, %24 ], [ %15, %21 ], [ null, %27 ]
%31 = tail call i32 @fclose(i32* %4)
br label %32
32: ; preds = %29, %1
%33 = phi i8* [ %30, %29 ], [ null, %1 ]
ret i8* %33
}