#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*/ target_path;
typedef int /*<<< orphan*/ source_path;
typedef int /*<<< orphan*/ root_path;
typedef int /*<<< orphan*/ base_path;

/* Variables and functions */
int AT_FDCWD;
int /*<<< orphan*/ basedir(char *, char *, int);
int /*<<< orphan*/ fdpath(int, char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ is_dir(char *);
int /*<<< orphan*/ join_path(char *, char const *, char *, int);
int /*<<< orphan*/ mkdirp(char *);
int /*<<< orphan*/ move(char *, char *);
int /*<<< orphan*/ rmdir(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char const *, int);
int unlinkat(int dirfd, const char *pathname, int flags) {
  char *operation_path, root_path[4 * 4096], source_path[4 * 4096],
      base_path[4 * 4096], target_path[4 * 4096];
  ((void)flags);
  operation_path = getenv("CARELESS_OPERATION_PATH");
  if (!operation_path) {
    fprintf(stderr, "CARELESS_OPERATION_PATH is undefined\n");
    return -1;
  }
  if (*pathname == '/') {
    strncpy(source_path, pathname, sizeof(source_path));
  } else if (AT_FDCWD == dirfd) {
    getcwd(root_path, sizeof(root_path));
    join_path(root_path, pathname, source_path, sizeof(source_path));
  } else {
    fdpath(dirfd, root_path, sizeof(root_path));
    join_path(root_path, pathname, source_path, sizeof(source_path));
  }
  snprintf(target_path, sizeof(target_path), "%s/%s", operation_path,
           source_path);
  basedir(target_path, base_path, sizeof(base_path));
  mkdirp(base_path);
  if (!is_dir(source_path) || !is_dir(target_path)) {
    move(source_path, target_path);
  } else {
    rmdir(source_path);
  }
  return 0;
}


x86 assembly (clang, -O0):
<unlinkat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    0xe24(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1211 <unlinkat+0x51>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   10b0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1350 <unlinkat+0x190>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    123b <unlinkat+0x7b>
lea    -0x8020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x4000,%edx
call   1040 <strncpy@plt>
jmp    12b4 <unlinkat+0xf4>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jne    127f <unlinkat+0xbf>
lea    -0x4020(%rbp),%rdi
mov    $0x4000,%esi
call   10a0 <getcwd@plt>
lea    -0x4020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x8020(%rbp),%rdx
mov    $0x4000,%ecx
call   1080 <join_path@plt>
jmp    12af <unlinkat+0xef>
mov    -0x8(%rbp),%edi
lea    -0x4020(%rbp),%rsi
mov    $0x4000,%edx
call   10e0 <fdpath@plt>
lea    -0x4020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x8020(%rbp),%rdx
mov    $0x4000,%ecx
call   1080 <join_path@plt>
jmp    12b4 <unlinkat+0xf4>
lea    -0x10020(%rbp),%rdi
mov    -0x20(%rbp),%rcx
lea    -0x8020(%rbp),%r8
mov    $0x4000,%esi
lea    0xd6c(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x10020(%rbp),%rdi
lea    -0xc020(%rbp),%rsi
mov    $0x4000,%edx
call   10c0 <basedir@plt>
lea    -0xc020(%rbp),%rdi
call   1090 <mkdirp@plt>
lea    -0x8020(%rbp),%rdi
call   1060 <is_dir@plt>
cmp    $0x0,%eax
je     1325 <unlinkat+0x165>
lea    -0x10020(%rbp),%rdi
call   1060 <is_dir@plt>
cmp    $0x0,%eax
jne    133d <unlinkat+0x17d>
lea    -0x8020(%rbp),%rdi
lea    -0x10020(%rbp),%rsi
call   10d0 <move@plt>
jmp    1349 <unlinkat+0x189>
lea    -0x8020(%rbp),%rdi
call   1050 <rmdir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlinkat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x10008,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe27(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11fd <unlinkat+0x3d>
mov    %rax,%r14
cmpb   $0x2f,(%rbx)
jne    121c <unlinkat+0x5c>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    $0x4000,%edx
call   1040 <strncpy@plt>
jmp    126e <unlinkat+0xae>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12ec <unlinkat+0x12c>
mov    0x2dad(%rip),%rax
cmp    %ebp,(%rax)
jne    1244 <unlinkat+0x84>
lea    0x8000(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x4000,%esi
call   10a0 <getcwd@plt>
mov    %rsp,%rdx
mov    %rbp,%rdi
jmp    1261 <unlinkat+0xa1>
lea    0x8000(%rsp),%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4000,%edx
call   10e0 <fdpath@plt>
mov    %rsp,%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x4000,%ecx
call   1080 <join_path@plt>
lea    0xdc9(%rip),%rdx
lea    0x4000(%rsp),%rbx
mov    %rsp,%r15
mov    %rbx,%rdi
mov    $0x4000,%esi
mov    %r14,%rcx
mov    %r15,%r8
call   1070 <snprintf@plt>
lea    0xc000(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x4000,%edx
call   10c0 <basedir@plt>
mov    %rbp,%rdi
call   1090 <mkdirp@plt>
mov    %r15,%rdi
call   1060 <is_dir@plt>
test   %eax,%eax
je     12da <unlinkat+0x11a>
lea    0x4000(%rsp),%rdi
call   1060 <is_dir@plt>
test   %eax,%eax
je     12da <unlinkat+0x11a>
mov    %rsp,%rdi
call   1050 <rmdir@plt>
jmp    12ea <unlinkat+0x12a>
mov    %rsp,%rdi
lea    0x4000(%rsp),%rsi
call   10d0 <move@plt>
xor    %eax,%eax
add    $0x10008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"CARELESS_OPERATION_PATH\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"CARELESS_OPERATION_PATH is undefined\0A\00"
@AT_FDCWD = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @unlinkat(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [16384 x i8]
%10 = alloca [16384 x i8]
%11 = alloca [16384 x i8]
%12 = alloca [16384 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = load i32, i32* %7
%14 = call i8* @getenv(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %74
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 47
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%27 = load i8*, i8** %6
%28 = call i32 @strncpy(i8* %26, i8* %27, i32 16384)
br label %49
29: ; preds = %20
%30 = load i32, i32* @AT_FDCWD
%31 = load i32, i32* %5
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = getelementptr inbounds [16384 x i8], [16384 x i8]* %9, i64 0, i64 0
%35 = call i32 @getcwd(i8* %34, i32 16384)
%36 = getelementptr inbounds [16384 x i8], [16384 x i8]* %9, i64 0, i64 0
%37 = load i8*, i8** %6
%38 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%39 = call i32 @join_path(i8* %36, i8* %37, i8* %38, i32 16384)
br label %48
40: ; preds = %29
%41 = load i32, i32* %5
%42 = getelementptr inbounds [16384 x i8], [16384 x i8]* %9, i64 0, i64 0
%43 = call i32 @fdpath(i32 %41, i8* %42, i32 16384)
%44 = getelementptr inbounds [16384 x i8], [16384 x i8]* %9, i64 0, i64 0
%45 = load i8*, i8** %6
%46 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%47 = call i32 @join_path(i8* %44, i8* %45, i8* %46, i32 16384)
br label %48
48: ; preds = %40, %33
br label %49
49: ; preds = %48, %25
%50 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%51 = load i8*, i8** %8
%52 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%53 = call i32 @snprintf(i8* %50, i32 16384, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %51, i8* %52)
%54 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%55 = getelementptr inbounds [16384 x i8], [16384 x i8]* %11, i64 0, i64 0
%56 = call i32 @basedir(i8* %54, i8* %55, i32 16384)
%57 = getelementptr inbounds [16384 x i8], [16384 x i8]* %11, i64 0, i64 0
%58 = call i32 @mkdirp(i8* %57)
%59 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%60 = call i32 @is_dir(i8* %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %49
%63 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%64 = call i32 @is_dir(i8* %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %70, label %66
66: ; preds = %62, %49
%67 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%68 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%69 = call i32 @move(i8* %67, i8* %68)
br label %73
70: ; preds = %62
%71 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%72 = call i32 @rmdir(i8* %71)
br label %73
73: ; preds = %70, %66
store i32 0, i32* %4
br label %74
74: ; preds = %73, %17
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@.str = [24 x i8] c"CARELESS_OPERATION_PATH\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"CARELESS_OPERATION_PATH is undefined\0A\00"
@AT_FDCWD = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @unlinkat(i32 %0, i8* %1, i32 %2) {
%4 = alloca [16384 x i8]
%5 = alloca [16384 x i8]
%6 = alloca [16384 x i8]
%7 = alloca [16384 x i8]
%8 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [16384 x i8], [16384 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [16384 x i8], [16384 x i8]* %7, i64 0, i64 0
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %44
17: ; preds = %3
%18 = load i8, i8* %1
%19 = icmp eq i8 %18, 47
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @strncpy(i8* %9, i8* %1, i32 16384)
br label %31
22: ; preds = %17
%23 = load i32, i32* @AT_FDCWD
%24 = icmp eq i32 %23, %0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @getcwd(i8* %8, i32 16384)
%27 = call i32 @join_path(i8* %8, i8* %1, i8* %9, i32 16384)
br label %31
28: ; preds = %22
%29 = call i32 @fdpath(i32 %0, i8* %8, i32 16384)
%30 = call i32 @join_path(i8* %8, i8* %1, i8* %9, i32 16384)
br label %31
31: ; preds = %25, %28, %20
%32 = call i32 @snprintf(i8* %11, i32 16384, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %12, i8* %9)
%33 = call i32 @basedir(i8* %11, i8* %10, i32 16384)
%34 = call i32 @mkdirp(i8* %10)
%35 = call i32 @is_dir(i8* %9)
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %31
%38 = call i32 @is_dir(i8* %11)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37, %31
%41 = call i32 @move(i8* %9, i8* %11)
br label %44
42: ; preds = %37
%43 = call i32 @rmdir(i8* %9)
br label %44
44: ; preds = %40, %42, %14
%45 = phi i32 [ -1, %14 ], [ 0, %42 ], [ 0, %40 ]
ret i32 %45
}