#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 */

/* Variables and functions */
int AT_FDCWD;
int AT_REMOVEDIR;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int);
int fchdir(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int rmdir(char const *);
int unlink(char const *);
char *xgetcwd();
int unlinkat(int dirfd, const char *pathname, int flags) {
  int fchdir_err = 0;
  char *save = xgetcwd();
  if (!save) {
    perror("fd_wrapper_"
           "unlinkat");
    exit(1);
  }
  if (dirfd != AT_FDCWD) {
    fchdir_err = fchdir(dirfd);
    if (fchdir_err == -1)
      perror("fchdir");
  }
  int result;
  if (flags & AT_REMOVEDIR) {
    result = rmdir(pathname);
  } else {
    result = unlink(pathname);
  }
  if (dirfd != AT_FDCWD)
    chdir(save);
  free(save);
  return result;
}


x86 assembly (clang, -O0):
<unlinkat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <xgetcwd@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c5 <unlinkat+0x45>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%eax
je     11fd <unlinkat+0x7d>
mov    -0x4(%rbp),%edi
call   1070 <fchdir@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11f8 <unlinkat+0x78>
lea    0xe21(%rip),%rdi
call   1080 <perror@plt>
jmp    11fd <unlinkat+0x7d>
mov    -0x14(%rbp),%eax
mov    0x2dd9(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1223 <unlinkat+0xa3>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x24(%rbp)
jmp    122f <unlinkat+0xaf>
mov    -0x10(%rbp),%rdi
call   1040 <unlink@plt>
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%eax
je     124a <unlinkat+0xca>
mov    -0x20(%rbp),%rdi
call   1060 <chdir@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlinkat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <xgetcwd@plt>
test   %rax,%rax
je     1210 <unlinkat+0x90>
mov    %rax,%r12
mov    0x2e27(%rip),%r13
cmp    %ebp,0x0(%r13)
je     11bb <unlinkat+0x3b>
mov    %ebp,%edi
call   1070 <fchdir@plt>
cmp    $0xffffffff,%eax
je     1202 <unlinkat+0x82>
mov    0x2e1e(%rip),%rax
test   %r15d,(%rax)
je     11d1 <unlinkat+0x51>
mov    %r14,%rdi
call   1050 <rmdir@plt>
jmp    11d9 <unlinkat+0x59>
mov    %r14,%rdi
call   1040 <unlink@plt>
mov    %eax,%ebx
cmp    %ebp,0x0(%r13)
je     11e9 <unlinkat+0x69>
mov    %r12,%rdi
call   1060 <chdir@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0b(%rip),%rdi
call   1080 <perror@plt>
jmp    11bb <unlinkat+0x3b>
lea    0xde9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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