#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 void (*func)();

/* Variables and functions */
int RTLD_GLOBAL;
int RTLD_NOW;
int dlclose(void *);
char *dlerror();
void *dlopen(int /*<<< orphan*/, int);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  typedef void (*func)();
  void *soh = dlopen(0, RTLD_NOW | RTLD_GLOBAL);
  if (!soh) {
    printf("%s\n", dlerror());
    return 1;
  }
  void *sym = (func)dlsym(soh, "foo_function");
  if (0 == sym) {
    printf("%s\n", dlerror());
    return 2;
  }
  int err = dlclose(soh);
  if (err) {
    printf("%s\n", dlerror());
    return err;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
xor    %edi,%edi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a9 <main+0x59>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1229 <main+0xd9>
mov    -0x10(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11eb <main+0x9b>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1229 <main+0xd9>
mov    -0x10(%rbp),%rdi
call   1070 <dlclose@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1222 <main+0xd2>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xdee(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1229 <main+0xd9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edi,%edi
call   1050 <dlopen@plt>
test   %rax,%rax
je     1198 <main+0x48>
mov    %rax,%rbx
lea    0xe8b(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     119f <main+0x4f>
mov    %rbx,%rdi
call   1070 <dlclose@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11a4 <main+0x54>
xor    %ebx,%ebx
jmp    11ba <main+0x6a>
mov    $0x1,%ebx
jmp    11a4 <main+0x54>
mov    $0x2,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe4e(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [13 x i8] c"foo_function\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_NOW
%2 = load i32, i32* @RTLD_GLOBAL
%3 = or i32 %2, %1
%4 = tail call i8* @dlopen(i32 0, i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %12, label %6
6: ; preds = %0
%7 = tail call i64 @dlsym(i8* %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = tail call i32 @dlclose(i8* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %9, %6, %0
%13 = phi i32 [ 1, %0 ], [ 2, %6 ], [ %10, %9 ]
%14 = tail call i8* (...) @dlerror()
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %12, %9
%17 = phi i32 [ 0, %9 ], [ %13, %12 ]
ret i32 %17
}