#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*/ RTLD_NOW;
int /*<<< orphan*/ dlclose(void *);
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
int main() {
  void *handle = dlopen("./libmax.so", RTLD_NOW);
  if (handle == NULL) {
    puts(dlerror());
    return 1;
  }
  double *pv = dlsym(handle, "v");
  int (*pf)(int[], int) = dlsym(handle, "max");
  printf("%lg\n", *pv);
  int x[] = {3, 9, 2, 6, 8, 5};
  printf("%d\n", pf(x, 6));
  dlclose(handle);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ad <main+0x4d>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1230 <main+0xd0>
mov    -0x10(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cvttsd2si (%rax),%esi
lea    0xe2e(%rip),%rdi
call   1050 <printf@plt>
mov    0xe30(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe2d(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe2a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
lea    -0x40(%rbp),%rdi
mov    $0x6,%esi
call   *%rax
mov    %eax,%esi
lea    0xdf5(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     11f7 <main+0x97>
mov    %rax,%rbx
lea    0xe7f(%rip),%rsi
mov    %rax,%rdi
call   1070 <dlsym@plt>
mov    %rax,%r14
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdi
call   1070 <dlsym@plt>
mov    %rax,%r15
cvttsd2si (%r14),%esi
lea    0xe5c(%rip),%rdi
call   1050 <printf@plt>
mov    0xe6e(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0xe52(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
mov    $0x6,%esi
call   *%r15
lea    0xe33(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
xor    %eax,%eax
jmp    120a <main+0xaa>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %eax,%edi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"./libmax.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [2 x i8] c"v\00"
@.str.2 = [4 x i8] c"max\00"
@.str.3 = [5 x i8] c"%lg\0A\00"
@__const.main.x = [6 x i32] [i32 3, i32 9, i32 2, i32 6, i32 8, i32 5]
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca double*
%4 = alloca i32 (i32*, i32)*
%5 = alloca [6 x i32]
store i32 0, i32* %1
%6 = load i32, i32* @RTLD_NOW
%7 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (...) @dlerror()
%12 = call i32 @puts(i32 %11)
store i32 1, i32* %1
br label %31
13: ; preds = %0
%14 = load i8*, i8** %2
%15 = call i8* @dlsym(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = bitcast i8* %15 to double*
store double* %16, double** %3
%17 = load i8*, i8** %2
%18 = call i8* @dlsym(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%19 = bitcast i8* %18 to i32 (i32*, i32)*
store i32 (i32*, i32)* %19, i32 (i32*, i32)** %4
%20 = load double*, double** %3
%21 = load double, double* %20
%22 = fptosi double %21 to i32
%23 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = bitcast [6 x i32]* %5 to i8*
%25 = load i32 (i32*, i32)*, i32 (i32*, i32)** %4
%26 = getelementptr inbounds [6 x i32], [6 x i32]* %5, i64 0, i64 0
%27 = call i32 %25(i32* %26, i32 6)
%28 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = load i8*, i8** %2
%30 = call i32 @dlclose(i8* %29)
br label %31
31: ; preds = %13, %10
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [12 x i8] c"./libmax.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [2 x i8] c"v\00"
@.str.2 = [4 x i8] c"max\00"
@.str.3 = [5 x i8] c"%lg\0A\00"
@__const.main.x = [6 x i32] [i32 3, i32 9, i32 2, i32 6, i32 8, i32 5]
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [6 x i32]
%2 = load i32, i32* @RTLD_NOW
%3 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 (...) @dlerror()
%7 = tail call i32 @puts(i32 %6)
br label %21
8: ; preds = %0
%9 = tail call i8* @dlsym(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = bitcast i8* %9 to double*
%11 = tail call i8* @dlsym(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%12 = bitcast i8* %11 to i32 (i32*, i32)*
%13 = load double, double* %10
%14 = fptosi double %13 to i32
%15 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = bitcast [6 x i32]* %1 to i8*
%17 = getelementptr inbounds [6 x i32], [6 x i32]* %1, i64 0, i64 0
%18 = call i32 %12(i32* %17, i32 6)
%19 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %18)
%20 = call i32 @dlclose(i8* %3)
br label %21
21: ; preds = %8, %5
%22 = phi i32 [ 1, %5 ], [ 0, %8 ]
ret i32 %22
}