#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, scalar_t__, char *, char const *);
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*/ *);
size_t read_file(const char *path, char *buf, size_t len) {
  FILE *ctl_file;
  size_t nread;
  while ((ctl_file = fopen(path, "r")) == NULL && errno == EINTR)
    ;
  if (ctl_file == NULL)
    error(EXIT_FAILURE, errno, "%s", path);
  nread = fread(buf, 1, len - 1, ctl_file);
  if (ferror(ctl_file))
    error(EXIT_FAILURE, 0, "%s: Error reading file", path);
  fclose(ctl_file);
  buf[nread] = '\0';
  return nread;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
jne    11a4 <read_file+0x54>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11b4 <read_file+0x64>
jmp    11b9 <read_file+0x69>
jmp    1164 <read_file+0x14>
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <read_file+0x97>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe20(%rip),%rdx
call   1060 <error@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     1235 <read_file+0xe5>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
lea    0xdd5(%rip),%rdx
call   1060 <error@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    $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    %rdx,%r12
mov    %rsi,(%rsp)
mov    %rdi,%r15
lea    0xe94(%rip),%r13
mov    0x2e6d(%rip),%rbp
mov    0x2e56(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r13,%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    0x0(%rbp),%rsi
test   %rax,%rax
jne    119c <read_file+0x4c>
cmp    (%r14),%rsi
je     1180 <read_file+0x30>
test   %rbx,%rbx
jne    11b9 <read_file+0x69>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rdx
mov    %r15,%rcx
call   1060 <error@plt>
add    $0xffffffffffffffff,%r12
mov    (%rsp),%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11fe <read_file+0xae>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rdx
xor    %esi,%esi
mov    %r15,%rcx
call   1060 <error@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
movb   $0x0,0x0(%rbp,%r12,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
@EINTR = global i64 0
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [23 x i8] c"%s: Error reading file\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
br label %9
9: ; preds = %19, %3
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br label %17
17: ; preds = %13, %9
%18 = phi i1 [ false, %9 ], [ %16, %13 ]
br i1 %18, label %19, label %20
19: ; preds = %17
br label %9
20: ; preds = %17
%21 = load i32*, i32** %7
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* @EXIT_FAILURE
%25 = load i64, i64* @errno
%26 = load i8*, i8** %4
%27 = call i32 @error(i32 %24, i64 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %23, %20
%29 = load i8*, i8** %5
%30 = load i64, i64* %6
%31 = sub i64 %30, 1
%32 = load i32*, i32** %7
%33 = call i64 @fread(i8* %29, i32 1, i64 %31, i32* %32)
store i64 %33, i64* %8
%34 = load i32*, i32** %7
%35 = call i64 @ferror(i32* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %28
%38 = load i32, i32* @EXIT_FAILURE
%39 = load i8*, i8** %4
%40 = call i32 @error(i32 %38, i64 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %37, %28
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %5
%45 = load i64, i64* %8
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8 0, i8* %46
%47 = load i64, i64* %8
ret i64 %47
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
@EINTR = global i64 0
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [23 x i8] c"%s: Error reading file\00"
define i64 @read_file(i8* %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %4, %3
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %4, label %11
11: ; preds = %4
br i1 %6, label %12, label %15
12: ; preds = %11
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @error(i32 %13, i64 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %12, %11
%16 = add i64 %2, -1
%17 = tail call i64 @fread(i8* %1, i32 1, i64 %16, i32* %5)
%18 = tail call i64 @ferror(i32* %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @error(i32 %21, i64 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %20, %15
%24 = tail call i32 @fclose(i32* %5)
%25 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 0, i8* %25
ret i64 %17
}