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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11af <main+0x4f>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe6a(%rip),%rdi
call   1080 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1070 <dlclose@plt>
cmp    $0x0,%rax
je     11de <main+0x7e>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe8b(%rip),%rdi
call   1080 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xeb2(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     119f <main+0x3f>
mov    %rax,%rdi
call   1070 <dlclose@plt>
test   %rax,%rax
jne    11af <main+0x4f>
lea    0xf08(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe60(%rip),%rdi
jmp    11bd <main+0x5d>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xea0(%rip),%rdi
mov    %eax,%esi
call   1080 <FAIL@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [80 x i8] c"dlclose-dylib-order: dlopen(\22libfoo.dylib\22, RTLD_LAZY) failed with dlerror()=%s\00"
@.str.2 = [58 x i8] c"dlclose-dylib-order: dlclose(handleFoo) != 0, dlerrr()=%s\00"
@.str.3 = [20 x i8] c"dlclose-dylib-order\00"
@EXIT_SUCCESS = global i32 0
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 ([80 x i8], [80 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i64 @dlclose(i8* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i32 (...) @dlerror()
%17 = call i32 @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %11
%20 = call i32 @PASS(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @EXIT_SUCCESS
ret i32 %21
}

LLVM IR (-O2):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [80 x i8] c"dlclose-dylib-order: dlopen(\22libfoo.dylib\22, RTLD_LAZY) failed with dlerror()=%s\00"
@.str.2 = [58 x i8] c"dlclose-dylib-order: dlclose(handleFoo) != 0, dlerrr()=%s\00"
@.str.3 = [20 x i8] c"dlclose-dylib-order\00"
@EXIT_SUCCESS = global i32 0
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 %8
4: ; preds = %0
%5 = tail call i32 (...) @dlerror()
%6 = tail call i32 @FAIL(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = tail call i64 @dlclose(i8* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = tail call i32 (...) @dlerror()
%13 = tail call i32 @FAIL(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %8
%16 = tail call i32 @PASS(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%17 = load i32, i32* @EXIT_SUCCESS
ret i32 %17
}