#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
long file_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
long fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *read_file(char *name, long *len) {
  FILE *f;
  char *buffer;
  long read;
  if ((f = fopen(name, "rb")) == NULL) {
    fprintf(stderr, "Opening file '%s' failed: %s\n", name, strerror(errno));
    return NULL;
  }
  if ((*len = file_size(f)) == -1L) {
    fclose(f);
    fprintf(stderr, "Getting file size failed\n");
    return NULL;
  }
  buffer = (char *)malloc(sizeof(char) * (*len));
  if (buffer == NULL) {
    fclose(f);
    fprintf(stderr, "Memory alloc failed\n");
    return NULL;
  }
  read = fread(buffer, 1, *len, f);
  if (read != *len) {
    fclose(f);
    free(buffer);
    fprintf(stderr, "File read error (read %ld of %ld)\n", read, *len);
    return NULL;
  }
  fclose(f);
  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
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11f5 <read_file+0x75>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130a <read_file+0x18a>
mov    -0x20(%rbp),%rdi
call   1060 <file_size@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0xffffffffffffffff,%rax
jne    123c <read_file+0xbc>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130a <read_file+0x18a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    128a <read_file+0x10a>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130a <read_file+0x18a>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     12f9 <read_file+0x179>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xd6b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130a <read_file+0x18a>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe6a(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1218 <read_file+0x98>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <file_size@plt>
mov    %rax,(%r14)
cmp    $0xffffffffffffffff,%rax
je     124a <read_file+0xca>
mov    %eax,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1264 <read_file+0xe4>
mov    %rax,%r15
mov    (%r14),%rdx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r12
mov    (%r14),%r13
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    %r13,%r12
je     1286 <read_file+0x106>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rcx
lea    0xe40(%rip),%rsi
xor    %r15d,%r15d
mov    %r12,%rdx
jmp    1241 <read_file+0xc1>
mov    0x2dc1(%rip),%rax
mov    (%rax),%ebx
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdcd(%rip),%rsi
xor    %r15d,%r15d
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1286 <read_file+0x106>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
jmp    127c <read_file+0xfc>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Opening file '%s' failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Getting file size failed\0A\00"
@.str.3 = [21 x i8] c"Memory alloc failed\0A\00"
@.str.4 = [35 x i8] c"File read error (read %ld of %ld)\0A\00"
define i8* @read_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %4
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %16)
store i8* null, i8** %3
br label %66
18: ; preds = %2
%19 = load i32*, i32** %6
%20 = call i64 @file_size(i32* %19)
%21 = load i64*, i64** %5
store i64 %20, i64* %21
%22 = icmp eq i64 %20, -1
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32*, i32** %6
%25 = call i32 @fclose(i32* %24)
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %3
br label %66
28: ; preds = %18
%29 = load i64*, i64** %5
%30 = load i64, i64* %29
%31 = mul i64 1, %30
%32 = trunc i64 %31 to i32
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %42
37: ; preds = %28
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %3
br label %66
42: ; preds = %28
%43 = load i8*, i8** %7
%44 = load i64*, i64** %5
%45 = load i64, i64* %44
%46 = load i32*, i32** %6
%47 = call i64 @fread(i8* %43, i32 1, i64 %45, i32* %46)
store i64 %47, i64* %8
%48 = load i64, i64* %8
%49 = load i64*, i64** %5
%50 = load i64, i64* %49
%51 = icmp ne i64 %48, %50
br i1 %51, label %52, label %62
52: ; preds = %42
%53 = load i32*, i32** %6
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %7
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* @stderr
%58 = load i64, i64* %8
%59 = load i64*, i64** %5
%60 = load i64, i64* %59
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i64 %58, i64 %60)
store i8* null, i8** %3
br label %66
62: ; preds = %42
%63 = load i32*, i32** %6
%64 = call i32 @fclose(i32* %63)
%65 = load i8*, i8** %7
store i8* %65, i8** %3
br label %66
66: ; preds = %62, %52, %37, %23, %12
%67 = load i8*, i8** %3
ret i8* %67
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Opening file '%s' failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Getting file size failed\0A\00"
@.str.3 = [21 x i8] c"Memory alloc failed\0A\00"
@.str.4 = [35 x i8] c"File read error (read %ld of %ld)\0A\00"
define i8* @read_file(i8* %0, i64* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %8)
br label %37
10: ; preds = %2
%11 = tail call i64 @file_size(i32* %3)
store i64 %11, i64* %1
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @fclose(i32* %3)
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %37
17: ; preds = %10
%18 = trunc i64 %11 to i32
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = tail call i32 @fclose(i32* %3)
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %37
26: ; preds = %17
%27 = load i64, i64* %1
%28 = tail call i64 @fread(i8* %20, i32 1, i64 %27, i32* %3)
%29 = load i64, i64* %1
%30 = icmp eq i64 %28, %29
%31 = tail call i32 @fclose(i32* %3)
br i1 %30, label %37, label %32
32: ; preds = %26
%33 = tail call i32 @free(i8* %20)
%34 = load i32, i32* @stderr
%35 = load i64, i64* %1
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i64 %28, i64 %35)
br label %37
37: ; preds = %26, %32, %22, %13, %5
%38 = phi i8* [ null, %5 ], [ null, %13 ], [ null, %22 ], [ null, %32 ], [ %20, %26 ]
ret i8* %38
}