#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 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;
  rc = dir_mutex_lock(dirfd);
  if (rc < 0)
    return rc;
  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,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <dir_mutex_lock@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1179 <fchownat+0x39>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c4 <fchownat+0x84>
mov    -0x1c(%rbp),%eax
mov    0x2e5d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11a5 <fchownat+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <lchown@plt>
mov    %eax,-0x20(%rbp)
jmp    11b7 <fchownat+0x77>
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
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fchownat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%ebp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%r12
call   1030 <dir_mutex_lock@plt>
test   %eax,%eax
js     1178 <fchownat+0x38>
mov    0x2e7c(%rip),%rax
test   %ebp,(%rax)
je     117c <fchownat+0x3c>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1040 <lchown@plt>
jmp    118a <fchownat+0x4a>
mov    %eax,%ebx
jmp    1193 <fchownat+0x53>
mov    %r12,%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
pop    %rbx
pop    %r12
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 i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = 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
%13 = load i32, i32* %7
%14 = call i32 @dir_mutex_lock(i32 %13)
store i32 %14, i32* %12
%15 = load i32, i32* %12
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = load i32, i32* %12
store i32 %18, i32* %6
br label %37
19: ; preds = %5
%20 = load i32, i32* %11
%21 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%22 = and i32 %20, %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i8*, i8** %8
%26 = load i32, i32* %9
%27 = load i32, i32* %10
%28 = call i32 @lchown(i8* %25, i32 %26, i32 %27)
store i32 %28, i32* %12
br label %34
29: ; preds = %19
%30 = load i8*, i8** %8
%31 = load i32, i32* %9
%32 = load i32, i32* %10
%33 = call i32 @chown(i8* %30, i32 %31, i32 %32)
store i32 %33, i32* %12
br label %34
34: ; preds = %29, %24
%35 = call i32 (...) @dir_mutex_unlock()
%36 = load i32, i32* %12
store i32 %36, i32* %6
br label %37
37: ; preds = %34, %17
%38 = load i32, i32* %6
ret i32 %38
}

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 = icmp slt i32 %6, 0
br i1 %7, label %19, label %8
8: ; preds = %5
%9 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%10 = and i32 %9, %4
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @lchown(i8* %1, i32 %2, i32 %3)
br label %16
14: ; preds = %8
%15 = tail call i32 @chown(i8* %1, i32 %2, i32 %3)
br label %16
16: ; preds = %14, %12
%17 = phi i32 [ %13, %12 ], [ %15, %14 ]
%18 = tail call i32 (...) @dir_mutex_unlock()
br label %19
19: ; preds = %5, %16
%20 = phi i32 [ %17, %16 ], [ %6, %5 ]
ret i32 %20
}