#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*/ FAIL(char *, ...);
int /*<<< orphan*/ PASS(char *);
int /*<<< orphan*/ RTLD_NOLOAD;
int /*<<< orphan*/ XFAIL(char *, char const *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ *strstr(char const *, char *);
int main() {
  void *handle = dlopen("libfoo.dylib", RTLD_NOLOAD);
  if (handle == NULL) {
    const char *msg = dlerror();
    if (strstr(msg, "RTLD_NOLOAD") != NULL)
      XFAIL("dlopen(libfoo.dylib, RTLD_NOLOAD) failed but it should have "
            "worked: %s",
            msg);
    else
      FAIL("dlopen(libfoo.dylib, RTLD_NOLOAD) failed but it should have "
           "worked: %s",
           msg);
    return 0;
  }
  void *sym = dlsym(handle, "foo");
  if (sym == NULL) {
    const char *msg = dlerror();
    FAIL("dlsym(handle, \"foo\") failed but it should have worked: %s", msg);
    return 0;
  }
  void *handle2 = dlopen("libfobbulate.dylib", RTLD_NOLOAD);
  if (handle2 != NULL) {
    FAIL("dlopen(libfobbulate.dylib, RTLD_NOLOAD) succeeded but it should have "
         "failed");
    return 0;
  }
  PASS("dlopen-RTLD_NOLOAD");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fb <main+0x8b>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xf3f(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     11dd <main+0x6d>
mov    -0x18(%rbp),%rsi
lea    0xe3a(%rip),%rdi
call   1060 <XFAIL@plt>
jmp    11ef <main+0x7f>
mov    -0x18(%rbp),%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1080 <FAIL@plt>
movl   $0x0,-0x4(%rbp)
jmp    1294 <main+0x124>
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1243 <main+0xd3>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1080 <FAIL@plt>
movl   $0x0,-0x4(%rbp)
jmp    1294 <main+0x124>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
lea    0xe3f(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1281 <main+0x111>
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1080 <FAIL@plt>
movl   $0x0,-0x4(%rbp)
jmp    1294 <main+0x124>
lea    0xe69(%rip),%rdi
call   1050 <PASS@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e68(%rip),%rbx
mov    (%rbx),%esi
lea    0xe7f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11c2 <main+0x52>
lea    0xec2(%rip),%rsi
mov    %rax,%rdi
call   1070 <dlsym@plt>
test   %rax,%rax
je     11f1 <main+0x81>
mov    (%rbx),%esi
lea    0xeea(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     120b <main+0x9b>
lea    0xeec(%rip),%rdi
xor    %eax,%eax
call   1080 <FAIL@plt>
jmp    1217 <main+0xa7>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rbx
lea    0xf25(%rip),%rsi
mov    %rax,%rdi
call   1090 <strstr@plt>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
test   %rax,%rax
je     1202 <main+0x92>
call   1060 <XFAIL@plt>
jmp    1217 <main+0xa7>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe59(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <FAIL@plt>
jmp    1217 <main+0xa7>
lea    0xedf(%rip),%rdi
call   1050 <PASS@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_NOLOAD = global i32 0
@.str.1 = [12 x i8] c"RTLD_NOLOAD\00"
@.str.2 = [71 x i8] c"dlopen(libfoo.dylib, RTLD_NOLOAD) failed but it should have worked: %s\00"
@.str.3 = [4 x i8] c"foo\00"
@.str.4 = [58 x i8] c"dlsym(handle, \22foo\22) failed but it should have worked: %s\00"
@.str.5 = [19 x i8] c"libfobbulate.dylib\00"
@.str.6 = [76 x i8] c"dlopen(libfobbulate.dylib, RTLD_NOLOAD) succeeded but it should have failed\00"
@.str.7 = [19 x i8] c"dlopen-RTLD_NOLOAD\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
%7 = load i32, i32* @RTLD_NOLOAD
%8 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %23
11: ; preds = %0
%12 = call i8* (...) @dlerror()
store i8* %12, i8** %3
%13 = load i8*, i8** %3
%14 = call i32* @strstr(i8* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = call i32 @XFAIL(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %22
19: ; preds = %11
%20 = load i8*, i8** %3
%21 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %16
store i32 0, i32* %1
br label %41
23: ; preds = %0
%24 = load i8*, i8** %2
%25 = call i8* @dlsym(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i8* %25, i8** %4
%26 = load i8*, i8** %4
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = call i8* (...) @dlerror()
store i8* %29, i8** %5
%30 = load i8*, i8** %5
%31 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i8* %30)
store i32 0, i32* %1
br label %41
32: ; preds = %23
%33 = load i32, i32* @RTLD_NOLOAD
%34 = call i8* @dlopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %33)
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %41
39: ; preds = %32
%40 = call i32 @PASS(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %41
41: ; preds = %39, %37, %28, %22
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_NOLOAD = global i32 0
@.str.1 = [12 x i8] c"RTLD_NOLOAD\00"
@.str.2 = [71 x i8] c"dlopen(libfoo.dylib, RTLD_NOLOAD) failed but it should have worked: %s\00"
@.str.3 = [4 x i8] c"foo\00"
@.str.4 = [58 x i8] c"dlsym(handle, \22foo\22) failed but it should have worked: %s\00"
@.str.5 = [19 x i8] c"libfobbulate.dylib\00"
@.str.6 = [76 x i8] c"dlopen(libfobbulate.dylib, RTLD_NOLOAD) succeeded but it should have failed\00"
@.str.7 = [19 x i8] c"dlopen-RTLD_NOLOAD\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_NOLOAD
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %12
4: ; preds = %0
%5 = tail call i8* (...) @dlerror()
%6 = tail call i32* @strstr(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call i32 @XFAIL(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %26
10: ; preds = %4
%11 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %26
12: ; preds = %0
%13 = tail call i8* @dlsym(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i8* (...) @dlerror()
%17 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i8* %16)
br label %26
18: ; preds = %12
%19 = load i32, i32* @RTLD_NOLOAD
%20 = tail call i8* @dlopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.6, i64 0, i64 0))
br label %26
24: ; preds = %18
%25 = tail call i32 @PASS(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
br label %26
26: ; preds = %15, %24, %22, %8, %10
ret i32 0
}