#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 */
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ *strstr(char const *, char *);
void *stub1(char const *, int);
void *dlopen(const char *file, int mode) {
  const char *loadfile = file;
  static void *(*o_dlopen)(const char *, int) = 0;
  if (file == NULL)
    loadfile == NULL;
  else if (strcmp(file, "REALPCSCLIB") == 0) {
    printf("==> proxy dlopen: PCSC proxy is loading the real PCSC lib\n");
    loadfile = "/usr/lib/libpcsclite.so";
  } else if (strstr(file, "libpcsclite.so") != NULL) {
    printf("==> proxy dlopen: loading the proxy PCSC lib\n");
    loadfile = "/usr/share/eidtestinfra/libpcsclite.so";
  }
  o_dlopen = (void *(*)(const char *, int))dlsym(((void *)-1L), "dlopen");
  return (*o_dlopen)(loadfile, mode);
}


x86 assembly (clang, -O0):
<dlopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1167 <dlopen+0x27>
jmp    11d8 <dlopen+0x98>
mov    -0x8(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    119d <dlopen+0x5d>
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
lea    0xeb3(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11d3 <dlopen+0x93>
mov    -0x8(%rbp),%rdi
lea    0xefd(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     11ce <dlopen+0x8e>
lea    0xea1(%rip),%rdi
call   1030 <printf@plt>
lea    0xec3(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11d3 <dlopen+0x93>
jmp    11d8 <dlopen+0x98>
mov    $0xffffffffffffffff,%rdi
lea    0xece(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,0x2e56(%rip)
mov    0x2e4f(%rip),%rax
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   *%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dlopen>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
test   %rdi,%rdi
je     1182 <dlopen+0x42>
mov    %rdi,%rbx
lea    0xeac(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1186 <dlopen+0x46>
lea    0xed7(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     1199 <dlopen+0x59>
lea    0xeab(%rip),%rbx
lea    0xed2(%rip),%rdi
jmp    1194 <dlopen+0x54>
xor    %ebx,%ebx
jmp    1199 <dlopen+0x59>
lea    0xe7f(%rip),%rbx
lea    0xeeb(%rip),%rdi
call   1030 <puts@plt>
lea    0xeab(%rip),%rsi
mov    $0xffffffffffffffff,%rdi
call   1050 <dlsym@plt>
mov    %rax,0x2e95(%rip)
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    *%rax

LLVM IR (-O0):
@dlopen.o_dlopen = internal global i8* (i8*, i32)* null
@.str = [12 x i8] c"REALPCSCLIB\00"
@.str.1 = [59 x i8] c"==> proxy dlopen: PCSC proxy is loading the real PCSC lib\0A\00"
@.str.2 = [24 x i8] c"/usr/lib/libpcsclite.so\00"
@.str.3 = [15 x i8] c"libpcsclite.so\00"
@.str.4 = [46 x i8] c"==> proxy dlopen: loading the proxy PCSC lib\0A\00"
@.str.5 = [39 x i8] c"/usr/share/eidtestinfra/libpcsclite.so\00"
@.str.6 = [7 x i8] c"dlopen\00"
define i8* @dlopen(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
store i8* %6, i8** %5
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
%12 = zext i1 %11 to i32
br label %27
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0))
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8** %5
br label %26
19: ; preds = %13
%20 = load i8*, i8** %3
%21 = call i32* @strstr(i8* %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
store i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i8** %5
br label %25
25: ; preds = %23, %19
br label %26
26: ; preds = %25, %17
br label %27
27: ; preds = %26, %9
%28 = call i64 @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%29 = inttoptr i64 %28 to i8* (i8*, i32)*
store i8* (i8*, i32)* %29, i8* (i8*, i32)** @dlopen.o_dlopen
%30 = load i8* (i8*, i32)*, i8* (i8*, i32)** @dlopen.o_dlopen
%31 = load i8*, i8** %5
%32 = load i32, i32* %4
%33 = call i8* %30(i8* %31, i32 %32)
ret i8* %33
}

LLVM IR (-O2):
@dlopen.o_dlopen = internal unnamed_addr global i8* (i8*, i32)* null
@.str = [12 x i8] c"REALPCSCLIB\00"
@.str.2 = [24 x i8] c"/usr/lib/libpcsclite.so\00"
@.str.3 = [15 x i8] c"libpcsclite.so\00"
@.str.5 = [39 x i8] c"/usr/share/eidtestinfra/libpcsclite.so\00"
@.str.6 = [7 x i8] c"dlopen\00"
@str = [45 x i8] c"==> proxy dlopen: loading the proxy PCSC lib\00"
@str.7 = [58 x i8] c"==> proxy dlopen: PCSC proxy is loading the real PCSC lib\00"
define i8* @dlopen(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call i32* @strstr(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %7, %4
%11 = phi i8* [ getelementptr inbounds ([58 x i8], [58 x i8]* @str.7, i64 0, i64 0), %4 ], [ getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0), %7 ]
%12 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), %4 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), %7 ]
%13 = tail call i32 @puts(i8* dereferenceable(1) %11)
br label %14
14: ; preds = %10, %2, %7
%15 = phi i8* [ %0, %7 ], [ null, %2 ], [ %12, %10 ]
%16 = tail call i64 @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%17 = inttoptr i64 %16 to i8* (i8*, i32)*
store i8* (i8*, i32)* %17, i8* (i8*, i32)** @dlopen.o_dlopen
%18 = tail call i8* %17(i8* %15, i32 %1)
ret i8* %18
}