#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 */
struct stat {
  int st_mode;
};

/* Variables and functions */
int R_OK;
int S_IEXEC;
int S_IREAD;
int S_IWRITE;
int W_OK;
int X_OK;
scalar_t__ stat(char const *, struct stat *);
int access(const char *path, int amode) {
  struct stat statbuf;
  if (stat(path, &statbuf))
    return -1;
  if (amode & R_OK) {
    if (!(statbuf.st_mode & S_IREAD))
      return -1;
  }
  if (amode & W_OK) {
    if (!(statbuf.st_mode & S_IWRITE))
      return -1;
  }
  if (amode & X_OK) {
    if (!(statbuf.st_mode & S_IEXEC))
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<access>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0x0,%rax
je     1142 <access+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <access+0xea>
mov    -0x14(%rbp),%eax
mov    0x2e74(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     117d <access+0x6d>
mov    -0x18(%rbp),%eax
mov    0x2e57(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1178 <access+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <access+0xea>
jmp    117d <access+0x6d>
mov    -0x14(%rbp),%eax
mov    0x2e59(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b8 <access+0xa8>
mov    -0x18(%rbp),%eax
mov    0x2e4c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11b3 <access+0xa3>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <access+0xea>
jmp    11b8 <access+0xa8>
mov    -0x14(%rbp),%eax
mov    0x2e16(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f3 <access+0xe3>
mov    -0x18(%rbp),%eax
mov    0x2df1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11ee <access+0xde>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <access+0xea>
jmp    11f3 <access+0xe3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<access>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
lea    0x8(%rsp),%rsi
call   1030 <stat@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    117e <access+0x6e>
mov    0x2e8b(%rip),%rcx
test   %ebx,(%rcx)
je     1148 <access+0x38>
mov    0x2e78(%rip),%rcx
mov    (%rcx),%ecx
test   %ecx,0x8(%rsp)
je     117e <access+0x6e>
mov    0x2e91(%rip),%rcx
test   %ebx,(%rcx)
je     1162 <access+0x52>
mov    0x2e8e(%rip),%rcx
mov    (%rcx),%ecx
test   %ecx,0x8(%rsp)
je     117e <access+0x6e>
mov    0x2e6f(%rip),%rcx
test   %ebx,(%rcx)
je     117c <access+0x6c>
mov    0x2e54(%rip),%rcx
mov    (%rcx),%ecx
test   %ecx,0x8(%rsp)
je     117e <access+0x6e>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@R_OK = global i32 0
@S_IREAD = global i32 0
@W_OK = global i32 0
@S_IWRITE = global i32 0
@X_OK = global i32 0
@S_IEXEC = global i32 0
define i32 @access(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i64 @stat(i8* %7, %struct.stat* %6)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %51
11: ; preds = %2
%12 = load i32, i32* %5
%13 = load i32, i32* @R_OK
%14 = and i32 %12, %13
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @S_IREAD
%20 = and i32 %18, %19
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %16
store i32 -1, i32* %3
br label %51
23: ; preds = %16
br label %24
24: ; preds = %23, %11
%25 = load i32, i32* %5
%26 = load i32, i32* @W_OK
%27 = and i32 %25, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %24
%30 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @S_IWRITE
%33 = and i32 %31, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %29
store i32 -1, i32* %3
br label %51
36: ; preds = %29
br label %37
37: ; preds = %36, %24
%38 = load i32, i32* %5
%39 = load i32, i32* @X_OK
%40 = and i32 %38, %39
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %37
%43 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @S_IEXEC
%46 = and i32 %44, %45
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %42
store i32 -1, i32* %3
br label %51
49: ; preds = %42
br label %50
50: ; preds = %49, %37
store i32 0, i32* %3
br label %51
51: ; preds = %50, %48, %35, %22, %10
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@R_OK = global i32 0
@S_IREAD = global i32 0
@W_OK = global i32 0
@S_IWRITE = global i32 0
@X_OK = global i32 0
@S_IEXEC = global i32 0
define i32 @access(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %38
7: ; preds = %2
%8 = load i32, i32* @R_OK
%9 = and i32 %8, %1
%10 = icmp eq i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @S_IREAD
%15 = and i32 %14, %13
%16 = icmp eq i32 %15, 0
br i1 %16, label %38, label %17
17: ; preds = %11, %7
%18 = load i32, i32* @W_OK
%19 = and i32 %18, %1
%20 = icmp eq i32 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %17
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @S_IWRITE
%25 = and i32 %24, %23
%26 = icmp eq i32 %25, 0
br i1 %26, label %38, label %27
27: ; preds = %21, %17
%28 = load i32, i32* @X_OK
%29 = and i32 %28, %1
%30 = icmp eq i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %27
%32 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%33 = load i32, i32* %32
%34 = load i32, i32* @S_IEXEC
%35 = and i32 %34, %33
%36 = icmp eq i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %31, %27
br label %38
38: ; preds = %31, %21, %11, %2, %37
%39 = phi i32 [ 0, %37 ], [ -1, %2 ], [ -1, %11 ], [ -1, %21 ], [ -1, %31 ]
ret i32 %39
}