#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 *, char *);
void stub1();
int main(void) {
  void *h = dlopen("firstobj.so", RTLD_LAZY);
  void *f;
  if (!h) {
    printf("cannot find firstobj.so: %s\n", dlerror());
    return 1;
  }
  f = dlsym(h, "foo");
  if (!f) {
    printf("cannot find symbol foo: %s\n", dlerror());
    return 2;
  }
  ((void (*)(void))f)();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1195 <main+0x55>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe88(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <main+0xa0>
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d6 <main+0x96>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    11e0 <main+0xa0>
call   *-0x18(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     1175 <main+0x35>
lea    0xec7(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     1192 <main+0x52>
call   *%rax
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe89(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0x1,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe8d(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0x2,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"firstobj.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [29 x i8] c"cannot find firstobj.so: %s\0A\00"
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [28 x i8] c"cannot find symbol foo: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @RTLD_LAZY
%5 = call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i8* (...) @dlerror()
%10 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %9)
store i32 1, i32* %1
br label %22
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i8* @dlsym(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = call i8* (...) @dlerror()
%18 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %17)
store i32 2, i32* %1
br label %22
19: ; preds = %11
%20 = load i8*, i8** %3
%21 = bitcast i8* %20 to void ()*
store i32 0, i32* %1
br label %22
22: ; preds = %19, %16, %8
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [12 x i8] c"firstobj.so\00"
@RTLD_LAZY = global i32 0
@.str.1 = [29 x i8] c"cannot find firstobj.so: %s\0A\00"
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [28 x i8] c"cannot find symbol foo: %s\0A\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i8* (...) @dlerror()
%6 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %15
7: ; preds = %0
%8 = tail call i8* @dlsym(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i8* (...) @dlerror()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %11)
br label %15
13: ; preds = %7
%14 = bitcast i8* %8 to void ()*
tail call void %14()
br label %15
15: ; preds = %13, %10, %4
%16 = phi i32 [ 0, %13 ], [ 2, %10 ], [ 1, %4 ]
ret i32 %16
}