#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ RTLD_NOW;
int /*<<< orphan*/ dlclose(void *);
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fatal(int /*<<< orphan*/);
int /*<<< orphan*/ invoke(void *, char *);
int /*<<< orphan*/ printf(char *);
int main(__attribute__((unused)) int argc,
         __attribute__((unused)) char *argv[]) {
  void *libx1_handle, *libx2_handle;
  libx1_handle = dlopen("libx1.so", RTLD_NOW);
  if (libx1_handle == NULL)
    fatal(dlerror());
  libx2_handle = dlopen("libx2.so", RTLD_NOW);
  if (libx2_handle == NULL)
    fatal(dlerror());
  invoke(libx2_handle, "libx2_f2");
  invoke(libx1_handle, "libx1_f1");
  printf("Going to `dlclose(3)' libx2, it should not be unloaded since libx1 "
         "depends on it.\n");
  dlclose(libx2_handle);
  printf("Main is finished.\n");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <main+0x48>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1080 <fatal@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe41(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ea <main+0x7a>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1080 <fatal@plt>
mov    -0x20(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   1050 <invoke@plt>
mov    -0x18(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1050 <invoke@plt>
lea    0xe13(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1090 <dlclose@plt>
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e6d(%rip),%rbx
mov    (%rbx),%esi
lea    0xe7c(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    119f <main+0x2f>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1080 <fatal@plt>
mov    (%rbx),%esi
lea    0xe61(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c3 <main+0x53>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1080 <fatal@plt>
lea    0xe48(%rip),%rsi
mov    %rbx,%rdi
call   1050 <invoke@plt>
lea    0xe42(%rip),%rsi
mov    %r14,%rdi
call   1050 <invoke@plt>
lea    0xe3c(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1090 <dlclose@plt>
lea    0xe7a(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"libx1.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [9 x i8] c"libx2.so\00"
@.str.2 = [9 x i8] c"libx2_f2\00"
@.str.3 = [9 x i8] c"libx1_f1\00"
@.str.4 = [83 x i8] c"Going to `dlclose(3)' libx2, it should not be unloaded since libx1 depends on it.\0A\00"
@.str.5 = [19 x i8] c"Main is finished.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @RTLD_NOW
%9 = call i8* @dlopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (...) @dlerror()
%14 = call i32 @fatal(i32 %13)
br label %15
15: ; preds = %12, %2
%16 = load i32, i32* @RTLD_NOW
%17 = call i8* @dlopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 (...) @dlerror()
%22 = call i32 @fatal(i32 %21)
br label %23
23: ; preds = %20, %15
%24 = load i8*, i8** %7
%25 = call i32 @invoke(i8* %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%26 = load i8*, i8** %6
%27 = call i32 @invoke(i8* %26, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.4, i64 0, i64 0))
%29 = load i8*, i8** %7
%30 = call i32 @dlclose(i8* %29)
%31 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%32 = load i32, i32* @EXIT_SUCCESS
%33 = call i32 @exit(i32 %32)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"libx1.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [9 x i8] c"libx2.so\00"
@.str.2 = [9 x i8] c"libx2_f2\00"
@.str.3 = [9 x i8] c"libx1_f1\00"
@EXIT_SUCCESS = global i32 0
@str = [82 x i8] c"Going to `dlclose(3)' libx2, it should not be unloaded since libx1 depends on it.\00"
@str.6 = [18 x i8] c"Main is finished.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @RTLD_NOW
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 (...) @dlerror()
%8 = tail call i32 @fatal(i32 %7)
br label %9
9: ; preds = %6, %2
%10 = load i32, i32* @RTLD_NOW
%11 = tail call i8* @dlopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 (...) @dlerror()
%15 = tail call i32 @fatal(i32 %14)
br label %16
16: ; preds = %13, %9
%17 = tail call i32 @invoke(i8* %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @invoke(i8* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([82 x i8], [82 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @dlclose(i8* %11)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.6, i64 0, i64 0))
%22 = load i32, i32* @EXIT_SUCCESS
%23 = tail call i32 @exit(i32 %22)
unreachable
}