#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int);
ssize_t file_read(const char *path, char *buf, size_t sz) {
  int fd = open(path, O_RDONLY);
  ssize_t ret = -1;
  if (fd >= 0) {
    ret = read(fd, buf, sz);
    close(fd);
  } else {
    fprintf(stderr,
            "[WARN]"
            "failed to open \"%s\" for reading:%d(%s)"
            "\n",
            path ?: "<NULL>", errno, strerror(errno));
    ;
  }
  return ret;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11aa <file_read+0x5a>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
jmp    1220 <file_read+0xd0>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11d5 <file_read+0x85>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11e5 <file_read+0x95>
lea    0xe52(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11e5 <file_read+0x95>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    -0x3c(%rbp),%ecx
mov    %rax,%r8
lea    0xde5(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     118e <file_read+0x3e>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
jmp    11d2 <file_read+0x82>
mov    0x2e43(%rip),%rax
mov    (%rax),%r14d
test   %rbx,%rbx
lea    0xe8c(%rip),%rbp
cmovne %rbx,%rbp
mov    0x2e23(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe43(%rip),%rsi
mov    %r14d,%edi
mov    %rbp,%rdx
mov    %ebx,%ecx
mov    %rax,%r8
call   1050 <fprintf@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 = [46 x i8] c"[WARN]failed to open \22%s\22 for reading:%d(%s)\0A\00"
@.str.1 = [7 x i8] c"<NULL>\00"
@errno = global i32 0
define i32 @file_read(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
store i32 -1, i32* %8
%12 = load i32, i32* %7
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %3
%15 = load i32, i32* %7
%16 = load i8*, i8** %5
%17 = load i64, i64* %6
%18 = call i32 @read(i32 %15, i8* %16, i64 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %7
%20 = call i32 @close(i32 %19)
br label %33
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %4
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %21
br label %27
26: ; preds = %21
br label %27
27: ; preds = %26, %25
%28 = phi i8* [ %23, %25 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %26 ]
%29 = load i32, i32* @errno
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %28, i32 %29, i8* %31)
br label %33
33: ; preds = %27, %14
%34 = load i32, i32* %8
ret i32 %34
}

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