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

/* Variables and functions */
scalar_t__ DT_REG;
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 *);
int /*<<< orphan*/ unlink(char *);
void rm(const char *path) {
  DIR *dir = opendir(path);
  if (!dir)
    return;
  struct dirent *entry = NULL;
  while ((entry = readdir(dir))) {
    if (entry->d_type != DT_REG)
      continue;
    char file[PATH_MAX];
    snprintf(file, sizeof(file), "%s/%s", path, entry->d_name);
    unlink(file);
  }
  closedir(dir);
  rmdir(path);
}


x86 assembly (clang, -O0):
<rm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1189 <rm+0x29>
jmp    1231 <rm+0xd1>
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     121f <rm+0xbf>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <rm+0x64>
jmp    1191 <rm+0x31>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%r8
lea    0xdfb(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
jmp    1191 <rm+0x31>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x8(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rm>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     1209 <rm+0xa9>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11f9 <rm+0x99>
mov    0x2e42(%rip),%r13
jmp    11ad <rm+0x4d>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11f9 <rm+0x99>
mov    (%rax),%rcx
cmp    0x0(%r13),%rcx
jne    11a0 <rm+0x40>
mov    %rsp,%r15
mov    0x2e30(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rbx
mov    %rsi,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    0x8(%rax),%r8
mov    %rbx,%rdi
lea    0xe1c(%rip),%rdx
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %r15,%rsp
jmp    11a0 <rm+0x40>
mov    %r12,%rdi
call   1070 <closedir@plt>
mov    %r14,%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 { i64, i8* }
@DT_REG = global i64 0
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define void @rm(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
br label %42
12: ; preds = %1
store %struct.dirent* null, %struct.dirent** %4
br label %13
13: ; preds = %24, %23, %12
%14 = load i32*, i32** %3
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %4
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %37
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %4
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @DT_REG
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %17
br label %13
24: ; preds = %17
%25 = load i32, i32* @PATH_MAX
%26 = zext i32 %25 to i64
%27 = call i8* @llvm.stacksave()
store i8* %27, i8** %5
%28 = alloca i8, i64 %26
store i64 %26, i64* %6
%29 = trunc i64 %26 to i32
%30 = load i8*, i8** %2
%31 = load %struct.dirent*, %struct.dirent** %4
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 1
%33 = load i8*, i8** %32
%34 = call i32 @snprintf(i8* %28, i32 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %30, i8* %33)
%35 = call i32 @unlink(i8* %28)
%36 = load i8*, i8** %5
br label %13
37: ; preds = %13
%38 = load i32*, i32** %3
%39 = call i32 @closedir(i32* %38)
%40 = load i8*, i8** %2
%41 = call i32 @rmdir(i8* %40)
br label %42
42: ; preds = %37, %11
ret void
}

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