#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 plugin_option_read {
  int dummy;
};
struct plugin_option {
  int dummy;
};
struct pevent {
  int dummy;
};

/* Variables and functions */
int RTLD_GLOBAL;
int RTLD_NOW;
int /*<<< orphan*/ append_option(struct plugin_option_read *,
                                 struct plugin_option *, char const *, void *);
int /*<<< orphan*/ dlclose(void *);
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ free(char *);
char *malloc_or_die(scalar_t__);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ warning(char *, char *, int /*<<< orphan*/);
void read_options(struct pevent *pevent, const char *path, const char *file,
                  void *data) {
  struct plugin_option_read *options = data;
  struct plugin_option *option;
  const char *alias;
  int unload = 0;
  char *plugin;
  void *handle;
  plugin = malloc_or_die(strlen(path) + strlen(file) + 2);
  strcpy(plugin, path);
  strcat(plugin, "/");
  strcat(plugin, file);
  handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL);
  if (!handle) {
    warning("cound not load plugin '%s'\n%s\n", plugin, dlerror());
    goto out_free;
  }
  alias = dlsym(handle, "pevent_plugin_alias");
  if (!alias)
    alias = file;
  option = dlsym(handle, "pevent_plugin_options");
  if (!option) {
    unload = 1;
    goto out_unload;
  }
  append_option(options, option, alias, handle);
out_unload:
  if (unload)
    dlclose(handle);
out_free:
  free(plugin);
}


