#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__ ENOENT;
int /*<<< orphan*/ PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ debug(int, char *, char const *);
scalar_t__ errno;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int true_lstat(char const *, struct stat *);
int /*<<< orphan*/ *true_opendir(char const *);
struct dirent *true_readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ true_rmdir(char const *);
int /*<<< orphan*/ true_unlink(char const *);
int unlink_recursive(const char *truepath) {
  int rcod;
  struct stat trueinfo;
  DIR *wdir;
  struct dirent *went;
  char wpath[PATH_MAX + 1];
  struct stat winfo;
  debug(2, "unlink_recursive(%s)\n", truepath);
  rcod = true_lstat(truepath, &trueinfo);
  if (rcod < 0 && errno != ENOENT)
    return -1;
  if (rcod != 0)
    return 0;
  if (S_ISDIR(trueinfo.st_mode)) {
    wdir = true_opendir(truepath);
    if (wdir == NULL)
      return -1;
    while ((went = true_readdir(wdir)) != NULL) {
      if (went->d_name[0] == '.' &&
          ((went->d_name[1] == '\0') ||
           (went->d_name[1] == '.' && went->d_name[2] == '\0'))) {
        continue;
      }
      strcpy(wpath, truepath);
      strcat(wpath, "/");
      strcat(wpath, went->d_name);
      rcod = true_lstat(wpath, &winfo);
      if (rcod != 0) {
        closedir(wdir);
        return -1;
      }
      if (S_ISDIR(winfo.st_mode)) {
        unlink_recursive(wpath);
        true_rmdir(wpath);
      } else {
        true_unlink(wpath);
      }
    }
    closedir(wdir);
    true_rmdir(truepath);
  } else {
    true_unlink(truepath);
  }
  return rcod;
}


x86 assembly (clang, -O0):
<unlink_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%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,-0x38(%rbp)
mov    -0x10(%rbp),%rdx
mov    $0x2,%edi
lea    0xe05(%rip),%rsi
call   1050 <debug@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10c0 <true_lstat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1247 <unlink_recursive+0x97>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
je     1247 <unlink_recursive+0x97>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13d8 <unlink_recursive+0x228>
cmpl   $0x0,-0x14(%rbp)
je     1264 <unlink_recursive+0xb4>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13d8 <unlink_recursive+0x228>
mov    -0x18(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%rax
je     13c2 <unlink_recursive+0x212>
mov    -0x10(%rbp),%rdi
call   1030 <true_opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12a1 <unlink_recursive+0xf1>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13d8 <unlink_recursive+0x228>
jmp    12a6 <unlink_recursive+0xf6>
mov    -0x20(%rbp),%rdi
call   10b0 <true_readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     13ab <unlink_recursive+0x1fb>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1311 <unlink_recursive+0x161>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     130c <unlink_recursive+0x15c>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    1311 <unlink_recursive+0x161>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    1311 <unlink_recursive+0x161>
jmp    12a6 <unlink_recursive+0xf6>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x50(%rbp),%rdi
lea    0xced(%rip),%rsi
call   10d0 <strcat@plt>
mov    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   10d0 <strcat@plt>
mov    -0x50(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   10c0 <true_lstat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1374 <unlink_recursive+0x1c4>
mov    -0x20(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13d8 <unlink_recursive+0x228>
mov    -0x40(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%rax
je     139d <unlink_recursive+0x1ed>
mov    -0x50(%rbp),%rdi
call   1060 <unlink_recursive@plt>
mov    -0x50(%rbp),%rdi
call   1070 <true_rmdir@plt>
jmp    13a6 <unlink_recursive+0x1f6>
mov    -0x50(%rbp),%rdi
call   10a0 <true_unlink@plt>
jmp    12a6 <unlink_recursive+0xf6>
mov    -0x20(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x10(%rbp),%rdi
call   1070 <true_rmdir@plt>
jmp    13cb <unlink_recursive+0x21b>
mov    -0x10(%rbp),%rdi
call   10a0 <true_unlink@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
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
sub    $0x18,%rsp
mov    %rdi,%r15
mov    %rsp,%r13
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe15(%rip),%rsi
mov    $0x2,%edi
mov    %r15,%rdx
call   1050 <debug@plt>
lea    -0x38(%rbp),%rsi
mov    %r15,%rdi
call   10c0 <true_lstat@plt>
test   %eax,%eax
js     12f4 <unlink_recursive+0x144>
xor    %r14d,%r14d
test   %eax,%eax
jne    1312 <unlink_recursive+0x162>
mov    -0x38(%rbp),%edi
call   1080 <S_ISDIR@plt>
test   %rax,%rax
je     1327 <unlink_recursive+0x177>
mov    %r15,%rdi
call   1030 <true_opendir@plt>
test   %rax,%rax
je     1347 <unlink_recursive+0x197>
mov    %rax,%r12
mov    %r13,-0x40(%rbp)
mov    %rax,%rdi
call   10b0 <true_readdir@plt>
test   %rax,%rax
je     1331 <unlink_recursive+0x181>
mov    %rax,%r13
jmp    1281 <unlink_recursive+0xd1>
cs nopw 0x0(%rax,%rax,1)
call   1060 <unlink_recursive@plt>
mov    %rbx,%rdi
call   1070 <true_rmdir@plt>
mov    %r12,%rdi
call   10b0 <true_readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1331 <unlink_recursive+0x181>
mov    0x0(%r13),%rax
cmpb   $0x2e,(%rax)
jne    12a0 <unlink_recursive+0xf0>
movzbl 0x1(%rax),%ecx
test   %cl,%cl
je     126d <unlink_recursive+0xbd>
cmp    $0x2e,%cl
jne    12a0 <unlink_recursive+0xf0>
cmpb   $0x0,0x2(%rax)
je     126d <unlink_recursive+0xbd>
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
lea    0xd61(%rip),%rsi
call   10d0 <strcat@plt>
mov    0x0(%r13),%rsi
mov    %rbx,%rdi
call   10d0 <strcat@plt>
mov    %rbx,%rdi
lea    -0x30(%rbp),%rsi
call   10c0 <true_lstat@plt>
test   %eax,%eax
jne    134f <unlink_recursive+0x19f>
mov    -0x30(%rbp),%edi
call   1080 <S_ISDIR@plt>
mov    %rbx,%rdi
test   %rax,%rax
jne    1260 <unlink_recursive+0xb0>
call   10a0 <true_unlink@plt>
jmp    126d <unlink_recursive+0xbd>
mov    0x2cdd(%rip),%rax
mov    (%rax),%rax
mov    0x2cc3(%rip),%rcx
xor    %r14d,%r14d
cmp    (%rcx),%rax
setne  %r14b
neg    %r14d
mov    %r13,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   10a0 <true_unlink@plt>
jmp    1312 <unlink_recursive+0x162>
mov    %r12,%rdi
call   1090 <closedir@plt>
mov    %r15,%rdi
call   1070 <true_rmdir@plt>
mov    -0x40(%rbp),%r13
jmp    1312 <unlink_recursive+0x162>
mov    $0xffffffff,%r14d
jmp    1312 <unlink_recursive+0x162>
mov    %r12,%rdi
call   1090 <closedir@plt>
mov    $0xffffffff,%r14d
mov    -0x40(%rbp),%r13
jmp    1312 <unlink_recursive+0x162>

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

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