#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int read(int, void *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int readfile(char *path, void *data, size_t len) {
  int fd, nrd;
  if ((fd = open(path, O_RDONLY)) == -1) {
    fprintf(stderr, "open(\"%s\"): %s\n", path, strerror(errno));
    return -1;
  }
  nrd = read(fd, data, len);
  if (close(fd) == -1) {
    fprintf(stderr, "close(\"%s\"): %s\n", path, strerror(errno));
    return -1;
  }
  return nrd;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11c6 <readfile+0x76>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <readfile+0xe4>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    122e <readfile+0xde>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf3(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <readfile+0xe4>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1199 <readfile+0x49>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    11cc <readfile+0x7c>
lea    0xe79(%rip),%rbx
jmp    11a0 <readfile+0x50>
lea    0xe60(%rip),%rbx
mov    0x2e31(%rip),%rax
mov    (%rax),%ebp
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"open(\22%s\22): %s\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"close(\22%s\22): %s\0A\00"
define i32 @readfile(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %5
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %18)
store i32 -1, i32* %4
br label %36
20: ; preds = %3
%21 = load i32, i32* %8
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = call i32 @read(i32 %21, i8* %22, i64 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %5
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %32)
store i32 -1, i32* %4
br label %36
34: ; preds = %20
%35 = load i32, i32* %9
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %28, %14
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"open(\22%s\22): %s\0A\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"close(\22%s\22): %s\0A\00"
define i32 @readfile(i8* %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = tail call i32 @read(i32 %5, i8* %1, i64 %2)
%9 = tail call i32 @close(i32 %5)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %7, %3
%12 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %7 ]
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* %12, i8* %0, i8* %15)
br label %17
17: ; preds = %11, %7
%18 = phi i32 [ %8, %7 ], [ -1, %11 ]
ret i32 %18
}