#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int read_file(const char *file, char *buf, int size) {
  int fh;
  fh = open(file, O_RDONLY);
  if (fh == -1) {
    fprintf(stderr, "Open %s failed (%s)\n", file, strerror(errno));
    return -1;
  }
  if (read(fh, buf, size) < 0) {
    fprintf(stderr, "Read %s failed (%s)\n", file, strerror(errno));
    close(fh);
    return -1;
  }
  close(fh);
  return 0;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c6 <read_file+0x76>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <read_file+0xea>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jge    122b <read_file+0xdb>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe03(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <read_file+0xea>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1192 <read_file+0x42>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <read@plt>
test   %rax,%rax
js     11bf <read_file+0x6f>
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
jmp    11f6 <read_file+0xa6>
mov    0x2e3f(%rip),%rax
mov    (%rax),%ebx
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe50(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
jmp    11f1 <read_file+0xa1>
mov    0x2e12(%rip),%rax
mov    (%rax),%ebx
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe38(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0xffffffff,%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 = [21 x i8] c"Open %s failed (%s)\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Read %s failed (%s)\0A\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
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, 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 ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %18)
store i32 -1, i32* %4
br label %37
20: ; preds = %3
%21 = load i32, i32* %8
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = call i64 @read(i32 %21, i8* %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %5
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %30)
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %4
br label %37
34: ; preds = %20
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
store i32 0, i32* %4
br label %37
37: ; preds = %34, %26, %14
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Open %s failed (%s)\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Read %s failed (%s)\0A\00"
define i32 @read_file(i8* %0, i8* nocapture %1, i32 %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 %7, label %12
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
br label %23
12: ; preds = %3
%13 = tail call i64 @read(i32 %5, i8* %1, i32 %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %18)
%20 = tail call i32 @close(i32 %5)
br label %23
21: ; preds = %12
%22 = tail call i32 @close(i32 %5)
br label %23
23: ; preds = %21, %15, %7
%24 = phi i32 [ -1, %7 ], [ -1, %15 ], [ 0, %21 ]
ret i32 %24
}