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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int lchown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int xfchownat(int dirfd, const char *pathname, uid_t owner, gid_t group,
              int 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);
  }
  if (flags & 2)
    ret = lchown(pathname, owner, group);
  else
    ret = chown(pathname, owner, group);
  error = errno;
  (void)fchdir(cfd);
  (void)close(cfd);
  errno = error;
  return ret;
}


x86 assembly (clang, -O0):
<xfchownat>:
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    %r8d,-0x1c(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1197 <xfchownat+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <xfchownat+0xea>
mov    -0x8(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11d4 <xfchownat+0x84>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%ecx
mov    0x2e12(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <xfchownat+0xea>
mov    -0x1c(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11fa <xfchownat+0xaa>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <lchown@plt>
mov    %eax,-0x24(%rbp)
jmp    120c <xfchownat+0xbc>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1060 <chown@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <fchdir@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%ecx
mov    0x2da6(%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):
<xfchownat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r13d
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11a7 <xfchownat+0x57>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11ae <xfchownat+0x5e>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
test   $0x2,%r13b
jne    11bf <xfchownat+0x6f>
call   1060 <chown@plt>
jmp    11c4 <xfchownat+0x74>
mov    $0xffffffff,%ebp
jmp    11e8 <xfchownat+0x98>
mov    0x2e23(%rip),%rax
mov    (%rax),%r14d
mov    $0xffffffff,%ebp
jmp    11d7 <xfchownat+0x87>
call   1040 <lchown@plt>
mov    %eax,%ebp
mov    0x2e0b(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1050 <fchdir@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2df3(%rip),%rax
mov    %r14d,(%rax)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 @xfchownat(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %12
%17 = load i32, i32* %12
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %5
store i32 -1, i32* %6
br label %51
20: ; preds = %5
%21 = load i32, i32* %7
%22 = call i32 @fchdir(i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @errno
store i32 %25, i32* %14
%26 = load i32, i32* %12
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %14
store i32 %28, i32* @errno
store i32 -1, i32* %6
br label %51
29: ; preds = %20
%30 = load i32, i32* %11
%31 = and i32 %30, 2
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i8*, i8** %8
%35 = load i32, i32* %9
%36 = load i32, i32* %10
%37 = call i32 @lchown(i8* %34, i32 %35, i32 %36)
store i32 %37, i32* %13
br label %43
38: ; preds = %29
%39 = load i8*, i8** %8
%40 = load i32, i32* %9
%41 = load i32, i32* %10
%42 = call i32 @chown(i8* %39, i32 %40, i32 %41)
store i32 %42, i32* %13
br label %43
43: ; preds = %38, %33
%44 = load i32, i32* @errno
store i32 %44, i32* %14
%45 = load i32, i32* %12
%46 = call i32 @fchdir(i32 %45)
%47 = load i32, i32* %12
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %14
store i32 %49, i32* @errno
%50 = load i32, i32* %13
store i32 %50, i32* %6
br label %51
51: ; preds = %43, %24, %19
%52 = load i32, i32* %6
ret i32 %52
}

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