#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 *, char const *);
int /*<<< orphan*/ RTLD_LAZY;
char *dlerror();
void *dlopen(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *strstr(char *, char const *);
void *work(void *arg) {
  const char *str = (char *)arg;
  for (int i = 0; i < 1000; ++i) {
    void *handle = dlopen(str, RTLD_LAZY);
    if (handle != NULL) {
      FAIL("dlopen(%s) unexpectedly succeeded", str);
      exit(0);
    }
    char *msg = dlerror();
    if ((msg == NULL) || (strstr(msg, str) == NULL)) {
      FAIL("dlerror() did not contain library name that could not be loaded",
           str);
      exit(0);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<work>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x3e8,-0x14(%rbp)
jge    1207 <work+0xb7>
mov    -0x10(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b0 <work+0x60>
mov    -0x10(%rbp),%rsi
lea    0xe5c(%rip),%rdi
call   1060 <FAIL@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11dd <work+0x8d>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
jne    11f4 <work+0xa4>
mov    -0x10(%rbp),%rsi
lea    0xe3a(%rip),%rdi
call   1060 <FAIL@plt>
xor    %edi,%edi
call   1050 <exit@plt>
jmp    11f9 <work+0xa9>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116b <work+0x1b>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<work>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x3e8,%ebp
mov    0x2e7d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
jne    11b1 <work+0x61>
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     11a8 <work+0x58>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11a8 <work+0x58>
add    $0xffffffff,%ebp
jne    1170 <work+0x20>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe73(%rip),%rdi
jmp    11b8 <work+0x68>
lea    0xe48(%rip),%rdi
mov    %rbx,%rsi
call   1060 <FAIL@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@.str = [34 x i8] c"dlopen(%s) unexpectedly succeeded\00"
@.str.1 = [64 x i8] c"dlerror() did not contain library name that could not be loaded\00"
define i8* @work(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
store i8* %7, i8** %3
store i32 0, i32* %4
br label %8
8: ; preds = %35, %1
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 1000
br i1 %10, label %11, label %38
11: ; preds = %8
%12 = load i8*, i8** %3
%13 = load i32, i32* @RTLD_LAZY
%14 = call i8* @dlopen(i8* %12, i32 %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i8*, i8** %3
%19 = call i32 @FAIL(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %11
%22 = call i8* (...) @dlerror()
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp eq i8* %23, null
br i1 %24, label %30, label %25
25: ; preds = %21
%26 = load i8*, i8** %6
%27 = load i8*, i8** %3
%28 = call i32* @strstr(i8* %26, i8* %27)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %25, %21
%31 = load i8*, i8** %3
%32 = call i32 @FAIL(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %25
br label %35
35: ; preds = %34
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
store i32 %37, i32* %4
br label %8
38: ; preds = %8
ret i8* null
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
@.str = [34 x i8] c"dlopen(%s) unexpectedly succeeded\00"
@.str.1 = [64 x i8] c"dlerror() did not contain library name that could not be loaded\00"
define noalias i8* @work(i8* %0) {
br label %6
2: ; preds = %17
%3 = add nuw nsw i32 %7, 1
%4 = icmp eq i32 %3, 1000
br i1 %4, label %5, label %6
5: ; preds = %2
ret i8* null
6: ; preds = %1, %2
%7 = phi i32 [ 0, %1 ], [ %3, %2 ]
%8 = load i32, i32* @RTLD_LAZY
%9 = tail call i8* @dlopen(i8* %0, i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = tail call i32 @FAIL(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %6
%15 = tail call i8* (...) @dlerror()
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i32* @strstr(i8* %15, i8* dereferenceable(1) %0)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %2
20: ; preds = %17, %14
%21 = tail call i32 @FAIL(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%22 = tail call i32 @exit(i32 0)
unreachable
}