#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 *, char const *);
void *stub1(char const *, int);
void *dlopen(const char *file, int mode) {
  printf("CALLWATCH: "
         "LibOpen %s \n",
         file);
  static void *(*original)(const char *file, int mode) = NULL;
  if (original == NULL) {
    original =
        (void *(*)(const char *file, int mode))dlsym(((void *)-1l), "dlopen");
  }
  return (*original)(file, mode);
}


x86 assembly (clang, -O0):
<dlopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xec6(%rip),%rdi
call   1030 <printf@plt>
cmpq   $0x0,0x2ef1(%rip)
jne    1167 <dlopen+0x47>
mov    $0xffffffffffffffff,%rdi
lea    0xebd(%rip),%rsi
call   1040 <dlsym@plt>
mov    %rax,0x2ed1(%rip)
mov    0x2eca(%rip),%rax
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   *%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dlopen>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xed1(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    0x2efa(%rip),%rax
test   %rax,%rax
jne    115d <dlopen+0x3d>
lea    0xece(%rip),%rsi
mov    $0xffffffffffffffff,%rdi
call   1040 <dlsym@plt>
mov    %rax,0x2edb(%rip)
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    *%rax

LLVM IR (-O0):
@.str = [24 x i8] c"CALLWATCH: LibOpen %s \0A\00"
@dlopen.original = internal global i8* (i8*, i32)* null
@.str.1 = [7 x i8] c"dlopen\00"
define i8* @dlopen(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i8*, i8** %3
%6 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i8* (i8*, i32)*, i8* (i8*, i32)** @dlopen.original
%8 = icmp eq i8* (i8*, i32)* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i64 @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%11 = inttoptr i64 %10 to i8* (i8*, i32)*
store i8* (i8*, i32)* %11, i8* (i8*, i32)** @dlopen.original
br label %12
12: ; preds = %9, %2
%13 = load i8* (i8*, i32)*, i8* (i8*, i32)** @dlopen.original
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = call i8* %13(i8* %14, i32 %15)
ret i8* %16
}

LLVM IR (-O2):
@.str = [24 x i8] c"CALLWATCH: LibOpen %s \0A\00"
@dlopen.original = internal unnamed_addr global i8* (i8*, i32)* null
@.str.1 = [7 x i8] c"dlopen\00"
define i8* @dlopen(i8* %0, i32 %1) {
%3 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = load i8* (i8*, i32)*, i8* (i8*, i32)** @dlopen.original
%5 = icmp eq i8* (i8*, i32)* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i64 @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%8 = inttoptr i64 %7 to i8* (i8*, i32)*
store i8* (i8*, i32)* %8, i8* (i8*, i32)** @dlopen.original
br label %9
9: ; preds = %6, %2
%10 = phi i8* (i8*, i32)* [ %8, %6 ], [ %4, %2 ]
%11 = tail call i8* %10(i8* %0, i32 %1)
ret i8* %11
}