#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 */

/* Variables and functions */
int /*<<< orphan*/ RTLD_NOW;
scalar_t__ dlclose(void *);
char *dlerror();
void *dlopen(char const *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
void stub1(int *);
int check_library(const char *libname) {
  void *lib = dlopen(libname, RTLD_NOW);
  int *to_x;
  void (*set_y)(int *);
  int y = 0;
  if (lib == NULL) {
    fprintf(stderr, "Could not load shared library %s: %s\n", libname,
            dlerror());
    return 1;
  }
  fprintf(stderr, "%s loaded.\n", libname);
  to_x = dlsym(lib, "x");
  if (to_x == NULL) {
    fprintf(stderr, "Could not access global DLL variable (x) in %s: %s\n",
            libname, dlerror());
    return 10;
  }
  if (*to_x != 1) {
    fprintf(stderr, "Constructor was not run on dlopen(\"%s\") !\n", libname);
    return 11;
  }
  set_y = dlsym(lib, "set_y");
  if (set_y == NULL) {
    fprintf(stderr, "Could not access global DLL function (set_y) in %s: %s\n",
            libname, dlerror());
    return 12;
  }
  y = 0;
  (*set_y)(&y);
  if (dlclose(lib) < 0) {
    fprintf(stderr, "Could not unload shared library %s: %s\n", libname,
            dlerror());
    return 2;
  }
  fprintf(stderr, "%s unloaded.\n", libname);
  if (y != 2) {
    fprintf(stderr, "Static destructors was not called on dlclose()!\n");
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_library>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c3 <check_library+0x73>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    137d <check_library+0x22d>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123c <check_library+0xec>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xa,-0x4(%rbp)
jmp    137d <check_library+0x22d>
mov    -0x20(%rbp),%rax
cmpl   $0x1,(%rax)
je     1270 <check_library+0x120>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xb,-0x4(%rbp)
jmp    137d <check_library+0x22d>
mov    -0x18(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12ce <check_library+0x17e>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xc,-0x4(%rbp)
jmp    137d <check_library+0x22d>
movl   $0x0,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
call   *-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <dlclose@plt>
cmp    $0x0,%rax
jge    132e <check_library+0x1de>
mov    0x2ce2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    137d <check_library+0x22d>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x2,-0x2c(%rbp)
je     1376 <check_library+0x226>
mov    0x2c7e(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    137d <check_library+0x22d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_library>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    0x2e69(%rip),%r15
mov    (%r15),%ebp
test   %rax,%rax
je     1223 <check_library+0xd3>
mov    %rax,%rbx
lea    0xea1(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe9a(%rip),%rsi
mov    %rbx,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     124a <check_library+0xfa>
cmpl   $0x1,(%rax)
jne    1271 <check_library+0x121>
lea    0xeda(%rip),%rsi
mov    %rbx,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     128c <check_library+0x13c>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   *%rax
mov    %rbx,%rdi
call   1070 <dlclose@plt>
mov    (%r15),%ebp
test   %rax,%rax
js     12b3 <check_library+0x163>
lea    0xf05(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
cmpl   $0x2,0x4(%rsp)
je     12d5 <check_library+0x185>
mov    (%r15),%edi
lea    0xef0(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12d0 <check_library+0x180>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xdcf(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%ebx
jmp    12d5 <check_library+0x185>
mov    (%r15),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xdd9(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xa,%ebx
jmp    12d5 <check_library+0x185>
mov    (%r15),%edi
lea    0xded(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xb,%ebx
jmp    12d5 <check_library+0x185>
mov    (%r15),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xdfc(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xc,%ebx
jmp    12d5 <check_library+0x185>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe10(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RTLD_NOW = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"Could not load shared library %s: %s\0A\00"
@.str.1 = [12 x i8] c"%s loaded.\0A\00"
@.str.2 = [2 x i8] c"x\00"
@.str.3 = [52 x i8] c"Could not access global DLL variable (x) in %s: %s\0A\00"
@.str.4 = [43 x i8] c"Constructor was not run on dlopen(\22%s\22) !\0A\00"
@.str.5 = [6 x i8] c"set_y\00"
@.str.6 = [56 x i8] c"Could not access global DLL function (set_y) in %s: %s\0A\00"
@.str.7 = [40 x i8] c"Could not unload shared library %s: %s\0A\00"
@.str.8 = [14 x i8] c"%s unloaded.\0A\00"
@.str.9 = [49 x i8] c"Static destructors was not called on dlclose()!\0A\00"
define i32 @check_library(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca void (i32*)*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @RTLD_NOW
%10 = call i8* @dlopen(i8* %8, i32 %9)
store i8* %10, i8** %4
store i32 0, i32* %7
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = call i8* (...) @dlerror()
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
store i32 1, i32* %2
br label %71
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %4
%23 = call i8* @dlsym(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = bitcast i8* %23 to i32*
store i32* %24, i32** %5
%25 = load i32*, i32** %5
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %3
%30 = call i8* (...) @dlerror()
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %29, i8* %30)
store i32 10, i32* %2
br label %71
32: ; preds = %18
%33 = load i32*, i32** %5
%34 = load i32, i32* %33
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %3
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %38)
store i32 11, i32* %2
br label %71
40: ; preds = %32
%41 = load i8*, i8** %4
%42 = call i8* @dlsym(i8* %41, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%43 = bitcast i8* %42 to void (i32*)*
store void (i32*)* %43, void (i32*)** %6
%44 = load void (i32*)*, void (i32*)** %6
%45 = icmp eq void (i32*)* %44, null
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %3
%49 = call i8* (...) @dlerror()
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i8* %48, i8* %49)
store i32 12, i32* %2
br label %71
51: ; preds = %40
store i32 0, i32* %7
%52 = load void (i32*)*, void (i32*)** %6
%53 = load i8*, i8** %4
%54 = call i64 @dlclose(i8* %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %51
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %3
%59 = call i8* (...) @dlerror()
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i8* %58, i8* %59)
store i32 2, i32* %2
br label %71
61: ; preds = %51
%62 = load i32, i32* @stderr
%63 = load i8*, i8** %3
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %63)
%65 = load i32, i32* %7
%66 = icmp ne i32 %65, 2
br i1 %66, label %67, label %70
67: ; preds = %61
%68 = load i32, i32* @stderr
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.9, i64 0, i64 0))
store i32 2, i32* %2
br label %71
70: ; preds = %61
store i32 0, i32* %2
br label %71
71: ; preds = %70, %67, %56, %46, %36, %27, %13
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
@RTLD_NOW = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"Could not load shared library %s: %s\0A\00"
@.str.1 = [12 x i8] c"%s loaded.\0A\00"
@.str.2 = [2 x i8] c"x\00"
@.str.3 = [52 x i8] c"Could not access global DLL variable (x) in %s: %s\0A\00"
@.str.4 = [43 x i8] c"Constructor was not run on dlopen(\22%s\22) !\0A\00"
@.str.5 = [6 x i8] c"set_y\00"
@.str.6 = [56 x i8] c"Could not access global DLL function (set_y) in %s: %s\0A\00"
@.str.7 = [40 x i8] c"Could not unload shared library %s: %s\0A\00"
@.str.8 = [14 x i8] c"%s unloaded.\0A\00"
@.str.9 = [49 x i8] c"Static destructors was not called on dlclose()!\0A\00"
define i32 @check_library(i8* %0) {
%2 = alloca i32
%3 = load i32, i32* @RTLD_NOW
%4 = tail call i8* @dlopen(i8* %0, i32 %3)
%5 = bitcast i32* %2 to i8*
%6 = icmp eq i8* %4, null
%7 = load i32, i32* @stderr
br i1 %6, label %8, label %11
8: ; preds = %1
%9 = tail call i8* (...) @dlerror()
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
br label %48
11: ; preds = %1
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = tail call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i8* (...) @dlerror()
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %17)
br label %48
19: ; preds = %11
%20 = bitcast i8* %13 to i32*
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, 1
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %48
26: ; preds = %19
%27 = tail call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i8* (...) @dlerror()
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i8* %0, i8* %31)
br label %48
33: ; preds = %26
%34 = bitcast i8* %27 to void (i32*)*
store i32 0, i32* %2
%35 = call i64 @dlclose(i8* %4)
%36 = icmp slt i64 %35, 0
%37 = load i32, i32* @stderr
br i1 %36, label %38, label %41
38: ; preds = %33
%39 = call i8* (...) @dlerror()
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %39)
br label %48
41: ; preds = %33
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %0)
%43 = load i32, i32* %2
%44 = icmp eq i32 %43, 2
br i1 %44, label %48, label %45
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.9, i64 0, i64 0))
br label %48
48: ; preds = %41, %45, %38, %29, %23, %15, %8
%49 = phi i32 [ 1, %8 ], [ 10, %15 ], [ 11, %23 ], [ 12, %29 ], [ 2, %38 ], [ 2, %45 ], [ 0, %41 ]
ret i32 %49
}