#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 /*<<< orphan*/ O_RDONLY;
int chmod(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int open(char *, int /*<<< orphan*/);
int xfchmodat(int dirfd, const char *pathname, mode_t mode, int flags) {
  (void)flags;
  int cfd, ret, error;
  cfd = open(".", O_RDONLY);
  if (cfd == -1)
    return (-1);
  if (fchdir(dirfd) == -1) {
    error = errno;
    (void)close(cfd);
    errno = error;
    return (-1);
  }
  ret = chmod(pathname, mode);
  error = errno;
  (void)fchdir(cfd);
  (void)close(cfd);
  errno = error;
  return ret;
}


x86 assembly (clang, -O0):
<xfchmodat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1183 <xfchmodat+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <xfchmodat+0xbd>
mov    -0x8(%rbp),%edi
call   1040 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11c0 <xfchmodat+0x80>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e26(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <xfchmodat+0xbd>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <chmod@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <fchdir@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2de3(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfchmodat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1197 <xfchmodat+0x57>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <fchdir@plt>
cmp    $0xffffffff,%eax
je     119e <xfchmodat+0x5e>
mov    %r15,%rdi
mov    %r14d,%esi
call   1050 <chmod@plt>
mov    %eax,%ebp
mov    0x2e4d(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1040 <fchdir@plt>
jmp    11ad <xfchmodat+0x6d>
mov    $0xffffffff,%ebp
jmp    11be <xfchmodat+0x7e>
mov    0x2e33(%rip),%rax
mov    (%rax),%r14d
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2e1d(%rip),%rax
mov    %r14d,(%rax)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @xfchmodat(i32 %0, i8* nocapture %1, i32 %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 %21, 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 %17
13: ; preds = %8
%14 = tail call i32 @chmod(i8* %1, i32 %2)
%15 = load i32, i32* @errno
%16 = tail call i32 @fchdir(i32 %6)
br label %17
17: ; preds = %11, %13
%18 = phi i32 [ %15, %13 ], [ %12, %11 ]
%19 = phi i32 [ %14, %13 ], [ -1, %11 ]
%20 = tail call i32 @close(i32 %6)
store i32 %18, i32* @errno
br label %21
21: ; preds = %17, %4
%22 = phi i32 [ -1, %4 ], [ %19, %17 ]
ret i32 %22
}