#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_NOW;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ puts(char const *);
int /*<<< orphan*/ *strstr(char const *, char *);
int main(void) {
  void *h;
  const char *s;
  s = dlerror();
  printf("dlerror() without prior dl*() call returned: %s\n", s);
  if (s != NULL)
    return 1;
  h = dlopen("errmsg1mod.so", RTLD_NOW);
  if (h != NULL) {
    dlclose(h);
    puts("errmsg1mod.so could be loaded !?");
    exit(1);
  }
  s = dlerror();
  puts(s);
  return strstr(s, "errmsg1mod.so") == NULL;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe6b(%rip),%rdi
call   1050 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
je     11b1 <main+0x41>
movl   $0x1,-0x4(%rbp)
jmp    1227 <main+0xb7>
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0xe70(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f4 <main+0x84>
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
lea    0xe5a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rbx
lea    0xe7e(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0x1,%eax
test   %rbx,%rbx
je     1196 <main+0x26>
pop    %rbx
ret
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
jne    11de <main+0x6e>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <puts@plt>
lea    0xe68(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strstr@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
pop    %rbx
ret
mov    %rax,%rdi
call   1080 <dlclose@plt>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"dlerror() without prior dl*() call returned: %s\0A\00"
@.str.1 = [14 x i8] c"errmsg1mod.so\00"
@RTLD_NOW = global i32 0
@.str.2 = [33 x i8] c"errmsg1mod.so could be loaded !?\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i8* (...) @dlerror()
store i8* %4, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 1, i32* %1
br label %28
10: ; preds = %0
%11 = load i32, i32* @RTLD_NOW
%12 = call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i8* %12, i8** %2
%13 = load i8*, i8** %2
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i8*, i8** %2
%17 = call i32 @dlclose(i8* %16)
%18 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = call i8* (...) @dlerror()
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = call i32 @puts(i8* %22)
%24 = load i8*, i8** %3
%25 = call i32* @strstr(i8* %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i32* %25, null
%27 = zext i1 %26 to i32
store i32 %27, i32* %1
br label %28
28: ; preds = %20, %9
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [49 x i8] c"dlerror() without prior dl*() call returned: %s\0A\00"
@.str.1 = [14 x i8] c"errmsg1mod.so\00"
@RTLD_NOW = global i32 0
@.str.2 = [33 x i8] c"errmsg1mod.so could be loaded !?\00"
define i32 @main() {
%1 = tail call i8* (...) @dlerror()
%2 = tail call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %1)
%3 = icmp eq i8* %1, null
br i1 %3, label %4, label %18
4: ; preds = %0
%5 = load i32, i32* @RTLD_NOW
%6 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = tail call i32 @dlclose(i8* %6)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %4
%13 = tail call i8* (...) @dlerror()
%14 = tail call i32 @puts(i8* dereferenceable(1) %13)
%15 = tail call i32* @strstr(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
%17 = zext i1 %16 to i32
br label %18
18: ; preds = %0, %12
%19 = phi i32 [ %17, %12 ], [ 1, %0 ]
ret i32 %19
}