#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(unsigned char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf_err(char *, char *);
int /*<<< orphan*/ printf_info(char *, int, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
unsigned char *x_calloc(int, long);
unsigned char *read_file(char *file_name) {
  FILE *fp;
  unsigned char *p;
  size_t tmp;
  int i = 0;
  long size;
  fp = fopen(file_name, "rb");
  if (fp == NULL)
    printf_err("Cannot open file '%s'\n", file_name);
  fseek(fp, 0L, SEEK_END);
  size = ftell(fp);
  rewind(fp);
  p = x_calloc(1, size);
  tmp = fread(p, size + 1, 1, fp);
  if (!feof(fp))
    printf_err("Didn't read the entire input file (%s)\n", file_name);
  if (tmp == 0)
    for (i = size - 1; p[i] == 0; i--)
      ;
  printf_info("read %i bytes from %s\n", i + 1, file_name);
  fclose(fp);
  return p;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e2 <read_file+0x42>
mov    -0x8(%rbp),%rsi
lea    0xe26(%rip),%rdi
call   1070 <printf_err@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x30(%rbp),%rsi
mov    $0x1,%edi
call   10c0 <x_calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
jne    1260 <read_file+0xc0>
mov    -0x8(%rbp),%rsi
lea    0xdbf(%rip),%rdi
call   1070 <printf_err@plt>
cmpq   $0x0,-0x20(%rbp)
jne    12a3 <read_file+0x103>
mov    -0x30(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    129e <read_file+0xfe>
jmp    1290 <read_file+0xf0>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1276 <read_file+0xd6>
jmp    12a3 <read_file+0x103>
mov    -0x24(%rbp),%esi
add    $0x1,%esi
mov    -0x8(%rbp),%rdx
lea    0xd8e(%rip),%rdi
call   1030 <printf_info@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe4b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11d1 <read_file+0x31>
lea    0xe3a(%rip),%rdi
mov    %r14,%rsi
call   1070 <printf_err@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r15,%rdi
call   1080 <ftell@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1060 <rewind@plt>
mov    $0x1,%ebx
mov    $0x1,%edi
mov    %r13,%rsi
call   10c0 <x_calloc@plt>
mov    %rax,%r12
lea    0x1(%r13),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1090 <feof@plt>
test   %eax,%eax
je     1236 <read_file+0x96>
test   %rbp,%rbp
jne    126f <read_file+0xcf>
jmp    124a <read_file+0xaa>
lea    0xddd(%rip),%rdi
mov    %r14,%rsi
call   1070 <printf_err@plt>
test   %rbp,%rbp
jne    126f <read_file+0xcf>
add    $0x1,%r13d
mov    %r13d,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    -0x2(%rbx),%eax
cltq
add    $0xffffffff,%ebx
cmpb   $0x0,(%r12,%rax,1)
je     1260 <read_file+0xc0>
lea    0xdcc(%rip),%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   1030 <printf_info@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [23 x i8] c"Cannot open file '%s'\0A\00"
@SEEK_END = global i32 0
@.str.2 = [40 x i8] c"Didn't read the entire input file (%s)\0A\00"
@.str.3 = [23 x i8] c"read %i bytes from %s\0A\00"
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %2
store i32 0, i32* %6
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call i32 @printf_err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %12, %1
%16 = load i32*, i32** %3
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i64 0, i32 %17)
%19 = load i32*, i32** %3
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %7
%21 = load i32*, i32** %3
%22 = call i32 @rewind(i32* %21)
%23 = load i64, i64* %7
%24 = call i8* @x_calloc(i32 1, i64 %23)
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = load i64, i64* %7
%27 = add nsw i64 %26, 1
%28 = load i32*, i32** %3
%29 = call i64 @fread(i8* %25, i64 %27, i32 1, i32* %28)
store i64 %29, i64* %5
%30 = load i32*, i32** %3
%31 = call i32 @feof(i32* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %15
%34 = load i8*, i8** %2
%35 = call i32 @printf_err(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %33, %15
%37 = load i64, i64* %5
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %56
39: ; preds = %36
%40 = load i64, i64* %7
%41 = sub nsw i64 %40, 1
%42 = trunc i64 %41 to i32
store i32 %42, i32* %6
br label %43
43: ; preds = %52, %39
%44 = load i8*, i8** %4
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %43
br label %52
52: ; preds = %51
%53 = load i32, i32* %6
%54 = add nsw i32 %53, -1
store i32 %54, i32* %6
br label %43
55: ; preds = %43
br label %56
56: ; preds = %55, %36
%57 = load i32, i32* %6
%58 = add nsw i32 %57, 1
%59 = load i8*, i8** %2
%60 = call i32 @printf_info(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %58, i8* %59)
%61 = load i32*, i32** %3
%62 = call i32 @fclose(i32* %61)
%63 = load i8*, i8** %4
ret i8* %63
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [23 x i8] c"Cannot open file '%s'\0A\00"
@SEEK_END = global i32 0
@.str.2 = [40 x i8] c"Didn't read the entire input file (%s)\0A\00"
@.str.3 = [23 x i8] c"read %i bytes from %s\0A\00"
define i8* @read_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @printf_err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %6
6: ; preds = %4, %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i64 0, i32 %7)
%9 = tail call i64 @ftell(i32* %2)
%10 = tail call i32 @rewind(i32* %2)
%11 = tail call i8* @x_calloc(i32 1, i64 %9)
%12 = add nsw i64 %9, 1
%13 = tail call i64 @fread(i8* %11, i64 %12, i32 1, i32* %2)
%14 = tail call i32 @feof(i32* %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %6
%17 = tail call i32 @printf_err(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %16, %6
%19 = icmp eq i64 %13, 0
br i1 %19, label %20, label %29
20: ; preds = %18
%21 = trunc i64 %9 to i32
br label %22
22: ; preds = %22, %20
%23 = phi i32 [ %21, %20 ], [ %24, %22 ]
%24 = add i32 %23, -1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %11, i64 %25
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 0
br i1 %28, label %22, label %29
29: ; preds = %22, %18
%30 = phi i32 [ 1, %18 ], [ %23, %22 ]
%31 = tail call i32 @printf_info(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %30, i8* %0)
%32 = tail call i32 @fclose(i32* %2)
ret i8* %11
}