#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 */
typedef int /*<<< orphan*/ subpath;
struct dirent {
  char *d_name;
};
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*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ strcmp(char *, char *);
int unlink_recursive(char *);
int unlink_dir_contents(const char *dirname) {
  DIR *dir = opendir(dirname);
  if (dir) {
    struct dirent *d;
    int rc = 0;
    while (rc == 0 && (d = readdir(dir))) {
      if (!(strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)) {
        char subpath[PATH_MAX];
        snprintf(subpath, sizeof(subpath), "%s/%s", dirname, d->d_name);
        rc = unlink_recursive(subpath);
        if (rc == -1) {
          closedir(dir);
          return -1;
        }
      }
    }
    closedir(dir);
    return rc;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<unlink_dir_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12b3 <unlink_dir_contents+0x153>
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x3d(%rbp)
jne    11b1 <unlink_dir_contents+0x51>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    11c1 <unlink_dir_contents+0x61>
jmp    129f <unlink_dir_contents+0x13f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe32(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     129a <unlink_dir_contents+0x13a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe14(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     129a <unlink_dir_contents+0x13a>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
lea    0xdc8(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rdi
call   1040 <unlink_recursive@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1272 <unlink_dir_contents+0x112>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1279 <unlink_dir_contents+0x119>
movl   $0x0,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x3c(%rbp),%eax
test   %eax,%eax
je     1295 <unlink_dir_contents+0x135>
jmp    1290 <unlink_dir_contents+0x130>
jmp    12ba <unlink_dir_contents+0x15a>
jmp    129a <unlink_dir_contents+0x13a>
jmp    118b <unlink_dir_contents+0x2b>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <unlink_dir_contents+0x15a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlink_dir_contents>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,-0x38(%rbp)
call   1030 <opendir@plt>
mov    %rax,%r15
mov    $0xffffffff,%eax
test   %r15,%r15
je     1240 <unlink_dir_contents+0xe0>
lea    0xe6f(%rip),%r12
mov    %r15,-0x30(%rbp)
jmp    11a9 <unlink_dir_contents+0x49>
nopl   0x0(%rax,%rax,1)
test   %r13d,%r13d
jne    1235 <unlink_dir_contents+0xd5>
mov    %r15,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1232 <unlink_dir_contents+0xd2>
mov    %rax,%r14
mov    (%rax),%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     11a0 <unlink_dir_contents+0x40>
mov    (%r14),%rdi
lea    0xe2a(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11a0 <unlink_dir_contents+0x40>
mov    %rsp,%r15
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%r14),%r8
mov    %rbx,%rdi
lea    0xdf6(%rip),%rdx
mov    -0x38(%rbp),%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <unlink_recursive@plt>
cmp    $0xffffffff,%eax
je     124f <unlink_dir_contents+0xef>
mov    %eax,%r13d
mov    %r15,%rsp
mov    -0x30(%rbp),%r15
jmp    11a0 <unlink_dir_contents+0x40>
xor    %r13d,%r13d
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
mov    %r15,%rsp
mov    $0xffffffff,%eax
jmp    1240 <unlink_dir_contents+0xe0>

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

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