#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 EXIT_SUCCESS;
int /*<<< orphan*/ FAIL(char *, ...);
int /*<<< orphan*/ PASS(char *, char const *);
int /*<<< orphan*/ RTLD_LAZY;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *strstr(char const *, char const *);
int main(int argc, const char *argv[]) {
  void *handle = dlopen("./libfoo.dylib", RTLD_LAZY);
  if (handle != NULL) {
    FAIL("dlopen-sandbox dylib should not have loaded");
    return EXIT_SUCCESS;
  }
  const char *errorMsg = dlerror();
  const char *shouldContain = argv[1];
  if (strstr(errorMsg, shouldContain) == NULL)
    FAIL("dlopen-sandbox dylib correctly failed to loaded, but with wrong "
         "error message: %s",
         errorMsg);
  else
    PASS("dlopen-sandbox: %s", shouldContain);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a9 <main+0x59>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1060 <FAIL@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120a <main+0xba>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
jne    11ee <main+0x9e>
mov    -0x20(%rbp),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1060 <FAIL@plt>
jmp    11fe <main+0xae>
mov    -0x28(%rbp),%rsi
lea    0xe94(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1181 <main+0x31>
lea    0xe97(%rip),%rdi
xor    %eax,%eax
call   1060 <FAIL@plt>
jmp    11c1 <main+0x71>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%r14
mov    0x8(%rbx),%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11b0 <main+0x60>
lea    0xee7(%rip),%rdi
mov    %rbx,%rsi
call   1050 <PASS@plt>
jmp    11c1 <main+0x71>
lea    0xe84(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <FAIL@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"./libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [44 x i8] c"dlopen-sandbox dylib should not have loaded\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [82 x i8] c"dlopen-sandbox dylib correctly failed to loaded, but with wrong error message: %s\00"
@.str.3 = [19 x i8] c"dlopen-sandbox: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @RTLD_LAZY
%10 = call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_SUCCESS
store i32 %15, i32* %3
br label %33
16: ; preds = %2
%17 = call i8* (...) @dlerror()
store i8* %17, i8** %7
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %8
%21 = load i8*, i8** %7
%22 = load i8*, i8** %8
%23 = call i32* @strstr(i8* %21, i8* %22)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = load i8*, i8** %7
%27 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %31
28: ; preds = %16
%29 = load i8*, i8** %8
%30 = call i32 @PASS(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* @EXIT_SUCCESS
store i32 %32, i32* %3
br label %33
33: ; preds = %31, %13
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [15 x i8] c"./libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [44 x i8] c"dlopen-sandbox dylib should not have loaded\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [82 x i8] c"dlopen-sandbox dylib correctly failed to loaded, but with wrong error message: %s\00"
@.str.3 = [19 x i8] c"dlopen-sandbox: %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @RTLD_LAZY
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %18
8: ; preds = %2
%9 = tail call i8* (...) @dlerror()
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @strstr(i8* dereferenceable(1) %9, i8* dereferenceable(1) %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %18
16: ; preds = %8
%17 = tail call i32 @PASS(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %11)
br label %18
18: ; preds = %14, %16, %6
%19 = load i32, i32* @EXIT_SUCCESS
ret i32 %19
}