#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 EINVAL;
int /*<<< orphan*/ O_RDONLY;
int access(char const *, int);
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int open(char *, int /*<<< orphan*/);
int xfaccessat(int fd, const char *path, int mode, int flag) {
  int cfd, error, ret;
  if (flag == 0x01) {
    errno = EINVAL;
    return (-1);
  }
  cfd = open(".", O_RDONLY);
  if (cfd == -1)
    return (-1);
  if (fchdir(fd) == -1) {
    error = errno;
    (void)close(cfd);
    errno = error;
    return (-1);
  }
  ret = access(path, mode);
  error = errno;
  (void)fchdir(cfd);
  (void)close(cfd);
  errno = error;
  return (ret);
}


x86 assembly (clang, -O0):
<xfaccessat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    117d <xfaccessat+0x3d>
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e69(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <xfaccessat+0xe5>
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ab <xfaccessat+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <xfaccessat+0xe5>
mov    -0x8(%rbp),%edi
call   1040 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11e8 <xfaccessat+0xa8>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2dfe(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <xfaccessat+0xe5>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <access@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <fchdir@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2dbb(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfaccessat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%ecx
jne    1158 <xfaccessat+0x18>
mov    0x2e7c(%rip),%rax
mov    (%rax),%ebp
jmp    11c9 <xfaccessat+0x89>
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b1 <xfaccessat+0x71>
mov    %eax,%r12d
mov    %ebx,%edi
call   1040 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11b8 <xfaccessat+0x78>
mov    %r15,%rdi
mov    %r14d,%esi
call   1060 <access@plt>
mov    %eax,%ebx
mov    0x2e3b(%rip),%rax
mov    (%rax),%ebp
mov    %r12d,%edi
call   1040 <fchdir@plt>
mov    %r12d,%edi
call   1030 <close@plt>
jmp    11ce <xfaccessat+0x8e>
mov    $0xffffffff,%ebx
jmp    11d7 <xfaccessat+0x97>
mov    0x2e19(%rip),%rax
mov    (%rax),%ebp
mov    %r12d,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
mov    0x2e03(%rip),%rax
mov    %ebp,(%rax)
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
define i32 @xfaccessat(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
store i32 -1, i32* %5
br label %43
17: ; preds = %4
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %5
br label %43
23: ; preds = %17
%24 = load i32, i32* %6
%25 = call i32 @fchdir(i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* @errno
store i32 %28, i32* %11
%29 = load i32, i32* %10
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %11
store i32 %31, i32* @errno
store i32 -1, i32* %5
br label %43
32: ; preds = %23
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = call i32 @access(i8* %33, i32 %34)
store i32 %35, i32* %12
%36 = load i32, i32* @errno
store i32 %36, i32* %11
%37 = load i32, i32* %10
%38 = call i32 @fchdir(i32 %37)
%39 = load i32, i32* %10
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %11
store i32 %41, i32* @errno
%42 = load i32, i32* %12
store i32 %42, i32* %5
br label %43
43: ; preds = %32, %27, %22, %15
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
define i32 @xfaccessat(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 1
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = load i32, i32* @EINVAL
br label %23
8: ; preds = %4
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %26, label %12
12: ; preds = %8
%13 = tail call i32 @fchdir(i32 %0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 @close(i32 %10)
br label %23
18: ; preds = %12
%19 = tail call i32 @access(i8* %1, i32 %2)
%20 = load i32, i32* @errno
%21 = tail call i32 @fchdir(i32 %10)
%22 = tail call i32 @close(i32 %10)
br label %23
23: ; preds = %6, %15, %18
%24 = phi i32 [ %20, %18 ], [ %16, %15 ], [ %7, %6 ]
%25 = phi i32 [ %19, %18 ], [ -1, %15 ], [ -1, %6 ]
store i32 %24, i32* @errno
br label %26
26: ; preds = %23, %8
%27 = phi i32 [ -1, %8 ], [ %25, %23 ]
ret i32 %27
}