#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;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int stub1(int);
int main(int argc, char **argv) {
  char *error;
  int (*extfunc)(int);
  if (!argc)
    return 2;
  void *handle = dlopen(argv[1], RTLD_LAZY);
  if (!handle) {
    fprintf(stderr, "%s\n", dlerror());
    return 1;
  }
  dlerror();
  *(void **)(&extfunc) = dlsym(handle, "plugin");
  if ((error = dlerror()) != NULL) {
    fprintf(stderr, "%s\n", error);
    return 1;
  }
  (*extfunc)(3);
  dlclose(handle);
  return 0;
}


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)
cmpl   $0x0,-0x8(%rbp)
jne    117c <main+0x2c>
movl   $0x2,-0x4(%rbp)
jmp    123f <main+0xef>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d2 <main+0x82>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe3f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <main+0xef>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x28(%rbp),%rdi
lea    0xe20(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1227 <main+0xd7>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdea(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123f <main+0xef>
mov    $0x3,%edi
call   *-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
test   %edi,%edi
je     11ac <main+0x5c>
mov    0x8(%rsi),%rdi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11b3 <main+0x63>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe81(%rip),%rsi
mov    %r14,%rdi
call   1060 <dlsym@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     11db <main+0x8b>
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%edi
lea    0xe56(%rip),%rsi
jmp    11cc <main+0x7c>
mov    $0x2,%ebx
jmp    11eb <main+0x9b>
mov    0x2e26(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe36(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%ebx
jmp    11eb <main+0x9b>
mov    $0x3,%edi
call   *%r15
mov    %r14,%rdi
call   1070 <dlclose@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [7 x i8] c"plugin\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32 (i32)*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 2, i32* %3
br label %40
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @RTLD_LAZY
%17 = call i8* @dlopen(i8* %15, i32 %16)
store i8* %17, i8** %8
%18 = load i8*, i8** %8
%19 = icmp ne i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %12
%21 = load i32, i32* @stderr
%22 = call i8* (...) @dlerror()
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 1, i32* %3
br label %40
24: ; preds = %12
%25 = call i8* (...) @dlerror()
%26 = load i8*, i8** %8
%27 = call i8* @dlsym(i8* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%28 = bitcast i32 (i32)** %7 to i8**
store i8* %27, i8** %28
%29 = call i8* (...) @dlerror()
store i8* %29, i8** %6
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %6
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %33)
store i32 1, i32* %3
br label %40
35: ; preds = %24
%36 = load i32 (i32)*, i32 (i32)** %7
%37 = call i32 %36(i32 3)
%38 = load i8*, i8** %8
%39 = call i32 @dlclose(i8* %38)
store i32 0, i32* %3
br label %40
40: ; preds = %35, %31, %20, %11
%41 = load i32, i32* %3
ret i32 %41
}

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