#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;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *strstr(char const *, char *);
int main() {
  void *handle = dlopen("libfoo.dylib", RTLD_LAZY);
  if (handle != NULL) {
    FAIL("dlopen-codesign: load of libfoo.dylib should have failed");
    return EXIT_SUCCESS;
  }
  const char *msg = dlerror();
  if (strstr(msg, "signature") == NULL) {
    FAIL("dlopen-codesign: load of libfoo.dylib failed, but message was wrong: "
         "%s",
         msg);
    return EXIT_SUCCESS;
  }
  PASS("dlopen-codesign");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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     11a2 <main+0x52>
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1060 <FAIL@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1202 <main+0xb2>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
jne    11ea <main+0x9a>
mov    -0x18(%rbp),%rsi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1060 <FAIL@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1202 <main+0xb2>
lea    0xea7(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     117b <main+0x2b>
lea    0xe9b(%rip),%rdi
xor    %eax,%eax
call   1060 <FAIL@plt>
jmp    11b8 <main+0x68>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rbx
lea    0xeba(%rip),%rsi
mov    %rax,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     11a7 <main+0x57>
lea    0xef8(%rip),%rdi
call   1050 <PASS@plt>
jmp    11b8 <main+0x68>
lea    0xea2(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <FAIL@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [57 x i8] c"dlopen-codesign: load of libfoo.dylib should have failed\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [10 x i8] c"signature\00"
@.str.3 = [72 x i8] c"dlopen-codesign: load of libfoo.dylib failed, but message was wrong: %s\00"
@.str.4 = [16 x i8] c"dlopen-codesign\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @RTLD_LAZY
%5 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_SUCCESS
store i32 %10, i32* %1
br label %23
11: ; preds = %0
%12 = call i8* (...) @dlerror()
store i8* %12, i8** %3
%13 = load i8*, i8** %3
%14 = call i32* @strstr(i8* %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_SUCCESS
store i32 %19, i32* %1
br label %23
20: ; preds = %11
%21 = call i32 @PASS(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%22 = load i32, i32* @EXIT_SUCCESS
store i32 %22, i32* %1
br label %23
23: ; preds = %20, %16, %8
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [57 x i8] c"dlopen-codesign: load of libfoo.dylib should have failed\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [10 x i8] c"signature\00"
@.str.3 = [72 x i8] c"dlopen-codesign: load of libfoo.dylib failed, but message was wrong: %s\00"
@.str.4 = [16 x i8] c"dlopen-codesign\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 %6, label %4
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
br label %14
6: ; preds = %0
%7 = tail call i8* (...) @dlerror()
%8 = tail call i32* @strstr(i8* dereferenceable(1) %7, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 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 (i8*, ...) @FAIL(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br label %14
12: ; preds = %6
%13 = tail call i32 @PASS(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %14
14: ; preds = %10, %12, %4
%15 = load i32, i32* @EXIT_SUCCESS
ret i32 %15
}