#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 /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *realloc(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
scalar_t__ unlink(char *);
int clear_hooks(char *dirname) {
  DIR *dir;
  struct dirent *ent;
  char *path = NULL;
  char *new_path;
  int path_len;
  int dirname_len;
  int status = 0;
  dirname_len = strlen(dirname);
  if ((dir = opendir(dirname)) != NULL) {
    while ((ent = readdir(dir)) != NULL) {
      if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
        continue;
      }
      path_len = dirname_len + strlen(ent->d_name) + 2;
      if (path == NULL) {
        path = malloc(path_len);
        if (path == NULL) {
          perror("malloc path failed");
          closedir(dir);
          return -1;
        }
      } else {
        new_path = realloc(path, path_len);
        if (new_path == NULL) {
          perror("realloc path failed");
          closedir(dir);
          return -1;
        }
        path = new_path;
      }
      snprintf(path, path_len, "%s/%s", dirname, ent->d_name);
      if (unlink(path) != 0) {
        perror("unlink failed");
        status = -1;
      }
    }
    closedir(dir);
    if (path != NULL) {
      free(path);
    }
  } else {
    status = -1;
  }
  return status;
}


x86 assembly (clang, -O0):
<clear_hooks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     135f <clear_hooks+0x1af>
jmp    11f3 <clear_hooks+0x43>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     133d <clear_hooks+0x18d>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xde9(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1244 <clear_hooks+0x94>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdcb(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1249 <clear_hooks+0x99>
jmp    11f3 <clear_hooks+0x43>
mov    -0x38(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12b0 <clear_hooks+0x100>
mov    -0x34(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12ab <clear_hooks+0xfb>
lea    0xd72(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136c <clear_hooks+0x1bc>
jmp    12f4 <clear_hooks+0x144>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10c0 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12ec <clear_hooks+0x13c>
lea    0xd44(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136c <clear_hooks+0x1bc>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
lea    0xd1d(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <unlink@plt>
cmp    $0x0,%rax
je     1338 <clear_hooks+0x188>
lea    0xd04(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x3c(%rbp)
jmp    11f3 <clear_hooks+0x43>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
cmpq   $0x0,-0x28(%rbp)
je     135a <clear_hooks+0x1aa>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1366 <clear_hooks+0x1b6>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clear_hooks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbp
call   1060 <strlen@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
call   1050 <opendir@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1312 <clear_hooks+0x162>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1307 <clear_hooks+0x157>
mov    %rax,%r14
mov    %rbp,0x10(%rsp)
add    $0x2,%r12d
mov    %r12d,0x4(%rsp)
xor    %r15d,%r15d
lea    0xdf2(%rip),%rbp
lea    0xdea(%rip),%r12
xor    %r13d,%r13d
jmp    1234 <clear_hooks+0x84>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12f0 <clear_hooks+0x140>
mov    (%r14),%rdi
mov    %rbp,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1220 <clear_hooks+0x70>
mov    (%r14),%rdi
mov    %r12,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1220 <clear_hooks+0x70>
mov    %r15,0x8(%rsp)
mov    (%r14),%rdi
call   1060 <strlen@plt>
mov    %eax,%r15d
add    0x4(%rsp),%r15d
test   %r13,%r13
je     1286 <clear_hooks+0xd6>
mov    %r13,%rdi
mov    %r15d,%esi
call   10c0 <realloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    129a <clear_hooks+0xea>
jmp    132e <clear_hooks+0x17e>
mov    %r15d,%edi
call   10b0 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
je     1337 <clear_hooks+0x187>
mov    (%r14),%r8
mov    %r13,%rdi
mov    %r15d,%esi
lea    0xd80(%rip),%rdx
mov    0x10(%rsp),%rcx
call   1070 <snprintf@plt>
mov    %r13,%rdi
call   1040 <unlink@plt>
test   %rax,%rax
mov    0x8(%rsp),%r15
jne    12dc <clear_hooks+0x12c>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1234 <clear_hooks+0x84>
jmp    1324 <clear_hooks+0x174>
lea    0xd4d(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%r15d
jmp    12c6 <clear_hooks+0x116>
mov    %rbx,%rdi
call   1080 <closedir@plt>
test   %r13,%r13
je     1312 <clear_hooks+0x162>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    1312 <clear_hooks+0x162>
mov    %rbx,%rdi
call   1080 <closedir@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1080 <closedir@plt>
jmp    12fd <clear_hooks+0x14d>
lea    0xce1(%rip),%rdi
jmp    133e <clear_hooks+0x18e>
lea    0xcc5(%rip),%rdi
call   10d0 <perror@plt>
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    $0xffffffff,%r15d
jmp    1312 <clear_hooks+0x162>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [19 x i8] c"malloc path failed\00"
@.str.3 = [20 x i8] c"realloc path failed\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [14 x i8] c"unlink failed\00"
define i32 @clear_hooks(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %6
store i32 0, i32* %10
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %9
%13 = load i8*, i8** %3
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %4
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %89
16: ; preds = %1
br label %17
17: ; preds = %79, %33, %16
%18 = load i32*, i32** %4
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %5
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %80
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %5
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %21
%28 = load %struct.dirent*, %struct.dirent** %5
%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 ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27, %21
br label %17
34: ; preds = %27
%35 = load i32, i32* %9
%36 = load %struct.dirent*, %struct.dirent** %5
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 @strlen(i8* %38)
%40 = add nsw i32 %35, %39
%41 = add nsw i32 %40, 2
store i32 %41, i32* %8
%42 = load i8*, i8** %6
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %54
44: ; preds = %34
%45 = load i32, i32* %8
%46 = call i8* @malloc(i32 %45)
store i8* %46, i8** %6
%47 = load i8*, i8** %6
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %53
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%51 = load i32*, i32** %4
%52 = call i32 @closedir(i32* %51)
store i32 -1, i32* %2
br label %92
53: ; preds = %44
br label %66
54: ; preds = %34
%55 = load i8*, i8** %6
%56 = load i32, i32* %8
%57 = call i8* @realloc(i8* %55, i32 %56)
store i8* %57, i8** %7
%58 = load i8*, i8** %7
%59 = icmp eq i8* %58, null
br i1 %59, label %60, label %64
60: ; preds = %54
%61 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%62 = load i32*, i32** %4
%63 = call i32 @closedir(i32* %62)
store i32 -1, i32* %2
br label %92
64: ; preds = %54
%65 = load i8*, i8** %7
store i8* %65, i8** %6
br label %66
66: ; preds = %64, %53
%67 = load i8*, i8** %6
%68 = load i32, i32* %8
%69 = load i8*, i8** %3
%70 = load %struct.dirent*, %struct.dirent** %5
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 0
%72 = load i8*, i8** %71
%73 = call i32 @snprintf(i8* %67, i32 %68, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %69, i8* %72)
%74 = load i8*, i8** %6
%75 = call i64 @unlink(i8* %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %66
%78 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %10
br label %79
79: ; preds = %77, %66
br label %17
80: ; preds = %17
%81 = load i32*, i32** %4
%82 = call i32 @closedir(i32* %81)
%83 = load i8*, i8** %6
%84 = icmp ne i8* %83, null
br i1 %84, label %85, label %88
85: ; preds = %80
%86 = load i8*, i8** %6
%87 = call i32 @free(i8* %86)
br label %88
88: ; preds = %85, %80
br label %90
89: ; preds = %1
store i32 -1, i32* %10
br label %90
90: ; preds = %89, %88
%91 = load i32, i32* %10
store i32 %91, i32* %2
br label %92
92: ; preds = %90, %60, %49
%93 = load i32, i32* %2
ret i32 %93
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [19 x i8] c"malloc path failed\00"
@.str.3 = [20 x i8] c"realloc path failed\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [14 x i8] c"unlink failed\00"
define i32 @clear_hooks(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %67, label %5
5: ; preds = %1
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @closedir(i32* %3)
br label %67
10: ; preds = %5
%11 = add i32 %2, 2
br label %12
12: ; preds = %10, %54
%13 = phi %struct.dirent* [ %6, %10 ], [ %56, %54 ]
%14 = phi i32 [ 0, %10 ], [ %55, %54 ]
%15 = phi i8* [ null, %10 ], [ %47, %54 ]
br label %16
16: ; preds = %12, %26
%17 = phi %struct.dirent* [ %13, %12 ], [ %27, %26 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %16
%23 = load i8*, i8** %18
%24 = tail call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22, %16
%27 = tail call %struct.dirent* @readdir(i32* %3)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %60, label %16
29: ; preds = %22
%30 = load i8*, i8** %18
%31 = tail call i32 @strlen(i8* %30)
%32 = add i32 %11, %31
%33 = icmp eq i8* %15, null
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = tail call i8* @malloc(i32 %32)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%39 = tail call i32 @closedir(i32* %3)
br label %67
40: ; preds = %29
%41 = tail call i8* @realloc(i8* %15, i32 %32)
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%45 = tail call i32 @closedir(i32* %3)
br label %67
46: ; preds = %40, %34
%47 = phi i8* [ %35, %34 ], [ %41, %40 ]
%48 = load i8*, i8** %18
%49 = tail call i32 @snprintf(i8* %47, i32 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %48)
%50 = tail call i64 @unlink(i8* %47)
%51 = icmp eq i64 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %46
%53 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %46
%55 = phi i32 [ -1, %52 ], [ %14, %46 ]
%56 = tail call %struct.dirent* @readdir(i32* %3)
%57 = icmp eq %struct.dirent* %56, null
br i1 %57, label %58, label %12
58: ; preds = %54
%59 = tail call i32 @closedir(i32* %3)
br label %63
60: ; preds = %26
%61 = tail call i32 @closedir(i32* %3)
%62 = icmp eq i8* %15, null
br i1 %62, label %67, label %63
63: ; preds = %58, %60
%64 = phi i32 [ %55, %58 ], [ %14, %60 ]
%65 = phi i8* [ %47, %58 ], [ %15, %60 ]
%66 = tail call i32 @free(i8* %65)
br label %67
67: ; preds = %8, %63, %60, %1, %43, %37
%68 = phi i32 [ -1, %37 ], [ -1, %43 ], [ %64, %63 ], [ %14, %60 ], [ -1, %1 ], [ 0, %8 ]
ret i32 %68
}