#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 AT_SYMLINK_NOFOLLOW;
int chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ dir_mutex_lock(int);
int /*<<< orphan*/ dir_mutex_unlock();
int lchown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group,
             int flags) {
  int rc;
  dir_mutex_lock(dirfd);
  if (flags & AT_SYMLINK_NOFOLLOW) {
    rc = lchown(pathname, owner, group);
  } else {
    rc = chown(pathname, owner, group);
  }
  dir_mutex_unlock();
  return rc;
}


x86 assembly (clang, -O0):
<fchownat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <dir_mutex_lock@plt>
mov    -0x1c(%rbp),%eax
mov    0x2e75(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     118d <fchownat+0x4d>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <lchown@plt>
mov    %eax,-0x20(%rbp)
jmp    119f <fchownat+0x5f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1050 <chown@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <dir_mutex_unlock@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fchownat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8d,%ebp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
call   1030 <dir_mutex_lock@plt>
mov    0x2e81(%rip),%rax
test   %ebp,(%rax)
je     1173 <fchownat+0x33>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1040 <lchown@plt>
jmp    1181 <fchownat+0x41>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1050 <chown@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <dir_mutex_unlock@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @fchownat(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%12 = load i32, i32* %6
%13 = call i32 @dir_mutex_lock(i32 %12)
%14 = load i32, i32* %10
%15 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%16 = and i32 %14, %15
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %5
%19 = load i8*, i8** %7
%20 = load i32, i32* %8
%21 = load i32, i32* %9
%22 = call i32 @lchown(i8* %19, i32 %20, i32 %21)
store i32 %22, i32* %11
br label %28
23: ; preds = %5
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = load i32, i32* %9
%27 = call i32 @chown(i8* %24, i32 %25, i32 %26)
store i32 %27, i32* %11
br label %28
28: ; preds = %23, %18
%29 = call i32 (...) @dir_mutex_unlock()
%30 = load i32, i32* %11
ret i32 %30
}

LLVM IR (-O2):
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @fchownat(i32 %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i32 @dir_mutex_lock(i32 %0)
%7 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%8 = and i32 %7, %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = tail call i32 @lchown(i8* %1, i32 %2, i32 %3)
br label %14
12: ; preds = %5
%13 = tail call i32 @chown(i8* %1, i32 %2, i32 %3)
br label %14
14: ; preds = %12, %10
%15 = phi i32 [ %11, %10 ], [ %13, %12 ]
%16 = tail call i32 (...) @dir_mutex_unlock()
ret i32 %15
}