#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *read_file(const char *path) {
  long file_size = -1L;
  FILE *file = fopen(path, "r");
  if (!file) {
    printf("could not open '%s'\n", path);
    perror("fopen");
    exit(1);
  }
  fseek(file, 0, SEEK_END);
  file_size = ftell(file);
  rewind(file);
  char *buffer = (char *)malloc(sizeof(char) * file_size);
  size_t result = fread(buffer, 1, file_size, file);
  fclose(file);
  if (!result) {
    free(buffer);
    return NULL;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0xffffffffffffffff,-0x18(%rbp)
ff
mov    -0x10(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1209 <read_file+0x59>
mov    -0x10(%rbp),%rsi
lea    0xe14(%rip),%rdi
call   1060 <printf@plt>
lea    0xe1d(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
jne    128a <read_file+0xda>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1292 <read_file+0xe2>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe41(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1234 <read_file+0x84>
mov    %rax,%rbx
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %r15,%r15
jne    122b <read_file+0x7b>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdc7(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
lea    0xdcd(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [21 x i8] c"could not open '%s'\0A\00"
@.str.2 = [6 x i8] c"fopen\00"
@SEEK_END = global i32 0
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store i64 -1, i64* %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %17, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = load i32*, i32** %5
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %5
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %4
%23 = load i32*, i32** %5
%24 = call i32 @rewind(i32* %23)
%25 = load i64, i64* %4
%26 = mul i64 1, %25
%27 = trunc i64 %26 to i32
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = load i64, i64* %4
%32 = load i32*, i32** %5
%33 = call i64 @fread(i8* %30, i32 1, i64 %31, i32* %32)
store i64 %33, i64* %7
%34 = load i32*, i32** %5
%35 = call i32 @fclose(i32* %34)
%36 = load i64, i64* %7
%37 = icmp ne i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %17
%39 = load i8*, i8** %6
%40 = call i32 @free(i8* %39)
store i8* null, i8** %2
br label %43
41: ; preds = %17
%42 = load i8*, i8** %6
store i8* %42, i8** %2
br label %43
43: ; preds = %41, %38
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [21 x i8] c"could not open '%s'\0A\00"
@.str.2 = [6 x i8] c"fopen\00"
@SEEK_END = global i32 0
define i8* @read_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %2, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %2)
%12 = tail call i32 @rewind(i32* %2)
%13 = trunc i64 %11 to i32
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i64 @fread(i8* %15, i32 1, i64 %11, i32* %2)
%17 = tail call i32 @fclose(i32* %2)
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %21
19: ; preds = %8
%20 = tail call i32 @free(i8* %15)
br label %21
21: ; preds = %8, %19
%22 = phi i8* [ null, %19 ], [ %15, %8 ]
ret i8* %22
}