#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_FAILURE;
int RTLD_GLOBAL;
int RTLD_NOW;
scalar_t__ dlclose(void *);
char *dlerror();
void *dlopen(char *, int);
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                         char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ mtrace();
int /*<<< orphan*/ printf(char *, int, char *);
int main(void) {
  void *su[5];
  void *h;
  int n;
  mtrace();
  if ((su[0] = dlopen("testobj1.so", RTLD_GLOBAL | RTLD_NOW)) == NULL ||
      (su[1] = dlopen("testobj2.so", RTLD_GLOBAL | RTLD_NOW)) == NULL ||
      (su[2] = dlopen("testobj3.so", RTLD_GLOBAL | RTLD_NOW)) == NULL ||
      (su[3] = dlopen("testobj4.so", RTLD_GLOBAL | RTLD_NOW)) == NULL ||
      (su[4] = dlopen("testobj5.so", RTLD_GLOBAL | RTLD_NOW)) == NULL)
    error(EXIT_FAILURE, 0, "failed to load shared object: %s", dlerror());
  h = dlopen("failobj.so", RTLD_GLOBAL | RTLD_NOW);
  printf("h = %p, %s\n", h, h == NULL ? "ok" : "fail");
  for (n = 0; n < 5; ++n)
    if (dlclose(su[n]) != 0) {
      printf("failed to unload su[%d]: %s\n", n, dlerror());
      exit(EXIT_FAILURE);
    }
  return h != NULL;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <mtrace@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
lea    0xe61(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1262 <main+0xf2>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
lea    0xe41(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1262 <main+0xf2>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
lea    0xe21(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1262 <main+0xf2>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
lea    0xe01(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1262 <main+0xf2>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    0x2d9a(%rip),%rax
or     (%rax),%esi
lea    0xde1(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1289 <main+0x119>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rcx
xor    %esi,%esi
lea    0xdb8(%rip),%rdx
call   1060 <error@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
mov    0x2d47(%rip),%rax
or     (%rax),%esi
lea    0xdbb(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x38(%rbp),%rcx
lea    0xdbb(%rip),%rdx
lea    0xdb1(%rip),%rax
cmp    $0x0,%rcx
cmove  %rax,%rdx
lea    0xd96(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x5,-0x3c(%rbp)
jge    1340 <main+0x1d0>
movslq -0x3c(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdi
call   1090 <dlclose@plt>
cmp    $0x0,%rax
je     132d <main+0x1bd>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x44(%rbp),%esi
mov    %rax,%rdx
lea    0xd62(%rip),%rdi
call   1040 <printf@plt>
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1332 <main+0x1c2>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    12de <main+0x16e>
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1070 <mtrace@plt>
mov    0x2e5c(%rip),%r12
mov    0x2e4d(%rip),%r15
mov    (%r15),%esi
or     (%r12),%esi
lea    0xe5f(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,%r13
test   %rax,%rax
je     1224 <main+0xb4>
mov    (%r15),%esi
or     (%r12),%esi
lea    0xe50(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     1226 <main+0xb6>
mov    %rax,%r14
mov    (%r15),%esi
or     (%r12),%esi
lea    0xe41(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     122b <main+0xbb>
mov    %rax,0x10(%rsp)
mov    (%r15),%esi
or     (%r12),%esi
lea    0xe30(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     12ec <main+0x17c>
mov    %rax,0x8(%rsp)
mov    (%r15),%esi
or     (%r12),%esi
lea    0xe1b(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1257 <main+0xe7>
jmp    1232 <main+0xc2>
jmp    1232 <main+0xc2>
xor    %r14d,%r14d
jmp    1232 <main+0xc2>
xor    %eax,%eax
mov    %rax,0x10(%rsp)
mov    0x2d97(%rip),%rax
mov    (%rax),%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xdf1(%rip),%rdx
mov    %ebp,%edi
xor    %esi,%esi
mov    %rax,%rcx
call   1060 <error@plt>
mov    (%r15),%esi
or     (%r12),%esi
lea    0xdf8(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,%rbp
test   %rax,%rax
lea    0xdfd(%rip),%rax
lea    0xdf9(%rip),%rdx
cmove  %rax,%rdx
lea    0xddf(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %r13,%rdi
call   1090 <dlclose@plt>
test   %rax,%rax
jne    12f8 <main+0x188>
mov    %r14,%rdi
call   1090 <dlclose@plt>
test   %rax,%rax
jne    12fc <main+0x18c>
mov    0x10(%rsp),%rdi
call   1090 <dlclose@plt>
test   %rax,%rax
jne    1303 <main+0x193>
mov    0x8(%rsp),%rdi
call   1090 <dlclose@plt>
test   %rax,%rax
jne    130a <main+0x19a>
mov    %rbx,%rdi
call   1090 <dlclose@plt>
test   %rax,%rax
jne    1311 <main+0x1a1>
xor    %eax,%eax
test   %rbp,%rbp
setne  %al
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
mov    %rax,0x8(%rsp)
jmp    1232 <main+0xc2>
xor    %ebx,%ebx
jmp    1316 <main+0x1a6>
mov    $0x1,%ebx
jmp    1316 <main+0x1a6>
mov    $0x2,%ebx
jmp    1316 <main+0x1a6>
mov    $0x3,%ebx
jmp    1316 <main+0x1a6>
mov    $0x4,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xd58(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"testobj1.so\00"
@RTLD_GLOBAL = global i32 0
@RTLD_NOW = global i32 0
@.str.1 = [12 x i8] c"testobj2.so\00"
@.str.2 = [12 x i8] c"testobj3.so\00"
@.str.3 = [12 x i8] c"testobj4.so\00"
@.str.4 = [12 x i8] c"testobj5.so\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [33 x i8] c"failed to load shared object: %s\00"
@.str.6 = [11 x i8] c"failobj.so\00"
@.str.7 = [12 x i8] c"h = %p, %s\0A\00"
@.str.8 = [3 x i8] c"ok\00"
@.str.9 = [5 x i8] c"fail\00"
@.str.10 = [29 x i8] c"failed to unload su[%d]: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [5 x i8*]
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @mtrace()
%6 = load i32, i32* @RTLD_GLOBAL
%7 = load i32, i32* @RTLD_NOW
%8 = or i32 %6, %7
%9 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 0
store i8* %9, i8** %10
%11 = icmp eq i8* %9, null
br i1 %11, label %40, label %12
12: ; preds = %0
%13 = load i32, i32* @RTLD_GLOBAL
%14 = load i32, i32* @RTLD_NOW
%15 = or i32 %13, %14
%16 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
store i8* %16, i8** %17
%18 = icmp eq i8* %16, null
br i1 %18, label %40, label %19
19: ; preds = %12
%20 = load i32, i32* @RTLD_GLOBAL
%21 = load i32, i32* @RTLD_NOW
%22 = or i32 %20, %21
%23 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 2
store i8* %23, i8** %24
%25 = icmp eq i8* %23, null
br i1 %25, label %40, label %26
26: ; preds = %19
%27 = load i32, i32* @RTLD_GLOBAL
%28 = load i32, i32* @RTLD_NOW
%29 = or i32 %27, %28
%30 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 3
store i8* %30, i8** %31
%32 = icmp eq i8* %30, null
br i1 %32, label %40, label %33
33: ; preds = %26
%34 = load i32, i32* @RTLD_GLOBAL
%35 = load i32, i32* @RTLD_NOW
%36 = or i32 %34, %35
%37 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 4
store i8* %37, i8** %38
%39 = icmp eq i8* %37, null
br i1 %39, label %40, label %44
40: ; preds = %33, %26, %19, %12, %0
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i8* (...) @dlerror()
%43 = call i32 @error(i32 %41, i32 0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %40, %33
%45 = load i32, i32* @RTLD_GLOBAL
%46 = load i32, i32* @RTLD_NOW
%47 = or i32 %45, %46
%48 = call i8* @dlopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %47)
store i8* %48, i8** %3
%49 = load i8*, i8** %3
%50 = ptrtoint i8* %49 to i32
%51 = load i8*, i8** %3
%52 = icmp eq i8* %51, null
%53 = zext i1 %52 to i64
%54 = select i1 %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0)
%55 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %50, i8* %54)
store i32 0, i32* %4
br label %56
56: ; preds = %73, %44
%57 = load i32, i32* %4
%58 = icmp slt i32 %57, 5
br i1 %58, label %59, label %76
59: ; preds = %56
%60 = load i32, i32* %4
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %61
%63 = load i8*, i8** %62
%64 = call i64 @dlclose(i8* %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %59
%67 = load i32, i32* %4
%68 = call i8* (...) @dlerror()
%69 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0), i32 %67, i8* %68)
%70 = load i32, i32* @EXIT_FAILURE
%71 = call i32 @exit(i32 %70)
unreachable
72: ; preds = %59
br label %73
73: ; preds = %72
%74 = load i32, i32* %4
%75 = add nsw i32 %74, 1
store i32 %75, i32* %4
br label %56
76: ; preds = %56
%77 = load i8*, i8** %3
%78 = icmp ne i8* %77, null
%79 = zext i1 %78 to i32
ret i32 %79
}

LLVM IR (-O2):
@.str = [12 x i8] c"testobj1.so\00"
@RTLD_GLOBAL = global i32 0
@RTLD_NOW = global i32 0
@.str.1 = [12 x i8] c"testobj2.so\00"
@.str.2 = [12 x i8] c"testobj3.so\00"
@.str.3 = [12 x i8] c"testobj4.so\00"
@.str.4 = [12 x i8] c"testobj5.so\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [33 x i8] c"failed to load shared object: %s\00"
@.str.6 = [11 x i8] c"failobj.so\00"
@.str.7 = [12 x i8] c"h = %p, %s\0A\00"
@.str.8 = [3 x i8] c"ok\00"
@.str.9 = [5 x i8] c"fail\00"
@.str.10 = [29 x i8] c"failed to unload su[%d]: %s\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @mtrace()
%2 = load i32, i32* @RTLD_GLOBAL
%3 = load i32, i32* @RTLD_NOW
%4 = or i32 %3, %2
%5 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %31, label %7
7: ; preds = %0
%8 = load i32, i32* @RTLD_GLOBAL
%9 = load i32, i32* @RTLD_NOW
%10 = or i32 %9, %8
%11 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %31, label %13
13: ; preds = %7
%14 = load i32, i32* @RTLD_GLOBAL
%15 = load i32, i32* @RTLD_NOW
%16 = or i32 %15, %14
%17 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %31, label %19
19: ; preds = %13
%20 = load i32, i32* @RTLD_GLOBAL
%21 = load i32, i32* @RTLD_NOW
%22 = or i32 %21, %20
%23 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %31, label %25
25: ; preds = %19
%26 = load i32, i32* @RTLD_GLOBAL
%27 = load i32, i32* @RTLD_NOW
%28 = or i32 %27, %26
%29 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %38
31: ; preds = %25, %19, %13, %7, %0
%32 = phi i8* [ undef, %0 ], [ undef, %7 ], [ undef, %13 ], [ null, %19 ], [ %23, %25 ]
%33 = phi i8* [ undef, %0 ], [ undef, %7 ], [ null, %13 ], [ %17, %19 ], [ %17, %25 ]
%34 = phi i8* [ undef, %0 ], [ null, %7 ], [ %11, %13 ], [ %11, %19 ], [ %11, %25 ]
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i8* (...) @dlerror()
%37 = tail call i32 @error(i32 %35, i32 0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %31, %25
%39 = phi i8* [ %32, %31 ], [ %23, %25 ]
%40 = phi i8* [ %33, %31 ], [ %17, %25 ]
%41 = phi i8* [ %34, %31 ], [ %11, %25 ]
%42 = phi i8* [ null, %31 ], [ %29, %25 ]
%43 = load i32, i32* @RTLD_GLOBAL
%44 = load i32, i32* @RTLD_NOW
%45 = or i32 %44, %43
%46 = tail call i8* @dlopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %45)
%47 = ptrtoint i8* %46 to i64
%48 = trunc i64 %47 to i32
%49 = icmp eq i8* %46, null
%50 = select i1 %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0)
%51 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %48, i8* %50)
%52 = tail call i64 @dlclose(i8* %5)
%53 = icmp eq i64 %52, 0
br i1 %53, label %60, label %54
54: ; preds = %69, %66, %63, %60, %38
%55 = phi i32 [ 0, %38 ], [ 1, %60 ], [ 2, %63 ], [ 3, %66 ], [ 4, %69 ]
%56 = tail call i8* (...) @dlerror()
%57 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0), i32 %55, i8* %56)
%58 = load i32, i32* @EXIT_FAILURE
%59 = tail call i32 @exit(i32 %58)
unreachable
60: ; preds = %38
%61 = tail call i64 @dlclose(i8* %41)
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %54
63: ; preds = %60
%64 = tail call i64 @dlclose(i8* %40)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %54
66: ; preds = %63
%67 = tail call i64 @dlclose(i8* %39)
%68 = icmp eq i64 %67, 0
br i1 %68, label %69, label %54
69: ; preds = %66
%70 = tail call i64 @dlclose(i8* %42)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %54
72: ; preds = %69
%73 = icmp ne i8* %46, null
%74 = zext i1 %73 to i32
ret i32 %74
}