#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_NOW;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *strstr(char const *, char *);
int main() {
  void *handle = dlopen("test.bundle", RTLD_NOW);
  if (handle == NULL) {
    const char *msg = dlerror();
    if (strstr(msg, "foo2") != NULL) {
      PASS("dlopen-RTLD_NOW");
      exit(0);
    }
    FAIL("dlopen(test.bundle, RTLD_NOW) failed but error message did not "
         "contain foo2: %s",
         msg);
    exit(0);
  }
  FAIL("dlopen(test.bundle, RTLD_NOW) succeed but should have failed because "
       "foo2 does not exist");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e4 <main+0x84>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     11cb <main+0x6b>
lea    0xe52(%rip),%rdi
call   1050 <PASS@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
jne    11b1 <main+0x51>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rbx
lea    0xe80(%rip),%rsi
mov    %rax,%rdi
call   1080 <strstr@plt>
test   %rax,%rax
jne    11c6 <main+0x66>
lea    0xe81(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xeb9(%rip),%rdi
xor    %eax,%eax
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe44(%rip),%rdi
call   1050 <PASS@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"test.bundle\00"
@RTLD_NOW = global i32 0
@.str.1 = [5 x i8] c"foo2\00"
@.str.2 = [16 x i8] c"dlopen-RTLD_NOW\00"
@.str.3 = [80 x i8] c"dlopen(test.bundle, RTLD_NOW) failed but error message did not contain foo2: %s\00"
@.str.4 = [89 x i8] c"dlopen(test.bundle, RTLD_NOW) succeed but should have failed because foo2 does not exist\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @RTLD_NOW
%5 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %20
8: ; preds = %0
%9 = call i8* (...) @dlerror()
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = call i32* @strstr(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = call i32 @PASS(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %8
%17 = load i8*, i8** %3
%18 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %0
%21 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"test.bundle\00"
@RTLD_NOW = global i32 0
@.str.1 = [5 x i8] c"foo2\00"
@.str.2 = [16 x i8] c"dlopen-RTLD_NOW\00"
@.str.3 = [80 x i8] c"dlopen(test.bundle, RTLD_NOW) failed but error message did not contain foo2: %s\00"
@.str.4 = [89 x i8] c"dlopen(test.bundle, RTLD_NOW) succeed but should have failed because foo2 does not exist\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_NOW
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %14
4: ; preds = %0
%5 = tail call i8* (...) @dlerror()
%6 = tail call i32* @strstr(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i32 @PASS(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %4
%12 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %0
%15 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.4, i64 0, i64 0))
%16 = tail call i32 @exit(i32 0)
unreachable
}