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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ F_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int remove(char const *);
int rmdir(char const *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
scalar_t__ strcmp(char const *, char *);
scalar_t__ strlen(char const *);
int pmembench_remove_file(const char *dname) {
  int ret = 0;
  DIR *dir;
  struct dirent *d;
  char *tmp;
  dir = opendir(dname);
  if (dir == NULL) {
    if (access(dname, F_OK) == 0)
      remove(dname);
    return ret;
  }
  while ((d = readdir(dir)) != NULL) {
    if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
      continue;
    tmp = malloc(strlen(dname) + strlen(d->d_name) + 2);
    sprintf(tmp, "%s/%s", dname, d->d_name);
    ret = (d->d_type == DT_DIR) ? pmembench_remove_file(tmp) : remove(tmp);
    free(tmp);
    if (ret != 0)
      return ret;
  }
  ret = rmdir(dname);
  return ret;
}


x86 assembly (clang, -O0):
<pmembench_remove_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120b <pmembench_remove_file+0x5b>
mov    -0x10(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
cmp    $0x0,%rax
jne    1200 <pmembench_remove_file+0x50>
mov    -0x10(%rbp),%rdi
call   1040 <remove@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    131e <pmembench_remove_file+0x16e>
jmp    1210 <pmembench_remove_file+0x60>
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     130c <pmembench_remove_file+0x15c>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdcc(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1261 <pmembench_remove_file+0xb1>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdae(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1266 <pmembench_remove_file+0xb6>
jmp    1210 <pmembench_remove_file+0x60>
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xd5a(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d23(%rip),%rcx
cmp    (%rcx),%rax
jne    12d7 <pmembench_remove_file+0x127>
mov    -0x30(%rbp),%rdi
call   10c0 <pmembench_remove_file@plt>
mov    %eax,-0x3c(%rbp)
jmp    12e3 <pmembench_remove_file+0x133>
mov    -0x30(%rbp),%rdi
call   1040 <remove@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x14(%rbp)
je     1307 <pmembench_remove_file+0x157>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    131e <pmembench_remove_file+0x16e>
jmp    1210 <pmembench_remove_file+0x60>
mov    -0x10(%rbp),%rdi
call   1060 <rmdir@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pmembench_remove_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1050 <opendir@plt>
test   %rax,%rax
je     12d2 <pmembench_remove_file+0x122>
mov    %rax,%r14
mov    %rbx,0x10(%rsp)
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12ba <pmembench_remove_file+0x10a>
mov    %rax,%rbp
lea    0xe0f(%rip),%rbx
mov    %r14,0x8(%rsp)
mov    0x10(%rsp),%r12
jmp    1234 <pmembench_remove_file+0x84>
mov    %r13,%rdi
call   1040 <remove@plt>
mov    %eax,%ebp
mov    %r14,%rbx
mov    0x8(%rsp),%r14
mov    %r13,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
jne    12f2 <pmembench_remove_file+0x142>
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12ba <pmembench_remove_file+0x10a>
mov    0x0(%rbp),%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1220 <pmembench_remove_file+0x70>
mov    0x0(%rbp),%rdi
lea    0xdb0(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1220 <pmembench_remove_file+0x70>
mov    %rbx,%r14
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %rax,%rbx
mov    0x0(%rbp),%r15
mov    %r15,%rdi
call   1070 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
lea    0xd75(%rip),%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   10d0 <sprintf@plt>
mov    0x8(%rbp),%rax
mov    0x2d3c(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <pmembench_remove_file+0x4e>
mov    %r13,%rdi
call   10c0 <pmembench_remove_file@plt>
jmp    1206 <pmembench_remove_file+0x56>
mov    0x10(%rsp),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <rmdir@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <access@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    12f2 <pmembench_remove_file+0x142>
mov    %rbx,%rdi
call   1040 <remove@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@F_OK = 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"
@DT_DIR = global i64 0
define i32 @pmembench_remove_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
store i8* %0, i8** %3
store i32 0, i32* %4
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %22
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = load i32, i32* @F_OK
%15 = call i64 @access(i8* %13, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = call i32 @remove(i8* %18)
br label %20
20: ; preds = %17, %12
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %80
22: ; preds = %1
br label %23
23: ; preds = %75, %39, %22
%24 = load i32*, i32** %5
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %6
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %76
27: ; preds = %23
%28 = load %struct.dirent*, %struct.dirent** %6
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %27
%34 = load %struct.dirent*, %struct.dirent** %6
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33, %27
br label %23
40: ; preds = %33
%41 = load i8*, i8** %3
%42 = call i64 @strlen(i8* %41)
%43 = load %struct.dirent*, %struct.dirent** %6
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i64 @strlen(i8* %45)
%47 = add nsw i64 %42, %46
%48 = add nsw i64 %47, 2
%49 = call i8* @malloc(i64 %48)
store i8* %49, i8** %7
%50 = load i8*, i8** %7
%51 = load i8*, i8** %3
%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 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %51, i8* %54)
%56 = load %struct.dirent*, %struct.dirent** %6
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 1
%58 = load i64, i64* %57
%59 = load i64, i64* @DT_DIR
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %64
61: ; preds = %40
%62 = load i8*, i8** %7
%63 = call i32 @pmembench_remove_file(i8* %62)
br label %67
64: ; preds = %40
%65 = load i8*, i8** %7
%66 = call i32 @remove(i8* %65)
br label %67
67: ; preds = %64, %61
%68 = phi i32 [ %63, %61 ], [ %66, %64 ]
store i32 %68, i32* %4
%69 = load i8*, i8** %7
%70 = call i32 @free(i8* %69)
%71 = load i32, i32* %4
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %67
%74 = load i32, i32* %4
store i32 %74, i32* %2
br label %80
75: ; preds = %67
br label %23
76: ; preds = %23
%77 = load i8*, i8** %3
%78 = call i32 @rmdir(i8* %77)
store i32 %78, i32* %4
%79 = load i32, i32* %4
store i32 %79, i32* %2
br label %80
80: ; preds = %76, %73, %20
%81 = load i32, i32* %2
ret i32 %81
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@F_OK = 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"
@DT_DIR = global i64 0
define i32 @pmembench_remove_file(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %46, label %13
7: ; preds = %1
%8 = load i32, i32* @F_OK
%9 = tail call i64 @access(i8* %0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %48
11: ; preds = %7
%12 = tail call i32 @remove(i8* %0)
br label %48
13: ; preds = %4, %23
%14 = phi %struct.dirent* [ %24, %23 ], [ %5, %4 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = tail call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %13
%20 = load i8*, i8** %15
%21 = tail call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %13, %19, %42
%24 = tail call %struct.dirent* @readdir(i32* %2)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %46, label %13
26: ; preds = %19
%27 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%28 = load i8*, i8** %15
%29 = tail call i64 @strlen(i8* dereferenceable(1) %28)
%30 = add i64 %27, 2
%31 = add i64 %30, %29
%32 = tail call i8* @malloc(i64 %31)
%33 = tail call i32 @sprintf(i8* dereferenceable(1) %32, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %28)
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%35 = load i64, i64* %34
%36 = load i64, i64* @DT_DIR
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %26
%39 = tail call i32 @pmembench_remove_file(i8* %32)
br label %42
40: ; preds = %26
%41 = tail call i32 @remove(i8* %32)
br label %42
42: ; preds = %40, %38
%43 = phi i32 [ %39, %38 ], [ %41, %40 ]
%44 = tail call i32 @free(i8* %32)
%45 = icmp eq i32 %43, 0
br i1 %45, label %23, label %48
46: ; preds = %23, %4
%47 = tail call i32 @rmdir(i8* %0)
br label %48
48: ; preds = %42, %7, %11, %46
%49 = phi i32 [ %47, %46 ], [ 0, %11 ], [ 0, %7 ], [ %43, %42 ]
ret i32 %49
}