#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 */
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ RTLD_LAZY;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ dlclose(void *);
void *dlopen(char const *, int /*<<< orphan*/);
int is_library_for_architecture(char const *, int /*<<< orphan*/);
int is_usable_library(const char *candidate_library,
                      uint16_t desired_architecture) {
  if (access(candidate_library, F_OK) == 0 &&
      is_library_for_architecture(candidate_library, desired_architecture) ==
          1) {
    void *ret_handle = dlopen(candidate_library, RTLD_LAZY);
    if (ret_handle == NULL) {
      return 0;
    }
    dlclose(ret_handle);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_usable_library>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
jne    11c2 <is_usable_library+0x82>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <is_library_for_architecture@plt>
cmp    $0x1,%eax
jne    11c2 <is_usable_library+0x82>
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1030 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ad <is_usable_library+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <is_usable_library+0x89>
mov    -0x20(%rbp),%rdi
call   1060 <dlclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c9 <is_usable_library+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_usable_library>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1192 <is_usable_library+0x52>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <is_library_for_architecture@plt>
cmp    $0x1,%eax
jne    1192 <is_usable_library+0x52>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     1192 <is_usable_library+0x52>
mov    %rax,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_OK = global i32 0
@RTLD_LAZY = global i32 0
define i32 @is_usable_library(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = load i32, i32* @F_OK
%9 = call i64 @access(i8* %7, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %26
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i32, i32* %5
%14 = call i32 @is_library_for_architecture(i8* %12, i32 %13)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %26
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = load i32, i32* @RTLD_LAZY
%19 = call i8* @dlopen(i8* %17, i32 %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 0, i32* %3
br label %27
23: ; preds = %16
%24 = load i8*, i8** %6
%25 = call i32 @dlclose(i8* %24)
store i32 1, i32* %3
br label %27
26: ; preds = %11, %2
store i32 0, i32* %3
br label %27
27: ; preds = %26, %23, %22
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@F_OK = global i32 0
@RTLD_LAZY = global i32 0
define i32 @is_usable_library(i8* %0, i32 %1) {
%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 %6, label %15
6: ; preds = %2
%7 = tail call i32 @is_library_for_architecture(i8* %0, i32 %1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = load i32, i32* @RTLD_LAZY
%11 = tail call i8* @dlopen(i8* %0, i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @dlclose(i8* %11)
br label %15
15: ; preds = %2, %6, %13, %9
%16 = phi i32 [ 1, %13 ], [ 0, %9 ], [ 0, %6 ], [ 0, %2 ]
ret i32 %16
}