#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
unsigned int fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int, unsigned int);
int read_file(const char *filename, char *buf, unsigned int len) {
  FILE *fp = NULL;
  size_t ret = 0;
  if (!buf || len == 0) {
    return -2;
  }
  if ((fp = fopen(filename, "rb")) == NULL) {
    return -1;
  }
  memset(buf, 0x00, len);
  while ((ret += fread(buf + ret, 1, len, fp)) < len) {
    if (ferror(fp) || feof(fp))
      break;
  }
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1198 <read_file+0x38>
cmpl   $0x0,-0x1c(%rbp)
jne    11a4 <read_file+0x44>
movl   $0xfffffffe,-0x4(%rbp)
jmp    124b <read_file+0xeb>
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ce <read_file+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <read_file+0xeb>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,%eax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%ecx
cmp    %rcx,%rax
jae    123b <read_file+0xdb>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    1231 <read_file+0xd1>
mov    -0x28(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     1236 <read_file+0xd6>
jmp    123b <read_file+0xdb>
jmp    11dc <read_file+0x7c>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
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    $0xfffffffe,%ebx
test   %rsi,%rsi
je     11fa <read_file+0x9a>
mov    %edx,%r15d
test   %edx,%edx
je     11fa <read_file+0x9a>
mov    %rsi,%r14
lea    0xe76(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f5 <read_file+0x95>
mov    %rax,%r13
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1060 <memset@plt>
mov    %r15d,%r12d
nopl   0x0(%rax)
mov    %rbx,%rbp
lea    (%r14,%rbx,1),%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
mov    %eax,%ebx
add    %rbp,%rbx
cmp    %r12,%rbx
jae    11eb <read_file+0x8b>
mov    %r13,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11eb <read_file+0x8b>
mov    %r13,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     11b0 <read_file+0x50>
mov    %r13,%rdi
call   1050 <fclose@plt>
jmp    11fa <read_file+0x9a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
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"
define i32 @read_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* null, i32** %8
store i64 0, i64* %9
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12, %3
store i32 -2, i32* %4
br label %53
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %8
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %4
br label %53
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = call i32 @memset(i8* %22, i32 0, i32 %23)
br label %25
25: ; preds = %47, %21
%26 = load i8*, i8** %6
%27 = load i64, i64* %9
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i32, i32* %7
%30 = load i32*, i32** %8
%31 = call i32 @fread(i8* %28, i32 1, i32 %29, i32* %30)
%32 = zext i32 %31 to i64
%33 = load i64, i64* %9
%34 = add i64 %33, %32
store i64 %34, i64* %9
%35 = load i32, i32* %7
%36 = zext i32 %35 to i64
%37 = icmp ult i64 %34, %36
br i1 %37, label %38, label %48
38: ; preds = %25
%39 = load i32*, i32** %8
%40 = call i64 @ferror(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = load i32*, i32** %8
%44 = call i64 @feof(i32* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42, %38
br label %48
47: ; preds = %42
br label %25
48: ; preds = %46, %25
%49 = load i32*, i32** %8
%50 = call i32 @fclose(i32* %49)
%51 = load i64, i64* %9
%52 = trunc i64 %51 to i32
store i32 %52, i32* %4
br label %53
53: ; preds = %48, %20, %15
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @read_file(i8* nocapture %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %1, null
%5 = icmp eq i32 %2, 0
%6 = or i1 %4, %5
br i1 %6, label %29, label %7
7: ; preds = %3
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %29, label %10
10: ; preds = %7
%11 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %23, %10
%14 = phi i64 [ 0, %10 ], [ %18, %23 ]
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = tail call i32 @fread(i8* %15, i32 1, i32 %2, i32* %8)
%17 = zext i32 %16 to i64
%18 = add i64 %14, %17
%19 = icmp ult i64 %18, %12
br i1 %19, label %20, label %26
20: ; preds = %13
%21 = tail call i64 @ferror(i32* %8)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = tail call i64 @feof(i32* %8)
%25 = icmp eq i64 %24, 0
br i1 %25, label %13, label %26
26: ; preds = %20, %23, %13
%27 = tail call i32 @fclose(i32* %8)
%28 = trunc i64 %18 to i32
br label %29
29: ; preds = %7, %3, %26
%30 = phi i32 [ %28, %26 ], [ -2, %3 ], [ -1, %7 ]
ret i32 %30
}