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

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dirfd(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
scalar_t__ fstatat(int /*<<< orphan*/, char *, struct stat *,
                   int /*<<< orphan*/);
int /*<<< orphan*/ log_printf(int /*<<< orphan*/, char *, char *, char *,
                              int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
scalar_t__ plugin_attach(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int plugins_load(char *path) {
  int rv = -1;
  struct dirent *entry;
  struct stat file_stat;
  DIR *directory = opendir(path);
  if (!directory) {
    log_printf(0, ("core/pl"),
               "Failed to open plugin search path %s: %s"
               "\n",
               path, strerror(errno));
    return 1;
  }
  for (entry = readdir(directory); entry; entry = readdir(directory)) {
    if (strlen(entry->d_name) < 4 ||
        strncmp(".so", entry->d_name + (strlen(entry->d_name) - 3), 3)) {
      continue;
    }
    if (fstatat(dirfd(directory), entry->d_name, &file_stat, 0) < 0) {
      log_printf(0, ("core/pl"),
                 "Failed to stat %s: %s"
                 "\n",
                 entry->d_name, strerror(errno));
      continue;
    }
    if (!S_ISREG(file_stat.st_mode)) {
      continue;
    }
    if (plugin_attach(path, entry->d_name)) {
      goto load_done;
    }
  }
  rv = 0;
load_done:
  if (closedir(directory) < 0) {
    log_printf(0, ("core/pl"),
               "Failed to close plugin directory %s: %s"
               "\n",
               path, strerror(errno));
    return -1;
  }
  return rv;
}


x86 assembly (clang, -O0):
<plugins_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1219 <plugins_load+0x69>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%rcx
mov    %eax,%r8d
xor    %edi,%edi
lea    0xdff(%rip),%rsi
lea    0xe00(%rip),%rdx
call   1080 <log_printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1394 <plugins_load+0x1e4>
mov    -0x30(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1336 <plugins_load+0x186>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
cmp    $0x4,%eax
jl     1284 <plugins_load+0xd4>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rsi
sub    $0x3,%eax
cltq
add    %rax,%rsi
lea    0xdc2(%rip),%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1289 <plugins_load+0xd9>
jmp    1324 <plugins_load+0x174>
mov    -0x30(%rbp),%rdi
call   1090 <dirfd@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   10d0 <fstatat@plt>
cmp    $0x0,%rax
jge    12ea <plugins_load+0x13a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x48(%rbp),%rcx
mov    %eax,%r8d
xor    %edi,%edi
lea    0xd27(%rip),%rsi
lea    0xd56(%rip),%rdx
call   1080 <log_printf@plt>
jmp    1324 <plugins_load+0x174>
mov    -0x28(%rbp),%edi
call   10a0 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1300 <plugins_load+0x150>
jmp    1324 <plugins_load+0x174>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1050 <plugin_attach@plt>
cmp    $0x0,%rax
je     131f <plugins_load+0x16f>
jmp    133d <plugins_load+0x18d>
jmp    1324 <plugins_load+0x174>
mov    -0x30(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
jmp    1226 <plugins_load+0x76>
movl   $0x0,-0x14(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <closedir@plt>
cmp    $0x0,%rax
jge    138e <plugins_load+0x1de>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x50(%rbp),%rcx
mov    %eax,%r8d
xor    %edi,%edi
lea    0xc8a(%rip),%rsi
lea    0xcd0(%rip),%rdx
call   1080 <log_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1394 <plugins_load+0x1e4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plugins_load>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     12ee <plugins_load+0x13e>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12af <plugins_load+0xff>
mov    %rax,%r13
lea    0xe48(%rip),%r15
mov    %rsp,%r12
jmp    1230 <plugins_load+0x80>
mov    0x0(%r13),%rbp
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
xor    %edi,%edi
lea    0xdf6(%rip),%rsi
lea    0xe25(%rip),%rdx
mov    %rbp,%rcx
mov    %eax,%r8d
call   1080 <log_printf@plt>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12af <plugins_load+0xff>
mov    0x0(%r13),%rdi
call   1060 <strlen@plt>
cmp    $0x4,%eax
jl     1220 <plugins_load+0x70>
mov    0x0(%r13),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rax,%rbp,1),%rsi
add    $0xfffffffffffffffd,%rsi
mov    %r15,%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1220 <plugins_load+0x70>
mov    %rbx,%rdi
call   1090 <dirfd@plt>
mov    0x0(%r13),%rsi
mov    %eax,%edi
mov    %r12,%rdx
xor    %ecx,%ecx
call   10d0 <fstatat@plt>
test   %rax,%rax
js     11ef <plugins_load+0x3f>
mov    (%rsp),%edi
call   10a0 <S_ISREG@plt>
test   %eax,%eax
je     1220 <plugins_load+0x70>
mov    0x0(%r13),%rsi
mov    %r14,%rdi
call   1050 <plugin_attach@plt>
test   %rax,%rax
je     1220 <plugins_load+0x70>
mov    $0xffffffff,%ebp
jmp    12b1 <plugins_load+0x101>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1070 <closedir@plt>
test   %rax,%rax
jns    131c <plugins_load+0x16c>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd2d(%rip),%rsi
lea    0xd73(%rip),%rdx
xor    %edi,%edi
mov    %r14,%rcx
mov    %eax,%r8d
call   1080 <log_printf@plt>
mov    $0xffffffff,%ebp
jmp    131c <plugins_load+0x16c>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xcfd(%rip),%rsi
lea    0xcfe(%rip),%rdx
xor    %edi,%edi
mov    %r14,%rcx
mov    %eax,%r8d
call   1080 <log_printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [8 x i8] c"core/pl\00"
@.str.1 = [42 x i8] c"Failed to open plugin search path %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c".so\00"
@.str.3 = [23 x i8] c"Failed to stat %s: %s\0A\00"
@.str.4 = [41 x i8] c"Failed to close plugin directory %s: %s\0A\00"
define i32 @plugins_load(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.dirent*
%6 = alloca %struct.stat
%7 = alloca i32*
store i8* %0, i8** %3
store i32 -1, i32* %4
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %17, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 @log_printf(i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 %15)
store i32 1, i32* %2
br label %88
17: ; preds = %1
%18 = load i32*, i32** %7
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %5
br label %20
20: ; preds = %73, %17
%21 = load %struct.dirent*, %struct.dirent** %5
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %76
23: ; preds = %20
%24 = load %struct.dirent*, %struct.dirent** %5
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @strlen(i8* %26)
%28 = icmp slt i32 %27, 4
br i1 %28, label %42, label %29
29: ; preds = %23
%30 = load %struct.dirent*, %struct.dirent** %5
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = load %struct.dirent*, %struct.dirent** %5
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strlen(i8* %35)
%37 = sub nsw i32 %36, 3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %32, i64 %38
%40 = call i64 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %39, i32 3)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %29, %23
br label %73
43: ; preds = %29
%44 = load i32*, i32** %7
%45 = call i32 @dirfd(i32* %44)
%46 = load %struct.dirent*, %struct.dirent** %5
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i64 @fstatat(i32 %45, i8* %48, %struct.stat* %6, i32 0)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %43
%52 = load %struct.dirent*, %struct.dirent** %5
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = load i32, i32* @errno
%56 = call i32 @strerror(i32 %55)
%57 = call i32 @log_printf(i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %54, i32 %56)
br label %73
58: ; preds = %43
%59 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 @S_ISREG(i32 %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %64, label %63
63: ; preds = %58
br label %73
64: ; preds = %58
%65 = load i8*, i8** %3
%66 = load %struct.dirent*, %struct.dirent** %5
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i64 @plugin_attach(i8* %65, i8* %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %64
br label %77
72: ; preds = %64
br label %73
73: ; preds = %72, %63, %51, %42
%74 = load i32*, i32** %7
%75 = call %struct.dirent* @readdir(i32* %74)
store %struct.dirent* %75, %struct.dirent** %5
br label %20
76: ; preds = %20
store i32 0, i32* %4
br label %77
77: ; preds = %76, %71
%78 = load i32*, i32** %7
%79 = call i64 @closedir(i32* %78)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %86
81: ; preds = %77
%82 = load i8*, i8** %3
%83 = load i32, i32* @errno
%84 = call i32 @strerror(i32 %83)
%85 = call i32 @log_printf(i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %82, i32 %84)
store i32 -1, i32* %2
br label %88
86: ; preds = %77
%87 = load i32, i32* %4
store i32 %87, i32* %2
br label %88
88: ; preds = %86, %81, %12
%89 = load i32, i32* %2
ret i32 %89
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [8 x i8] c"core/pl\00"
@.str.1 = [42 x i8] c"Failed to open plugin search path %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c".so\00"
@.str.3 = [23 x i8] c"Failed to stat %s: %s\0A\00"
@.str.4 = [41 x i8] c"Failed to close plugin directory %s: %s\0A\00"
define i32 @plugins_load(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @log_printf(i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %58
10: ; preds = %1
%11 = tail call %struct.dirent* @readdir(i32* %4)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %50, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %15
15: ; preds = %13, %47
%16 = phi %struct.dirent* [ %11, %13 ], [ %48, %47 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @strlen(i8* %18)
%20 = icmp slt i32 %19, 4
br i1 %20, label %47, label %21
21: ; preds = %15
%22 = load i8*, i8** %17
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %23, -3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = call i64 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %26, i32 3)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %47
29: ; preds = %21
%30 = call i32 @dirfd(i32* %4)
%31 = load i8*, i8** %17
%32 = call i64 @fstatat(i32 %30, i8* %31, %struct.stat* %2, i32 0)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i8*, i8** %17
%36 = load i32, i32* @errno
%37 = call i32 @strerror(i32 %36)
%38 = call i32 @log_printf(i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %35, i32 %37)
br label %47
39: ; preds = %29
%40 = load i32, i32* %14
%41 = call i32 @S_ISREG(i32 %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %47, label %43
43: ; preds = %39
%44 = load i8*, i8** %17
%45 = call i64 @plugin_attach(i8* %0, i8* %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43, %39, %15, %21, %34
%48 = call %struct.dirent* @readdir(i32* %4)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %50, label %15
50: ; preds = %43, %47, %10
%51 = phi i32 [ 0, %10 ], [ 0, %47 ], [ -1, %43 ]
%52 = call i64 @closedir(i32* %4)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = load i32, i32* @errno
%56 = call i32 @strerror(i32 %55)
%57 = call i32 @log_printf(i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %56)
br label %58
58: ; preds = %50, %54, %6
%59 = phi i32 [ -1, %54 ], [ 1, %6 ], [ %51, %50 ]
ret i32 %59
}