#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*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *fs_basepath(int /*<<< orphan*/);
scalar_t__ fs_is_dir(char const *);
int /*<<< orphan*/ fs_rm(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int fs_empty_dir(const char *restrict dirpath) {
  DIR *read_this_dir;
  struct dirent *dir_ent;
  char *restrict dir_ent_path;
  if (fs_is_dir(dirpath)) {
    read_this_dir = opendir(dirpath);
    while ((dir_ent = readdir(read_this_dir))) {
      dir_ent_path = fs_basepath(dir_ent->d_name);
      fs_rm(dir_ent_path);
      free(dir_ent_path);
    }
    closedir(read_this_dir);
  }
  return 0;
}


x86 assembly (clang, -O0):
<fs_empty_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <fs_is_dir@plt>
cmp    $0x0,%rax
je     11e2 <fs_empty_dir+0x72>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11d9 <fs_empty_dir+0x69>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1040 <fs_basepath@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <fs_rm@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    119c <fs_empty_dir+0x2c>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_empty_dir>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1070 <fs_is_dir@plt>
test   %rax,%rax
je     11c1 <fs_empty_dir+0x51>
mov    %rbx,%rdi
call   1050 <opendir@plt>
mov    %rax,%r14
mov    %rax,%rdi
nop
call   1080 <readdir@plt>
test   %rax,%rax
je     11b9 <fs_empty_dir+0x49>
mov    (%rax),%edi
call   1040 <fs_basepath@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <fs_rm@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
jmp    1190 <fs_empty_dir+0x20>
mov    %r14,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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