#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*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
size_t read_file(const char *path, char *buf, size_t len) {
  FILE *ctl_file;
  size_t nread;
  ctl_file = fopen(path, "r");
  if (ctl_file == NULL)
    die("could not open file: %s: %s", path, strerror(errno));
  nread = fread(buf, 1, len - 1, ctl_file);
  if (ferror(ctl_file))
    die("could not read file: %s: %s", path, strerror(errno));
  fclose(ctl_file);
  buf[nread] = '\0';
  return nread;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bb <read_file+0x5b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe4c(%rip),%rdi
call   1070 <die@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1214 <read_file+0xb4>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe0f(%rip),%rdi
call   1070 <die@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe88(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11a4 <read_file+0x44>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe68(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1070 <die@plt>
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11ea <read_file+0x8a>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe3e(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1070 <die@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
movb   $0x0,(%r14,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [28 x i8] c"could not open file: %s: %s\00"
@errno = global i32 0
@.str.2 = [28 x i8] c"could not read file: %s: %s\00"
define i64 @read_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %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
%11 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 %16)
br label %18
18: ; preds = %13, %3
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
%21 = sub i64 %20, 1
%22 = load i32*, i32** %7
%23 = call i64 @fread(i8* %19, i32 1, i64 %21, i32* %22)
store i64 %23, i64* %8
%24 = load i32*, i32** %7
%25 = call i64 @ferror(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i8*, i8** %4
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %28, i32 %30)
br label %32
32: ; preds = %27, %18
%33 = load i32*, i32** %7
%34 = call i32 @fclose(i32* %33)
%35 = load i8*, i8** %5
%36 = load i64, i64* %8
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
%38 = load i64, i64* %8
ret i64 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [28 x i8] c"could not open file: %s: %s\00"
@errno = global i32 0
@.str.2 = [28 x i8] c"could not read file: %s: %s\00"
define i64 @read_file(i8* %0, i8* nocapture %1, i64 %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 %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %10
10: ; preds = %6, %3
%11 = add i64 %2, -1
%12 = tail call i64 @fread(i8* %1, i32 1, i64 %11, i32* %4)
%13 = tail call i64 @ferror(i32* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %17)
br label %19
19: ; preds = %15, %10
%20 = tail call i32 @fclose(i32* %4)
%21 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 0, i8* %21
ret i64 %12
}