#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_LAZY;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int main() {
  void *handle1 = dlopen("frobulite", RTLD_LAZY);
  if (handle1 != NULL) {
    FAIL("dlerror-clear: dlopen(\"frobulite\", RTLD_LAZY) succeeded but should "
         "have failed");
    exit(0);
  }
  const char *msg = dlerror();
  if (msg == NULL) {
    FAIL("dlerror-clear: dlerror() returned NULL but should have returned an "
         "error message");
    exit(0);
  }
  const char *msg2 = dlerror();
  if (msg2 != NULL) {
    FAIL("dlerror-clear: dlerror() returned message but should have returned "
         "NULL");
    exit(0);
  }
  PASS("dlerror-clear");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1196 <main+0x46>
lea    0xe80(%rip),%rdi
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bf <main+0x6f>
lea    0xea6(%rip),%rdi
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e8 <main+0x98>
lea    0xece(%rip),%rdi
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xf03(%rip),%rdi
call   1050 <PASS@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
jne    1193 <main+0x43>
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     119c <main+0x4c>
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    11a5 <main+0x55>
lea    0xf68(%rip),%rdi
call   1050 <PASS@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe70(%rip),%rdi
jmp    11ac <main+0x5c>
lea    0xeb6(%rip),%rdi
jmp    11ac <main+0x5c>
lea    0xefe(%rip),%rdi
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"frobulite\00"
@RTLD_LAZY = global i32 0
@.str.1 = [79 x i8] c"dlerror-clear: dlopen(\22frobulite\22, RTLD_LAZY) succeeded but should have failed\00"
@.str.2 = [81 x i8] c"dlerror-clear: dlerror() returned NULL but should have returned an error message\00"
@.str.3 = [72 x i8] c"dlerror-clear: dlerror() returned message but should have returned NULL\00"
@.str.4 = [14 x i8] c"dlerror-clear\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @RTLD_LAZY
%6 = call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @FAIL(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 0)
unreachable
12: ; preds = %0
%13 = call i8* (...) @dlerror()
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @FAIL(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %12
%20 = call i8* (...) @dlerror()
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @FAIL(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %19
%27 = call i32 @PASS(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"frobulite\00"
@RTLD_LAZY = global i32 0
@.str.1 = [79 x i8] c"dlerror-clear: dlopen(\22frobulite\22, RTLD_LAZY) succeeded but should have failed\00"
@.str.2 = [81 x i8] c"dlerror-clear: dlerror() returned NULL but should have returned an error message\00"
@.str.3 = [72 x i8] c"dlerror-clear: dlerror() returned message but should have returned NULL\00"
@.str.4 = [14 x i8] c"dlerror-clear\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = tail call i32 @FAIL(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = tail call i8* (...) @dlerror()
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 @FAIL(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %7
%14 = tail call i8* (...) @dlerror()
%15 = icmp eq i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @FAIL(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.3, i64 0, i64 0))
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %13
%20 = tail call i32 @PASS(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}