#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 test_suite {
  int dummy;
};
struct test_loader {
  int dummy;
};
typedef struct test_suite *(*_Loaderhook)(struct test_loader *);

/* Variables and functions */
int RTLD_LAZY;
int RTLD_LOCAL;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char const *, int);
scalar_t__ dlsym(void *, char *);
struct test_suite *suite_error_new(char *);
struct test_suite *load_test_from_dyn_library(struct test_loader *loader,
                                              const char *filename) {
  void *handle;
  struct test_suite *suite;
  typedef struct test_suite *(_Loaderhook)(struct test_loader *);
  _Loaderhook *load_suite;
  assert(loader != NULL);
  if ((handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL)) == NULL)
    return suite_error_new(dlerror());
  load_suite = (_Loaderhook *)dlsym(handle, "load_test_suite");
  if (load_suite == NULL)
    suite = suite_error_new("no suite found");
  else if ((suite = load_suite(loader)) == NULL)
    suite = suite_error_new("error while loading suite");
  dlclose(handle);
  return suite;
}


x86 assembly (clang, -O0):
<load_test_from_dyn_library>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c3 <load_test_from_dyn_library+0x63>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rdi
call   1040 <suite_error_new@plt>
mov    %rax,-0x8(%rbp)
jmp    1235 <load_test_from_dyn_library+0xd5>
mov    -0x20(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f7 <load_test_from_dyn_library+0x97>
lea    0xe27(%rip),%rdi
call   1040 <suite_error_new@plt>
mov    %rax,-0x28(%rbp)
jmp    1224 <load_test_from_dyn_library+0xc4>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rdi
call   *%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    121f <load_test_from_dyn_library+0xbf>
lea    0xe09(%rip),%rdi
call   1040 <suite_error_new@plt>
mov    %rax,-0x28(%rbp)
jmp    1224 <load_test_from_dyn_library+0xc4>
mov    -0x20(%rbp),%rdi
call   1080 <dlclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_test_from_dyn_library>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1060 <assert@plt>
mov    0x2e61(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     11c4 <load_test_from_dyn_library+0x64>
mov    %rax,%rbx
lea    0xe5f(%rip),%rsi
mov    %rax,%rdi
call   1070 <dlsym@plt>
test   %rax,%rax
je     11da <load_test_from_dyn_library+0x7a>
mov    %r14,%rdi
call   *%rax
mov    %rax,%r14
test   %rax,%rax
jne    11e9 <load_test_from_dyn_library+0x89>
lea    0xe5d(%rip),%rdi
jmp    11e1 <load_test_from_dyn_library+0x81>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <suite_error_new@plt>
lea    0xe2f(%rip),%rdi
call   1040 <suite_error_new@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1080 <dlclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.test_suite = type { i32 }
%struct.test_loader = type { i32 }
@RTLD_LAZY = global i32 0
@RTLD_LOCAL = global i32 0
@.str = [16 x i8] c"load_test_suite\00"
@.str.1 = [15 x i8] c"no suite found\00"
@.str.2 = [26 x i8] c"error while loading suite\00"
define %struct.test_suite* @load_test_from_dyn_library(%struct.test_loader* %0, i8* %1) {
%3 = alloca %struct.test_suite*
%4 = alloca %struct.test_loader*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.test_suite*
%8 = alloca %struct.test_suite* (%struct.test_loader*)*
store %struct.test_loader* %0, %struct.test_loader** %4
store i8* %1, i8** %5
%9 = load %struct.test_loader*, %struct.test_loader** %4
%10 = icmp ne %struct.test_loader* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i8*, i8** %5
%14 = load i32, i32* @RTLD_LAZY
%15 = load i32, i32* @RTLD_LOCAL
%16 = or i32 %14, %15
%17 = call i8* @dlopen(i8* %13, i32 %16)
store i8* %17, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i8* (...) @dlerror()
%21 = call %struct.test_suite* @suite_error_new(i8* %20)
store %struct.test_suite* %21, %struct.test_suite** %3
br label %42
22: ; preds = %2
%23 = load i8*, i8** %6
%24 = call i64 @dlsym(i8* %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%25 = inttoptr i64 %24 to %struct.test_suite* (%struct.test_loader*)*
store %struct.test_suite* (%struct.test_loader*)* %25, %struct.test_suite* (%struct.test_loader*)** %8
%26 = load %struct.test_suite* (%struct.test_loader*)*, %struct.test_suite* (%struct.test_loader*)** %8
%27 = icmp eq %struct.test_suite* (%struct.test_loader*)* %26, null
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call %struct.test_suite* @suite_error_new(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store %struct.test_suite* %29, %struct.test_suite** %7
br label %38
30: ; preds = %22
%31 = load %struct.test_suite* (%struct.test_loader*)*, %struct.test_suite* (%struct.test_loader*)** %8
%32 = load %struct.test_loader*, %struct.test_loader** %4
%33 = call %struct.test_suite* %31(%struct.test_loader* %32)
store %struct.test_suite* %33, %struct.test_suite** %7
%34 = icmp eq %struct.test_suite* %33, null
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call %struct.test_suite* @suite_error_new(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store %struct.test_suite* %36, %struct.test_suite** %7
br label %37
37: ; preds = %35, %30
br label %38
38: ; preds = %37, %28
%39 = load i8*, i8** %6
%40 = call i32 @dlclose(i8* %39)
%41 = load %struct.test_suite*, %struct.test_suite** %7
store %struct.test_suite* %41, %struct.test_suite** %3
br label %42
42: ; preds = %38, %19
%43 = load %struct.test_suite*, %struct.test_suite** %3
ret %struct.test_suite* %43
}

LLVM IR (-O2):
%struct.test_suite = type { i32 }
%struct.test_loader = type { i32 }
@RTLD_LAZY = global i32 0
@RTLD_LOCAL = global i32 0
@.str = [16 x i8] c"load_test_suite\00"
@.str.1 = [15 x i8] c"no suite found\00"
@.str.2 = [26 x i8] c"error while loading suite\00"
define %struct.test_suite* @load_test_from_dyn_library(%struct.test_loader* %0, i8* %1) {
%3 = icmp ne %struct.test_loader* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = load i32, i32* @RTLD_LAZY
%7 = load i32, i32* @RTLD_LOCAL
%8 = or i32 %7, %6
%9 = tail call i8* @dlopen(i8* %1, i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i8* (...) @dlerror()
%13 = tail call %struct.test_suite* @suite_error_new(i8* %12)
br label %27
14: ; preds = %2
%15 = tail call i64 @dlsym(i8* %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = inttoptr i64 %15 to %struct.test_suite* (%struct.test_loader*)*
%19 = tail call %struct.test_suite* %18(%struct.test_loader* %0)
%20 = icmp eq %struct.test_suite* %19, null
br i1 %20, label %21, label %24
21: ; preds = %17, %14
%22 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), %17 ]
%23 = tail call %struct.test_suite* @suite_error_new(i8* %22)
br label %24
24: ; preds = %21, %17
%25 = phi %struct.test_suite* [ %19, %17 ], [ %23, %21 ]
%26 = tail call i32 @dlclose(i8* %9)
br label %27
27: ; preds = %24, %11
%28 = phi %struct.test_suite* [ %13, %11 ], [ %25, %24 ]
ret %struct.test_suite* %28
}