#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 */
typedef int (*signature)();

/* Variables and functions */
void *load_shared_object(char *);
int /*<<< orphan*/ printf(char *, int);
void *resolve_symbol(void *, char *);
int stub1();
int stub2();
int /*<<< orphan*/ unload_shared_object(void *);
int main(int argc, char **argv) {
  typedef int (*signature)();
  void *so_a = NULL;
  void *so_b = NULL;
  void *fct_a = NULL;
  void *fct_b = NULL;
  int retval = 0;
  so_a = load_shared_object("./libannelid_test_a.so");
  so_b = load_shared_object("./libannelid_test_b.so");
  fct_a = resolve_symbol(so_a, "annelid_test");
  fct_b = resolve_symbol(so_b, "annelid_test");
  if (NULL != fct_a) {
    retval = ((signature)fct_a)();
    printf("got: %d\n", retval);
  }
  if (NULL != fct_b) {
    retval = ((signature)fct_b)();
    printf("got: %d\n", retval);
  }
  unload_shared_object(so_a);
  unload_shared_object(so_b);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
lea    0xe7c(%rip),%rdi
call   1040 <load_shared_object@plt>
mov    %rax,-0x18(%rbp)
lea    0xe83(%rip),%rdi
call   1040 <load_shared_object@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe86(%rip),%rsi
call   1050 <resolve_symbol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1050 <resolve_symbol@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
je     11e8 <main+0xa8>
mov    $0x0,%al
call   *-0x28(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
je     120b <main+0xcb>
mov    $0x0,%al
call   *-0x30(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xe35(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <unload_shared_object@plt>
mov    -0x20(%rbp),%rdi
call   1060 <unload_shared_object@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xeb1(%rip),%rdi
call   1040 <load_shared_object@plt>
mov    %rax,%r14
lea    0xeb9(%rip),%rdi
call   1040 <load_shared_object@plt>
mov    %rax,%r15
lea    0xec1(%rip),%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <resolve_symbol@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <resolve_symbol@plt>
mov    %rax,%r12
test   %rbx,%rbx
je     11a0 <main+0x60>
xor    %eax,%eax
call   *%rbx
lea    0xea2(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
test   %r12,%r12
je     11b8 <main+0x78>
xor    %eax,%eax
call   *%r12
lea    0xe8a(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %r14,%rdi
call   1060 <unload_shared_object@plt>
mov    %r15,%rdi
call   1060 <unload_shared_object@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [23 x i8] c"./libannelid_test_a.so\00"
@.str.1 = [23 x i8] c"./libannelid_test_b.so\00"
@.str.2 = [13 x i8] c"annelid_test\00"
@.str.3 = [9 x i8] c"got: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
store i8* null, i8** %9
store i32 0, i32* %10
%11 = call i8* @load_shared_object(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %6
%12 = call i8* @load_shared_object(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i8* %12, i8** %7
%13 = load i8*, i8** %6
%14 = call i8* @resolve_symbol(i8* %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i8* %14, i8** %8
%15 = load i8*, i8** %7
%16 = call i8* @resolve_symbol(i8* %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i8* %16, i8** %9
%17 = load i8*, i8** %8
%18 = icmp ne i8* null, %17
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i8*, i8** %8
%21 = bitcast i8* %20 to i32 (...)*
%22 = call i32 (...) %21()
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %19, %2
%26 = load i8*, i8** %9
%27 = icmp ne i8* null, %26
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i8*, i8** %9
%30 = bitcast i8* %29 to i32 (...)*
%31 = call i32 (...) %30()
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %28, %25
%35 = load i8*, i8** %6
%36 = call i32 @unload_shared_object(i8* %35)
%37 = load i8*, i8** %7
%38 = call i32 @unload_shared_object(i8* %37)
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"./libannelid_test_a.so\00"
@.str.1 = [23 x i8] c"./libannelid_test_b.so\00"
@.str.2 = [13 x i8] c"annelid_test\00"
@.str.3 = [9 x i8] c"got: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* @load_shared_object(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @load_shared_object(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i8* @resolve_symbol(i8* %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i8* @resolve_symbol(i8* %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i8* %5, null
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = bitcast i8* %5 to i32 (...)*
%10 = tail call i32 (...) %9()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %8, %2
%13 = icmp eq i8* %6, null
br i1 %13, label %18, label %14
14: ; preds = %12
%15 = bitcast i8* %6 to i32 (...)*
%16 = tail call i32 (...) %15()
%17 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %14, %12
%19 = tail call i32 @unload_shared_object(i8* %3)
%20 = tail call i32 @unload_shared_object(i8* %4)
ret i32 0
}