#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*/ RTLD_LAZY;
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ dlsym(void *, char *);
int errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ wrout(char *);
void init(void) {
  wrout("REFACTORY injector init()\n");
  DIR *d;
  struct dirent *dir;
  int errsv = errno;
  d = opendir("./patches");
  if (d) {
    while ((dir = readdir(d)) != NULL) {
      if (strcmp(dir->d_name, ".") || strcmp(dir->d_name, ".."))
        continue;
      printf("Attempting to load %s\n", dir->d_name);
      void *handle = dlopen(dir->d_name, RTLD_LAZY);
      if (handle) {
        dlsym(handle, "init");
        printf("Loaded %s\n", dir->d_name);
      } else {
        printf("Failed to load %s\n", dir->d_name);
      }
    }
  } else {
    wrout("No patches to load. Passing on\n");
    return;
  }
  errno = errsv;
}


x86 assembly (clang, -O0):
<init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
call   1070 <wrout@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xe84(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1282 <init+0x112>
jmp    11b0 <init+0x40>
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     127d <init+0x10d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe51(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1201 <init+0x91>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe33(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1206 <init+0x96>
jmp    11b0 <init+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
call   1060 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1265 <init+0xf5>
mov    -0x20(%rbp),%rdi
lea    0xdf7(%rip),%rsi
call   1090 <dlsym@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde9(%rip),%rdi
call   1040 <printf@plt>
jmp    1278 <init+0x108>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xddc(%rip),%rdi
call   1040 <printf@plt>
jmp    11b0 <init+0x40>
jmp    1293 <init+0x123>
lea    0xdd9(%rip),%rdi
call   1070 <wrout@plt>
jmp    129f <init+0x12f>
mov    -0x14(%rbp),%ecx
mov    0x2d43(%rip),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
call   1070 <wrout@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%r13d
lea    0xe83(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1270 <init+0x100>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1257 <init+0xe7>
mov    %rax,%rbp
lea    0xe62(%rip),%r14
lea    0xe5a(%rip),%r15
lea    0xe7d(%rip),%r12
jmp    11f0 <init+0x80>
mov    0x0(%rbp),%rsi
call   1040 <printf@plt>
nopl   (%rax)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1257 <init+0xe7>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11e0 <init+0x70>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11e0 <init+0x70>
mov    0x0(%rbp),%rsi
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
mov    0x0(%rbp),%rdi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
call   1060 <dlopen@plt>
mov    %r12,%rdi
test   %rax,%rax
je     11d4 <init+0x64>
mov    %rax,%rdi
lea    0xdf9(%rip),%rsi
call   1090 <dlsym@plt>
lea    0xdf2(%rip),%rdi
jmp    11d4 <init+0x64>
mov    0x2d82(%rip),%rax
mov    %r13d,(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdeb(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <wrout@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [27 x i8] c"REFACTORY injector init()\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"./patches\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [23 x i8] c"Attempting to load %s\0A\00"
@RTLD_LAZY = global i32 0
@.str.5 = [5 x i8] c"init\00"
@.str.6 = [11 x i8] c"Loaded %s\0A\00"
@.str.7 = [19 x i8] c"Failed to load %s\0A\00"
@.str.8 = [32 x i8] c"No patches to load. Passing on\0A\00"
define void @init() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i32
%4 = alloca i8*
%5 = call i32 @wrout(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @errno
store i32 %6, i32* %3
%7 = call i32* @opendir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %1
%8 = load i32*, i32** %1
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %54
10: ; preds = %0
br label %11
11: ; preds = %52, %27, %10
%12 = load i32*, i32** %1
%13 = call %struct.dirent* @readdir(i32* %12)
store %struct.dirent* %13, %struct.dirent** %2
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %53
15: ; preds = %11
%16 = load %struct.dirent*, %struct.dirent** %2
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp ne i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %15
%22 = load %struct.dirent*, %struct.dirent** %2
%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 ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21, %15
br label %11
28: ; preds = %21
%29 = load %struct.dirent*, %struct.dirent** %2
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %31)
%33 = load %struct.dirent*, %struct.dirent** %2
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = load i32, i32* @RTLD_LAZY
%37 = call i8* @dlopen(i8* %35, i32 %36)
store i8* %37, i8** %4
%38 = load i8*, i8** %4
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %47
40: ; preds = %28
%41 = load i8*, i8** %4
%42 = call i32 @dlsym(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%43 = load %struct.dirent*, %struct.dirent** %2
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %45)
br label %52
47: ; preds = %28
%48 = load %struct.dirent*, %struct.dirent** %2
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %47, %40
br label %11
53: ; preds = %11
br label %56
54: ; preds = %0
%55 = call i32 @wrout(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0))
br label %58
56: ; preds = %53
%57 = load i32, i32* %3
store i32 %57, i32* @errno
br label %58
58: ; preds = %56, %54
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [27 x i8] c"REFACTORY injector init()\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"./patches\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [23 x i8] c"Attempting to load %s\0A\00"
@RTLD_LAZY = global i32 0
@.str.5 = [5 x i8] c"init\00"
@.str.6 = [11 x i8] c"Loaded %s\0A\00"
@.str.7 = [19 x i8] c"Failed to load %s\0A\00"
@.str.8 = [32 x i8] c"No patches to load. Passing on\0A\00"
define void @init() {
%1 = tail call i32 @wrout(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @errno
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %34, label %5
5: ; preds = %0
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %36, label %8
8: ; preds = %5, %22
%9 = phi %struct.dirent* [ %23, %22 ], [ %6, %5 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = tail call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %8
%15 = load i8*, i8** %10
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %25, label %22
18: ; preds = %25, %32
%19 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), %32 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), %25 ]
%20 = load i8*, i8** %10
%21 = tail call i32 @printf(i8* %19, i8* %20)
br label %22
22: ; preds = %18, %8, %14
%23 = tail call %struct.dirent* @readdir(i32* %3)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %36, label %8
25: ; preds = %14
%26 = load i8*, i8** %10
%27 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %10
%29 = load i32, i32* @RTLD_LAZY
%30 = tail call i8* @dlopen(i8* %28, i32 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %18, label %32
32: ; preds = %25
%33 = tail call i32 @dlsym(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %18
34: ; preds = %0
%35 = tail call i32 @wrout(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0))
br label %37
36: ; preds = %22, %5
store i32 %2, i32* @errno
br label %37
37: ; preds = %36, %34
ret void
}