#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*/ EACCES;
int /*<<< orphan*/ ENOENT;
size_t O_RDONLY;
size_t O_RDWR;
size_t O_WRONLY;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char const *, size_t);
int access(const char *file, int mode) {
  size_t m = 0;
  switch (mode) {
  case 0:
  case 4:
    m |= O_RDONLY;
    break;
  case 2:
    m |= O_WRONLY;
    break;
  case 6:
    m |= O_RDWR;
    break;
  }
  int fd = open(file, m);
  if (fd == -1) {
    if (m != O_RDONLY) {
      fd = open(file, O_RDONLY);
      if (fd != -1) {
        __set_errno(EACCES);
        close(fd);
        return -1;
      }
      __set_errno(ENOENT);
      return -1;
    }
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<access>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %rax,-0x30(%rbp)
sub    $0x6,%rax
ja     11ac <access+0x7c>
mov    -0x30(%rbp),%rax
lea    0xe9d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ac <access+0x7c>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ac <access+0x7c>
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1242 <access+0x112>
mov    -0x20(%rbp),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
je     1236 <access+0x106>
mov    -0x10(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%rsi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     121c <access+0xec>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <access+0x121>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <access+0x121>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <access+0x121>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<access>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
cmp    $0x6,%esi
ja     116c <access+0x3c>
mov    %esi,%eax
lea    0xeb9(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2e89(%rip),%rax
jmp    1169 <access+0x39>
mov    0x2e58(%rip),%rax
jmp    1169 <access+0x39>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1187 <access+0x57>
mov    %eax,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
jmp    11cf <access+0x9f>
mov    0x2e52(%rip),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%ebp
cmp    %rsi,%rbx
je     11cf <access+0x9f>
mov    %r14,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11c1 <access+0x91>
mov    %eax,%ebx
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11cf <access+0x9f>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i64 0
@O_WRONLY = global i64 0
@O_RDWR = global i64 0
@EACCES = global i32 0
@ENOENT = global i32 0
define i32 @access(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 0, i64* %6
%8 = load i32, i32* %5
switch i32 %8, label %21 [
i32 0, label %9
i32 4, label %9
i32 2, label %13
i32 6, label %17
]
9: ; preds = %2, %2
%10 = load i64, i64* @O_RDONLY
%11 = load i64, i64* %6
%12 = or i64 %11, %10
store i64 %12, i64* %6
br label %21
13: ; preds = %2
%14 = load i64, i64* @O_WRONLY
%15 = load i64, i64* %6
%16 = or i64 %15, %14
store i64 %16, i64* %6
br label %21
17: ; preds = %2
%18 = load i64, i64* @O_RDWR
%19 = load i64, i64* %6
%20 = or i64 %19, %18
store i64 %20, i64* %6
br label %21
21: ; preds = %2, %17, %13, %9
%22 = load i8*, i8** %4
%23 = load i64, i64* %6
%24 = call i32 @open(i8* %22, i64 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %46
27: ; preds = %21
%28 = load i64, i64* %6
%29 = load i64, i64* @O_RDONLY
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %45
31: ; preds = %27
%32 = load i8*, i8** %4
%33 = load i64, i64* @O_RDONLY
%34 = call i32 @open(i8* %32, i64 %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %31
%38 = load i32, i32* @EACCES
%39 = call i32 @__set_errno(i32 %38)
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %3
br label %49
42: ; preds = %31
%43 = load i32, i32* @ENOENT
%44 = call i32 @__set_errno(i32 %43)
store i32 -1, i32* %3
br label %49
45: ; preds = %27
store i32 -1, i32* %3
br label %49
46: ; preds = %21
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
store i32 0, i32* %3
br label %49
49: ; preds = %46, %45, %42, %37
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@O_RDONLY = global i64 0
@O_WRONLY = global i64 0
@O_RDWR = global i64 0
@EACCES = global i32 0
@ENOENT = global i32 0
define i32 @access(i8* nocapture %0, i32 %1) {
switch i32 %1, label %9 [
i32 0, label %3
i32 4, label %3
i32 2, label %5
i32 6, label %7
]
3: ; preds = %2, %2
%4 = load i64, i64* @O_RDONLY
br label %9
5: ; preds = %2
%6 = load i64, i64* @O_WRONLY
br label %9
7: ; preds = %2
%8 = load i64, i64* @O_RDWR
br label %9
9: ; preds = %2, %7, %5, %3
%10 = phi i64 [ 0, %2 ], [ %8, %7 ], [ %6, %5 ], [ %4, %3 ]
%11 = tail call i32 @open(i8* %0, i64 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %26
13: ; preds = %9
%14 = load i64, i64* @O_RDONLY
%15 = icmp eq i64 %10, %14
br i1 %15, label %28, label %16
16: ; preds = %13
%17 = tail call i32 @open(i8* %0, i64 %14)
%18 = icmp eq i32 %17, -1
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = load i32, i32* @EACCES
%21 = tail call i32 @__set_errno(i32 %20)
%22 = tail call i32 @close(i32 %17)
br label %28
23: ; preds = %16
%24 = load i32, i32* @ENOENT
%25 = tail call i32 @__set_errno(i32 %24)
br label %28
26: ; preds = %9
%27 = tail call i32 @close(i32 %11)
br label %28
28: ; preds = %13, %26, %23, %19
%29 = phi i32 [ -1, %19 ], [ -1, %23 ], [ 0, %26 ], [ -1, %13 ]
ret i32 %29
}