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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int _POSIX_PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rmdir(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcmp(char *, char *);
int unlink(char *);
int mutt_rmtree(const char *path) {
  DIR *dirp;
  struct dirent *de;
  char cur[_POSIX_PATH_MAX];
  struct stat statbuf;
  int rc = 0;
  if (!(dirp = opendir(path))) {
    do {
    } while (0);
    return -1;
  }
  while ((de = readdir(dirp))) {
    if (!strcmp(".", de->d_name) || !strcmp("..", de->d_name))
      continue;
    snprintf(cur, sizeof(cur), "%s/%s", path, de->d_name);
    if (stat(cur, &statbuf) == -1) {
      rc = 1;
      continue;
    }
    if (S_ISDIR(statbuf.st_mode))
      rc |= mutt_rmtree(cur);
    else
      rc |= unlink(cur);
  }
  closedir(dirp);
  rc |= rmdir(path);
  return rc;
}


x86 assembly (clang, -O0):
<mutt_rmtree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    121c <mutt_rmtree+0x7c>
jmp    11ff <mutt_rmtree+0x5f>
jmp    1204 <mutt_rmtree+0x64>
jmp    1209 <mutt_rmtree+0x69>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1317 <mutt_rmtree+0x177>
jmp    1221 <mutt_rmtree+0x81>
mov    -0x18(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12f2 <mutt_rmtree+0x152>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdbb(%rip),%rdi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     1270 <mutt_rmtree+0xd0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd9e(%rip),%rdi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    1275 <mutt_rmtree+0xd5>
jmp    1221 <mutt_rmtree+0x81>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
lea    0xd72(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    12b8 <mutt_rmtree+0x118>
movl   $0x1,-0x3c(%rbp)
jmp    1221 <mutt_rmtree+0x81>
mov    -0x38(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12de <mutt_rmtree+0x13e>
mov    -0x48(%rbp),%rdi
call   1030 <mutt_rmtree@plt>
or     -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    12ed <mutt_rmtree+0x14d>
mov    -0x48(%rbp),%rdi
call   1040 <unlink@plt>
or     -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1221 <mutt_rmtree+0x81>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x10(%rbp),%rdi
call   1060 <rmdir@plt>
or     -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mutt_rmtree>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e3d(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
call   1050 <opendir@plt>
test   %rax,%rax
je     12b1 <mutt_rmtree+0x111>
mov    %rax,%rbx
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
mov    %r14,-0x38(%rbp)
je     12b8 <mutt_rmtree+0x118>
mov    %r15,-0x40(%rbp)
xor    %r15d,%r15d
lea    0xdfe(%rip),%r13
lea    0xdf6(%rip),%r14
mov    %r15d,-0x2c(%rbp)
jmp    1221 <mutt_rmtree+0x81>
mov    %rbx,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12ab <mutt_rmtree+0x10b>
mov    (%rax),%r15
mov    %r13,%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1210 <mutt_rmtree+0x70>
mov    %r14,%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1210 <mutt_rmtree+0x70>
mov    %r12,%rdi
mov    -0x40(%rbp),%rsi
lea    0xdb3(%rip),%rdx
mov    -0x38(%rbp),%rcx
mov    %r15,%r8
call   1080 <snprintf@plt>
mov    %r12,%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
je     1284 <mutt_rmtree+0xe4>
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
mov    %r12,%rdi
test   %rax,%rax
je     128c <mutt_rmtree+0xec>
call   1030 <mutt_rmtree@plt>
jmp    1291 <mutt_rmtree+0xf1>
mov    $0x1,%r15d
jmp    1298 <mutt_rmtree+0xf8>
call   1040 <unlink@plt>
mov    -0x2c(%rbp),%r15d
or     %eax,%r15d
mov    %rbx,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
jne    120a <mutt_rmtree+0x6a>
jmp    12bb <mutt_rmtree+0x11b>
mov    -0x2c(%rbp),%r15d
jmp    12bb <mutt_rmtree+0x11b>
mov    $0xffffffff,%eax
jmp    12cf <mutt_rmtree+0x12f>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    -0x38(%rbp),%rdi
call   1060 <rmdir@plt>
or     %r15d,%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* }
%struct.stat = type { i32 }
@_POSIX_PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @mutt_rmtree(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i32, i32* @_POSIX_PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
store i32 0, i32* %9
%15 = load i8*, i8** %3
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %4
%17 = icmp ne i32* %16, null
br i1 %17, label %22, label %18
18: ; preds = %1
br label %19
19: ; preds = %18
br label %20
20: ; preds = %19
br label %21
21: ; preds = %20
store i32 -1, i32* %2
store i32 1, i32* %10
br label %72
22: ; preds = %1
br label %23
23: ; preds = %63, %49, %39, %22
%24 = load i32*, i32** %4
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %5
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %64
27: ; preds = %23
%28 = load %struct.dirent*, %struct.dirent** %5
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = load %struct.dirent*, %struct.dirent** %5
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %33, %27
br label %23
40: ; preds = %33
%41 = trunc i64 %12 to i32
%42 = load i8*, i8** %3
%43 = load %struct.dirent*, %struct.dirent** %5
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 @snprintf(i8* %14, i32 %41, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %42, i8* %45)
%47 = call i32 @stat(i8* %14, %struct.stat* %8)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %40
store i32 1, i32* %9
br label %23
50: ; preds = %40
%51 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @S_ISDIR(i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %50
%56 = call i32 @mutt_rmtree(i8* %14)
%57 = load i32, i32* %9
%58 = or i32 %57, %56
store i32 %58, i32* %9
br label %63
59: ; preds = %50
%60 = call i32 @unlink(i8* %14)
%61 = load i32, i32* %9
%62 = or i32 %61, %60
store i32 %62, i32* %9
br label %63
63: ; preds = %59, %55
br label %23
64: ; preds = %23
%65 = load i32*, i32** %4
%66 = call i32 @closedir(i32* %65)
%67 = load i8*, i8** %3
%68 = call i32 @rmdir(i8* %67)
%69 = load i32, i32* %9
%70 = or i32 %69, %68
store i32 %70, i32* %9
%71 = load i32, i32* %9
store i32 %71, i32* %2
store i32 1, i32* %10
br label %72
72: ; preds = %64, %21
%73 = load i8*, i8** %6
%74 = load i32, i32* %2
ret i32 %74
}

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