#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 RTLD_LAZY;
int RTLD_NOLOAD;
scalar_t__ dlclose(void *);
char *dlerror();
int /*<<< orphan*/ *dlopen(char *, int);
int /*<<< orphan*/ mtrace();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int main(void) {
  int result = 0;
  void *p;
  mtrace();
  p = dlopen("testobj1.so", RTLD_LAZY | RTLD_NOLOAD);
  if (p == NULL) {
    printf("cannot open \"testobj1.so\": %s\n", dlerror());
    result = 1;
  } else {
    puts("loading \"testobj1.so\" succeeded, OK");
    dlclose(p);
  }
  if (dlopen("testobj5.so", RTLD_LAZY | RTLD_NOLOAD) != NULL) {
    puts("succeeded in loading \"testobj5.so\"");
    result = 1;
  } else {
    puts("\"testobj5.so\" wasn't loaded and RTLD_NOLOAD prevented it, OK");
    p = dlopen("testobj5.so", RTLD_LAZY);
    if (p == NULL) {
      printf("cannot open \"testobj5.so\" without RTLD_NOLOAD: %s\n",
             dlerror());
      result = 1;
    } else {
      puts("loading \"testobj5.so\" succeeded, OK");
      void *q = dlopen("testobj5.so", RTLD_LAZY | RTLD_NOLOAD);
      if (q == NULL) {
        printf("cannot open \"testobj5.so\": %s\n", dlerror());
        result = 1;
      } else {
        puts("loading \"testobj5.so\" with RTLD_NOLOAD succeeded, OK");
        dlclose(q);
      }
      if (dlclose(p) != 0) {
        printf("cannot close \"testobj5.so\": %s\n", dlerror());
        result = 1;
      } else
        puts("closing \"testobj5.so\" succeeded, OK");
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1070 <mtrace@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cc <main+0x6c>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x8(%rbp)
jmp    11e1 <main+0x81>
lea    0xe58(%rip),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
or     (%rax),%esi
lea    0xe55(%rip),%rdi
call   1060 <dlopen@plt>
cmp    $0x0,%rax
je     1221 <main+0xc1>
lea    0xe4b(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x8(%rbp)
jmp    132e <main+0x1ce>
lea    0xe56(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
lea    0xe12(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1273 <main+0x113>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x8(%rbp)
jmp    1329 <main+0x1c9>
lea    0xe74(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
mov    0x2d49(%rip),%rax
or     (%rax),%esi
lea    0xdb7(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12ce <main+0x16e>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe55(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x8(%rbp)
jmp    12e3 <main+0x183>
lea    0xe5c(%rip),%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%rdi
call   1080 <dlclose@plt>
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
cmp    $0x0,%rax
je     1318 <main+0x1b8>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe5f(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x8(%rbp)
jmp    1324 <main+0x1c4>
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
jmp    1329 <main+0x1c9>
jmp    132e <main+0x1ce>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <mtrace@plt>
mov    0x2e68(%rip),%r15
mov    0x2e59(%rip),%r12
mov    (%r12),%esi
or     (%r15),%esi
lea    0xe73(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     11b0 <main+0x50>
mov    %rax,%rbx
lea    0xe8a(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
jmp    11cb <main+0x6b>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe4e(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    (%r12),%esi
or     (%r15),%esi
lea    0xe76(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     11f9 <main+0x99>
lea    0xe71(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
jmp    12ba <main+0x15a>
lea    0xe7e(%rip),%rdi
call   1040 <puts@plt>
mov    (%r15),%esi
lea    0xe40(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     1259 <main+0xf9>
mov    %rax,%r14
lea    0xecb(%rip),%rdi
call   1040 <puts@plt>
mov    (%r12),%esi
or     (%r15),%esi
lea    0xe19(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     1269 <main+0x109>
mov    %rax,%rbx
lea    0xee7(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
jmp    1284 <main+0x124>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe54(%rip),%rdi
jmp    129f <main+0x13f>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe9b(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %r14,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
je     12ae <main+0x14e>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xec7(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    12ba <main+0x15a>
lea    0xed1(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"testobj1.so\00"
@RTLD_LAZY = global i32 0
@RTLD_NOLOAD = global i32 0
@.str.1 = [31 x i8] c"cannot open \22testobj1.so\22: %s\0A\00"
@.str.2 = [36 x i8] c"loading \22testobj1.so\22 succeeded, OK\00"
@.str.3 = [12 x i8] c"testobj5.so\00"
@.str.4 = [35 x i8] c"succeeded in loading \22testobj5.so\22\00"
@.str.5 = [61 x i8] c"\22testobj5.so\22 wasn't loaded and RTLD_NOLOAD prevented it, OK\00"
@.str.6 = [51 x i8] c"cannot open \22testobj5.so\22 without RTLD_NOLOAD: %s\0A\00"
@.str.7 = [36 x i8] c"loading \22testobj5.so\22 succeeded, OK\00"
@.str.8 = [31 x i8] c"cannot open \22testobj5.so\22: %s\0A\00"
@.str.9 = [53 x i8] c"loading \22testobj5.so\22 with RTLD_NOLOAD succeeded, OK\00"
@.str.10 = [32 x i8] c"cannot close \22testobj5.so\22: %s\0A\00"
@.str.11 = [36 x i8] c"closing \22testobj5.so\22 succeeded, OK\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %2
%5 = call i32 (...) @mtrace()
%6 = load i32, i32* @RTLD_LAZY
%7 = load i32, i32* @RTLD_NOLOAD
%8 = or i32 %6, %7
%9 = call i32* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = bitcast i32* %9 to i8*
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i8* (...) @dlerror()
%15 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 1, i32* %2
br label %20
16: ; preds = %0
%17 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%18 = load i8*, i8** %3
%19 = call i64 @dlclose(i8* %18)
br label %20
20: ; preds = %16, %13
%21 = load i32, i32* @RTLD_LAZY
%22 = load i32, i32* @RTLD_NOLOAD
%23 = or i32 %21, %22
%24 = call i32* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @puts(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %2
br label %65
28: ; preds = %20
%29 = call i32 @puts(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.5, i64 0, i64 0))
%30 = load i32, i32* @RTLD_LAZY
%31 = call i32* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = bitcast i32* %31 to i8*
store i8* %32, i8** %3
%33 = load i8*, i8** %3
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = call i8* (...) @dlerror()
%37 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0), i8* %36)
store i32 1, i32* %2
br label %64
38: ; preds = %28
%39 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
%40 = load i32, i32* @RTLD_LAZY
%41 = load i32, i32* @RTLD_NOLOAD
%42 = or i32 %40, %41
%43 = call i32* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = bitcast i32* %43 to i8*
store i8* %44, i8** %4
%45 = load i8*, i8** %4
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %50
47: ; preds = %38
%48 = call i8* (...) @dlerror()
%49 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %48)
store i32 1, i32* %2
br label %54
50: ; preds = %38
%51 = call i32 @puts(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.9, i64 0, i64 0))
%52 = load i8*, i8** %4
%53 = call i64 @dlclose(i8* %52)
br label %54
54: ; preds = %50, %47
%55 = load i8*, i8** %3
%56 = call i64 @dlclose(i8* %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = call i8* (...) @dlerror()
%60 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.10, i64 0, i64 0), i8* %59)
store i32 1, i32* %2
br label %63
61: ; preds = %54
%62 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.11, i64 0, i64 0))
br label %63
63: ; preds = %61, %58
br label %64
64: ; preds = %63, %35
br label %65
65: ; preds = %64, %26
%66 = load i32, i32* %2
ret i32 %66
}

LLVM IR (-O2):
@.str = [12 x i8] c"testobj1.so\00"
@RTLD_LAZY = global i32 0
@RTLD_NOLOAD = global i32 0
@.str.1 = [31 x i8] c"cannot open \22testobj1.so\22: %s\0A\00"
@.str.2 = [36 x i8] c"loading \22testobj1.so\22 succeeded, OK\00"
@.str.3 = [12 x i8] c"testobj5.so\00"
@.str.4 = [35 x i8] c"succeeded in loading \22testobj5.so\22\00"
@.str.5 = [61 x i8] c"\22testobj5.so\22 wasn't loaded and RTLD_NOLOAD prevented it, OK\00"
@.str.6 = [51 x i8] c"cannot open \22testobj5.so\22 without RTLD_NOLOAD: %s\0A\00"
@.str.7 = [36 x i8] c"loading \22testobj5.so\22 succeeded, OK\00"
@.str.8 = [31 x i8] c"cannot open \22testobj5.so\22: %s\0A\00"
@.str.9 = [53 x i8] c"loading \22testobj5.so\22 with RTLD_NOLOAD succeeded, OK\00"
@.str.10 = [32 x i8] c"cannot close \22testobj5.so\22: %s\0A\00"
@.str.11 = [36 x i8] c"closing \22testobj5.so\22 succeeded, OK\00"
define i32 @main() {
%1 = tail call i32 (...) @mtrace()
%2 = load i32, i32* @RTLD_LAZY
%3 = load i32, i32* @RTLD_NOLOAD
%4 = or i32 %3, %2
%5 = tail call i32* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i8* (...) @dlerror()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %14
10: ; preds = %0
%11 = bitcast i32* %5 to i8*
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i64 @dlclose(i8* %11)
br label %14
14: ; preds = %10, %7
%15 = phi i32 [ 1, %7 ], [ 0, %10 ]
%16 = load i32, i32* @RTLD_LAZY
%17 = load i32, i32* @RTLD_NOLOAD
%18 = or i32 %17, %16
%19 = tail call i32* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %14
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
br label %55
23: ; preds = %14
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.5, i64 0, i64 0))
%25 = load i32, i32* @RTLD_LAZY
%26 = tail call i32* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = bitcast i32* %26 to i8*
%28 = icmp eq i32* %26, null
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = tail call i8* (...) @dlerror()
%31 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0), i8* %30)
br label %55
32: ; preds = %23
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
%34 = load i32, i32* @RTLD_LAZY
%35 = load i32, i32* @RTLD_NOLOAD
%36 = or i32 %35, %34
%37 = tail call i32* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = tail call i8* (...) @dlerror()
%41 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %40)
br label %46
42: ; preds = %32
%43 = bitcast i32* %37 to i8*
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.9, i64 0, i64 0))
%45 = tail call i64 @dlclose(i8* %43)
br label %46
46: ; preds = %42, %39
%47 = phi i32 [ 1, %39 ], [ %15, %42 ]
%48 = tail call i64 @dlclose(i8* %27)
%49 = icmp eq i64 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %46
%51 = tail call i8* (...) @dlerror()
%52 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.10, i64 0, i64 0), i8* %51)
br label %55
53: ; preds = %46
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.11, i64 0, i64 0))
br label %55
55: ; preds = %50, %53, %29, %21
%56 = phi i32 [ 1, %21 ], [ 1, %29 ], [ 1, %50 ], [ %47, %53 ]
ret i32 %56
}