#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_LAZY;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, ...);
void stub1(int, int);
void stub2(int, int);
int stub3(int, int);
int main() {
  void *descritor;
  void (*PonteiroParaMinhaProcSoma)(int a, int b);
  int (*PonteiroParaMinhaFuncMaiorValor)(int a, int b);
  int x, y, MaiorValor;
  x = 10;
  y = 11;
  if (descritor = dlopen("MinhaLib.so.1", RTLD_LAZY)) {
    PonteiroParaMinhaProcSoma = dlsym(descritor, "MinhaProcSomal");
    PonteiroParaMinhaFuncMaiorValor = dlsym(descritor, "MinhaFuncMaiorValor");
    (*PonteiroParaMinhaProcSoma)(2, 4);
    (*PonteiroParaMinhaProcSoma)(x, y);
    MaiorValor = (*PonteiroParaMinhaFuncMaiorValor)(x, y);
    printf("\nO Maior valor é: %d", MaiorValor);
  } else {
    printf("\nErro ao tentar usar a biblioteca dinâmica\n");
    printf("\n%s\n", dlerror());
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xa,-0x24(%rbp)
movl   $0xb,-0x28(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe93(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11e6 <main+0xa6>
mov    -0x10(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    $0x2,%edi
mov    $0x4,%esi
call   *-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   *%rax
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   *%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1213 <main+0xd3>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     11cd <main+0x7d>
mov    %rax,%rbx
lea    0xe96(%rip),%rsi
mov    %rax,%rdi
call   1070 <dlsym@plt>
mov    %rax,%r14
lea    0xe93(%rip),%rsi
mov    %rbx,%rdi
call   1070 <dlsym@plt>
mov    %rax,%rbx
mov    $0x2,%edi
mov    $0x4,%esi
call   *%r14
mov    $0xa,%edi
mov    $0xb,%esi
call   *%r14
mov    $0xa,%edi
mov    $0xb,%esi
call   *%rbx
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f1 <main+0xa1>
lea    0xe78(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe60(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"MinhaLib.so.1\00"
@RTLD_LAZY = global i32 0
@.str.1 = [15 x i8] c"MinhaProcSomal\00"
@.str.2 = [20 x i8] c"MinhaFuncMaiorValor\00"
@.str.3 = [22 x i8] c"\0AO Maior valor \C3\A9: %d\00"
@.str.4 = [45 x i8] c"\0AErro ao tentar usar a biblioteca din\C3\A2mica\0A\00"
@.str.5 = [5 x i8] c"\0A%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca void (i32, i32)*
%4 = alloca i32 (i32, i32)*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 10, i32* %5
store i32 11, i32* %6
%8 = load i32, i32* @RTLD_LAZY
%9 = call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
store i8* %9, i8** %2
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %28
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i8* @dlsym(i8* %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%14 = bitcast i8* %13 to void (i32, i32)*
store void (i32, i32)* %14, void (i32, i32)** %3
%15 = load i8*, i8** %2
%16 = call i8* @dlsym(i8* %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%17 = bitcast i8* %16 to i32 (i32, i32)*
store i32 (i32, i32)* %17, i32 (i32, i32)** %4
%18 = load void (i32, i32)*, void (i32, i32)** %3
%19 = load void (i32, i32)*, void (i32, i32)** %3
%20 = load i32, i32* %5
%21 = load i32, i32* %6
%22 = load i32 (i32, i32)*, i32 (i32, i32)** %4
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = call i32 %22(i32 %23, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %32
28: ; preds = %0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
%30 = call i8* (...) @dlerror()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %30)
store i32 0, i32* %1
br label %32
32: ; preds = %28, %11
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [14 x i8] c"MinhaLib.so.1\00"
@RTLD_LAZY = global i32 0
@.str.1 = [15 x i8] c"MinhaProcSomal\00"
@.str.2 = [20 x i8] c"MinhaFuncMaiorValor\00"
@.str.3 = [22 x i8] c"\0AO Maior valor \C3\A9: %d\00"
@.str.5 = [5 x i8] c"\0A%s\0A\00"
@str = [44 x i8] c"\0AErro ao tentar usar a biblioteca din\C3\A2mica\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %11, label %4
4: ; preds = %0
%5 = tail call i8* @dlsym(i8* %2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%6 = bitcast i8* %5 to void (i32, i32)*
%7 = tail call i8* @dlsym(i8* %2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%8 = bitcast i8* %7 to i32 (i32, i32)*
tail call void %6(i32 2, i32 4)
tail call void %6(i32 10, i32 11)
%9 = tail call i32 %8(i32 10, i32 11)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %9)
br label %15
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%13 = tail call i8* (...) @dlerror()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %4, %11
ret i32 0
}