#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 */
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ AT_REMOVEDIR;
#define EISDIR 130
#define ENOENT 129
#define EPERM 128
int /*<<< orphan*/ O_DIRECTORY;
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ *fdopendir(int);
int openat(int, char const *, int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ unlinkat(int, char const *, int /*<<< orphan*/);
int rmrfat(int dd, const char *path) {
  if (!unlinkat(dd, path, 0)) {
    return 0;
  } else {
    struct dirent *de;
    DIR *d;
    int fd;
    switch (errno) {
    case ENOENT:
      return 0;
    case EPERM:
    case EISDIR:
      break;
    default:
      return 0;
    }
    fd = openat(dd, path, O_DIRECTORY);
    d = fdopendir(fd);
    if (!d) {
      return 0;
    }
    for (de = readdir(d); de != NULL; de = readdir(d)) {
      if (strcmp(de->d_name, "..") != 0 && strcmp(de->d_name, ".") != 0) {
        char name[PATH_MAX];
        snprintf(name, PATH_MAX, "%s/%s", path, de->d_name);
        rmrfat(dd, name);
      }
    }
    closedir(d);
    unlinkat(dd, path, AT_REMOVEDIR);
  }
  return 0;
}


x86 assembly (clang, -O0):
<rmrfat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1030 <unlinkat@plt>
cmp    $0x0,%eax
jne    11b2 <rmrfat+0x32>
movl   $0x0,-0x4(%rbp)
jmp    1339 <rmrfat+0x1b9>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x80,%eax
je     1200 <rmrfat+0x80>
jmp    11ce <rmrfat+0x4e>
mov    -0x3c(%rbp),%eax
sub    $0x81,%eax
je     11f4 <rmrfat+0x74>
jmp    11e1 <rmrfat+0x61>
mov    -0x3c(%rbp),%eax
sub    $0x82,%eax
je     1200 <rmrfat+0x80>
jmp    1205 <rmrfat+0x85>
movl   $0x0,-0x4(%rbp)
jmp    1339 <rmrfat+0x1b9>
jmp    1211 <rmrfat+0x91>
movl   $0x0,-0x4(%rbp)
jmp    1339 <rmrfat+0x1b9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
call   1040 <openat@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   10a0 <fdopendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    124c <rmrfat+0xcc>
movl   $0x0,-0x4(%rbp)
jmp    1339 <rmrfat+0x1b9>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1314 <rmrfat+0x194>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xd8e(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12fd <rmrfat+0x17d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xd72(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12fd <rmrfat+0x17d>
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
lea    0xd1e(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1090 <rmrfat@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
jmp    1302 <rmrfat+0x182>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
jmp    1259 <rmrfat+0xd9>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2ca5(%rip),%rax
mov    (%rax),%edx
call   1030 <unlinkat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmrfat>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,-0x2c(%rbp)
mov    %rsi,-0x38(%rbp)
xor    %edx,%edx
call   1030 <unlinkat@plt>
test   %eax,%eax
je     12a9 <rmrfat+0x129>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
or     $0x2,%eax
cmp    $0x82,%eax
jne    12a9 <rmrfat+0x129>
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1040 <openat@plt>
mov    %eax,%edi
call   10a0 <fdopendir@plt>
test   %rax,%rax
je     12a9 <rmrfat+0x129>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     128c <rmrfat+0x10c>
mov    %rax,%r13
lea    0xdff(%rip),%rbx
jmp    1220 <rmrfat+0xa0>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     128c <rmrfat+0x10c>
mov    0x0(%r13),%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1210 <rmrfat+0x90>
mov    0x0(%r13),%rdi
lea    0xdc5(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1210 <rmrfat+0x90>
mov    %rsp,%r14
mov    0x2da0(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    (%rcx),%esi
mov    0x0(%r13),%r8
mov    %r12,%rdi
lea    0xd90(%rip),%rdx
mov    -0x38(%rbp),%rcx
call   1050 <snprintf@plt>
mov    -0x2c(%rbp),%edi
mov    %r12,%rsi
call   1090 <rmrfat@plt>
mov    %r14,%rsp
jmp    1210 <rmrfat+0x90>
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edx
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1030 <unlinkat@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@errno = global i32 0
@O_DIRECTORY = global i32 0
@.str = [3 x i8] c"..\00"
@.str.1 = [2 x i8] c".\00"
@PATH_MAX = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@AT_REMOVEDIR = global i32 0
define i32 @rmrfat(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
%11 = load i32, i32* %4
%12 = load i8*, i8** %5
%13 = call i32 @unlinkat(i32 %11, i8* %12, i32 0)
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %2
store i32 0, i32* %3
br label %75
16: ; preds = %2
%17 = load i32, i32* @errno
switch i32 %17, label %20 [
i32 129, label %18
i32 128, label %19
i32 130, label %19
]
18: ; preds = %16
store i32 0, i32* %3
br label %75
19: ; preds = %16, %16
br label %21
20: ; preds = %16
store i32 0, i32* %3
br label %75
21: ; preds = %19
%22 = load i32, i32* %4
%23 = load i8*, i8** %5
%24 = load i32, i32* @O_DIRECTORY
%25 = call i32 @openat(i32 %22, i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = call i32* @fdopendir(i32 %26)
store i32* %27, i32** %7
%28 = load i32*, i32** %7
%29 = icmp ne i32* %28, null
br i1 %29, label %31, label %30
30: ; preds = %21
store i32 0, i32* %3
br label %75
31: ; preds = %21
%32 = load i32*, i32** %7
%33 = call %struct.dirent* @readdir(i32* %32)
store %struct.dirent* %33, %struct.dirent** %6
br label %34
34: ; preds = %64, %31
%35 = load %struct.dirent*, %struct.dirent** %6
%36 = icmp ne %struct.dirent* %35, null
br i1 %36, label %37, label %67
37: ; preds = %34
%38 = load %struct.dirent*, %struct.dirent** %6
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %63
43: ; preds = %37
%44 = load %struct.dirent*, %struct.dirent** %6
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %63
49: ; preds = %43
%50 = load i32, i32* @PATH_MAX
%51 = zext i32 %50 to i64
%52 = call i8* @llvm.stacksave()
store i8* %52, i8** %9
%53 = alloca i8, i64 %51
store i64 %51, i64* %10
%54 = load i32, i32* @PATH_MAX
%55 = load i8*, i8** %5
%56 = load %struct.dirent*, %struct.dirent** %6
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @snprintf(i8* %53, i32 %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %55, i8* %58)
%60 = load i32, i32* %4
%61 = call i32 @rmrfat(i32 %60, i8* %53)
%62 = load i8*, i8** %9
br label %63
63: ; preds = %49, %43, %37
br label %64
64: ; preds = %63
%65 = load i32*, i32** %7
%66 = call %struct.dirent* @readdir(i32* %65)
store %struct.dirent* %66, %struct.dirent** %6
br label %34
67: ; preds = %34
%68 = load i32*, i32** %7
%69 = call i32 @closedir(i32* %68)
%70 = load i32, i32* %4
%71 = load i8*, i8** %5
%72 = load i32, i32* @AT_REMOVEDIR
%73 = call i32 @unlinkat(i32 %70, i8* %71, i32 %72)
br label %74
74: ; preds = %67
store i32 0, i32* %3
br label %75
75: ; preds = %74, %30, %20, %18, %15
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@errno = global i32 0
@O_DIRECTORY = global i32 0
@.str = [3 x i8] c"..\00"
@.str.1 = [2 x i8] c".\00"
@PATH_MAX = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@AT_REMOVEDIR = global i32 0
define i32 @rmrfat(i32 %0, i8* %1) {
%3 = tail call i32 @unlinkat(i32 %0, i8* %1, i32 0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %41, label %5
5: ; preds = %2
%6 = load i32, i32* @errno
switch i32 %6, label %41 [
i32 130, label %7
i32 128, label %7
]
7: ; preds = %5, %5
%8 = load i32, i32* @O_DIRECTORY
%9 = tail call i32 @openat(i32 %0, i8* %1, i32 %8)
%10 = tail call i32* @fdopendir(i32 %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %41, label %12
12: ; preds = %7
%13 = tail call %struct.dirent* @readdir(i32* %10)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %37, label %15
15: ; preds = %12, %34
%16 = phi %struct.dirent* [ %35, %34 ], [ %13, %12 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %34, label %21
21: ; preds = %15
%22 = load i8*, i8** %17
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %34, label %25
25: ; preds = %21
%26 = load i32, i32* @PATH_MAX
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
%29 = alloca i8, i64 %27
%30 = load i32, i32* @PATH_MAX
%31 = load i8*, i8** %17
%32 = call i32 @snprintf(i8* %29, i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %31)
%33 = call i32 @rmrfat(i32 %0, i8* %29)
br label %34
34: ; preds = %15, %21, %25
%35 = call %struct.dirent* @readdir(i32* %10)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %15
37: ; preds = %34, %12
%38 = call i32 @closedir(i32* %10)
%39 = load i32, i32* @AT_REMOVEDIR
%40 = call i32 @unlinkat(i32 %0, i8* %1, i32 %39)
br label %41
41: ; preds = %37, %5, %7, %2
ret i32 0
}