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

/* Variables and functions */
scalar_t__ EISDIR;
scalar_t__ EPERM;
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rmdir(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ strcmp(char *, char *);
int unlink(char const *);
int unlink_recursive(const char *path) {
  int rc = unlink(path);
  if (rc == -1 && (errno == EISDIR || errno == EPERM)) {
    DIR *dir = opendir(path);
    if (dir) {
      struct dirent *d;
      rc = 0;
      while (rc == 0 && (d = readdir(dir))) {
        if (!(strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)) {
          char subpath[PATH_MAX];
          snprintf(subpath, sizeof(subpath), "%s/%s", path, d->d_name);
          rc = unlink_recursive(subpath);
          if (rc == -1) {
            closedir(dir);
            return -1;
          }
        }
      }
      closedir(dir);
      rc = rmdir(path);
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<unlink_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1323 <unlink_recursive+0x1a3>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
je     11d6 <unlink_recursive+0x56>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
jne    1323 <unlink_recursive+0x1a3>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     131e <unlink_recursive+0x19e>
movl   $0x0,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x3d(%rbp)
jne    121b <unlink_recursive+0x9b>
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    122b <unlink_recursive+0xab>
jmp    1309 <unlink_recursive+0x189>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc8(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1304 <unlink_recursive+0x184>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdaa(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1304 <unlink_recursive+0x184>
mov    0x2d84(%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    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xd5e(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x48(%rbp),%rdi
call   1060 <unlink_recursive@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12dc <unlink_recursive+0x15c>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12e3 <unlink_recursive+0x163>
movl   $0x0,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x3c(%rbp),%eax
test   %eax,%eax
je     12ff <unlink_recursive+0x17f>
jmp    12fa <unlink_recursive+0x17a>
jmp    1329 <unlink_recursive+0x1a9>
jmp    1304 <unlink_recursive+0x184>
jmp    11f5 <unlink_recursive+0x75>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x14(%rbp)
jmp    1323 <unlink_recursive+0x1a3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlink_recursive>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <unlink@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    129e <unlink_recursive+0x11e>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
je     11cc <unlink_recursive+0x4c>
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%rax
jne    129e <unlink_recursive+0x11e>
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     129e <unlink_recursive+0x11e>
mov    %rax,%r15
mov    %r14,-0x30(%rbp)
lea    0xe16(%rip),%rbx
jmp    11f9 <unlink_recursive+0x79>
nopl   (%rax)
test   %r12d,%r12d
jne    128b <unlink_recursive+0x10b>
mov    %r15,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     128b <unlink_recursive+0x10b>
mov    %rax,%r13
mov    (%rax),%rdi
mov    %rbx,%rsi
call   1090 <strcmp@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     11f0 <unlink_recursive+0x70>
mov    0x0(%r13),%rdi
lea    0xdd5(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     11f0 <unlink_recursive+0x70>
mov    %rsp,%r14
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x0(%r13),%r8
mov    %rbx,%rdi
lea    0xda0(%rip),%rdx
mov    -0x30(%rbp),%rcx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <unlink_recursive@plt>
cmp    $0xffffffff,%eax
je     12af <unlink_recursive+0x12f>
mov    %eax,%r12d
mov    %r14,%rsp
lea    0xd7b(%rip),%rbx
jmp    11f0 <unlink_recursive+0x70>
mov    %r15,%rdi
call   1080 <closedir@plt>
mov    -0x30(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1080 <closedir@plt>
mov    %r14,%rsp
mov    $0xffffffff,%ebx
jmp    129e <unlink_recursive+0x11e>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@errno = global i64 0
@EISDIR = global i64 0
@EPERM = global i64 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@PATH_MAX = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @unlink_recursive(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @unlink(i8* %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %78
14: ; preds = %1
%15 = load i64, i64* @errno
%16 = load i64, i64* @EISDIR
%17 = icmp eq i64 %15, %16
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i64, i64* @errno
%20 = load i64, i64* @EPERM
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %78
22: ; preds = %18, %14
%23 = load i8*, i8** %3
%24 = call i32* @opendir(i8* %23)
store i32* %24, i32** %5
%25 = load i32*, i32** %5
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %77
27: ; preds = %22
store i32 0, i32* %4
br label %28
28: ; preds = %71, %27
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%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 label %35
35: ; preds = %31, %28
%36 = phi i1 [ false, %28 ], [ %34, %31 ]
br i1 %36, label %37, label %72
37: ; preds = %35
%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 ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %71, label %43
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 ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %71, label %49
49: ; preds = %43
%50 = load i32, i32* @PATH_MAX
%51 = zext i32 %50 to i64
%52 = call i8* @llvm.stacksave()
store i8* %52, i8** %7
%53 = alloca i8, i64 %51
store i64 %51, i64* %8
%54 = trunc i64 %51 to i32
%55 = load i8*, i8** %3
%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 = call i32 @unlink_recursive(i8* %53)
store i32 %60, i32* %4
%61 = load i32, i32* %4
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %66
63: ; preds = %49
%64 = load i32*, i32** %5
%65 = call i32 @closedir(i32* %64)
store i32 -1, i32* %2
store i32 1, i32* %9
br label %67
66: ; preds = %49
store i32 0, i32* %9
br label %67
67: ; preds = %66, %63
%68 = load i8*, i8** %7
%69 = load i32, i32* %9
switch i32 %69, label %82 [
i32 0, label %70
i32 1, label %80
]
70: ; preds = %67
br label %71
71: ; preds = %70, %43, %37
br label %28
72: ; preds = %35
%73 = load i32*, i32** %5
%74 = call i32 @closedir(i32* %73)
%75 = load i8*, i8** %3
%76 = call i32 @rmdir(i8* %75)
store i32 %76, i32* %4
br label %77
77: ; preds = %72, %22
br label %78
78: ; preds = %77, %18, %1
%79 = load i32, i32* %4
store i32 %79, i32* %2
br label %80
80: ; preds = %78, %67
%81 = load i32, i32* %2
ret i32 %81
82: ; preds = %67
unreachable
}

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