#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 */
int PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ closedir(int /*<<< orphan*/ *);
int errno;
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rmdir(char const *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int unlink(char const *);
int unlink_recursive(const char *name) {
  struct stat st;
  DIR *dir;
  struct dirent *de;
  int fail = 0;
  if (lstat(name, &st) < 0)
    return -1;
  if (!S_ISDIR(st.st_mode))
    return unlink(name);
  dir = opendir(name);
  if (dir == NULL)
    return -1;
  errno = 0;
  while ((de = readdir(dir)) != NULL) {
    char dn[PATH_MAX];
    if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
      continue;
    sprintf(dn, "%s/%s", name, de->d_name);
    if (unlink_recursive(dn) < 0) {
      fail = 1;
      break;
    }
    errno = 0;
  }
  if (fail || errno < 0) {
    int save = errno;
    closedir(dir);
    errno = save;
    return -1;
  }
  if (closedir(dir) < 0)
    return -1;
  return rmdir(name);
}


x86 assembly (clang, -O0):
<unlink_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <lstat@plt>
cmp    $0x0,%rax
jge    11d6 <unlink_recursive+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <unlink_recursive+0x20b>
mov    -0x18(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11f8 <unlink_recursive+0x58>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x4(%rbp)
jmp    13ab <unlink_recursive+0x20b>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121c <unlink_recursive+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <unlink_recursive+0x20b>
mov    0x2db5(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1339 <unlink_recursive+0x199>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%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,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd86(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     12a4 <unlink_recursive+0x104>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd6b(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    12b0 <unlink_recursive+0x110>
movl   $0x2,-0x44(%rbp)
jmp    1304 <unlink_recursive+0x164>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xd3d(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x50(%rbp),%rdi
call   1060 <unlink_recursive@plt>
cmp    $0x0,%eax
jge    12f0 <unlink_recursive+0x150>
movl   $0x1,-0x2c(%rbp)
movl   $0x3,-0x44(%rbp)
jmp    1304 <unlink_recursive+0x164>
mov    0x2ce1(%rip),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
mov    %eax,-0x54(%rbp)
test   %eax,%eax
je     1334 <unlink_recursive+0x194>
jmp    131e <unlink_recursive+0x17e>
mov    -0x54(%rbp),%eax
sub    $0x2,%eax
je     1229 <unlink_recursive+0x89>
jmp    132f <unlink_recursive+0x18f>
jmp    1339 <unlink_recursive+0x199>
jmp    1229 <unlink_recursive+0x89>
cmpl   $0x0,-0x2c(%rbp)
jne    1353 <unlink_recursive+0x1b3>
mov    0x2c8e(%rip),%rax
cmpl   $0x0,(%rax)
jge    1380 <unlink_recursive+0x1e0>
mov    0x2c7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x48(%rbp),%ecx
mov    0x2c66(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <unlink_recursive+0x20b>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
cmp    $0x0,%rax
jge    139f <unlink_recursive+0x1ff>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <unlink_recursive+0x20b>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
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,%r15
lea    -0x30(%rbp),%rsi
call   1090 <lstat@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     12f3 <unlink_recursive+0x153>
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     128d <unlink_recursive+0xed>
mov    %r15,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12f3 <unlink_recursive+0x153>
mov    %rax,%rbx
mov    0x2de4(%rip),%rax
movl   $0x0,(%rax)
jmp    1203 <unlink_recursive+0x63>
nopl   0x0(%rax)
mov    %r12,%rsp
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12a3 <unlink_recursive+0x103>
mov    %rsp,%r12
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%r14
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r14
mov    %r14,%rsp
mov    (%rax),%r13
mov    %r13,%rdi
lea    0xdc2(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1200 <unlink_recursive+0x60>
mov    %r13,%rdi
lea    0xdb0(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1200 <unlink_recursive+0x60>
mov    %r14,%rdi
lea    0xd9f(%rip),%rsi
mov    %r15,%rdx
mov    %r13,%rcx
call   10c0 <sprintf@plt>
mov    %r14,%rdi
call   1060 <unlink_recursive@plt>
test   %eax,%eax
js     12d3 <unlink_recursive+0x133>
mov    0x2d56(%rip),%rax
movl   $0x0,(%rax)
jmp    1200 <unlink_recursive+0x60>
mov    %r15,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <unlink@plt>
mov    0x2d2e(%rip),%r12
mov    (%r12),%r14d
test   %r14d,%r14d
js     12e1 <unlink_recursive+0x141>
mov    %rbx,%rdi
call   1080 <closedir@plt>
test   %rax,%rax
mov    $0xffffffff,%r14d
js     12f3 <unlink_recursive+0x153>
mov    %r15,%rdi
call   1050 <rmdir@plt>
mov    %eax,%r14d
jmp    12f3 <unlink_recursive+0x153>
mov    %r12,%rsp
mov    0x2cfb(%rip),%r12
mov    (%r12),%r14d
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    %r14d,(%r12)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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