#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 RTLD_GLOBAL;
int RTLD_LAZY;
char *dlerror();
void *dlopen(char *, int);
int /*<<< orphan*/ dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  void *handle = dlopen(argv[1], RTLD_LAZY | RTLD_GLOBAL);
  if (!handle) {
    fprintf(stderr, "ERROR: failed to open the shared library: %s\n",
            dlerror());
    return 2;
  }
  uintptr_t main_fn = (uintptr_t)dlsym(handle, "main.main");
  if (!main_fn) {
    fprintf(stderr, "ERROR: missing main.main: %s\n", dlerror());
    return 2;
  }
  printf("PASS\n");
  return 0;
}


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    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c5 <main+0x75>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe4c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    122a <main+0xda>
mov    -0x18(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1070 <dlsym@plt>
cltq
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1217 <main+0xc7>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe32(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    122a <main+0xda>
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x8(%rsi),%rdi
mov    0x2e74(%rip),%rax
mov    0x2e85(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <dlopen@plt>
test   %rax,%rax
je     1194 <main+0x44>
lea    0xeb6(%rip),%rsi
mov    %rax,%rdi
call   1070 <dlsym@plt>
test   %eax,%eax
je     11ad <main+0x5d>
lea    0xecb(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e3d(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe55(%rip),%rsi
jmp    11c4 <main+0x74>
mov    0x2e24(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe74(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x2,%eax
pop    %rbx
ret

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@RTLD_GLOBAL = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"ERROR: failed to open the shared library: %s\0A\00"
@.str.1 = [10 x i8] c"main.main\00"
@.str.2 = [30 x i8] c"ERROR: missing main.main: %s\0A\00"
@.str.3 = [6 x i8] c"PASS\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @RTLD_LAZY
%12 = load i32, i32* @RTLD_GLOBAL
%13 = or i32 %11, %12
%14 = call i8* @dlopen(i8* %10, i32 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i8* (...) @dlerror()
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 2, i32* %3
br label %33
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = call i32 @dlsym(i8* %22, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%24 = sext i32 %23 to i64
store i64 %24, i64* %7
%25 = load i64, i64* %7
%26 = icmp ne i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = call i8* (...) @dlerror()
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %29)
store i32 2, i32* %3
br label %33
31: ; preds = %21
%32 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %33
33: ; preds = %31, %27, %17
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
@RTLD_GLOBAL = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"ERROR: failed to open the shared library: %s\0A\00"
@.str.1 = [10 x i8] c"main.main\00"
@.str.2 = [30 x i8] c"ERROR: missing main.main: %s\0A\00"
@str = [5 x i8] c"PASS\00"
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 = load i32, i32* @RTLD_GLOBAL
%7 = or i32 %6, %5
%8 = tail call i8* @dlopen(i8* %4, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i8* (...) @dlerror()
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %23
14: ; preds = %2
%15 = tail call i32 @dlsym(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i8* (...) @dlerror()
%20 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %23
21: ; preds = %14
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %17, %21, %10
%24 = phi i32 [ 2, %10 ], [ 0, %21 ], [ 2, %17 ]
ret i32 %24
}