#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 EXIT_SUCCESS;
int /*<<< orphan*/ FAIL(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ PASS(char *);
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int main() {
  void *handle = dlopen("libfoo.dylib", RTLD_LAZY);
  if (handle == NULL) {
    FAIL("dlopen(\"%s\") failed with: %s", "libfoo.dylib", dlerror());
    return EXIT_SUCCESS;
  }
  dlclose(handle);
  PASS("cxa_finalize");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b0 <main+0x60>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edx
lea    0xe7a(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1070 <FAIL@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <main+0x81>
mov    -0x10(%rbp),%rdi
call   1060 <dlclose@plt>
lea    0xe6a(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1181 <main+0x31>
mov    %rax,%rdi
call   1060 <dlclose@plt>
lea    0xeb0(%rip),%rdi
call   1050 <PASS@plt>
jmp    119d <main+0x4d>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe7e(%rip),%rdi
lea    0xe6a(%rip),%rsi
mov    %eax,%edx
call   1070 <FAIL@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [29 x i8] c"dlopen(\22%s\22) failed with: %s\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [13 x i8] c"cxa_finalize\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
store i32 0, i32* %1
%3 = load i32, i32* @RTLD_LAZY
%4 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 (...) @dlerror()
%9 = call i32 @FAIL(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @EXIT_SUCCESS
store i32 %10, i32* %1
br label %16
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i32 @dlclose(i8* %12)
%14 = call i32 @PASS(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @EXIT_SUCCESS
store i32 %15, i32* %1
br label %16
16: ; preds = %11, %7
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [29 x i8] c"dlopen(\22%s\22) failed with: %s\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [13 x i8] c"cxa_finalize\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 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 i32 (...) @dlerror()
%6 = tail call i32 @FAIL(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %10
7: ; preds = %0
%8 = tail call i32 @dlclose(i8* %2)
%9 = tail call i32 @PASS(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %10
10: ; preds = %7, %4
%11 = load i32, i32* @EXIT_SUCCESS
ret i32 %11
}