#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*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    err_sys("usage: access.out <pathname>\n");
    exit(1);
  }
  if (access(argv[1], R_OK) < 0) {
    printf("access error for %s: %s\n", argv[1], strerror(errno));
  } else {
    printf("read access OK\n");
  }
  if (open(argv[1], O_RDONLY) < 0) {
    printf("open error for %s: %s\n", argv[1], strerror(errno));
  } else {
    printf("open for reading OK\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1196 <main+0x36>
lea    0xe79(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jge    11ea <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x18(%rbp),%rsi
mov    %rax,%rdx
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f8 <main+0x98>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0x0,%rax
jge    124c <main+0xec>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125a <main+0xfa>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1220 <main+0xb0>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
test   %rax,%rax
js     11a5 <main+0x35>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
jmp    11cb <main+0x5b>
mov    0x8(%rbx),%r14
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe60(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %rax,%rax
js     11f0 <main+0x80>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
jmp    1216 <main+0xa6>
mov    0x8(%rbx),%rbx
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe2e(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd9(%rip),%rdi
call   1040 <err_sys@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"usage: access.out <pathname>\0A\00"
@R_OK = global i32 0
@.str.1 = [25 x i8] c"access error for %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [16 x i8] c"read access OK\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [23 x i8] c"open error for %s: %s\0A\00"
@.str.4 = [21 x i8] c"open for reading OK\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* %4
%7 = icmp ne i32 %6, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = call i32 @err_sys(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @R_OK
%16 = call i64 @access(i8* %14, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %11
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %23)
br label %27
25: ; preds = %11
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_RDONLY
%32 = call i64 @open(i8* %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %27
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %37, i8* %39)
br label %43
41: ; preds = %27
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %34
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"usage: access.out <pathname>\0A\00"
@R_OK = global i32 0
@.str.1 = [25 x i8] c"access error for %s: %s\0A\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [23 x i8] c"open error for %s: %s\0A\00"
@str = [15 x i8] c"read access OK\00"
@str.5 = [20 x i8] c"open for reading OK\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @err_sys(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @R_OK
%11 = tail call i64 @access(i8* %9, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = load i8*, i8** %8
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %16)
br label %20
18: ; preds = %7
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = load i8*, i8** %8
%22 = load i32, i32* @O_RDONLY
%23 = tail call i64 @open(i8* %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i8*, i8** %8
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %26, i8* %28)
br label %32
30: ; preds = %20
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
ret i32 0
}