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

/* Variables and functions */
int /*<<< orphan*/ ace_fs_does_directory_exist(char *);
int /*<<< orphan*/ ace_fs_is_current_or_parent_directory(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
bool ace_tu_fs_is_directory_empty(char *dirpath) {
  if (!ace_fs_does_directory_exist(dirpath))
    return true;
  bool is_empty = true;
  struct dirent *entity;
  DIR *directory = opendir(dirpath);
  while ((entity = readdir(directory)) != NULL) {
    if (!ace_fs_is_current_or_parent_directory(entity->d_name)) {
      is_empty = false;
      break;
    }
  }
  closedir(directory);
  return is_empty;
}


x86 assembly (clang, -O0):
<ace_tu_fs_is_directory_empty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <ace_fs_does_directory_exist@plt>
cmp    $0x0,%eax
jne    117a <ace_tu_fs_is_directory_empty+0x2a>
movl   $0x1,-0x4(%rbp)
jmp    11d9 <ace_tu_fs_is_directory_empty+0x89>
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ca <ace_tu_fs_is_directory_empty+0x7a>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1070 <ace_fs_is_current_or_parent_directory@plt>
cmp    $0x0,%eax
jne    11c5 <ace_tu_fs_is_directory_empty+0x75>
movl   $0x0,-0x14(%rbp)
jmp    11ca <ace_tu_fs_is_directory_empty+0x7a>
jmp    118e <ace_tu_fs_is_directory_empty+0x3e>
mov    -0x28(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ace_tu_fs_is_directory_empty>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1060 <ace_fs_does_directory_exist@plt>
test   %eax,%eax
je     118c <ace_tu_fs_is_directory_empty+0x3c>
mov    %rbx,%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     1193 <ace_tu_fs_is_directory_empty+0x43>
mov    (%rax),%edi
call   1070 <ace_fs_is_current_or_parent_directory@plt>
test   %eax,%eax
jne    1170 <ace_tu_fs_is_directory_empty+0x20>
xor    %ebp,%ebp
jmp    1198 <ace_tu_fs_is_directory_empty+0x48>
mov    $0x1,%ebp
jmp    11a0 <ace_tu_fs_is_directory_empty+0x50>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
define i32 @ace_tu_fs_is_directory_empty(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.dirent*
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @ace_fs_does_directory_exist(i8* %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 1, i32* %2
br label %30
11: ; preds = %1
store i32 1, i32* %4
%12 = load i8*, i8** %3
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %6
br label %14
14: ; preds = %25, %11
%15 = load i32*, i32** %6
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %5
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %5
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @ace_fs_is_current_or_parent_directory(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %18
store i32 0, i32* %4
br label %26
25: ; preds = %18
br label %14
26: ; preds = %24, %14
%27 = load i32*, i32** %6
%28 = call i32 @closedir(i32* %27)
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %30
30: ; preds = %26, %10
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
define i32 @ace_tu_fs_is_directory_empty(i8* %0) {
%2 = tail call i32 @ace_fs_does_directory_exist(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = tail call i32* @opendir(i8* %0)
br label %6
6: ; preds = %9, %4
%7 = tail call %struct.dirent* @readdir(i32* %5)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %14, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @ace_fs_is_current_or_parent_directory(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %6
14: ; preds = %9, %6
%15 = phi i32 [ 1, %6 ], [ 0, %9 ]
%16 = tail call i32 @closedir(i32* %5)
br label %17
17: ; preds = %1, %14
%18 = phi i32 [ %15, %14 ], [ 1, %1 ]
ret i32 %18
}