#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 *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*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
scalar_t__ string_match(char const *, char *);
int /*<<< orphan*/ unlink(char *);
int clean_dir(const char *dirname, const char *delete_pattern) {
  char subdir[PATH_MAX];
  struct dirent *d;
  DIR *dir;
  dir = opendir(dirname);
  if (!dir)
    return 0;
  while ((d = readdir(dir))) {
    if (!strcmp(d->d_name, "."))
      continue;
    if (!strcmp(d->d_name, ".."))
      continue;
    sprintf(subdir, "%s/%s", dirname, d->d_name);
    clean_dir(subdir, delete_pattern);
    if (string_match(delete_pattern, d->d_name)) {
      unlink(d->d_name);
    }
  }
  closedir(dir);
  return 1;
}


x86 assembly (clang, -O0):
<clean_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11e7 <clean_dir+0x67>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12af <clean_dir+0x12f>
jmp    11ec <clean_dir+0x6c>
mov    -0x38(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1298 <clean_dir+0x118>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf0(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1224 <clean_dir+0xa4>
jmp    11ec <clean_dir+0x6c>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdce(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1245 <clean_dir+0xc5>
jmp    11ec <clean_dir+0x6c>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xda8(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <clean_dir@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   10a0 <string_match@plt>
cmp    $0x0,%rax
je     1293 <clean_dir+0x113>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1030 <unlink@plt>
jmp    11ec <clean_dir+0x6c>
mov    -0x38(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clean_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rdi,-0x30(%rbp)
call   1040 <opendir@plt>
test   %rax,%rax
je     1263 <clean_dir+0xe3>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1254 <clean_dir+0xd4>
mov    %rax,%r14
jmp    11f0 <clean_dir+0x70>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1254 <clean_dir+0xd4>
mov    (%r14),%r13
mov    %r13,%rdi
lea    0xe04(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e0 <clean_dir+0x60>
mov    %r13,%rdi
lea    0xdf0(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e0 <clean_dir+0x60>
mov    %r12,%rdi
lea    0xde0(%rip),%rsi
mov    -0x30(%rbp),%rdx
mov    %r13,%rcx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <clean_dir@plt>
mov    (%r14),%rsi
mov    %r15,%rdi
call   10a0 <string_match@plt>
test   %rax,%rax
je     11e0 <clean_dir+0x60>
mov    (%r14),%rdi
call   1030 <unlink@plt>
jmp    11e0 <clean_dir+0x60>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    $0x1,%eax
jmp    1265 <clean_dir+0xe5>
xor    %eax,%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* }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @clean_dir(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.dirent*
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i8*, i8** %4
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %10
br label %62
20: ; preds = %2
br label %21
21: ; preds = %58, %38, %31, %20
%22 = load i32*, i32** %9
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %8
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %59
25: ; preds = %21
%26 = load %struct.dirent*, %struct.dirent** %8
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %25
br label %21
32: ; preds = %25
%33 = load %struct.dirent*, %struct.dirent** %8
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strcmp(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %32
br label %21
39: ; preds = %32
%40 = load i8*, i8** %4
%41 = load %struct.dirent*, %struct.dirent** %8
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %40, i8* %43)
%45 = load i8*, i8** %5
%46 = call i32 @clean_dir(i8* %14, i8* %45)
%47 = load i8*, i8** %5
%48 = load %struct.dirent*, %struct.dirent** %8
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i64 @string_match(i8* %47, i8* %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %39
%54 = load %struct.dirent*, %struct.dirent** %8
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = call i32 @unlink(i8* %56)
br label %58
58: ; preds = %53, %39
br label %21
59: ; preds = %21
%60 = load i32*, i32** %9
%61 = call i32 @closedir(i32* %60)
store i32 1, i32* %3
store i32 1, i32* %10
br label %62
62: ; preds = %59, %19
%63 = load i8*, i8** %6
%64 = load i32, i32* %3
ret i32 %64
}

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