#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*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  printf("[BEGIN] dlopen-rpath-implicit\n");
  void *handle = dlopen("libimplicitrpath.dylib", RTLD_LAZY);
  if (handle == NULL) {
    printf("[FAIL] dlopen-rpath-implicit: dlopen(libimplicitrpath.dylib) "
           "failed: %s\n",
           dlerror());
    return 0;
  }
  dlclose(handle);
  printf("[PASS] dlopen-rpath-implicit\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xeb2(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a5 <main+0x65>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <main+0x83>
mov    -0x10(%rbp),%rdi
call   1060 <dlclose@plt>
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xf08(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe93(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     118d <main+0x3d>
mov    %rax,%rdi
call   1070 <dlclose@plt>
lea    0xef8(%rip),%rdi
call   1040 <puts@plt>
jmp    11a5 <main+0x55>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe7c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
@.str.1 = [23 x i8] c"libimplicitrpath.dylib\00"
@RTLD_LAZY = global i32 0
@.str.2 = [73 x i8] c"[FAIL] dlopen-rpath-implicit: dlopen(libimplicitrpath.dylib) failed: %s\0A\00"
@str = [30 x i8] c"[BEGIN] dlopen-rpath-implicit\00"
@str.4 = [29 x i8] c"[PASS] dlopen-rpath-implicit\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @RTLD_LAZY
%3 = tail call i8* @dlopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i8* (...) @dlerror()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %11
8: ; preds = %0
%9 = tail call i32 @dlclose(i8* %3)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.4, i64 0, i64 0))
br label %11
11: ; preds = %8, %5
ret i32 0
}