#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 EINVAL;
int ENOENT;
int ENOTDIR;
int EPROTO;
int RTLD_LAZY;
int RTLD_LOCAL;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char const *, int);
int dlr_are_symbol_objects_compatible(void *, void *);
int /*<<< orphan*/ dlr_debug(int, char *, char const *);
int /*<<< orphan*/ dlr_set_error(char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ strerror(int);
int dlr_check_file_compatibility(const char *file, void **handle) {
  void *h_global, *h_file;
  int status;
  errno = 0;
  dlr_set_error(NULL);
  dlr_debug(2, "Entering dlr_check_file_compatbility(\"%s\") ...", file);
  if (file != NULL) {
    h_file = dlopen(file, RTLD_LAZY | RTLD_LOCAL);
    if (handle != NULL) {
      *handle = h_file;
    }
    if (h_file == NULL) {
      dlr_set_error(dlerror());
      return -ENOENT;
    }
  } else if (handle != NULL) {
    h_file = *handle;
  } else {
    dlr_set_error("dlr_check_file_compatibility(): %s", strerror(EINVAL));
    return -EINVAL;
  }
  h_global = dlopen(0, RTLD_LAZY | RTLD_LOCAL);
  if (h_global == NULL) {
    dlr_set_error("unable to dlopen() global symbol object");
    if (handle == NULL) {
      dlclose(h_file);
    }
    return -ENOTDIR;
  }
  status = dlr_are_symbol_objects_compatible(h_global, h_file);
  if (status < 0) {
    status = -EPROTO;
  }
  dlclose(h_global);
  if (handle == NULL) {
    dlclose(h_file);
  }
  return status;
}


x86 assembly (clang, -O0):
<dlr_check_file_compatibility>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e49(%rip),%rax
movq   $0x0,(%rax)
xor    %eax,%eax
mov    %eax,%edi
mov    $0x0,%al
call   1060 <dlr_set_error@plt>
mov    -0x10(%rbp),%rdx
mov    $0x2,%edi
lea    0xe57(%rip),%rsi
call   1070 <dlr_debug@plt>
cmpq   $0x0,-0x10(%rbp)
je     1222 <dlr_check_file_compatibility+0xb2>
mov    -0x10(%rbp),%rdi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
or     (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ee <dlr_check_file_compatibility+0x7e>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x28(%rbp)
jne    121d <dlr_check_file_compatibility+0xad>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rdi
mov    $0x0,%al
call   1060 <dlr_set_error@plt>
xor    %eax,%eax
mov    0x2d9d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    131d <dlr_check_file_compatibility+0x1ad>
jmp    1273 <dlr_check_file_compatibility+0x103>
cmpq   $0x0,-0x18(%rbp)
je     123d <dlr_check_file_compatibility+0xcd>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    126e <dlr_check_file_compatibility+0xfe>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1060 <dlr_set_error@plt>
xor    %eax,%eax
mov    0x2d64(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    131d <dlr_check_file_compatibility+0x1ad>
jmp    1273 <dlr_check_file_compatibility+0x103>
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    0x2d25(%rip),%rax
or     (%rax),%esi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12d2 <dlr_check_file_compatibility+0x162>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1060 <dlr_set_error@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12bf <dlr_check_file_compatibility+0x14f>
mov    -0x28(%rbp),%rdi
call   1090 <dlclose@plt>
xor    %eax,%eax
mov    0x2d30(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    131d <dlr_check_file_compatibility+0x1ad>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <dlr_are_symbol_objects_compatible@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    12fa <dlr_check_file_compatibility+0x18a>
xor    %eax,%eax
mov    0x2ce3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <dlclose@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1317 <dlr_check_file_compatibility+0x1a7>
mov    -0x28(%rbp),%rdi
call   1090 <dlclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dlr_check_file_compatibility>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e4b(%rip),%rax
movq   $0x0,(%rax)
xor    %edi,%edi
xor    %eax,%eax
call   1060 <dlr_set_error@plt>
lea    0xe64(%rip),%rsi
mov    $0x2,%edi
mov    %rbx,%rdx
call   1070 <dlr_debug@plt>
test   %rbx,%rbx
je     11fb <dlr_check_file_compatibility+0x8b>
mov    0x2e0b(%rip),%rax
mov    0x2dec(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <dlopen@plt>
mov    %rax,%r15
test   %r14,%r14
je     11d3 <dlr_check_file_compatibility+0x63>
mov    %r15,(%r14)
test   %r15,%r15
jne    1207 <dlr_check_file_compatibility+0x97>
xor    %r12d,%r12d
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rdi
xor    %eax,%eax
call   1060 <dlr_set_error@plt>
mov    0x2dbd(%rip),%rax
sub    (%rax),%r12d
jmp    12ab <dlr_check_file_compatibility+0x13b>
test   %r14,%r14
je     1287 <dlr_check_file_compatibility+0x117>
mov    (%r14),%r15
mov    0x2db2(%rip),%rax
mov    0x2d93(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %r12d,%r12d
xor    %edi,%edi
call   1050 <dlopen@plt>
test   %rax,%rax
je     125d <dlr_check_file_compatibility+0xed>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <dlr_are_symbol_objects_compatible@plt>
mov    0x2d9b(%rip),%rcx
sub    (%rcx),%r12d
test   %eax,%eax
cmovns %eax,%r12d
mov    %rbx,%rdi
call   1090 <dlclose@plt>
test   %r14,%r14
jne    12ab <dlr_check_file_compatibility+0x13b>
mov    %r15,%rdi
call   1090 <dlclose@plt>
jmp    12ab <dlr_check_file_compatibility+0x13b>
lea    0xdee(%rip),%rdi
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <dlr_set_error@plt>
test   %r14,%r14
jne    127b <dlr_check_file_compatibility+0x10b>
mov    %r15,%rdi
call   1090 <dlclose@plt>
mov    0x2d76(%rip),%rax
sub    (%rax),%r12d
jmp    12ab <dlr_check_file_compatibility+0x13b>
mov    0x2d3a(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
lea    0xd93(%rip),%rdi
xor    %r12d,%r12d
mov    %eax,%esi
xor    %eax,%eax
call   1060 <dlr_set_error@plt>
sub    (%rbx),%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [47 x i8] c"Entering dlr_check_file_compatbility(\22%s\22) ...\00"
@RTLD_LAZY = global i32 0
@RTLD_LOCAL = global i32 0
@ENOENT = global i32 0
@.str.1 = [35 x i8] c"dlr_check_file_compatibility(): %s\00"
@EINVAL = global i32 0
@.str.2 = [40 x i8] c"unable to dlopen() global symbol object\00"
@ENOTDIR = global i32 0
@EPROTO = global i32 0
define i32 @dlr_check_file_compatibility(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64 0, i64* @errno
%9 = call i32 (i8*, ...) @dlr_set_error(i8* null)
%10 = load i8*, i8** %4
%11 = call i32 @dlr_debug(i32 2, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %34
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i32, i32* @RTLD_LAZY
%17 = load i32, i32* @RTLD_LOCAL
%18 = or i32 %16, %17
%19 = call i8* @dlopen(i8* %15, i32 %18)
store i8* %19, i8** %7
%20 = load i8**, i8*** %5
%21 = icmp ne i8** %20, null
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i8*, i8** %7
%24 = load i8**, i8*** %5
store i8* %23, i8** %24
br label %25
25: ; preds = %22, %14
%26 = load i8*, i8** %7
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = call i8* (...) @dlerror()
%30 = call i32 (i8*, ...) @dlr_set_error(i8* %29)
%31 = load i32, i32* @ENOENT
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
br label %83
33: ; preds = %25
br label %47
34: ; preds = %2
%35 = load i8**, i8*** %5
%36 = icmp ne i8** %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i8**, i8*** %5
%39 = load i8*, i8** %38
store i8* %39, i8** %7
br label %46
40: ; preds = %34
%41 = load i32, i32* @EINVAL
%42 = call i32 @strerror(i32 %41)
%43 = call i32 (i8*, ...) @dlr_set_error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = load i32, i32* @EINVAL
%45 = sub nsw i32 0, %44
store i32 %45, i32* %3
br label %83
46: ; preds = %37
br label %47
47: ; preds = %46, %33
%48 = load i32, i32* @RTLD_LAZY
%49 = load i32, i32* @RTLD_LOCAL
%50 = or i32 %48, %49
%51 = call i8* @dlopen(i8* null, i32 %50)
store i8* %51, i8** %6
%52 = load i8*, i8** %6
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %64
54: ; preds = %47
%55 = call i32 (i8*, ...) @dlr_set_error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%56 = load i8**, i8*** %5
%57 = icmp eq i8** %56, null
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = load i8*, i8** %7
%60 = call i32 @dlclose(i8* %59)
br label %61
61: ; preds = %58, %54
%62 = load i32, i32* @ENOTDIR
%63 = sub nsw i32 0, %62
store i32 %63, i32* %3
br label %83
64: ; preds = %47
%65 = load i8*, i8** %6
%66 = load i8*, i8** %7
%67 = call i32 @dlr_are_symbol_objects_compatible(i8* %65, i8* %66)
store i32 %67, i32* %8
%68 = load i32, i32* %8
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %64
%71 = load i32, i32* @EPROTO
%72 = sub nsw i32 0, %71
store i32 %72, i32* %8
br label %73
73: ; preds = %70, %64
%74 = load i8*, i8** %6
%75 = call i32 @dlclose(i8* %74)
%76 = load i8**, i8*** %5
%77 = icmp eq i8** %76, null
br i1 %77, label %78, label %81
78: ; preds = %73
%79 = load i8*, i8** %7
%80 = call i32 @dlclose(i8* %79)
br label %81
81: ; preds = %78, %73
%82 = load i32, i32* %8
store i32 %82, i32* %3
br label %83
83: ; preds = %81, %61, %40, %28
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [47 x i8] c"Entering dlr_check_file_compatbility(\22%s\22) ...\00"
@RTLD_LAZY = global i32 0
@RTLD_LOCAL = global i32 0
@ENOENT = global i32 0
@.str.1 = [35 x i8] c"dlr_check_file_compatibility(): %s\00"
@EINVAL = global i32 0
@.str.2 = [40 x i8] c"unable to dlopen() global symbol object\00"
@ENOTDIR = global i32 0
@EPROTO = global i32 0
define i32 @dlr_check_file_compatibility(i8* %0, i8** %1) {
store i64 0, i64* @errno
%3 = tail call i32 (i8*, ...) @dlr_set_error(i8* null)
%4 = tail call i32 @dlr_debug(i32 2, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = icmp eq i8* %0, null
br i1 %5, label %20, label %6
6: ; preds = %2
%7 = load i32, i32* @RTLD_LAZY
%8 = load i32, i32* @RTLD_LOCAL
%9 = or i32 %8, %7
%10 = tail call i8* @dlopen(i8* %0, i32 %9)
%11 = icmp eq i8** %1, null
br i1 %11, label %13, label %12
12: ; preds = %6
store i8* %10, i8** %1
br label %13
13: ; preds = %12, %6
%14 = icmp eq i8* %10, null
br i1 %14, label %15, label %30
15: ; preds = %13
%16 = tail call i8* (...) @dlerror()
%17 = tail call i32 (i8*, ...) @dlr_set_error(i8* %16)
%18 = load i32, i32* @ENOENT
%19 = sub nsw i32 0, %18
br label %55
20: ; preds = %2
%21 = icmp eq i8** %1, null
br i1 %21, label %24, label %22
22: ; preds = %20
%23 = load i8*, i8** %1
br label %30
24: ; preds = %20
%25 = load i32, i32* @EINVAL
%26 = tail call i32 @strerror(i32 %25)
%27 = tail call i32 (i8*, ...) @dlr_set_error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @EINVAL
%29 = sub nsw i32 0, %28
br label %55
30: ; preds = %13, %22
%31 = phi i8* [ %10, %13 ], [ %23, %22 ]
%32 = load i32, i32* @RTLD_LAZY
%33 = load i32, i32* @RTLD_LOCAL
%34 = or i32 %33, %32
%35 = tail call i8* @dlopen(i8* null, i32 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %45
37: ; preds = %30
%38 = tail call i32 (i8*, ...) @dlr_set_error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp eq i8** %1, null
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = tail call i32 @dlclose(i8* %31)
br label %42
42: ; preds = %40, %37
%43 = load i32, i32* @ENOTDIR
%44 = sub nsw i32 0, %43
br label %55
45: ; preds = %30
%46 = tail call i32 @dlr_are_symbol_objects_compatible(i8* %35, i8* %31)
%47 = icmp slt i32 %46, 0
%48 = load i32, i32* @EPROTO
%49 = sub nsw i32 0, %48
%50 = select i1 %47, i32 %49, i32 %46
%51 = tail call i32 @dlclose(i8* %35)
%52 = icmp eq i8** %1, null
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = tail call i32 @dlclose(i8* %31)
br label %55
55: ; preds = %45, %53, %42, %24, %15
%56 = phi i32 [ %19, %15 ], [ %44, %42 ], [ %29, %24 ], [ %50, %53 ], [ %50, %45 ]
ret i32 %56
}