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

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int asprintf(char **, char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ assert(char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ readdir_r(int /*<<< orphan*/ *, struct dirent *,
                             struct dirent **);
scalar_t__ rmdir(char const *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
int unlink(char const *);
int dir_remove(const char *path) {
  assert(path);
  struct stat st;
  if (stat(path, &st)) {
    return (-1);
  }
  if (!S_ISDIR(st.st_mode)) {
    return (unlink(path));
  }
  DIR *dir;
  if (!(dir = opendir(path))) {
    return (-1);
  }
  struct dirent *saveptr;
  struct dirent ent;
  char *buf;
  int ret = 0;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  while (!readdir_r(dir, &ent, &saveptr) && saveptr) {
#pragma GCC diagnostic pop
    if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, "..")) {
      continue;
    }
    if (-1 == asprintf(&buf, "%s/%s", path, ent.d_name)) {
      errno = ENOMEM;
      ret = -1;
      break;
    }
    if (dir_remove(buf)) {
      ret = -1;
    }
    free(buf);
  }
  closedir(dir);
  if (rmdir(path)) {
    ret = -1;
  }
  return (ret);
}


x86 assembly (clang, -O0):
<dir_remove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10d0 <stat@plt>
cmp    $0x0,%rax
je     11f8 <dir_remove+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <dir_remove+0x189>
mov    -0x18(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    121a <dir_remove+0x5a>
mov    -0x10(%rbp),%rdi
call   1040 <unlink@plt>
mov    %eax,-0x4(%rbp)
jmp    1349 <dir_remove+0x189>
mov    -0x10(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    123d <dir_remove+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <dir_remove+0x189>
movl   $0x0,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <readdir_r@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x3d(%rbp)
jne    1270 <dir_remove+0xb0>
cmpq   $0x0,-0x28(%rbp)
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    1280 <dir_remove+0xc0>
jmp    1320 <dir_remove+0x160>
mov    -0x30(%rbp),%edi
lea    0xd77(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
je     12b0 <dir_remove+0xf0>
mov    -0x30(%rbp),%edi
lea    0xd5e(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
jne    12b5 <dir_remove+0xf5>
jmp    1244 <dir_remove+0x84>
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%ecx
lea    -0x38(%rbp),%rdi
lea    0xd3c(%rip),%rsi
call   10e0 <asprintf@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12f9 <dir_remove+0x139>
mov    0x2cfe(%rip),%rax
mov    (%rax),%ecx
mov    0x2ced(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x3c(%rbp)
jmp    1320 <dir_remove+0x160>
mov    -0x38(%rbp),%rdi
call   1090 <dir_remove@plt>
cmp    $0x0,%eax
je     1312 <dir_remove+0x152>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    1244 <dir_remove+0x84>
mov    -0x20(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x10(%rbp),%rdi
call   1070 <rmdir@plt>
cmp    $0x0,%rax
je     1343 <dir_remove+0x183>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_remove>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
call   10c0 <assert@plt>
lea    0x18(%rsp),%rsi
mov    %r14,%rdi
call   10d0 <stat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11fe <dir_remove+0x3e>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x18(%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %eax,%eax
je     12e7 <dir_remove+0x127>
mov    %r14,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     11ed <dir_remove+0x2d>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1050 <readdir_r@plt>
test   %eax,%eax
jne    12f6 <dir_remove+0x136>
movl   $0x0,0x4(%rsp)
cmpq   $0x0,0x10(%rsp)
je     12fe <dir_remove+0x13e>
movl   $0x0,0x4(%rsp)
lea    0xda5(%rip),%r15
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xd93(%rip),%rbp
nopl   (%rax)
mov    0x8(%rsp),%edi
mov    %r15,%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
je     1290 <dir_remove+0xd0>
mov    0x8(%rsp),%edi
mov    %rbp,%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
jne    12ac <dir_remove+0xec>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <readdir_r@plt>
test   %eax,%eax
jne    12fe <dir_remove+0x13e>
cmpq   $0x0,0x10(%rsp)
jne    1270 <dir_remove+0xb0>
jmp    12fe <dir_remove+0x13e>
mov    0x8(%rsp),%ecx
lea    0x20(%rsp),%rdi
lea    0xd47(%rip),%rsi
mov    %r14,%rdx
call   10e0 <asprintf@plt>
cmp    $0xffffffff,%eax
je     131c <dir_remove+0x15c>
mov    0x20(%rsp),%rdi
call   1090 <dir_remove@plt>
neg    %eax
sbb    %eax,%eax
or     %eax,0x4(%rsp)
mov    0x20(%rsp),%rdi
call   1030 <free@plt>
jmp    1290 <dir_remove+0xd0>
mov    %r14,%rdi
call   1040 <unlink@plt>
mov    %eax,%ebp
jmp    11ed <dir_remove+0x2d>
movl   $0x0,0x4(%rsp)
mov    %rbx,%rdi
call   10a0 <closedir@plt>
mov    %r14,%rdi
call   1070 <rmdir@plt>
neg    %rax
sbb    %ebp,%ebp
or     0x4(%rsp),%ebp
jmp    11ed <dir_remove+0x2d>
mov    0x2cbd(%rip),%rax
mov    (%rax),%eax
mov    0x2cac(%rip),%rcx
mov    %eax,(%rcx)
movl   $0xffffffff,0x4(%rsp)
ff
jmp    12fe <dir_remove+0x13e>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @dir_remove(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.dirent
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @assert(i8* %10)
%12 = load i8*, i8** %3
%13 = call i64 @stat(i8* %12, %struct.stat* %4)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %75
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 %75
24: ; preds = %16
%25 = load i8*, i8** %3
%26 = call i32* @opendir(i8* %25)
store i32* %26, i32** %5
%27 = icmp ne i32* %26, null
br i1 %27, label %29, label %28
28: ; preds = %24
store i32 -1, i32* %2
br label %75
29: ; preds = %24
store i32 0, i32* %9
br label %30
30: ; preds = %63, %49, %29
%31 = load i32*, i32** %5
%32 = call i32 @readdir_r(i32* %31, %struct.dirent* %7, %struct.dirent** %6)
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = load %struct.dirent*, %struct.dirent** %6
%36 = icmp ne %struct.dirent* %35, null
br label %37
37: ; preds = %34, %30
%38 = phi i1 [ false, %30 ], [ %36, %34 ]
br i1 %38, label %39, label %66
39: ; preds = %37
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @strcmp(i32 %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @strcmp(i32 %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %44, %39
br label %30
50: ; preds = %44
%51 = load i8*, i8** %3
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %51, i32 %53)
%55 = icmp eq i32 -1, %54
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = load i32, i32* @ENOMEM
store i32 %57, i32* @errno
store i32 -1, i32* %9
br label %66
58: ; preds = %50
%59 = load i8*, i8** %8
%60 = call i32 @dir_remove(i8* %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %58
store i32 -1, i32* %9
br label %63
63: ; preds = %62, %58
%64 = load i8*, i8** %8
%65 = call i32 @free(i8* %64)
br label %30
66: ; preds = %56, %37
%67 = load i32*, i32** %5
%68 = call i32 @closedir(i32* %67)
%69 = load i8*, i8** %3
%70 = call i64 @rmdir(i8* %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %66
store i32 -1, i32* %9
br label %73
73: ; preds = %72, %66
%74 = load i32, i32* %9
store i32 %74, i32* %2
br label %75
75: ; preds = %73, %28, %21, %15
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @dir_remove(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca %struct.dirent*
%4 = alloca %struct.dirent
%5 = alloca i8*
%6 = tail call i32 @assert(i8* %0)
%7 = bitcast %struct.stat* %2 to i8*
%8 = call i64 @stat(i8* %0, %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %71
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @S_ISDIR(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @unlink(i8* %0)
br label %71
17: ; preds = %10
%18 = call i32* @opendir(i8* %0)
%19 = icmp eq i32* %18, null
br i1 %19, label %71, label %20
20: ; preds = %17
%21 = bitcast %struct.dirent** %3 to i8*
%22 = bitcast %struct.dirent* %4 to i8*
%23 = bitcast i8** %5 to i8*
%24 = call i32 @readdir_r(i32* %18, %struct.dirent* %4, %struct.dirent** %3)
%25 = icmp eq i32 %24, 0
%26 = load %struct.dirent*, %struct.dirent** %3
%27 = icmp ne %struct.dirent* %26, null
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %29, label %65
29: ; preds = %20
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %4, i64 0, i32 0
br label %31
31: ; preds = %29, %53
%32 = phi i32 [ 0, %29 ], [ %57, %53 ]
br label %33
33: ; preds = %31, %41
%34 = load i32, i32* %30
%35 = call i32 @strcmp(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%36 = icmp eq i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %33
%38 = load i32, i32* %30
%39 = call i32 @strcmp(i32 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %37, %33
%42 = call i32 @readdir_r(i32* %18, %struct.dirent* %4, %struct.dirent** %3)
%43 = icmp eq i32 %42, 0
%44 = load %struct.dirent*, %struct.dirent** %3
%45 = icmp ne %struct.dirent* %44, null
%46 = select i1 %43, i1 %45, i1 false
br i1 %46, label %33, label %65
47: ; preds = %37
%48 = load i32, i32* %30
%49 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = load i32, i32* @ENOMEM
store i32 %52, i32* @errno
br label %65
53: ; preds = %47
%54 = load i8*, i8** %5
%55 = call i32 @dir_remove(i8* %54)
%56 = icmp eq i32 %55, 0
%57 = select i1 %56, i32 %32, i32 -1
%58 = load i8*, i8** %5
%59 = call i32 @free(i8* %58)
%60 = call i32 @readdir_r(i32* %18, %struct.dirent* %4, %struct.dirent** %3)
%61 = icmp eq i32 %60, 0
%62 = load %struct.dirent*, %struct.dirent** %3
%63 = icmp ne %struct.dirent* %62, null
%64 = select i1 %61, i1 %63, i1 false
br i1 %64, label %31, label %65
65: ; preds = %53, %41, %20, %51
%66 = phi i32 [ -1, %51 ], [ 0, %20 ], [ %32, %41 ], [ %57, %53 ]
%67 = call i32 @closedir(i32* %18)
%68 = call i64 @rmdir(i8* %0)
%69 = icmp eq i64 %68, 0
%70 = select i1 %69, i32 %66, i32 -1
br label %71
71: ; preds = %65, %17, %1, %15
%72 = phi i32 [ %16, %15 ], [ -1, %1 ], [ %70, %65 ], [ -1, %17 ]
ret i32 %72
}