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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ closedir(int /*<<< orphan*/ *);
int errno;
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rmdir(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int unlink(char const *);
int dirUnlinkHierarchy(const char *path) {
  struct stat st;
  DIR *dir;
  struct dirent *de;
  int fail = 0;
  if (lstat(path, &st) < 0) {
    return -1;
  }
  if (!S_ISDIR(st.st_mode)) {
    return unlink(path);
  }
  dir = opendir(path);
  if (dir == NULL) {
    return -1;
  }
  errno = 0;
  while ((de = readdir(dir)) != NULL) {
    char dn[PATH_MAX];
    if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, ".")) {
      continue;
    }
    snprintf(dn, sizeof(dn), "%s/%s", path, de->d_name);
    if (dirUnlinkHierarchy(dn) < 0) {
      fail = 1;
      break;
    }
    errno = 0;
  }
  if (fail || errno < 0) {
    int save = errno;
    closedir(dir);
    errno = save;
    return -1;
  }
  if (closedir(dir) < 0) {
    return -1;
  }
  return rmdir(path);
}


x86 assembly (clang, -O0):
<dirUnlinkHierarchy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <lstat@plt>
cmp    $0x0,%rax
jge    11d6 <dirUnlinkHierarchy+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <dirUnlinkHierarchy+0x215>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11f8 <dirUnlinkHierarchy+0x58>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x4(%rbp)
jmp    13b5 <dirUnlinkHierarchy+0x215>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121c <dirUnlinkHierarchy+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <dirUnlinkHierarchy+0x215>
mov    0x2db5(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1343 <dirUnlinkHierarchy+0x1a3>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd82(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     12a8 <dirUnlinkHierarchy+0x108>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd67(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    12b4 <dirUnlinkHierarchy+0x114>
movl   $0x2,-0x44(%rbp)
jmp    130e <dirUnlinkHierarchy+0x16e>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xd33(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
call   10c0 <dirUnlinkHierarchy@plt>
cmp    $0x0,%eax
jge    12fa <dirUnlinkHierarchy+0x15a>
movl   $0x1,-0x2c(%rbp)
movl   $0x3,-0x44(%rbp)
jmp    130e <dirUnlinkHierarchy+0x16e>
mov    0x2cd7(%rip),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
mov    %eax,-0x5c(%rbp)
test   %eax,%eax
je     133e <dirUnlinkHierarchy+0x19e>
jmp    1328 <dirUnlinkHierarchy+0x188>
mov    -0x5c(%rbp),%eax
sub    $0x2,%eax
je     1229 <dirUnlinkHierarchy+0x89>
jmp    1339 <dirUnlinkHierarchy+0x199>
jmp    1343 <dirUnlinkHierarchy+0x1a3>
jmp    1229 <dirUnlinkHierarchy+0x89>
cmpl   $0x0,-0x2c(%rbp)
jne    135d <dirUnlinkHierarchy+0x1bd>
mov    0x2c84(%rip),%rax
cmpl   $0x0,(%rax)
jge    138a <dirUnlinkHierarchy+0x1ea>
mov    0x2c74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x48(%rbp),%ecx
mov    0x2c5c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <dirUnlinkHierarchy+0x215>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
cmp    $0x0,%rax
jge    13a9 <dirUnlinkHierarchy+0x209>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <dirUnlinkHierarchy+0x215>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirUnlinkHierarchy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    -0x30(%rbp),%rsi
call   1090 <lstat@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     1312 <dirUnlinkHierarchy+0x172>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     12de <dirUnlinkHierarchy+0x13e>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1312 <dirUnlinkHierarchy+0x172>
mov    %rax,%r15
mov    %rbx,-0x38(%rbp)
mov    0x2ddd(%rip),%rax
movl   $0x0,(%rax)
mov    %r15,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
mov    %r15,%rbx
jne    1260 <dirUnlinkHierarchy+0xc0>
mov    0x2dc0(%rip),%r15
mov    (%r15),%r14d
test   %r14d,%r14d
js     1301 <dirUnlinkHierarchy+0x161>
mov    %rbx,%rdi
call   1080 <closedir@plt>
test   %rax,%rax
mov    $0xffffffff,%r14d
js     1312 <dirUnlinkHierarchy+0x172>
mov    -0x38(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,%r14d
jmp    1312 <dirUnlinkHierarchy+0x172>
nopl   0x0(%rax)
mov    %r15,%rsp
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1211 <dirUnlinkHierarchy+0x71>
mov    %rsp,%r15
mov    0x2d86(%rip),%rcx
mov    (%rcx),%r13d
mov    %rsp,%r14
lea    0xf(%r13),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r14
mov    %r14,%rsp
mov    (%rax),%r12
mov    %r12,%rdi
lea    0xd75(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1250 <dirUnlinkHierarchy+0xb0>
mov    %r12,%rdi
lea    0xd63(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1250 <dirUnlinkHierarchy+0xb0>
mov    %r14,%rdi
mov    %r13d,%esi
lea    0xd4f(%rip),%rdx
mov    -0x38(%rbp),%rcx
mov    %r12,%r8
call   1070 <snprintf@plt>
mov    %r14,%rdi
call   10c0 <dirUnlinkHierarchy@plt>
test   %eax,%eax
js     12f4 <dirUnlinkHierarchy+0x154>
mov    0x2d05(%rip),%rax
movl   $0x0,(%rax)
jmp    1250 <dirUnlinkHierarchy+0xb0>
mov    %rbx,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <unlink@plt>
mov    %r15,%rsp
mov    0x2cda(%rip),%r15
mov    (%r15),%r14d
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    %r14d,(%r15)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@errno = global i32 0
@PATH_MAX = global i32 0
@.str = [3 x i8] c"..\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @dirUnlinkHierarchy(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @lstat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %53, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @S_ISDIR(i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @unlink(i8* %0)
br label %53
13: ; preds = %6
%14 = tail call i32* @opendir(i8* %0)
%15 = icmp eq i32* %14, null
br i1 %15, label %53, label %16
16: ; preds = %13
store i32 0, i32* @errno
%17 = call %struct.dirent* @readdir(i32* %14)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %42, label %19
19: ; preds = %16, %39
%20 = phi %struct.dirent* [ %40, %39 ], [ %17, %16 ]
%21 = load i32, i32* @PATH_MAX
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
%24 = alloca i8, i64 %22
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @strcmp(i8* dereferenceable(1) %26, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%28 = icmp eq i32 %27, 0
br i1 %28, label %39, label %29
29: ; preds = %19
%30 = call i32 @strcmp(i8* dereferenceable(1) %26, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i32 %30, 0
br i1 %31, label %39, label %32
32: ; preds = %29
%33 = call i32 @snprintf(i8* %24, i32 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %26)
%34 = call i32 @dirUnlinkHierarchy(i8* %24)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32, i32* @errno
br label %45
38: ; preds = %32
store i32 0, i32* @errno
br label %39
39: ; preds = %19, %29, %38
%40 = call %struct.dirent* @readdir(i32* %14)
%41 = icmp eq %struct.dirent* %40, null
br i1 %41, label %42, label %19
42: ; preds = %39, %16
%43 = load i32, i32* @errno
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %36, %42
%46 = phi i32 [ %37, %36 ], [ %43, %42 ]
%47 = call i64 @closedir(i32* %14)
store i32 %46, i32* @errno
br label %53
48: ; preds = %42
%49 = call i64 @closedir(i32* %14)
%50 = icmp slt i64 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %48
%52 = call i32 @rmdir(i8* %0)
br label %53
53: ; preds = %48, %13, %1, %51, %45, %11
%54 = phi i32 [ -1, %45 ], [ %52, %51 ], [ %12, %11 ], [ -1, %1 ], [ -1, %13 ], [ -1, %48 ]
ret i32 %54
}