#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int lstat(char const *, struct stat *);
int open(char *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
int xfstatat(int fd, const char *path, struct stat *buf, int flag) {
  int cfd, error, ret;
  cfd = open(".", O_RDONLY);
  if (cfd == -1)
    return (-1);
  if (fchdir(fd) == -1) {
    error = errno;
    (void)close(cfd);
    errno = error;
    return (-1);
  }
  if (flag == 2)
    ret = lstat(path, buf);
  else
    ret = stat(path, buf);
  error = errno;
  (void)fchdir(cfd);
  (void)close(cfd);
  errno = error;
  return (ret);
}


x86 assembly (clang, -O0):
<xfstatat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1194 <xfstatat+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <xfstatat+0xde>
mov    -0x8(%rbp),%edi
call   1060 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11d1 <xfstatat+0x81>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e15(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <xfstatat+0xde>
cmpl   $0x2,-0x1c(%rbp)
jne    11f0 <xfstatat+0xa0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x28(%rbp)
jmp    1200 <xfstatat+0xb0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x28(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <fchdir@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2db2(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfstatat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     119e <xfstatat+0x4e>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11a5 <xfstatat+0x55>
mov    %r15,%rdi
mov    %r14,%rsi
cmp    $0x2,%r12d
jne    11b6 <xfstatat+0x66>
call   1040 <lstat@plt>
jmp    11bb <xfstatat+0x6b>
mov    $0xffffffff,%ebp
jmp    11df <xfstatat+0x8f>
mov    0x2e2c(%rip),%rax
mov    (%rax),%r14d
mov    $0xffffffff,%ebp
jmp    11ce <xfstatat+0x7e>
call   1050 <stat@plt>
mov    %eax,%ebp
mov    0x2e14(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1060 <fchdir@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2dfc(%rip),%rax
mov    %r14d,(%rax)
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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