#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*/ PASS(char *);
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *strstr(int /*<<< orphan*/, char *);
int main() {
  void *handle = dlopen("libdoesnotexist.dylib", RTLD_LAZY);
  if (handle != NULL) {
    FAIL("dlopen-error: dlopen(libdoesnotexist.dylib, RTLD_LAZY) should have "
         "failed");
    return 0;
  }
  if (strstr(dlerror(), "image not found") == NULL) {
    FAIL("dlopen-error: expected 'image not found' in dlerror() string");
    return 0;
  }
  handle = dlopen("libnoread.dylib", RTLD_LAZY);
  if (handle != NULL) {
    FAIL("dlopen-error: dlopen(libnoread.dylib, RTLD_LAZY) should have failed");
    return 0;
  }
  if (strstr(dlerror(), "failed with errno=13") == NULL) {
    FAIL("dlopen-error: expected 'failed with errno=13' in dlerror() string");
    return 0;
  }
  PASS("dlopen-error");
  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)
je     119b <main+0x4b>
lea    0xe8c(%rip),%rdi
call   1060 <FAIL@plt>
movl   $0x0,-0x4(%rbp)
jmp    125d <main+0x10d>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
lea    0xeb5(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
jne    11d2 <main+0x82>
lea    0xeaf(%rip),%rdi
call   1060 <FAIL@plt>
movl   $0x0,-0x4(%rbp)
jmp    125d <main+0x10d>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0xecb(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     120e <main+0xbe>
lea    0xec0(%rip),%rdi
call   1060 <FAIL@plt>
movl   $0x0,-0x4(%rbp)
jmp    125d <main+0x10d>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edi
lea    0xee3(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
jne    1245 <main+0xf5>
lea    0xee2(%rip),%rdi
call   1060 <FAIL@plt>
movl   $0x0,-0x4(%rbp)
jmp    125d <main+0x10d>
lea    0xf0c(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2d88(%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   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%r14
mov    (%r14),%esi
lea    0xe9b(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1187 <main+0x37>
lea    0xea0(%rip),%rdi
call   1060 <FAIL@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xec9(%rip),%rsi
mov    %eax,%edi
call   1070 <strstr@plt>
test   %rax,%rax
je     11c0 <main+0x70>
mov    (%r14),%esi
lea    0xf00(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11ce <main+0x7e>
lea    0xeff(%rip),%rdi
jmp    1176 <main+0x26>
lea    0xea9(%rip),%rdi
call   1060 <FAIL@plt>
jmp    117d <main+0x2d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xf23(%rip),%rsi
mov    %eax,%edi
call   1070 <strstr@plt>
test   %rax,%rax
je     1204 <main+0xb4>
lea    0xf67(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%ebx
jmp    117d <main+0x2d>
lea    0xf0b(%rip),%rdi
call   1060 <FAIL@plt>
jmp    117d <main+0x2d>

LLVM IR (-O0):
@.str = [22 x i8] c"libdoesnotexist.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [74 x i8] c"dlopen-error: dlopen(libdoesnotexist.dylib, RTLD_LAZY) should have failed\00"
@.str.2 = [16 x i8] c"image not found\00"
@.str.3 = [61 x i8] c"dlopen-error: expected 'image not found' in dlerror() string\00"
@.str.4 = [16 x i8] c"libnoread.dylib\00"
@.str.5 = [68 x i8] c"dlopen-error: dlopen(libnoread.dylib, RTLD_LAZY) should have failed\00"
@.str.6 = [21 x i8] c"failed with errno=13\00"
@.str.7 = [66 x i8] c"dlopen-error: expected 'failed with errno=13' in dlerror() string\00"
@.str.8 = [13 x i8] c"dlopen-error\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 ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %3)
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @FAIL(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %31
9: ; preds = %0
%10 = call i32 (...) @dlerror()
%11 = call i32* @strstr(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @FAIL(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %31
15: ; preds = %9
%16 = load i32, i32* @RTLD_LAZY
%17 = call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %16)
store i8* %17, i8** %2
%18 = load i8*, i8** %2
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @FAIL(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %31
22: ; preds = %15
%23 = call i32 (...) @dlerror()
%24 = call i32* @strstr(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %31
28: ; preds = %22
%29 = call i32 @PASS(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%30 = load i32, i32* @EXIT_SUCCESS
store i32 %30, i32* %1
br label %31
31: ; preds = %28, %26, %20, %13, %7
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [22 x i8] c"libdoesnotexist.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [74 x i8] c"dlopen-error: dlopen(libdoesnotexist.dylib, RTLD_LAZY) should have failed\00"
@.str.2 = [16 x i8] c"image not found\00"
@.str.3 = [61 x i8] c"dlopen-error: expected 'image not found' in dlerror() string\00"
@.str.4 = [16 x i8] c"libnoread.dylib\00"
@.str.5 = [68 x i8] c"dlopen-error: dlopen(libnoread.dylib, RTLD_LAZY) should have failed\00"
@.str.6 = [21 x i8] c"failed with errno=13\00"
@.str.7 = [66 x i8] c"dlopen-error: expected 'failed with errno=13' in dlerror() string\00"
@.str.8 = [13 x i8] c"dlopen-error\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = tail call i32 @FAIL(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0))
br label %27
6: ; preds = %0
%7 = tail call i32 (...) @dlerror()
%8 = tail call i32* @strstr(i32 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @FAIL(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0))
br label %27
12: ; preds = %6
%13 = load i32, i32* @RTLD_LAZY
%14 = tail call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @FAIL(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.5, i64 0, i64 0))
br label %27
18: ; preds = %12
%19 = tail call i32 (...) @dlerror()
%20 = tail call i32* @strstr(i32 %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @FAIL(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.7, i64 0, i64 0))
br label %27
24: ; preds = %18
%25 = tail call i32 @PASS(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%26 = load i32, i32* @EXIT_SUCCESS
br label %27
27: ; preds = %24, %22, %16, %10, %4
%28 = phi i32 [ 0, %4 ], [ 0, %10 ], [ 0, %16 ], [ 0, %22 ], [ %26, %24 ]
ret i32 %28
}