#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 */
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int AT_FDCWD;
int /*<<< orphan*/ chdir(char *);
int chmod(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fchdir(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
char *xgetcwd();
int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags) {
  int fchdir_err = 0;
  char *save = xgetcwd();
  if (!save) {
    perror("fd_wrapper_"
           "fchmodat");
    exit(1);
  }
  if (dirfd != AT_FDCWD) {
    fchdir_err = fchdir(dirfd);
    if (fchdir_err == -1)
      perror("fchdir");
  }
  int result = chmod(pathname, mode);
  if (dirfd != AT_FDCWD)
    chdir(save);
  free(save);
  return result;
}


x86 assembly (clang, -O0):
<fchmodat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <xgetcwd@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <fchmodat+0x48>
lea    0xe57(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%eax
je     11f0 <fchmodat+0x80>
mov    -0x4(%rbp),%edi
call   1050 <fchdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11eb <fchmodat+0x7b>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
jmp    11f0 <fchmodat+0x80>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <chmod@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%eax
je     121a <fchmodat+0xaa>
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):
<fchmodat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <xgetcwd@plt>
test   %rax,%rax
je     11e6 <fchmodat+0x76>
mov    %rax,%rbx
mov    0x2e42(%rip),%r12
cmp    %ebp,(%r12)
je     11a8 <fchmodat+0x38>
mov    %ebp,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11d8 <fchmodat+0x68>
mov    %r15,%rdi
mov    %r14d,%esi
call   1060 <chmod@plt>
mov    %eax,%r14d
cmp    %ebp,(%r12)
je     11c4 <fchmodat+0x54>
mov    %rbx,%rdi
call   1040 <chdir@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
jmp    11a8 <fchmodat+0x38>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"fd_wrapper_fchmodat\00"
@AT_FDCWD = global i32 0
@.str.1 = [7 x i8] c"fchdir\00"
define i32 @fchmodat(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %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 ([20 x i8], [20 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 i8*, i8** %6
%32 = load i32, i32* %7
%33 = call i32 @chmod(i8* %31, i32 %32)
store i32 %33, i32* %11
%34 = load i32, i32* %5
%35 = load i32, i32* @AT_FDCWD
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = load i8*, i8** %10
%39 = call i32 @chdir(i8* %38)
br label %40
40: ; preds = %37, %30
%41 = load i8*, i8** %10
%42 = call i32 @free(i8* %41)
%43 = load i32, i32* %11
ret i32 %43
}

LLVM IR (-O2):
@.str = [20 x i8] c"fd_wrapper_fchmodat\00"
@AT_FDCWD = global i32 0
@.str.1 = [7 x i8] c"fchdir\00"
define i32 @fchmodat(i32 %0, i8* nocapture %1, i32 %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 ([20 x i8], [20 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 = tail call i32 @chmod(i8* %1, i32 %2)
%20 = load i32, i32* @AT_FDCWD
%21 = icmp eq i32 %20, %0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 @chdir(i8* %5)
br label %24
24: ; preds = %22, %18
%25 = tail call i32 @free(i8* %5)
ret i32 %19
}