#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 ENOENT;
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, int flags) {
  struct stat st;
  DIR *dir;
  struct dirent *de;
  int fail = 0;
  if (lstat(name, &st) < 0)
    return ((flags & 2) && errno == ENOENT) ? 0 : -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, flags) < 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)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <lstat@plt>
cmp    $0x0,%rax
jge    1211 <unlink_recursive+0x71>
mov    -0x14(%rbp),%ecx
and    $0x2,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x49(%rbp)
je     11f9 <unlink_recursive+0x59>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%dl
mov    $0xffffffff,%eax
xor    %ecx,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    13e9 <unlink_recursive+0x249>
mov    -0x18(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1233 <unlink_recursive+0x93>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x4(%rbp)
jmp    13e9 <unlink_recursive+0x249>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1257 <unlink_recursive+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e9 <unlink_recursive+0x249>
mov    0x2d7a(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1377 <unlink_recursive+0x1d7>
mov    0x2d6e(%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,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd4b(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     12df <unlink_recursive+0x13f>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd30(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    12eb <unlink_recursive+0x14b>
movl   $0x2,-0x44(%rbp)
jmp    1342 <unlink_recursive+0x1a2>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xd02(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <unlink_recursive@plt>
cmp    $0x0,%eax
jge    132e <unlink_recursive+0x18e>
movl   $0x1,-0x2c(%rbp)
movl   $0x3,-0x44(%rbp)
jmp    1342 <unlink_recursive+0x1a2>
mov    0x2ca3(%rip),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
mov    %eax,-0x5c(%rbp)
test   %eax,%eax
je     1372 <unlink_recursive+0x1d2>
jmp    135c <unlink_recursive+0x1bc>
mov    -0x5c(%rbp),%eax
sub    $0x2,%eax
je     1264 <unlink_recursive+0xc4>
jmp    136d <unlink_recursive+0x1cd>
jmp    1377 <unlink_recursive+0x1d7>
jmp    1264 <unlink_recursive+0xc4>
cmpl   $0x0,-0x2c(%rbp)
jne    1391 <unlink_recursive+0x1f1>
mov    0x2c50(%rip),%rax
cmpl   $0x0,(%rax)
jge    13be <unlink_recursive+0x21e>
mov    0x2c40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x48(%rbp),%ecx
mov    0x2c28(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13e9 <unlink_recursive+0x249>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
cmp    $0x0,%rax
jge    13dd <unlink_recursive+0x23d>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e9 <unlink_recursive+0x249>
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
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r12
lea    -0x30(%rbp),%rsi
call   1090 <lstat@plt>
test   %rax,%rax
js     12a0 <unlink_recursive+0x100>
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     12c5 <unlink_recursive+0x125>
mov    %r12,%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     1323 <unlink_recursive+0x183>
mov    %r14d,-0x34(%rbp)
mov    0x2ddb(%rip),%rax
movl   $0x0,(%rax)
jmp    1213 <unlink_recursive+0x73>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rsp
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12db <unlink_recursive+0x13b>
mov    %rsp,%r13
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%r14
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r14
mov    %r14,%rsp
mov    (%rax),%r15
mov    %r15,%rdi
lea    0xdb2(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1210 <unlink_recursive+0x70>
mov    %r15,%rdi
lea    0xda0(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1210 <unlink_recursive+0x70>
mov    %r14,%rdi
lea    0xd8f(%rip),%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   10c0 <sprintf@plt>
mov    %r14,%rdi
mov    -0x34(%rbp),%esi
call   1060 <unlink_recursive@plt>
test   %eax,%eax
js     1306 <unlink_recursive+0x166>
mov    0x2d43(%rip),%rax
movl   $0x0,(%rax)
jmp    1210 <unlink_recursive+0x70>
test   $0x2,%r14b
sete   %al
mov    0x2d2a(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d11(%rip),%rdx
cmp    (%rdx),%ecx
setne  %cl
or     %al,%cl
movzbl %cl,%eax
neg    %eax
jmp    1323 <unlink_recursive+0x183>
mov    %r12,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <unlink@plt>
mov    0x2cf6(%rip),%r15
mov    (%r15),%r14d
test   %r14d,%r14d
js     1313 <unlink_recursive+0x173>
mov    %rbx,%rdi
call   1080 <closedir@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
js     1323 <unlink_recursive+0x183>
mov    %r12,%rdi
call   1050 <rmdir@plt>
jmp    1323 <unlink_recursive+0x183>
mov    %r13,%rsp
mov    0x2cc8(%rip),%r15
mov    (%r15),%r14d
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    %r14d,(%r15)
mov    $0xffffffff,%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
@ENOENT = 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, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %9
%14 = load i8*, i8** %4
%15 = call i64 @lstat(i8* %14, %struct.stat* %6)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %2
%18 = load i32, i32* %5
%19 = and i32 %18, 2
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @errno
%23 = load i32, i32* @ENOENT
%24 = icmp eq i32 %22, %23
br label %25
25: ; preds = %21, %17
%26 = phi i1 [ false, %17 ], [ %24, %21 ]
%27 = zext i1 %26 to i64
%28 = select i1 %26, i32 0, i32 -1
store i32 %28, i32* %3
br label %99
29: ; preds = %2
%30 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @S_ISDIR(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i8*, i8** %4
%36 = call i32 @unlink(i8* %35)
store i32 %36, i32* %3
br label %99
37: ; preds = %29
%38 = load i8*, i8** %4
%39 = call i32* @opendir(i8* %38)
store i32* %39, i32** %7
%40 = load i32*, i32** %7
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %43
42: ; preds = %37
store i32 -1, i32* %3
br label %99
43: ; preds = %37
store i32 0, i32* @errno
br label %44
44: ; preds = %79, %76, %43
%45 = load i32*, i32** %7
%46 = call %struct.dirent* @readdir(i32* %45)
store %struct.dirent* %46, %struct.dirent** %8
%47 = icmp ne %struct.dirent* %46, null
br i1 %47, label %48, label %80
48: ; preds = %44
%49 = load i32, i32* @PATH_MAX
%50 = zext i32 %49 to i64
%51 = call i8* @llvm.stacksave()
store i8* %51, i8** %10
%52 = alloca i8, i64 %50
store i64 %50, i64* %11
%53 = load %struct.dirent*, %struct.dirent** %8
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @strcmp(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %48
%59 = load %struct.dirent*, %struct.dirent** %8
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 @strcmp(i8* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %58, %48
store i32 2, i32* %12
br label %76
65: ; preds = %58
%66 = load i8*, i8** %4
%67 = load %struct.dirent*, %struct.dirent** %8
%68 = getelementptr inbounds %struct.dirent, %struct.dirent* %67, i32 0, i32 0
%69 = load i8*, i8** %68
%70 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %66, i8* %69)
%71 = load i32, i32* %5
%72 = call i32 @unlink_recursive(i8* %52, i32 %71)
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %65
store i32 1, i32* %9
store i32 3, i32* %12
br label %76
75: ; preds = %65
store i32 0, i32* @errno
store i32 0, i32* %12
br label %76
76: ; preds = %75, %74, %64
%77 = load i8*, i8** %10
%78 = load i32, i32* %12
switch i32 %78, label %101 [
i32 0, label %79
i32 2, label %44
i32 3, label %80
]
79: ; preds = %76
br label %44
80: ; preds = %76, %44
%81 = load i32, i32* %9
%82 = icmp ne i32 %81, 0
br i1 %82, label %86, label %83
83: ; preds = %80
%84 = load i32, i32* @errno
%85 = icmp slt i32 %84, 0
br i1 %85, label %86, label %91
86: ; preds = %83, %80
%87 = load i32, i32* @errno
store i32 %87, i32* %13
%88 = load i32*, i32** %7
%89 = call i64 @closedir(i32* %88)
%90 = load i32, i32* %13
store i32 %90, i32* @errno
store i32 -1, i32* %3
br label %99
91: ; preds = %83
%92 = load i32*, i32** %7
%93 = call i64 @closedir(i32* %92)
%94 = icmp slt i64 %93, 0
br i1 %94, label %95, label %96
95: ; preds = %91
store i32 -1, i32* %3
br label %99
96: ; preds = %91
%97 = load i8*, i8** %4
%98 = call i32 @rmdir(i8* %97)
store i32 %98, i32* %3
br label %99
99: ; preds = %96, %95, %86, %42, %34, %25
%100 = load i32, i32* %3
ret i32 %100
101: ; preds = %76
unreachable
}

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