#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*/ fail(char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int read_file(char *buf, const char *fn) {
  int len;
  FILE *fp = fopen(fn, "rb");
  if (!fp)
    fail("can't open file: %s", fn);
  len = fread(buf, 1, (1024 * 1024 * 32), fp);
  if (len <= 0)
    fail("read failed");
  if (len >= (1024 * 1024 * 32))
    fail("data buffer is too small");
  fclose(fp);
  return len;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1181 <read_file+0x41>
mov    -0x10(%rbp),%rsi
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <fail@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000000,%edx
call   1030 <fread@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11b3 <read_file+0x73>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <fail@plt>
cmpl   $0x2000000,-0x14(%rbp)
jl     11ce <read_file+0x8e>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <fail@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xeaf(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1172 <read_file+0x32>
lea    0xe9b(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <fail@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x2000000,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    119e <read_file+0x5e>
cmp    $0x2000000,%ebp
jb     11ac <read_file+0x6c>
lea    0xe87(%rip),%rdi
jmp    11a5 <read_file+0x65>
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1040 <fail@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"can't open file: %s\00"
@.str.2 = [12 x i8] c"read failed\00"
@.str.3 = [25 x i8] c"data buffer is too small\00"
define i32 @read_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %2
%15 = load i8*, i8** %3
%16 = load i32*, i32** %6
%17 = call i32 @fread(i8* %15, i32 1, i32 33554432, i32* %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %14
%23 = load i32, i32* %5
%24 = icmp sge i32 %23, 33554432
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* %5
ret i32 %30
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"can't open file: %s\00"
@.str.2 = [12 x i8] c"read failed\00"
@.str.3 = [25 x i8] c"data buffer is too small\00"
define i32 @read_file(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %1, 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 %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @fail(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %7
7: ; preds = %5, %2
%8 = tail call i32 @fread(i8* %0, i32 1, i32 33554432, i32* %3)
%9 = icmp slt i32 %8, 1
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = icmp ugt i32 %8, 33554431
br i1 %11, label %12, label %15
12: ; preds = %10, %7
%13 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %7 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), %10 ]
%14 = tail call i32 (i8*, ...) @fail(i8* %13)
br label %15
15: ; preds = %12, %10
%16 = tail call i32 @fclose(i32* %3)
ret i32 %8
}