#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*/ die(char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(void *, int, unsigned int, int /*<<< orphan*/ *);
size_t read_file(const char *file, void *buf, unsigned buflen) {
  FILE *fp = fopen(file, "r");
  size_t size = 0;
  if (fp == NULL)
    die("Couldn't open %s", file);
  while (!feof(fp)) {
    size += fread(buf + size, 1, buflen - size, fp);
    if (ferror(fp) || (size == buflen && !feof(fp)))
      die("Error reading %s", file);
  }
  fclose(fp);
  return size;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11aa <read_file+0x4a>
mov    -0x8(%rbp),%rsi
lea    0xe5d(%rip),%rdi
call   1080 <die@plt>
jmp    11af <read_file+0x4f>
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11cd <read_file+0x6d>
jmp    123e <read_file+0xde>
mov    -0x10(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%eax
sub    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    1229 <read_file+0xc9>
mov    -0x28(%rbp),%rax
mov    -0x14(%rbp),%ecx
cmp    %rcx,%rax
jne    1239 <read_file+0xd9>
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
jne    1239 <read_file+0xd9>
mov    -0x8(%rbp),%rsi
lea    0xddf(%rip),%rdi
call   1080 <die@plt>
jmp    11af <read_file+0x4f>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%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    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1197 <read_file+0x37>
lea    0xe73(%rip),%rdi
mov    %r15,%rsi
call   1080 <die@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11c0 <read_file+0x60>
xor    %r13d,%r13d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12d,%eax
mov    %rax,(%rsp)
xor    %r13d,%r13d
jmp    11eb <read_file+0x8b>
nopl   0x0(%rax)
lea    0xe3c(%rip),%rdi
mov    %r15,%rsi
call   1080 <die@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11a6 <read_file+0x46>
mov    %r13,%rbp
lea    (%r14,%r13,1),%rdi
mov    %r12d,%edx
sub    %ebp,%edx
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r13
add    %rbp,%r13
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11d0 <read_file+0x70>
cmp    (%rsp),%r13
jne    11df <read_file+0x7f>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11df <read_file+0x7f>
jmp    11d0 <read_file+0x70>

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

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