x86 assembly (clang, -O0):
<read_options>:
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    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x58(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1090 <malloc_or_die@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x48(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x48(%rbp),%rdi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
call   1080 <dlopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    127b <read_options+0xcb>
mov    -0x48(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
lea    0xd91(%rip),%rdi
call   1060 <warning@plt>
jmp    12fa <read_options+0x14a>
mov    -0x50(%rbp),%rdi
lea    0xd9b(%rip),%rsi
call   10a0 <dlsym@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12a2 <read_options+0xf2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x50(%rbp),%rdi
lea    0xd88(%rip),%rsi
call   10a0 <dlsym@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12cd <read_options+0x11d>
movl   $0x1,-0x3c(%rbp)
jmp    12e2 <read_options+0x132>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x50(%rbp),%rcx
call   10c0 <append_option@plt>
cmpl   $0x0,-0x3c(%rbp)
je     12f5 <read_options+0x145>
mov    -0x50(%rbp),%rdi
call   10d0 <dlclose@plt>
jmp    12fa <read_options+0x14a>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_options>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1070 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1070 <strlen@plt>
lea    (%r12,%rax,1),%rdi
add    $0x2,%rdi
call   1090 <malloc_or_die@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
lea    0xe09(%rip),%rsi
mov    %r12,%rdi
call   10b0 <strcat@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
mov    0x2dcf(%rip),%rax
mov    0x2dd0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r12,%rdi
call   1080 <dlopen@plt>
test   %rax,%rax
je     126c <read_options+0xbc>
mov    %rax,%rbx
lea    0xdee(%rip),%rsi
mov    %rax,%rdi
call   10a0 <dlsym@plt>
mov    %rax,%r13
lea    0xdf0(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <dlsym@plt>
test   %rax,%rax
je     1286 <read_options+0xd6>
test   %r13,%r13
cmovne %r13,%r14
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   10c0 <append_option@plt>
jmp    128e <read_options+0xde>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xd88(%rip),%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <warning@plt>
jmp    128e <read_options+0xde>
mov    %rbx,%rdi
call   10d0 <dlclose@plt>
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <free@plt>

LLVM IR (-O0):
%struct.pevent = type { i32 }
%struct.plugin_option_read = type { i32 }
%struct.plugin_option = type { i32 }
@.str = [2 x i8] c"/\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [31 x i8] c"cound not load plugin '%s'\0A%s\0A\00"
@.str.2 = [20 x i8] c"pevent_plugin_alias\00"
@.str.3 = [22 x i8] c"pevent_plugin_options\00"
define void @read_options(%struct.pevent* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca %struct.pevent*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.plugin_option_read*
%10 = alloca %struct.plugin_option*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store %struct.pevent* %0, %struct.pevent** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%15 = load i8*, i8** %8
%16 = bitcast i8* %15 to %struct.plugin_option_read*
store %struct.plugin_option_read* %16, %struct.plugin_option_read** %9
store i32 0, i32* %12
%17 = load i8*, i8** %6
%18 = call i64 @strlen(i8* %17)
%19 = load i8*, i8** %7
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %18, %20
%22 = add nsw i64 %21, 2
%23 = call i8* @malloc_or_die(i64 %22)
store i8* %23, i8** %13
%24 = load i8*, i8** %13
%25 = load i8*, i8** %6
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = load i8*, i8** %13
%28 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%29 = load i8*, i8** %13
%30 = load i8*, i8** %7
%31 = call i32 @strcat(i8* %29, i8* %30)
%32 = load i8*, i8** %13
%33 = load i32, i32* @RTLD_NOW
%34 = load i32, i32* @RTLD_GLOBAL
%35 = or i32 %33, %34
%36 = call i8* @dlopen(i8* %32, i32 %35)
store i8* %36, i8** %14
%37 = load i8*, i8** %14
%38 = icmp ne i8* %37, null
br i1 %38, label %43, label %39
39: ; preds = %4
%40 = load i8*, i8** %13
%41 = call i32 (...) @dlerror()
%42 = call i32 @warning(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %40, i32 %41)
br label %70
43: ; preds = %4
%44 = load i8*, i8** %14
%45 = call i8* @dlsym(i8* %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i8* %45, i8** %11
%46 = load i8*, i8** %11
%47 = icmp ne i8* %46, null
br i1 %47, label %50, label %48
48: ; preds = %43
%49 = load i8*, i8** %7
store i8* %49, i8** %11
br label %50
50: ; preds = %48, %43
%51 = load i8*, i8** %14
%52 = call i8* @dlsym(i8* %51, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%53 = bitcast i8* %52 to %struct.plugin_option*
store %struct.plugin_option* %53, %struct.plugin_option** %10
%54 = load %struct.plugin_option*, %struct.plugin_option** %10
%55 = icmp ne %struct.plugin_option* %54, null
br i1 %55, label %57, label %56
56: ; preds = %50
store i32 1, i32* %12
br label %63
57: ; preds = %50
%58 = load %struct.plugin_option_read*, %struct.plugin_option_read** %9
%59 = load %struct.plugin_option*, %struct.plugin_option** %10
%60 = load i8*, i8** %11
%61 = load i8*, i8** %14
%62 = call i32 @append_option(%struct.plugin_option_read* %58, %struct.plugin_option* %59, i8* %60, i8* %61)
br label %63
63: ; preds = %57, %56
%64 = load i32, i32* %12
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i8*, i8** %14
%68 = call i32 @dlclose(i8* %67)
br label %69
69: ; preds = %66, %63
br label %70
70: ; preds = %69, %39
%71 = load i8*, i8** %13
%72 = call i32 @free(i8* %71)
ret void
}

LLVM IR (-O2):
%struct.pevent = type { i32 }
%struct.plugin_option_read = type { i32 }
%struct.plugin_option = type { i32 }
@.str = [2 x i8] c"/\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [31 x i8] c"cound not load plugin '%s'\0A%s\0A\00"
@.str.2 = [20 x i8] c"pevent_plugin_alias\00"
@.str.3 = [22 x i8] c"pevent_plugin_options\00"
define void @read_options(%struct.pevent* nocapture readnone %0, i8* %1, i8* %2, i8* %3) {
%5 = bitcast i8* %3 to %struct.plugin_option_read*
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%8 = add i64 %6, 2
%9 = add i64 %8, %7
%10 = tail call i8* @malloc_or_die(i64 %9)
%11 = tail call i32 @strcpy(i8* %10, i8* %1)
%12 = tail call i32 @strcat(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @strcat(i8* %10, i8* %2)
%14 = load i32, i32* @RTLD_NOW
%15 = load i32, i32* @RTLD_GLOBAL
%16 = or i32 %15, %14
%17 = tail call i8* @dlopen(i8* %10, i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %4
%20 = tail call i32 (...) @dlerror()
%21 = tail call i32 @warning(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %10, i32 %20)
br label %33
22: ; preds = %4
%23 = tail call i8* @dlsym(i8* %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i8* @dlsym(i8* %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %31, label %26
26: ; preds = %22
%27 = icmp eq i8* %23, null
%28 = select i1 %27, i8* %2, i8* %23
%29 = bitcast i8* %24 to %struct.plugin_option*
%30 = tail call i32 @append_option(%struct.plugin_option_read* %5, %struct.plugin_option* %29, i8* %28, i8* %17)
br label %33
31: ; preds = %22
%32 = tail call i32 @dlclose(i8* %17)
br label %33
33: ; preds = %26, %31, %19
%34 = tail call i32 @free(i8* %10)
ret void
}