#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*/ _dbg(char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int file_size(char const *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ strcmp(char const *, char *);
unsigned char *read_file(const char *filename, int *size) {
  int fsize;
  char *big_buf;
  FILE *file = NULL;
  fsize = file_size(filename);
  *size = fsize;
  if (fsize < 0)
    return NULL;
  big_buf = malloc(fsize);
  if (!big_buf) {
    perror("malloc");
    return NULL;
  }
  memset(big_buf, 0, fsize);
  if (!strcmp(filename, "-"))
    file = stdin;
  else
    file = fopen(filename, "r");
  if (!file) {
    perror(NULL);
    return NULL;
  }
  if (0)
    _dbg("reading from %s\n", filename);
  fread(big_buf, fsize, 1, file);
  fclose(file);
  return big_buf;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <file_size@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x1c(%rbp)
jge    11c4 <read_file+0x44>
movq   $0x0,-0x8(%rbp)
jmp    1289 <read_file+0x109>
mov    -0x1c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f4 <read_file+0x74>
lea    0xe1e(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1289 <read_file+0x109>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    122e <read_file+0xae>
mov    0x2dc6(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1242 <read_file+0xc2>
mov    -0x10(%rbp),%rdi
lea    0xdd0(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1263 <read_file+0xe3>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1289 <read_file+0x109>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
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   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
call   1060 <file_size@plt>
mov    %eax,(%rbx)
test   %eax,%eax
js     1226 <read_file+0xa6>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     121a <read_file+0x9a>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
lea    0xe47(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11ff <read_file+0x7f>
lea    0xe36(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rbx,%rbx
je     120e <read_file+0x8e>
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1229 <read_file+0xa9>
mov    0x2de2(%rip),%rax
mov    (%rax),%rbx
test   %rbx,%rbx
jne    11e3 <read_file+0x63>
xor    %r14d,%r14d
xor    %edi,%edi
call   10a0 <perror@plt>
jmp    1229 <read_file+0xa9>
lea    0xddf(%rip),%rdi
call   10a0 <perror@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.2 = [2 x i8] c"r\00"
define i8* @read_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* null, i32** %8
%9 = load i8*, i8** %4
%10 = call i32 @file_size(i8* %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = load i32*, i32** %5
store i32 %11, i32* %12
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i8* null, i8** %3
br label %48
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %48
23: ; preds = %16
%24 = load i8*, i8** %7
%25 = load i32, i32* %6
%26 = call i32 @memset(i8* %24, i32 0, i32 %25)
%27 = load i8*, i8** %4
%28 = call i32 @strcmp(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %23
%31 = load i32*, i32** @stdin
store i32* %31, i32** %8
br label %35
32: ; preds = %23
%33 = load i8*, i8** %4
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %8
br label %35
35: ; preds = %32, %30
%36 = load i32*, i32** %8
%37 = icmp ne i32* %36, null
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @perror(i8* null)
store i8* null, i8** %3
br label %48
40: ; preds = %35
%41 = load i8*, i8** %7
%42 = load i32, i32* %6
%43 = load i32*, i32** %8
%44 = call i32 @fread(i8* %41, i32 %42, i32 1, i32* %43)
%45 = load i32*, i32** %8
%46 = call i32 @fclose(i32* %45)
%47 = load i8*, i8** %7
store i8* %47, i8** %3
br label %48
48: ; preds = %40, %38, %21, %15
%49 = load i8*, i8** %3
ret i8* %49
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.2 = [2 x i8] c"r\00"
define i8* @read_file(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @file_size(i8* %0)
store i32 %3, i32* %1
%4 = icmp slt i32 %3, 0
br i1 %4, label %26, label %5
5: ; preds = %2
%6 = tail call i8* @malloc(i32 %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %5
%11 = tail call i32 @memset(i8* %6, i32 0, i32 %3)
%12 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = load i32*, i32** @stdin
br label %18
16: ; preds = %10
%17 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
%19 = phi i32* [ %17, %16 ], [ %15, %14 ]
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @perror(i8* null)
br label %26
23: ; preds = %18
%24 = tail call i32 @fread(i8* %6, i32 %3, i32 1, i32* %19)
%25 = tail call i32 @fclose(i32* %19)
br label %26
26: ; preds = %2, %23, %21, %8
%27 = phi i8* [ %6, %23 ], [ null, %21 ], [ null, %8 ], [ null, %2 ]
ret i8* %27
}