#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*/ DIR;

/* Variables and functions */
scalar_t__ EACCES;
int PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rmdir(char const *);
int snprintf(char *, int, char *, char const *, char const *);
scalar_t__ stat(char const *, struct stat *);
int strlen(char const *);
scalar_t__ unlink(char const *);
int nuke(const char *src) {
  struct stat sb;
  DIR *dir;
  struct dirent *d;
  char path[PATH_MAX];
  int len;
  memset(&sb, 0, sizeof(struct stat));
  if (stat(src, &sb) < 0)
    return 0;
  if (!S_ISDIR(sb.st_mode)) {
    if (unlink(src) == 0)
      return 0;
    else
      return 1;
  }
  dir = opendir(src);
  if (!dir) {
    if (errno != EACCES)
      return -1;
  }
  while ((d = readdir(dir))) {
    if (d->d_name[0] == '.' &&
        (d->d_name[1] == '\0' || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
      continue;
    if (strlen(src) + 1 + strlen(d->d_name) >= PATH_MAX - 1)
      continue;
    len = snprintf(path, sizeof path, "%s/%s", src, d->d_name);
    if (len >= sizeof path)
      return -1;
    memset(&sb, 0, sizeof(struct stat));
    if (stat(path, &sb) < 0) {
      closedir(dir);
      return -1;
    }
    if (nuke(path) < 0)
      return -1;
  }
  closedir(dir);
  rmdir(src);
  return 0;
}


x86 assembly (clang, -O0):
<nuke>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%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,-0x38(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1090 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jge    1226 <nuke+0x76>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    143a <nuke+0x28a>
mov    -0x18(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1270 <nuke+0xc0>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jne    125d <nuke+0xad>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    143a <nuke+0x28a>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    143a <nuke+0x28a>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12ba <nuke+0x10a>
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%rax
je     12b5 <nuke+0x105>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    143a <nuke+0x28a>
jmp    12ba <nuke+0x10a>
jmp    12bf <nuke+0x10f>
mov    -0x20(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     141a <nuke+0x26a>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    132a <nuke+0x17a>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     1325 <nuke+0x175>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    132a <nuke+0x17a>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    132a <nuke+0x17a>
jmp    12bf <nuke+0x10f>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
add    %ecx,%eax
mov    0x2c9d(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     1365 <nuke+0x1b5>
jmp    12bf <nuke+0x10f>
mov    -0x50(%rbp),%rax
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xc7f(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x50(%rbp),%rcx
mov    %eax,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
cmp    %rcx,%rax
jb     13ad <nuke+0x1fd>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    143a <nuke+0x28a>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1090 <memset@plt>
mov    -0x48(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jge    13f0 <nuke+0x240>
mov    -0x20(%rbp),%rdi
call   10a0 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    143a <nuke+0x28a>
mov    -0x48(%rbp),%rdi
call   10d0 <nuke@plt>
cmp    $0x0,%eax
jge    1415 <nuke+0x265>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    143a <nuke+0x28a>
jmp    12bf <nuke+0x10f>
mov    -0x20(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nuke>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
mov    %rsp,%r12
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
mov    %rax,-0x40(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    -0x30(%rbp),%rbx
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1090 <memset@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10b0 <stat@plt>
test   %rax,%rax
mov    %r12,-0x38(%rbp)
js     134d <nuke+0x19d>
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     1326 <nuke+0x176>
mov    %r15,%rdi
call   1040 <opendir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1253 <nuke+0xa3>
mov    0x2d96(%rip),%rax
mov    (%rax),%rax
mov    0x2d9c(%rip),%rcx
mov    $0xffffffff,%r13d
cmp    (%rcx),%rax
jne    134d <nuke+0x19d>
mov    %r12,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     133a <nuke+0x18a>
mov    %rax,%rbx
jmp    1284 <nuke+0xd4>
nopl   0x0(%rax)
mov    %r12,%rdi
call   10c0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     133a <nuke+0x18a>
mov    (%rbx),%rax
cmpb   $0x2e,(%rax)
jne    12a0 <nuke+0xf0>
movzbl 0x1(%rax),%ecx
test   %cl,%cl
je     1270 <nuke+0xc0>
cmp    $0x2e,%cl
jne    12a0 <nuke+0xf0>
cmpb   $0x0,0x2(%rax)
je     1270 <nuke+0xc0>
nop
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%r13d
mov    (%rbx),%rdi
call   1060 <strlen@plt>
add    %r13d,%eax
add    $0x1,%eax
mov    0x2d30(%rip),%rcx
mov    (%rcx),%ecx
add    $0xffffffff,%ecx
cmp    %ecx,%eax
jge    1270 <nuke+0xc0>
mov    (%rbx),%r8
mov    %r14,%rdi
mov    -0x40(%rbp),%rbx
mov    %ebx,%esi
lea    0xd24(%rip),%rdx
mov    %r15,%rcx
call   1080 <snprintf@plt>
cltq
mov    $0xffffffff,%r13d
cmp    %rbx,%rax
jae    134d <nuke+0x19d>
lea    -0x30(%rbp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1090 <memset@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10b0 <stat@plt>
test   %rax,%rax
js     1363 <nuke+0x1b3>
mov    %r14,%rdi
call   10d0 <nuke@plt>
test   %eax,%eax
jns    1270 <nuke+0xc0>
jmp    134d <nuke+0x19d>
mov    %r15,%rdi
call   1030 <unlink@plt>
xor    %r13d,%r13d
test   %rax,%rax
setne  %r13b
jmp    134d <nuke+0x19d>
mov    %r12,%rdi
call   10a0 <closedir@plt>
mov    %r15,%rdi
call   1050 <rmdir@plt>
xor    %r13d,%r13d
mov    -0x38(%rbp),%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   10a0 <closedir@plt>
jmp    134d <nuke+0x19d>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @nuke(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = call i32 @memset(%struct.stat* %4, i32 0, i32 4)
%16 = load i8*, i8** %3
%17 = call i64 @stat(i8* %16, %struct.stat* %4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %1
store i32 0, i32* %2
store i32 1, i32* %10
br label %121
20: ; preds = %1
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @S_ISDIR(i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %20
%26 = load i8*, i8** %3
%27 = call i64 @unlink(i8* %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 0, i32* %2
store i32 1, i32* %10
br label %121
30: ; preds = %25
store i32 1, i32* %2
store i32 1, i32* %10
br label %121
31: ; preds = %20
%32 = load i8*, i8** %3
%33 = call i32* @opendir(i8* %32)
store i32* %33, i32** %5
%34 = load i32*, i32** %5
%35 = icmp ne i32* %34, null
br i1 %35, label %42, label %36
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = load i64, i64* @EACCES
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 -1, i32* %2
store i32 1, i32* %10
br label %121
41: ; preds = %36
br label %42
42: ; preds = %41, %31
br label %43
43: ; preds = %115, %92, %79, %42
%44 = load i32*, i32** %5
%45 = call %struct.dirent* @readdir(i32* %44)
store %struct.dirent* %45, %struct.dirent** %6
%46 = icmp ne %struct.dirent* %45, null
br i1 %46, label %47, label %116
47: ; preds = %43
%48 = load %struct.dirent*, %struct.dirent** %6
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = getelementptr inbounds i8, i8* %50, i64 0
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 46
br i1 %54, label %55, label %80
55: ; preds = %47
%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 = getelementptr inbounds i8, i8* %58, i64 1
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %61, 0
br i1 %62, label %79, label %63
63: ; preds = %55
%64 = load %struct.dirent*, %struct.dirent** %6
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = getelementptr inbounds i8, i8* %66, i64 1
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 46
br i1 %70, label %71, label %80
71: ; preds = %63
%72 = load %struct.dirent*, %struct.dirent** %6
%73 = getelementptr inbounds %struct.dirent, %struct.dirent* %72, i32 0, i32 0
%74 = load i8*, i8** %73
%75 = getelementptr inbounds i8, i8* %74, i64 2
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %71, %55
br label %43
80: ; preds = %71, %63, %47
%81 = load i8*, i8** %3
%82 = call i32 @strlen(i8* %81)
%83 = add nsw i32 %82, 1
%84 = load %struct.dirent*, %struct.dirent** %6
%85 = getelementptr inbounds %struct.dirent, %struct.dirent* %84, i32 0, i32 0
%86 = load i8*, i8** %85
%87 = call i32 @strlen(i8* %86)
%88 = add nsw i32 %83, %87
%89 = load i32, i32* @PATH_MAX
%90 = sub nsw i32 %89, 1
%91 = icmp sge i32 %88, %90
br i1 %91, label %92, label %93
92: ; preds = %80
br label %43
93: ; preds = %80
%94 = trunc i64 %12 to i32
%95 = load i8*, i8** %3
%96 = load %struct.dirent*, %struct.dirent** %6
%97 = getelementptr inbounds %struct.dirent, %struct.dirent* %96, i32 0, i32 0
%98 = load i8*, i8** %97
%99 = call i32 @snprintf(i8* %14, i32 %94, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %95, i8* %98)
store i32 %99, i32* %9
%100 = load i32, i32* %9
%101 = sext i32 %100 to i64
%102 = icmp uge i64 %101, %12
br i1 %102, label %103, label %104
103: ; preds = %93
store i32 -1, i32* %2
store i32 1, i32* %10
br label %121
104: ; preds = %93
%105 = call i32 @memset(%struct.stat* %4, i32 0, i32 4)
%106 = call i64 @stat(i8* %14, %struct.stat* %4)
%107 = icmp slt i64 %106, 0
br i1 %107, label %108, label %111
108: ; preds = %104
%109 = load i32*, i32** %5
%110 = call i32 @closedir(i32* %109)
store i32 -1, i32* %2
store i32 1, i32* %10
br label %121
111: ; preds = %104
%112 = call i32 @nuke(i8* %14)
%113 = icmp slt i32 %112, 0
br i1 %113, label %114, label %115
114: ; preds = %111
store i32 -1, i32* %2
store i32 1, i32* %10
br label %121
115: ; preds = %111
br label %43
116: ; preds = %43
%117 = load i32*, i32** %5
%118 = call i32 @closedir(i32* %117)
%119 = load i8*, i8** %3
%120 = call i32 @rmdir(i8* %119)
store i32 0, i32* %2
store i32 1, i32* %10
br label %121
121: ; preds = %116, %114, %108, %103, %40, %30, %29, %19
%122 = load i8*, i8** %7
%123 = load i32, i32* %2
ret i32 %123
}

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