#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 *);
struct dirent *readdir(int /*<<< orphan*/ *);
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 *entp;
    while ((entp = readdir(dp)) != 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    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%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,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1266 <unlink_dir+0xe6>
jmp    11d8 <unlink_dir+0x58>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     125d <unlink_dir+0xdd>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xe04(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1229 <unlink_dir+0xa9>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xde6(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    122e <unlink_dir+0xae>
jmp    11d8 <unlink_dir+0x58>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xdb9(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1070 <unlink_dir@plt>
jmp    11d8 <unlink_dir+0x58>
mov    -0x10(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%rdi
call   1050 <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
push   %rax
mov    %rdi,%r15
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
mov    %rax,-0x30(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
call   1040 <opendir@plt>
test   %rax,%rax
je     123f <unlink_dir+0xbf>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1237 <unlink_dir+0xb7>
mov    %rax,%r14
lea    0xe2a(%rip),%r13
jmp    11f0 <unlink_dir+0x70>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1237 <unlink_dir+0xb7>
mov    (%r14),%rdi
mov    %r13,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     11e0 <unlink_dir+0x60>
mov    (%r14),%rdi
lea    0xdf6(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     11e0 <unlink_dir+0x60>
mov    (%r14),%r8
mov    %r12,%rdi
mov    -0x30(%rbp),%rsi
lea    0xdde(%rip),%rdx
mov    %r15,%rcx
call   1060 <snprintf@plt>
mov    %r12,%rdi
call   1070 <unlink_dir@plt>
jmp    11e0 <unlink_dir+0x60>
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
mov    %r15,%rdi
call   1050 <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*
store i8* %0, i8** %2
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
%11 = load i8*, i8** %2
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %3
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %42
14: ; preds = %1
br label %15
15: ; preds = %32, %31, %14
%16 = load i32*, i32** %3
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %6
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %39
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %6
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %19
%26 = load %struct.dirent*, %struct.dirent** %6
%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 ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25, %19
br label %15
32: ; preds = %25
%33 = trunc i64 %8 to i32
%34 = load i8*, i8** %2
%35 = load %struct.dirent*, %struct.dirent** %6
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @snprintf(i8* %10, i32 %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %34, i8* %37)
br label %15
39: ; preds = %15
%40 = load i32*, i32** %3
%41 = call i32 @closedir(i32* %40)
br label %42
42: ; preds = %39, %1
%43 = load i8*, i8** %2
%44 = call i32 @unlink(i8* %43)
%45 = load i8*, i8** %2
%46 = call i32 @rmdir(i8* %45)
%47 = 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 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %28, label %7
7: ; preds = %1
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %26, label %10
10: ; preds = %7, %20
%11 = phi %struct.dirent* [ %21, %20 ], [ %8, %7 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = load i8*, i8** %12
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %10, %16, %23
%21 = call %struct.dirent* @readdir(i32* %5)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %26, label %10
23: ; preds = %16
%24 = load i8*, i8** %12
%25 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %24)
br label %20
26: ; preds = %20, %7
%27 = call i32 @closedir(i32* %5)
br label %28
28: ; preds = %26, %1
%29 = call i32 @unlink(i8* %0)
%30 = call i32 @rmdir(i8* %0)
ret void
}