#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 {
  scalar_t__ st_uid;
  int st_mode;
  scalar_t__ st_gid;
};
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ EACCES;
int /*<<< orphan*/ errno;
scalar_t__ getegid();
scalar_t__ geteuid();
scalar_t__ stat(char const *, struct stat *);
int access(const char *path, int amode) {
  struct stat st;
  if (stat(path, &st) != 0) {
    return -1;
  };
  mode_t amask = (mode_t)amode & 0777;
  mode_t rmask;
  if (st.st_uid == geteuid()) {
    rmask = (st.st_mode >> 6) & 7;
  } else if (st.st_gid == getegid()) {
    rmask = (st.st_mode >> 3) & 7;
  } else {
    rmask = (st.st_mode) & 7;
  };
  if ((rmask & amask) == amask) {
    return 0;
  };
  errno = EACCES;
  return -1;
}


x86 assembly (clang, -O0):
<access>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1162 <access+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <access+0xdf>
mov    -0x14(%rbp),%eax
and    $0x1ff,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jne    119d <access+0x6d>
mov    -0x28(%rbp),%eax
sar    $0x6,%eax
and    $0x7,%eax
mov    %eax,-0x38(%rbp)
jmp    11db <access+0xab>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <getegid@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
jne    11cd <access+0x9d>
mov    -0x28(%rbp),%eax
sar    $0x3,%eax
and    $0x7,%eax
mov    %eax,-0x38(%rbp)
jmp    11d6 <access+0xa6>
mov    -0x28(%rbp),%eax
and    $0x7,%eax
mov    %eax,-0x38(%rbp)
jmp    11db <access+0xab>
mov    -0x38(%rbp),%eax
and    -0x34(%rbp),%eax
cmp    -0x34(%rbp),%eax
jne    11f6 <access+0xc6>
movl   $0x0,-0x4(%rbp)
jmp    120f <access+0xdf>
mov    0x2deb(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<access>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11a8 <access+0x78>
mov    0x8(%rsp),%rbp
xor    %eax,%eax
call   1030 <geteuid@plt>
cmp    %rax,%rbp
jne    1169 <access+0x39>
mov    0x10(%rsp),%ecx
sar    $0x6,%ecx
jmp    1184 <access+0x54>
mov    0x18(%rsp),%rbp
xor    %eax,%eax
call   1050 <getegid@plt>
mov    0x10(%rsp),%edx
mov    %edx,%ecx
sar    $0x3,%ecx
cmp    %rax,%rbp
cmovne %edx,%ecx
and    $0x7,%ecx
xor    $0x1ff,%ecx
test   %r14d,%ecx
je     11a6 <access+0x76>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    0x2e36(%rip),%rcx
mov    %eax,(%rcx)
jmp    11a8 <access+0x78>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i64, i32, i64 }
@EACCES = global i32 0
@errno = 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 %33
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i64, i64* %8
%10 = call i64 (...) @geteuid()
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = ashr i32 %14, 6
br label %25
16: ; preds = %7
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%18 = load i64, i64* %17
%19 = call i64 (...) @getegid()
%20 = icmp eq i64 %18, %19
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = ashr i32 %22, 3
%24 = select i1 %20, i32 %23, i32 %22
br label %25
25: ; preds = %16, %12
%26 = phi i32 [ %15, %12 ], [ %24, %16 ]
%27 = and i32 %26, 7
%28 = xor i32 %27, 511
%29 = and i32 %28, %1
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = load i32, i32* @EACCES
store i32 %32, i32* @errno
br label %33
33: ; preds = %31, %25, %2
%34 = phi i32 [ -1, %2 ], [ -1, %31 ], [ 0, %25 ]
ret i32 %34
}