#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct pevent {
  int dummy;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int stat(char const *, struct stat *);
scalar_t__ strcmp(char const *, char const *);
int strlen(char const *);
void load_plugins_dir(struct pevent *pevent, const char *suffix,
                      const char *path,
                      void (*load_plugin)(struct pevent *pevent,
                                          const char *path, const char *name,
                                          void *data),
                      void *data) {
  struct dirent *dent;
  struct stat st;
  DIR *dir;
  int ret;
  ret = stat(path, &st);
  if (ret < 0) {
    return;
  }
  if (!S_ISDIR(st.st_mode)) {
    return;
  }
  dir = opendir(path);
  if (!dir) {
    return;
  }
  while ((dent = readdir(dir))) {
    const char *name = dent->d_name;
    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
      continue;
    }
    if (strcmp(name + (strlen(name) - strlen(suffix)), suffix) != 0) {
      continue;
    }
    load_plugin(pevent, path, name, data);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<load_plugins_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11ab <load_plugins_dir+0x3b>
jmp    12a7 <load_plugins_dir+0x137>
mov    -0x38(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11c1 <load_plugins_dir+0x51>
jmp    12a7 <load_plugins_dir+0x137>
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11de <load_plugins_dir+0x6e>
jmp    12a7 <load_plugins_dir+0x137>
jmp    11e3 <load_plugins_dir+0x73>
mov    -0x40(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     129e <load_plugins_dir+0x12e>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1239 <load_plugins_dir+0xc9>
mov    -0x50(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    123e <load_plugins_dir+0xce>
jmp    11e3 <load_plugins_dir+0x73>
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
sub    %ecx,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1283 <load_plugins_dir+0x113>
jmp    11e3 <load_plugins_dir+0x73>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   *%rax
jmp    11e3 <load_plugins_dir+0x73>
mov    -0x40(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_plugins_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
lea    0x8(%rsp),%rsi
mov    %rdx,%rdi
call   1080 <stat@plt>
test   %eax,%eax
js     1263 <load_plugins_dir+0xf3>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     1263 <load_plugins_dir+0xf3>
mov    %r12,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1263 <load_plugins_dir+0xf3>
mov    %rax,%rbp
mov    %r13,0x10(%rsp)
mov    %r15,0x18(%rsp)
mov    %r14,0x20(%rsp)
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1272 <load_plugins_dir+0x102>
lea    0xe13(%rip),%r14
jmp    11fd <load_plugins_dir+0x8d>
mov    %rbp,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1272 <load_plugins_dir+0x102>
mov    (%rax),%r13
mov    %r13,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11f0 <load_plugins_dir+0x80>
mov    %r13,%rdi
lea    0xde6(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11f0 <load_plugins_dir+0x80>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %eax,%r15d
movslq %r15d,%rdi
add    %r13,%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    11f0 <load_plugins_dir+0x80>
mov    0x10(%rsp),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    0x20(%rsp),%rcx
call   *0x18(%rsp)
jmp    11f0 <load_plugins_dir+0x80>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <closedir@plt>

LLVM IR (-O0):
%struct.pevent = type { i32 }
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
define void @load_plugins_dir(%struct.pevent* %0, i8* %1, i8* %2, void (%struct.pevent*, i8*, i8*, i8*)* %3, i8* %4) {
%6 = alloca %struct.pevent*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca void (%struct.pevent*, i8*, i8*, i8*)*
%10 = alloca i8*
%11 = alloca %struct.dirent*
%12 = alloca %struct.stat
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i8*
store %struct.pevent* %0, %struct.pevent** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store void (%struct.pevent*, i8*, i8*, i8*)* %3, void (%struct.pevent*, i8*, i8*, i8*)** %9
store i8* %4, i8** %10
%16 = load i8*, i8** %8
%17 = call i32 @stat(i8* %16, %struct.stat* %12)
store i32 %17, i32* %14
%18 = load i32, i32* %14
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %5
br label %72
21: ; preds = %5
%22 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @S_ISDIR(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %21
br label %72
27: ; preds = %21
%28 = load i8*, i8** %8
%29 = call i32* @opendir(i8* %28)
store i32* %29, i32** %13
%30 = load i32*, i32** %13
%31 = icmp ne i32* %30, null
br i1 %31, label %33, label %32
32: ; preds = %27
br label %72
33: ; preds = %27
br label %34
34: ; preds = %63, %62, %49, %33
%35 = load i32*, i32** %13
%36 = call %struct.dirent* @readdir(i32* %35)
store %struct.dirent* %36, %struct.dirent** %11
%37 = icmp ne %struct.dirent* %36, null
br i1 %37, label %38, label %69
38: ; preds = %34
%39 = load %struct.dirent*, %struct.dirent** %11
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
store i8* %41, i8** %15
%42 = load i8*, i8** %15
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %38
%46 = load i8*, i8** %15
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45, %38
br label %34
50: ; preds = %45
%51 = load i8*, i8** %15
%52 = load i8*, i8** %15
%53 = call i32 @strlen(i8* %52)
%54 = load i8*, i8** %7
%55 = call i32 @strlen(i8* %54)
%56 = sub nsw i32 %53, %55
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %51, i64 %57
%59 = load i8*, i8** %7
%60 = call i64 @strcmp(i8* %58, i8* %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %50
br label %34
63: ; preds = %50
%64 = load void (%struct.pevent*, i8*, i8*, i8*)*, void (%struct.pevent*, i8*, i8*, i8*)** %9
%65 = load %struct.pevent*, %struct.pevent** %6
%66 = load i8*, i8** %8
%67 = load i8*, i8** %15
%68 = load i8*, i8** %10
br label %34
69: ; preds = %34
%70 = load i32*, i32** %13
%71 = call i32 @closedir(i32* %70)
br label %72
72: ; preds = %69, %32, %26, %20
ret void
}

LLVM IR (-O2):
%struct.pevent = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
define void @load_plugins_dir(%struct.pevent* %0, i8* %1, i8* %2, void (%struct.pevent*, i8*, i8*, i8*)* nocapture %3, i8* %4) {
%6 = alloca %struct.stat
%7 = bitcast %struct.stat* %6 to i8*
%8 = call i32 @stat(i8* %2, %struct.stat* %6)
%9 = icmp slt i32 %8, 0
br i1 %9, label %44, label %10
10: ; preds = %5
%11 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @S_ISDIR(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %44, label %15
15: ; preds = %10
%16 = tail call i32* @opendir(i8* %2)
%17 = icmp eq i32* %16, null
br i1 %17, label %44, label %18
18: ; preds = %15
%19 = tail call %struct.dirent* @readdir(i32* %16)
%20 = icmp eq %struct.dirent* %19, null
br i1 %20, label %42, label %21
21: ; preds = %18, %39
%22 = phi %struct.dirent* [ %40, %39 ], [ %19, %18 ]
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = tail 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 %39, label %27
27: ; preds = %21
%28 = tail call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %39, label %30
30: ; preds = %27
%31 = tail call i32 @strlen(i8* %24)
%32 = tail call i32 @strlen(i8* %1)
%33 = sub nsw i32 %31, %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %24, i64 %34
%36 = tail call i64 @strcmp(i8* %35, i8* %1)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %30
tail call void %3(%struct.pevent* %0, i8* %2, i8* %24, i8* %4)
br label %39
39: ; preds = %30, %21, %27, %38
%40 = tail call %struct.dirent* @readdir(i32* %16)
%41 = icmp eq %struct.dirent* %40, null
br i1 %41, label %42, label %21
42: ; preds = %39, %18
%43 = tail call i32 @closedir(i32* %16)
br label %44
44: ; preds = %15, %10, %5, %42
ret void
}