#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ stderr;
double stub1(double);
double stub2(double);
int main(int argc, char **argv) {
  void *handle;
  double (*cosine)(double);
  char *error;
  handle = dlopen("libm.so", RTLD_LAZY);
  if (!handle) {
    fprintf(stderr, "%s\n", dlerror());
    exit(EXIT_FAILURE);
  }
  dlerror();
  cosine = (double (*)(double))dlsym(handle, "cos");
  error = dlerror();
  if (error != NULL) {
    fprintf(stderr, "%s\n", error);
    exit(EXIT_FAILURE);
  }
  printf("%f\n", (*cosine)(2.0));
  dlclose(handle);
  exit(EXIT_SUCCESS);
}


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    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dd <main+0x6d>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe46(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x18(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1235 <main+0xc5>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdee(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movsd  0xdc3(%rip),%xmm0
call   *-0x20(%rbp)
lea    0xdd1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <dlclose@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
jne    11a9 <main+0x39>
mov    0x2e4b(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe6b(%rip),%rsi
mov    %ebx,%edi
jmp    120e <main+0x9e>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe5a(%rip),%rsi
mov    %rbx,%rdi
call   1070 <dlsym@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    11fe <main+0x8e>
movsd  0xe27(%rip),%xmm0
call   *%r14
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1090 <dlclose@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%edi
lea    0xe02(%rip),%rsi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"libm.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [4 x i8] c"cos\00"
@.str.3 = [4 x i8] c"%f\0A\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 double (double)*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @RTLD_LAZY
%10 = call i8* @dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %19, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i8* (...) @dlerror()
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = call i8* (...) @dlerror()
%21 = load i8*, i8** %6
%22 = call i64 @dlsym(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%23 = inttoptr i64 %22 to double (double)*
store double (double)* %23, double (double)** %7
%24 = call i8* (...) @dlerror()
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %33
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %8
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %19
%34 = load double (double)*, double (double)** %7
%35 = call double %34(double 2.000000e+00)
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double %35)
%37 = load i8*, i8** %6
%38 = call i32 @dlclose(i8* %37)
%39 = load i32, i32* @EXIT_SUCCESS
%40 = call i32 @exit(i32 %39)
unreachable
}

LLVM IR (-O2):
@.str = [8 x i8] c"libm.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [4 x i8] c"cos\00"
@.str.3 = [4 x i8] c"%f\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @RTLD_LAZY
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i8* (...) @dlerror()
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = tail call i8* (...) @dlerror()
%14 = tail call i64 @dlsym(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i8* (...) @dlerror()
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = inttoptr i64 %14 to double (double)*
%24 = tail call double %23(double 2.000000e+00)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double %24)
%26 = tail call i32 @dlclose(i8* %4)
%27 = load i32, i32* @EXIT_SUCCESS
%28 = tail call i32 @exit(i32 %27)
unreachable
}