#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 AT_FDCWD;
int AT_SYMLINK_NOFOLLOW;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int);
int fchdir(int);
int /*<<< orphan*/ free(char *);
int lstat(char const *, struct stat *);
int /*<<< orphan*/ perror(char *);
int stat(char const *, struct stat *);
char *xgetcwd();
int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags) {
  int fchdir_err = 0;
  char *save = xgetcwd();
  if (!save) {
    perror("fd_wrapper_"
           "fstatat");
    exit(1);
  }
  if (dirfd != AT_FDCWD) {
    fchdir_err = fchdir(dirfd);
    if (fchdir_err == -1)
      perror("fchdir");
  }
  int result;
  if (flags & AT_SYMLINK_NOFOLLOW)
    result = lstat(pathname, buf);
  else
    result = stat(pathname, buf);
  if (dirfd != AT_FDCWD)
    chdir(save);
  free(save);
  return result;
}


x86 assembly (clang, -O0):
<fstatat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   10a0 <xgetcwd@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c9 <fstatat+0x49>
lea    0xe46(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%eax
je     1201 <fstatat+0x81>
mov    -0x4(%rbp),%edi
call   1070 <fchdir@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11fc <fstatat+0x7c>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
jmp    1201 <fstatat+0x81>
mov    -0x1c(%rbp),%eax
mov    0x2dd5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     122b <fstatat+0xab>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
mov    %eax,-0x2c(%rbp)
jmp    123b <fstatat+0xbb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%eax
je     1256 <fstatat+0xd6>
mov    -0x28(%rbp),%rdi
call   1040 <chdir@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fstatat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <xgetcwd@plt>
test   %rax,%rax
je     121c <fstatat+0x9c>
mov    %rax,%r13
mov    0x2e24(%rip),%rax
cmp    %ebp,(%rax)
je     11bc <fstatat+0x3c>
mov    %ebp,%edi
call   1070 <fchdir@plt>
cmp    $0xffffffff,%eax
je     120e <fstatat+0x8e>
mov    0x2e1d(%rip),%rax
test   %r12d,(%rax)
je     11d5 <fstatat+0x55>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <lstat@plt>
jmp    11e0 <fstatat+0x60>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <stat@plt>
mov    %eax,%ebx
mov    0x2de7(%rip),%rax
cmp    %ebp,(%rax)
je     11f5 <fstatat+0x75>
mov    %r13,%rdi
call   1040 <chdir@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
jmp    11bc <fstatat+0x3c>
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [19 x i8] c"fd_wrapper_fstatat\00"
@AT_FDCWD = global i32 0
@.str.1 = [7 x i8] c"fchdir\00"
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @fstatat(i32 %0, i8* %1, %struct.stat* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.stat*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store %struct.stat* %2, %struct.stat** %7
store i32 %3, i32* %8
store i32 0, i32* %9
%12 = call i8* (...) @xgetcwd()
store i8* %12, i8** %10
%13 = load i8*, i8** %10
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %4
%16 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %4
%19 = load i32, i32* %5
%20 = load i32, i32* @AT_FDCWD
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = load i32, i32* %5
%24 = call i32 @fchdir(i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
br label %30
30: ; preds = %29, %18
%31 = load i32, i32* %8
%32 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%33 = and i32 %31, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i8*, i8** %6
%37 = load %struct.stat*, %struct.stat** %7
%38 = call i32 @lstat(i8* %36, %struct.stat* %37)
store i32 %38, i32* %11
br label %43
39: ; preds = %30
%40 = load i8*, i8** %6
%41 = load %struct.stat*, %struct.stat** %7
%42 = call i32 @stat(i8* %40, %struct.stat* %41)
store i32 %42, i32* %11
br label %43
43: ; preds = %39, %35
%44 = load i32, i32* %5
%45 = load i32, i32* @AT_FDCWD
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i8*, i8** %10
%49 = call i32 @chdir(i8* %48)
br label %50
50: ; preds = %47, %43
%51 = load i8*, i8** %10
%52 = call i32 @free(i8* %51)
%53 = load i32, i32* %11
ret i32 %53
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [19 x i8] c"fd_wrapper_fstatat\00"
@AT_FDCWD = global i32 0
@.str.1 = [7 x i8] c"fchdir\00"
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @fstatat(i32 %0, i8* nocapture %1, %struct.stat* nocapture %2, i32 %3) {
%5 = tail call i8* (...) @xgetcwd()
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %4
%11 = load i32, i32* @AT_FDCWD
%12 = icmp eq i32 %11, %0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = tail call i32 @fchdir(i32 %0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %13, %16, %10
%19 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%20 = and i32 %19, %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 @lstat(i8* %1, %struct.stat* %2)
br label %26
24: ; preds = %18
%25 = tail call i32 @stat(i8* %1, %struct.stat* %2)
br label %26
26: ; preds = %24, %22
%27 = phi i32 [ %23, %22 ], [ %25, %24 ]
%28 = load i32, i32* @AT_FDCWD
%29 = icmp eq i32 %28, %0
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = tail call i32 @chdir(i8* %5)
br label %32
32: ; preds = %30, %26
%33 = tail call i32 @free(i8* %5)
ret i32 %27
}