#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_FDCWD;
int /*<<< orphan*/ chdir(char *);
int chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fchdir(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
char *xgetcwd();
int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group,
             int flags) {
  int fchdir_err = 0;
  char *save = xgetcwd();
  if (!save) {
    perror("fd_wrapper_"
           "fchownat");
    exit(1);
  }
  if (dirfd != AT_FDCWD) {
    fchdir_err = fchdir(dirfd);
    if (fchdir_err == -1)
      perror("fchdir");
  }
  int result = chown(pathname, owner, group);
  if (dirfd != AT_FDCWD)
    chdir(save);
  free(save);
  return result;
}


x86 assembly (clang, -O0):
<fchownat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1090 <xgetcwd@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bc <fchownat+0x4c>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%eax
je     11f4 <fchownat+0x84>
mov    -0x4(%rbp),%edi
call   1050 <fchdir@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11ef <fchownat+0x7f>
lea    0xe2a(%rip),%rdi
call   1070 <perror@plt>
jmp    11f4 <fchownat+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1060 <chown@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%eax
je     1221 <fchownat+0xb1>
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):
<fchownat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <xgetcwd@plt>
test   %rax,%rax
je     11f5 <fchownat+0x85>
mov    %rax,%rbx
mov    0x2e3c(%rip),%r13
cmp    %ebp,0x0(%r13)
je     11ae <fchownat+0x3e>
mov    %ebp,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11e7 <fchownat+0x77>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1060 <chown@plt>
mov    %eax,%r14d
cmp    %ebp,0x0(%r13)
je     11cd <fchownat+0x5d>
mov    %rbx,%rdi
call   1040 <chdir@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
jmp    11ae <fchownat+0x3e>
lea    0xe04(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"fd_wrapper_fchownat\00"
@AT_FDCWD = global i32 0
@.str.1 = [7 x i8] c"fchdir\00"
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
%12 = alloca i8*
%13 = 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
store i32 0, i32* %11
%14 = call i8* (...) @xgetcwd()
store i8* %14, i8** %12
%15 = load i8*, i8** %12
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %5
%18 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %5
%21 = load i32, i32* %6
%22 = load i32, i32* @AT_FDCWD
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i32, i32* %6
%26 = call i32 @fchdir(i32 %25)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
br label %32
32: ; preds = %31, %20
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = load i32, i32* %9
%36 = call i32 @chown(i8* %33, i32 %34, i32 %35)
store i32 %36, i32* %13
%37 = load i32, i32* %6
%38 = load i32, i32* @AT_FDCWD
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i8*, i8** %12
%42 = call i32 @chdir(i8* %41)
br label %43
43: ; preds = %40, %32
%44 = load i8*, i8** %12
%45 = call i32 @free(i8* %44)
%46 = load i32, i32* %13
ret i32 %46
}

LLVM IR (-O2):
@.str = [20 x i8] c"fd_wrapper_fchownat\00"
@AT_FDCWD = global i32 0
@.str.1 = [7 x i8] c"fchdir\00"
define i32 @fchownat(i32 %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i8* (...) @xgetcwd()
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %5
%12 = load i32, i32* @AT_FDCWD
%13 = icmp eq i32 %12, %0
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = tail call i32 @fchdir(i32 %0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %14, %17, %11
%20 = tail call i32 @chown(i8* %1, i32 %2, i32 %3)
%21 = load i32, i32* @AT_FDCWD
%22 = icmp eq i32 %21, %0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @chdir(i8* %6)
br label %25
25: ; preds = %23, %19
%26 = tail call i32 @free(i8* %6)
ret i32 %20
}