#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 *);
int /*<<< orphan*/ dlerror();
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *strstr(int /*<<< orphan*/, char *);
int main() {
  void *handle = (void *)0x12345;
  void *sym = dlsym(handle, "foo");
  if (sym != NULL) {
    FAIL("dlsym(handle, \"foo\") should not have succeeded");
    exit(1);
  }
  if (strstr(dlerror(), "handle") == NULL) {
    FAIL("dlerror() after dlsym(handle, \"foo\") does not mention \"handle\"");
    exit(1);
  }
  PASS("dlsym-error");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x12345,%eax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ad <main+0x4d>
lea    0xe66(%rip),%rdi
call   1070 <FAIL@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
lea    0xe76(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
jne    11e2 <main+0x82>
lea    0xe67(%rip),%rdi
call   1070 <FAIL@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe90(%rip),%rdi
call   1040 <PASS@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xe98(%rip),%rsi
mov    $0x12345,%edi
call   1050 <dlsym@plt>
test   %rax,%rax
jne    11a8 <main+0x48>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xeae(%rip),%rsi
mov    %eax,%edi
call   1080 <strstr@plt>
test   %rax,%rax
je     11b1 <main+0x51>
lea    0xee1(%rip),%rdi
call   1040 <PASS@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
lea    0xe55(%rip),%rdi
jmp    11b8 <main+0x58>
lea    0xe82(%rip),%rdi
call   1070 <FAIL@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@.str.1 = [47 x i8] c"dlsym(handle, \22foo\22) should not have succeeded\00"
@.str.2 = [7 x i8] c"handle\00"
@.str.3 = [63 x i8] c"dlerror() after dlsym(handle, \22foo\22) does not mention \22handle\22\00"
@.str.4 = [12 x i8] c"dlsym-error\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
store i8* inttoptr (i64 74565 to i8*), i8** %2
%4 = load i8*, i8** %2
%5 = call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @FAIL(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 (...) @dlerror()
%13 = call i32* @strstr(i32 %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 @FAIL(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = call i32 @PASS(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%20 = load i32, i32* @EXIT_SUCCESS
ret i32 %20
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@.str.1 = [47 x i8] c"dlsym(handle, \22foo\22) should not have succeeded\00"
@.str.2 = [7 x i8] c"handle\00"
@.str.3 = [63 x i8] c"dlerror() after dlsym(handle, \22foo\22) does not mention \22handle\22\00"
@.str.4 = [12 x i8] c"dlsym-error\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i8* @dlsym(i8* inttoptr (i64 74565 to i8*), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call i32 @FAIL(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 (...) @dlerror()
%8 = tail call i32* @strstr(i32 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = tail call i32 @FAIL(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %6
%14 = tail call i32 @PASS(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%15 = load i32, i32* @EXIT_SUCCESS
ret i32 %15
}