#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*/ F_OK;
int RTLD_LOCAL;
int RTLD_NOW;
scalar_t__ access(char const *, int /*<<< orphan*/);
char const *dlerror();
void *dlopen(char const *, int);
int /*<<< orphan*/ linuxtrack_log(char *, ...);
void *linuxtrack_try_library(const char *path) {
  void *handle = NULL;
  linuxtrack_log("Trying to load '%s'... ", path);
  if (access(path, F_OK) != 0) {
    linuxtrack_log("Not found.\n");
    return NULL;
  }
  dlerror();
  handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
  if (handle != NULL) {
    linuxtrack_log("Loaded OK.\n");
    return handle;
  }
  linuxtrack_log("Couldn't load library - %s!\n", dlerror());
  return NULL;
}


x86 assembly (clang, -O0):
<linuxtrack_try_library>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1060 <linuxtrack_log@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
je     119d <linuxtrack_try_library+0x5d>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1060 <linuxtrack_log@plt>
movq   $0x0,-0x8(%rbp)
jmp    1209 <linuxtrack_try_library+0xc9>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e9 <linuxtrack_try_library+0xa9>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1060 <linuxtrack_log@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <linuxtrack_try_library+0xc9>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1060 <linuxtrack_log@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linuxtrack_try_library>:
push   %rbx
mov    %rdi,%rbx
lea    0xeb5(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <linuxtrack_log@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <access@plt>
test   %rax,%rax
je     1176 <linuxtrack_try_library+0x36>
lea    0xea6(%rip),%rdi
xor    %ebx,%ebx
jmp    11a6 <linuxtrack_try_library+0x66>
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x2e64(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11b2 <linuxtrack_try_library+0x72>
mov    %rax,%rbx
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
call   1060 <linuxtrack_log@plt>
mov    %rbx,%rax
pop    %rbx
ret
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe6e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <linuxtrack_log@plt>
jmp    11ad <linuxtrack_try_library+0x6d>

LLVM IR (-O0):
@.str = [24 x i8] c"Trying to load '%s'... \00"
@F_OK = global i32 0
@.str.1 = [12 x i8] c"Not found.\0A\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.2 = [12 x i8] c"Loaded OK.\0A\00"
@.str.3 = [29 x i8] c"Couldn't load library - %s!\0A\00"
define i8* @linuxtrack_try_library(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %4
%5 = load i8*, i8** %3
%6 = call i32 (i8*, ...) @linuxtrack_log(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i8*, i8** %3
%8 = load i32, i32* @F_OK
%9 = call i64 @access(i8* %7, i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 (i8*, ...) @linuxtrack_log(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %28
13: ; preds = %1
%14 = call i8* (...) @dlerror()
%15 = load i8*, i8** %3
%16 = load i32, i32* @RTLD_NOW
%17 = load i32, i32* @RTLD_LOCAL
%18 = or i32 %16, %17
%19 = call i8* @dlopen(i8* %15, i32 %18)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = call i32 (i8*, ...) @linuxtrack_log(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = load i8*, i8** %4
store i8* %24, i8** %2
br label %28
25: ; preds = %13
%26 = call i8* (...) @dlerror()
%27 = call i32 (i8*, ...) @linuxtrack_log(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %26)
store i8* null, i8** %2
br label %28
28: ; preds = %25, %22, %11
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@.str = [24 x i8] c"Trying to load '%s'... \00"
@F_OK = global i32 0
@.str.1 = [12 x i8] c"Not found.\0A\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.2 = [12 x i8] c"Loaded OK.\0A\00"
@.str.3 = [29 x i8] c"Couldn't load library - %s!\0A\00"
define i8* @linuxtrack_try_library(i8* %0) {
%2 = tail call i32 (i8*, ...) @linuxtrack_log(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = load i32, i32* @F_OK
%4 = tail call i64 @access(i8* %0, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 (i8*, ...) @linuxtrack_log(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %20
8: ; preds = %1
%9 = tail call i8* (...) @dlerror()
%10 = load i32, i32* @RTLD_NOW
%11 = load i32, i32* @RTLD_LOCAL
%12 = or i32 %11, %10
%13 = tail call i8* @dlopen(i8* %0, i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = tail call i32 (i8*, ...) @linuxtrack_log(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %20
17: ; preds = %8
%18 = tail call i8* (...) @dlerror()
%19 = tail call i32 (i8*, ...) @linuxtrack_log(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %15, %6
%21 = phi i8* [ null, %6 ], [ %13, %15 ], [ null, %17 ]
ret i8* %21
}