#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_LOCAL;
int RTLD_NOW;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  void *handle;
  void (*hello_world)(const char *name);
  char *error;
  handle = dlopen("./output-of-test-compile-to-dynamic-library.c.so",
                  RTLD_NOW | RTLD_LOCAL);
  if (!handle) {
    fprintf(stderr, "dlopen failed: %s\n", dlerror());
    exit(1);
  }
  dlerror();
  *(void **)(&hello_world) = dlsym(handle, "hello_world");
  if ((error = dlerror()) != NULL) {
    fprintf(stderr, "dlsym failed: %s\n", error);
    exit(2);
  }
  hello_world(argv[0]);
  dlclose(handle);
  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    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
lea    0xe71(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <main+0x72>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe6e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x18(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1225 <main+0xc5>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe3a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rdi
call   *%rax
mov    -0x18(%rbp),%rdi
call   1080 <dlclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2e79(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11c9 <main+0x69>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xea8(%rip),%rsi
mov    %rbx,%rdi
call   1060 <dlsym@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    11f4 <main+0x94>
mov    (%r14),%rdi
call   *%r15
mov    %rbx,%rdi
call   1080 <dlclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e08(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe51(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%edi
lea    0xe4c(%rip),%rsi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"./output-of-test-compile-to-dynamic-library.c.so\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"dlopen failed: %s\0A\00"
@.str.2 = [12 x i8] c"hello_world\00"
@.str.3 = [18 x i8] c"dlsym failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca void (i8*)*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @RTLD_NOW
%10 = load i32, i32* @RTLD_LOCAL
%11 = or i32 %9, %10
%12 = call i8* @dlopen(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %20, label %15
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i8* (...) @dlerror()
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i8* (...) @dlerror()
%22 = load i8*, i8** %6
%23 = call i8* @dlsym(i8* %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = bitcast void (i8*)** %7 to i8**
store i8* %23, i8** %24
%25 = call i8* (...) @dlerror()
store i8* %25, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %8
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 2)
unreachable
32: ; preds = %20
%33 = load void (i8*)*, void (i8*)** %7
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = load i8*, i8** %6
%38 = call i32 @dlclose(i8* %37)
ret i32 0
}

LLVM IR (-O2):
@.str = [49 x i8] c"./output-of-test-compile-to-dynamic-library.c.so\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"dlopen failed: %s\0A\00"
@.str.2 = [12 x i8] c"hello_world\00"
@.str.3 = [18 x i8] c"dlsym failed: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @RTLD_NOW
%4 = load i32, i32* @RTLD_LOCAL
%5 = or i32 %4, %3
%6 = tail call i8* @dlopen(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i8* (...) @dlerror()
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i8* (...) @dlerror()
%15 = tail call i8* @dlsym(i8* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i8* (...) @dlerror()
%17 = icmp eq i8* %16, null
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %13
%23 = bitcast i8* %15 to void (i8*)*
%24 = load i8*, i8** %1
tail call void %23(i8* %24)
%25 = tail call i32 @dlclose(i8* %6)
ret i32 0
}