#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 S_IFDIR;
int S_IWRITE;
int W_OK;
scalar_t__ stat(char const *, struct stat *);
int access(const char *fn, int flags) {
  struct stat s;
  if (stat(fn, &s))
    return -1;
  if (s.st_mode & S_IFDIR)
    return 0;
  if (flags & W_OK) {
    if (s.st_mode & S_IWRITE)
      return 0;
    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    11ac <access+0x9c>
mov    -0x18(%rbp),%eax
mov    0x2e7c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1163 <access+0x53>
movl   $0x0,-0x4(%rbp)
jmp    11ac <access+0x9c>
mov    -0x14(%rbp),%eax
mov    0x2e73(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11a5 <access+0x95>
mov    -0x18(%rbp),%eax
mov    0x2e66(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1199 <access+0x89>
movl   $0x0,-0x4(%rbp)
jmp    11ac <access+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <access+0x9c>
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    1158 <access+0x48>
mov    0x8(%rsp),%ecx
mov    0x2e8f(%rip),%rdx
xor    %eax,%eax
test   %ecx,(%rdx)
jne    1158 <access+0x48>
mov    0x2e9a(%rip),%rdx
test   %ebx,(%rdx)
je     1158 <access+0x48>
mov    0x2e97(%rip),%rax
and    (%rax),%ecx
cmp    $0x1,%ecx
sbb    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFDIR = global i32 0
@W_OK = global i32 0
@S_IWRITE = 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 %32
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @S_IFDIR
%15 = and i32 %13, %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %11
store i32 0, i32* %3
br label %32
18: ; preds = %11
%19 = load i32, i32* %5
%20 = load i32, i32* @W_OK
%21 = and i32 %19, %20
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @S_IWRITE
%27 = and i32 %25, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 0, i32* %3
br label %32
30: ; preds = %23
store i32 -1, i32* %3
br label %32
31: ; preds = %18
store i32 0, i32* %3
br label %32
32: ; preds = %31, %30, %29, %17, %10
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFDIR = global i32 0
@W_OK = global i32 0
@S_IWRITE = 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 %22
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @S_IFDIR
%11 = and i32 %10, %9
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %7
%14 = load i32, i32* @W_OK
%15 = and i32 %14, %1
%16 = icmp eq i32 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = load i32, i32* @S_IWRITE
%19 = and i32 %18, %9
%20 = icmp eq i32 %19, 0
%21 = sext i1 %20 to i32
br label %22
22: ; preds = %13, %17, %7, %2
%23 = phi i32 [ -1, %2 ], [ 0, %7 ], [ %21, %17 ], [ 0, %13 ]
ret i32 %23
}