#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;
void *dlopen(char const *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  const char *path = argv[1];
  const char *otherPath = "foo3.dylib";
  void *handle = dlopen(path, RTLD_LAZY);
  if (handle == NULL) {
    FAIL("dlsym-indirect dlopen(\"%s\") failed", path);
    exit(0);
  }
  void *handle3 = dlopen(otherPath, RTLD_LAZY);
  if (handle3 == NULL) {
    FAIL("dlsym-indirect dlopen(\"%s\") failed", otherPath);
    exit(0);
  }
  void *foo = dlsym(handle, "foo");
  if (foo == NULL) {
    FAIL("dlsym-indirect dlsym(handle, \"foo\") failed");
    exit(0);
  }
  void *foo1 = dlsym(handle, "foo1");
  if (foo1 == NULL) {
    FAIL("dlsym-indirect dlsym(handle, \"foo1\") failed");
  }
  void *foo2 = dlsym(handle, "foo2");
  if (foo2 == NULL) {
    FAIL("dlsym-indirect dlsym(handle, \"foo2\") failed");
  }
  void *foo3 = dlsym(handle, "foo3");
  if (foo3 != NULL) {
    FAIL("dlsym-indirect dlsym(handle, \"foo3\") should have failed");
  }
  PASS("dlsym-indirect %s", path);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1030 <dlopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b7 <main+0x67>
mov    -0x18(%rbp),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1030 <dlopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f1 <main+0xa1>
mov    -0x20(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1225 <main+0xd5>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1252 <main+0x102>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
mov    -0x28(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    127f <main+0x12f>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
mov    -0x28(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12ac <main+0x15c>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1070 <FAIL@plt>
mov    -0x18(%rbp),%rsi
lea    0xe45(%rip),%rdi
call   1040 <PASS@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x8(%rsi),%r14
mov    0x2e78(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     122b <main+0xdb>
mov    %rax,%rbx
mov    (%r15),%esi
lea    0xe7f(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     1237 <main+0xe7>
lea    0xe98(%rip),%rsi
mov    %rbx,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     1253 <main+0x103>
lea    0xeaf(%rip),%rsi
mov    %rbx,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
jne    11c9 <main+0x79>
lea    0xea0(%rip),%rdi
xor    %eax,%eax
call   1070 <FAIL@plt>
lea    0xebe(%rip),%rsi
mov    %rbx,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
jne    11eb <main+0x9b>
lea    0xeaf(%rip),%rdi
xor    %eax,%eax
call   1070 <FAIL@plt>
lea    0xecd(%rip),%rsi
mov    %rbx,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     120d <main+0xbd>
lea    0xebe(%rip),%rdi
xor    %eax,%eax
call   1070 <FAIL@plt>
lea    0xee8(%rip),%rdi
mov    %r14,%rsi
call   1040 <PASS@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdd9(%rip),%rdi
mov    %r14,%rsi
jmp    1245 <main+0xf5>
lea    0xdcd(%rip),%rdi
lea    0xdbb(%rip),%rsi
xor    %eax,%eax
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1070 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"foo3.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [35 x i8] c"dlsym-indirect dlopen(\22%s\22) failed\00"
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [43 x i8] c"dlsym-indirect dlsym(handle, \22foo\22) failed\00"
@.str.4 = [5 x i8] c"foo1\00"
@.str.5 = [44 x i8] c"dlsym-indirect dlsym(handle, \22foo1\22) failed\00"
@.str.6 = [5 x i8] c"foo2\00"
@.str.7 = [44 x i8] c"dlsym-indirect dlsym(handle, \22foo2\22) failed\00"
@.str.8 = [5 x i8] c"foo3\00"
@.str.9 = [56 x i8] c"dlsym-indirect dlsym(handle, \22foo3\22) should have failed\00"
@.str.10 = [18 x i8] c"dlsym-indirect %s\00"
@EXIT_SUCCESS = global i32 0
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*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %7
%17 = load i8*, i8** %6
%18 = load i32, i32* @RTLD_LAZY
%19 = call i8* @dlopen(i8* %17, i32 %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %2
%23 = load i8*, i8** %6
%24 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %2
%27 = load i8*, i8** %7
%28 = load i32, i32* @RTLD_LAZY
%29 = call i8* @dlopen(i8* %27, i32 %28)
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = load i8*, i8** %7
%34 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %26
%37 = load i8*, i8** %8
%38 = call i8* @dlsym(i8* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %36
%45 = load i8*, i8** %8
%46 = call i8* @dlsym(i8* %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i8* %46, i8** %11
%47 = load i8*, i8** %11
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %44
%52 = load i8*, i8** %8
%53 = call i8* @dlsym(i8* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
store i8* %53, i8** %12
%54 = load i8*, i8** %12
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0))
br label %58
58: ; preds = %56, %51
%59 = load i8*, i8** %8
%60 = call i8* @dlsym(i8* %59, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
store i8* %60, i8** %13
%61 = load i8*, i8** %13
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %65
63: ; preds = %58
%64 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.9, i64 0, i64 0))
br label %65
65: ; preds = %63, %58
%66 = load i8*, i8** %6
%67 = call i32 @PASS(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8* %66)
%68 = load i32, i32* @EXIT_SUCCESS
ret i32 %68
}

LLVM IR (-O2):
@.str = [11 x i8] c"foo3.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [35 x i8] c"dlsym-indirect dlopen(\22%s\22) failed\00"
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [43 x i8] c"dlsym-indirect dlsym(handle, \22foo\22) failed\00"
@.str.4 = [5 x i8] c"foo1\00"
@.str.5 = [44 x i8] c"dlsym-indirect dlsym(handle, \22foo1\22) failed\00"
@.str.6 = [5 x i8] c"foo2\00"
@.str.7 = [44 x i8] c"dlsym-indirect dlsym(handle, \22foo2\22) failed\00"
@.str.8 = [5 x i8] c"foo3\00"
@.str.9 = [56 x i8] c"dlsym-indirect dlsym(handle, \22foo3\22) should have failed\00"
@.str.10 = [18 x i8] c"dlsym-indirect %s\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i32, i32* @RTLD_LAZY
%6 = tail call i8* @dlopen(i8* %4, i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %2
%12 = load i32, i32* @RTLD_LAZY
%13 = tail call i8* @dlopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %11
%19 = tail call i8* @dlsym(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %18
%25 = tail call i8* @dlsym(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = tail call i8* @dlsym(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = tail call i8* @dlsym(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%36 = icmp eq i8* %35, null
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.9, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = tail call i32 @PASS(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8* %4)
%41 = load i32, i32* @EXIT_SUCCESS
ret i32 %41
}