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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
scalar_t__ readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
int /*<<< orphan*/ rmdir(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ unlink(char const *);
void unlink_dir(const char *path) {
  DIR *dp;
  char buf[PATH_MAX];
  if ((dp = opendir(path)) != NULL) {
    struct dirent entbuf, *entp;
    while (readdir_r(dp, &entbuf, &entp) == 0 && entp != NULL) {
      if (strcmp(entp->d_name, ".") == 0 || strcmp(entp->d_name, "..") == 0)
        continue;
      snprintf(buf, sizeof(buf), "%s/%s", path, entp->d_name);
      unlink_dir(buf);
    }
    closedir(dp);
  }
  unlink(path);
  rmdir(path);
}


x86 assembly (clang, -O0):
<unlink_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     128d <unlink_dir+0x10d>
jmp    11d8 <unlink_dir+0x58>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <readdir_r@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x41(%rbp)
jne    1206 <unlink_dir+0x86>
cmpq   $0x0,-0x30(%rbp)
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1216 <unlink_dir+0x96>
jmp    1284 <unlink_dir+0x104>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xddd(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1250 <unlink_dir+0xd0>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdbf(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1255 <unlink_dir+0xd5>
jmp    11d8 <unlink_dir+0x58>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    (%rax),%r8
lea    0xd92(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x38(%rbp),%rdi
call   1080 <unlink_dir@plt>
jmp    11d8 <unlink_dir+0x58>
mov    -0x10(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%rdi
call   1060 <rmdir@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlink_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
mov    %rax,-0x40(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rdi,-0x38(%rbp)
call   1050 <opendir@plt>
test   %rax,%rax
je     1272 <unlink_dir+0xf2>
mov    %rax,%rbx
lea    -0x48(%rbp),%rsi
lea    -0x30(%rbp),%rdx
mov    %rax,%rdi
call   1040 <readdir_r@plt>
test   %rax,%rax
jne    126a <unlink_dir+0xea>
mov    -0x30(%rbp),%rax
test   %rax,%rax
je     126a <unlink_dir+0xea>
lea    0xe10(%rip),%r13
lea    -0x48(%rbp),%r14
lea    -0x30(%rbp),%r15
nopl   0x0(%rax)
mov    (%rax),%rdi
mov    %r13,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     124e <unlink_dir+0xce>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xde2(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     124e <unlink_dir+0xce>
mov    -0x30(%rbp),%rax
mov    (%rax),%r8
mov    %r12,%rdi
mov    -0x40(%rbp),%rsi
lea    0xdc6(%rip),%rdx
mov    -0x38(%rbp),%rcx
call   1070 <snprintf@plt>
mov    %r12,%rdi
call   1080 <unlink_dir@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <readdir_r@plt>
test   %rax,%rax
jne    126a <unlink_dir+0xea>
mov    -0x30(%rbp),%rax
test   %rax,%rax
jne    1200 <unlink_dir+0x80>
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    -0x38(%rbp),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
call   1060 <rmdir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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