#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int test_access_helper(char *path, void *dummy) {
  int fd;
  char buffer[1];
  if ((fd = open(path, O_RDONLY)) < 0) {
    fprintf(stderr, "Cannot open %s: %s\n", path, strerror(errno));
    return -1;
  }
  if (read(fd, buffer, 1) < 0) {
    fprintf(stderr, "Cannot read %s: %s\n", path, strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_access_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11b2 <test_access_helper+0x72>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe5f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <test_access_helper+0xd8>
mov    -0x1c(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jge    1211 <test_access_helper+0xd1>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe14(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <test_access_helper+0xd8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_access_helper>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1181 <test_access_helper+0x41>
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    11b4 <test_access_helper+0x74>
lea    0xe95(%rip),%r14
jmp    1188 <test_access_helper+0x48>
lea    0xe78(%rip),%r14
mov    0x2e49(%rip),%rax
mov    (%rax),%ebp
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"Cannot read %s: %s\0A\00"
define i32 @test_access_helper(i8* %0, i8* nocapture readnone %1) {
%3 = alloca [1 x i8]
%4 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = call i64 @read(i32 %6, i8* %4, i32 1)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %8, %2
%12 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), %8 ]
%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, %8
%18 = phi i32 [ 0, %8 ], [ -1, %11 ]
ret i32 %18
}