#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 const *d_name;
};
typedef int /*<<< orphan*/ cpy;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rm_node(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
int strlen(char const *);
int dir_empty(const char *dirpath) {
  DIR *dir;
  int ret;
  int pathlen = strlen(dirpath);
  struct dirent *ent;
  dir = opendir(dirpath);
  while ((ent = readdir(dir)) != NULL) {
    char cpy[pathlen + strlen(ent->d_name) + 2];
    if (ent->d_name[0] == '.' &&
        (!ent->d_name[1] || (ent->d_name[1] == '.' && !ent->d_name[2])))
      continue;
    snprintf(cpy, sizeof(cpy), "%s/%s", dirpath, ent->d_name);
    ret = rm_node(cpy);
    if (ret == -1)
      goto close_dir;
  }
  ret = 0;
close_dir:
  closedir(dir);
  return ret;
}


x86 assembly (clang, -O0):
<dir_empty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12b6 <dir_empty+0x156>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%ecx
mov    %eax,%edx
mov    %ecx,%eax
mov    %edx,%ecx
lea    0x2(%rax,%rcx,1),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1237 <dir_empty+0xd7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmpb   $0x0,0x1(%rax)
je     122b <dir_empty+0xcb>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    1237 <dir_empty+0xd7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmpb   $0x0,0x2(%rax)
jne    1237 <dir_empty+0xd7>
movl   $0x2,-0x34(%rbp)
jmp    1281 <dir_empty+0x121>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
lea    0xdad(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x40(%rbp),%rdi
call   1060 <rm_node@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    127a <dir_empty+0x11a>
movl   $0x4,-0x34(%rbp)
jmp    1281 <dir_empty+0x121>
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x34(%rbp),%eax
mov    %eax,-0x50(%rbp)
test   %eax,%eax
je     12b1 <dir_empty+0x151>
jmp    129b <dir_empty+0x13b>
mov    -0x50(%rbp),%eax
sub    $0x2,%eax
je     1185 <dir_empty+0x25>
jmp    12ac <dir_empty+0x14c>
jmp    12bd <dir_empty+0x15d>
jmp    1185 <dir_empty+0x25>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x14(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_empty>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %rbx,-0x30(%rbp)
mov    %rbx,%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1232 <dir_empty+0xd2>
mov    %rax,%r15
add    $0x2,%r12d
jmp    11e5 <dir_empty+0x85>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
mov    %eax,%esi
lea    0xe44(%rip),%rdx
mov    -0x30(%rbp),%rcx
call   1050 <snprintf@plt>
mov    %r13,%rdi
call   1060 <rm_node@plt>
cmp    $0xffffffff,%eax
je     1229 <dir_empty+0xc9>
mov    %r14,%rsp
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1224 <dir_empty+0xc4>
mov    (%r15),%rdi
call   1040 <strlen@plt>
mov    %rsp,%r14
add    %r12d,%eax
lea    0xf(%rax),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%r13
sub    %rcx,%r13
mov    %r13,%rsp
mov    (%r15),%r8
cmpb   $0x2e,(%r8)
jne    11b0 <dir_empty+0x50>
movzbl 0x1(%r8),%ecx
test   %cl,%cl
je     11d2 <dir_empty+0x72>
cmp    $0x2e,%cl
jne    11b0 <dir_empty+0x50>
cmpb   $0x0,0x2(%r8)
jne    11b0 <dir_empty+0x50>
jmp    11d2 <dir_empty+0x72>
xor    %r14d,%r14d
jmp    1232 <dir_empty+0xd2>
mov    %r14,%rsp
mov    $0xffffffff,%r14d
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define i32 @dir_empty(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.dirent*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %5
%12 = load i8*, i8** %2
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %3
br label %14
14: ; preds = %74, %71, %1
%15 = load i32*, i32** %3
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %6
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %75
18: ; preds = %14
%19 = load i32, i32* %5
%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 i32 @strlen(i8* %22)
%24 = add nsw i32 %19, %23
%25 = add nsw i32 %24, 2
%26 = zext i32 %25 to i64
%27 = call i8* @llvm.stacksave()
store i8* %27, i8** %7
%28 = alloca i8, i64 %26
store i64 %26, i64* %8
%29 = load %struct.dirent*, %struct.dirent** %6
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = getelementptr inbounds i8, i8* %31, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 46
br i1 %35, label %36, label %59
36: ; preds = %18
%37 = load %struct.dirent*, %struct.dirent** %6
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8, i8* %40
%42 = icmp ne i8 %41, 0
br i1 %42, label %43, label %58
43: ; preds = %36
%44 = load %struct.dirent*, %struct.dirent** %6
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 46
br i1 %50, label %51, label %59
51: ; preds = %43
%52 = load %struct.dirent*, %struct.dirent** %6
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = getelementptr inbounds i8, i8* %54, i64 2
%56 = load i8, i8* %55
%57 = icmp ne i8 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %51, %36
store i32 2, i32* %9
br label %71
59: ; preds = %51, %43, %18
%60 = trunc i64 %26 to i32
%61 = load i8*, i8** %2
%62 = load %struct.dirent*, %struct.dirent** %6
%63 = getelementptr inbounds %struct.dirent, %struct.dirent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
%65 = call i32 @snprintf(i8* %28, i32 %60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %61, i8* %64)
%66 = call i32 @rm_node(i8* %28)
store i32 %66, i32* %4
%67 = load i32, i32* %4
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %70
69: ; preds = %59
store i32 4, i32* %9
br label %71
70: ; preds = %59
store i32 0, i32* %9
br label %71
71: ; preds = %69, %70, %58
%72 = load i8*, i8** %7
%73 = load i32, i32* %9
switch i32 %73, label %80 [
i32 0, label %74
i32 2, label %14
i32 4, label %76
]
74: ; preds = %71
br label %14
75: ; preds = %14
store i32 0, i32* %4
br label %76
76: ; preds = %75, %71
%77 = load i32*, i32** %3
%78 = call i32 @closedir(i32* %77)
%79 = load i32, i32* %4
ret i32 %79
80: ; preds = %71
unreachable
}

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