#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 */
char *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int file_size(char const *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
const char *read_file(const char *file_name, int *len) {
  char *buf = NULL;
  *len = file_size(file_name);
  if (*len > 0) {
    FILE *src_file = fopen(file_name, "r");
    int flen;
    if (src_file != NULL) {
      buf = calloc(1, (*len) + 1);
      if (buf != NULL) {
        flen = (int)fread(buf, 1, *len, src_file);
        if (flen != *len)
          fprintf(stderr, "couldn't read %s\n", file_name);
      } else
        fprintf(stderr, "couldn't allocate buf\n");
      fclose(src_file);
    } else
      fprintf(stderr, "failed to open %s\n", file_name);
  }
  return buf;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <file_size@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jle    1266 <read_file+0x106>
mov    -0x8(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1246 <read_file+0xe6>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1221 <read_file+0xc1>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
je     121c <read_file+0xbc>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1238 <read_file+0xd8>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1261 <read_file+0x101>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1266 <read_file+0x106>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r12
call   1050 <file_size@plt>
mov    %eax,(%rbx)
test   %eax,%eax
jle    11dd <read_file+0x7d>
lea    0xe7f(%rip),%rsi
mov    %r12,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e2 <read_file+0x82>
mov    %rax,%r15
mov    (%rbx),%r13d
lea    0x1(%r13),%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1201 <read_file+0xa1>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    %eax,(%rbx)
je     1218 <read_file+0xb8>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1218 <read_file+0xb8>
xor    %r14d,%r14d
jmp    1220 <read_file+0xc0>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
xor    %r14d,%r14d
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1220 <read_file+0xc0>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"couldn't read %s\0A\00"
@.str.2 = [23 x i8] c"couldn't allocate buf\0A\00"
@.str.3 = [19 x i8] c"failed to open %s\0A\00"
define i8* @read_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i8* null, i8** %5
%8 = load i8*, i8** %3
%9 = call i32 @file_size(i8* %8)
%10 = load i32*, i32** %4
store i32 %9, i32* %10
%11 = load i32*, i32** %4
%12 = load i32, i32* %11
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %53
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %48
19: ; preds = %14
%20 = load i32*, i32** %4
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
%23 = call i8* @calloc(i32 1, i32 %22)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %42
26: ; preds = %19
%27 = load i8*, i8** %5
%28 = load i32*, i32** %4
%29 = load i32, i32* %28
%30 = load i32*, i32** %6
%31 = call i64 @fread(i8* %27, i32 1, i32 %29, i32* %30)
%32 = trunc i64 %31 to i32
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = load i32*, i32** %4
%35 = load i32, i32* %34
%36 = icmp ne i32 %33, %35
br i1 %36, label %37, label %41
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %3
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %37, %26
br label %45
42: ; preds = %19
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %42, %41
%46 = load i32*, i32** %6
%47 = call i32 @fclose(i32* %46)
br label %52
48: ; preds = %14
%49 = load i32, i32* @stderr
%50 = load i8*, i8** %3
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %48, %45
br label %53
53: ; preds = %52, %2
%54 = load i8*, i8** %5
ret i8* %54
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"couldn't read %s\0A\00"
@.str.2 = [23 x i8] c"couldn't allocate buf\0A\00"
@.str.3 = [19 x i8] c"failed to open %s\0A\00"
define noalias i8* @read_file(i8* %0, i32* nocapture %1) {
%3 = tail call i32 @file_size(i8* %0)
store i32 %3, i32* %1
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %29
5: ; preds = %2
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %26, label %8
8: ; preds = %5
%9 = load i32, i32* %1
%10 = add nsw i32 %9, 1
%11 = tail call i8* @calloc(i32 1, i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %21, label %13
13: ; preds = %8
%14 = tail call i64 @fread(i8* %11, i32 1, i32 %9, i32* %6)
%15 = trunc i64 %14 to i32
%16 = load i32, i32* %1
%17 = icmp eq i32 %16, %15
br i1 %17, label %24, label %18
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %24
21: ; preds = %8
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %13, %18, %21
%25 = tail call i32 @fclose(i32* %6)
br label %29
26: ; preds = %5
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %29
29: ; preds = %24, %26, %2
%30 = phi i8* [ null, %2 ], [ %11, %24 ], [ null, %26 ]
ret i8* %30
}