#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_REMOVEDIR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int open(char *, int /*<<< orphan*/);
int rmdir(char const *);
int unlink(char const *);
int xunlinkat(int dirfd, const char *pathname, 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 & AT_REMOVEDIR)
    ret = rmdir(pathname);
  else
    ret = unlink(pathname);
  error = errno;
  (void)fchdir(cfd);
  (void)close(cfd);
  errno = error;
  return ret;
}


x86 assembly (clang, -O0):
<xunlinkat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1190 <xunlinkat+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    122d <xunlinkat+0xdd>
mov    -0x8(%rbp),%edi
call   1060 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11cd <xunlinkat+0x7d>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2e11(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    122d <xunlinkat+0xdd>
mov    -0x14(%rbp),%eax
mov    0x2e01(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f3 <xunlinkat+0xa3>
mov    -0x10(%rbp),%rdi
call   1040 <rmdir@plt>
mov    %eax,-0x1c(%rbp)
jmp    11ff <xunlinkat+0xaf>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <fchdir@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2dab(%rip),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xunlinkat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     119d <xunlinkat+0x4d>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11a4 <xunlinkat+0x54>
mov    0x2e4a(%rip),%rax
test   %r15d,(%rax)
je     11b5 <xunlinkat+0x65>
mov    %r14,%rdi
call   1040 <rmdir@plt>
jmp    11bd <xunlinkat+0x6d>
mov    $0xffffffff,%ebp
jmp    11e1 <xunlinkat+0x91>
mov    0x2e25(%rip),%rax
mov    (%rax),%r14d
mov    $0xffffffff,%ebp
jmp    11d0 <xunlinkat+0x80>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %eax,%ebp
mov    0x2e0a(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1060 <fchdir@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2df2(%rip),%rax
mov    %r14d,(%rax)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
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
@AT_REMOVEDIR = global i32 0
define i32 @xunlinkat(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %44
16: ; preds = %3
%17 = load i32, i32* %5
%18 = call i32 @fchdir(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i32, i32* @errno
store i32 %21, i32* %10
%22 = load i32, i32* %8
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %10
store i32 %24, i32* @errno
store i32 -1, i32* %4
br label %44
25: ; preds = %16
%26 = load i32, i32* %7
%27 = load i32, i32* @AT_REMOVEDIR
%28 = and i32 %26, %27
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i8*, i8** %6
%32 = call i32 @rmdir(i8* %31)
store i32 %32, i32* %9
br label %36
33: ; preds = %25
%34 = load i8*, i8** %6
%35 = call i32 @unlink(i8* %34)
store i32 %35, i32* %9
br label %36
36: ; preds = %33, %30
%37 = load i32, i32* @errno
store i32 %37, i32* %10
%38 = load i32, i32* %8
%39 = call i32 @fchdir(i32 %38)
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %10
store i32 %42, i32* @errno
%43 = load i32, i32* %9
store i32 %43, i32* %4
br label %44
44: ; preds = %36, %20, %15
%45 = load i32, i32* %4
ret i32 %45
}

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