#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;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  printf("[BEGIN] dlopen-framework-fallback\n");
  void *handle =
      dlopen("/System/Library/BadPath/CoreFoundation.framework/CoreFoundation",
             RTLD_LAZY);
  if (handle == NULL) {
    printf("dlerror(): %s\n", dlerror());
    printf("[FAIL] dlopen-framework-fallback\n");
    return 0;
  }
  void *sym = dlsym(handle, "CFRetain");
  if (sym == NULL) {
    printf("dlerror(): %s\n", dlerror());
    printf("[FAIL] dlopen-framework-fallback\n");
    return 0;
  }
  printf("[PASS] dlopen-framework-fallback\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b3 <main+0x73>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xed1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1219 <main+0xd9>
mov    -0x10(%rbp),%rdi
lea    0xed6(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1204 <main+0xc4>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1219 <main+0xd9>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xf00(%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     1194 <main+0x44>
lea    0xed1(%rip),%rsi
mov    %rax,%rdi
call   1070 <dlsym@plt>
test   %rax,%rax
je     1194 <main+0x44>
lea    0xee8(%rip),%rdi
jmp    11b3 <main+0x63>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe9e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xee8(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [35 x i8] c"[BEGIN] dlopen-framework-fallback\0A\00"
@.str.1 = [64 x i8] c"/System/Library/BadPath/CoreFoundation.framework/CoreFoundation\00"
@RTLD_LAZY = global i32 0
@.str.2 = [15 x i8] c"dlerror(): %s\0A\00"
@.str.3 = [34 x i8] c"[FAIL] dlopen-framework-fallback\0A\00"
@.str.4 = [9 x i8] c"CFRetain\00"
@.str.5 = [34 x i8] c"[PASS] dlopen-framework-fallback\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @RTLD_LAZY
%6 = call i8* @dlopen(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i32 %5)
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i8* (...) @dlerror()
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %24
13: ; preds = %0
%14 = load i8*, i8** %2
%15 = call i8* @dlsym(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = call i8* (...) @dlerror()
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %24
22: ; preds = %13
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %24
24: ; preds = %22, %18, %9
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str.1 = [64 x i8] c"/System/Library/BadPath/CoreFoundation.framework/CoreFoundation\00"
@RTLD_LAZY = global i32 0
@.str.2 = [15 x i8] c"dlerror(): %s\0A\00"
@.str.4 = [9 x i8] c"CFRetain\00"
@str = [34 x i8] c"[BEGIN] dlopen-framework-fallback\00"
@str.6 = [33 x i8] c"[PASS] dlopen-framework-fallback\00"
@str.8 = [33 x i8] c"[FAIL] dlopen-framework-fallback\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @RTLD_LAZY
%3 = tail call i8* @dlopen(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = tail call i8* @dlsym(i8* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %5, %0
%9 = phi i8* [ getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0), %0 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0), %5 ]
%10 = tail call i8* (...) @dlerror()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %8, %5
%13 = phi i8* [ getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0), %5 ], [ %9, %8 ]
%14 = tail call i32 @puts(i8* dereferenceable(1) %13)
ret i32 0
}