#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 int /*<<< orphan*/ time_t;
struct stat {
  int st_size;
  int /*<<< orphan*/ st_mtime;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *get_contents(char *path, time_t *last_modified) {
  FILE *f = fopen(path, "r");
  if (f == NULL) {
    goto err;
  }
  struct stat f_stat;
  if (fstat(fileno(f), &f_stat) < 0) {
    goto err;
  }
  if (last_modified != NULL) {
    *last_modified = f_stat.st_mtime;
  }
  char *buf = malloc(f_stat.st_size + 1);
  memset(buf, 0, f_stat.st_size);
  size_t n = fread(buf, f_stat.st_size, 1, f);
  if (n != 1) {
    free(buf);
    goto err;
  }
  buf[f_stat.st_size] = '\0';
  if (ferror(f)) {
    free(buf);
    goto err;
  }
  if (fclose(f) < 0) {
    free(buf);
    goto err;
  }
  return buf;
err:
  return NULL;
}


x86 assembly (clang, -O0):
<get_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c4 <get_contents+0x34>
jmp    12a5 <get_contents+0x115>
mov    -0x20(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x28(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jge    11e7 <get_contents+0x57>
jmp    12a5 <get_contents+0x115>
cmpq   $0x0,-0x18(%rbp)
je     11fb <get_contents+0x6b>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%edi
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x1,-0x38(%rbp)
je     124a <get_contents+0xba>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    12a5 <get_contents+0x115>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1277 <get_contents+0xe7>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    12a5 <get_contents+0x115>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
cmp    $0x0,%rax
jge    1298 <get_contents+0x108>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    12a5 <get_contents+0x115>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <get_contents+0x11d>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_contents>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0xe5d(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1238 <get_contents+0xa8>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
js     1238 <get_contents+0xa8>
test   %rbx,%rbx
je     11d8 <get_contents+0x48>
mov    0xc(%rsp),%eax
mov    %eax,(%rbx)
mov    0x8(%rsp),%r15d
lea    0x1(%r15),%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    $0x1,%rax
jne    1230 <get_contents+0xa0>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1230 <get_contents+0xa0>
mov    %r14,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
jns    123a <get_contents+0xaa>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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