#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*/ RTLD_LAZY;
int /*<<< orphan*/ bind_hook(void *, char *);
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
void __attribute__((constructor)) init(void) {
  void *h = dlopen(NULL, RTLD_LAZY);
  if (h != NULL) {
    bind_hook(h, "glgrab_glXSwapBuffers");
    bind_hook(h, "glgrab_glXDestroyContext");
    bind_hook(h, "glgrab_glXCreateWindow");
    bind_hook(h, "glgrab_glXDestroyWindow");
    bind_hook(h, "glgrab_XDestroyWindow");
    dlclose(h);
  } else {
    fprintf(stderr, "glgrab: failed to dlopen() myself: %s\n", dlerror());
  }
}


x86 assembly (clang, -O0):
<init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <dlopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11d7 <init+0x87>
mov    -0x8(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1040 <bind_hook@plt>
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1040 <bind_hook@plt>
mov    -0x8(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1040 <bind_hook@plt>
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1040 <bind_hook@plt>
mov    -0x8(%rbp),%rdi
lea    0xe9a(%rip),%rsi
call   1040 <bind_hook@plt>
mov    -0x8(%rbp),%rdi
call   1070 <dlclose@plt>
jmp    11fc <init+0xac>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%rdx
lea    0xe7d(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1050 <dlopen@plt>
test   %rax,%rax
je     11bd <init+0x6d>
mov    %rax,%rbx
lea    0xe90(%rip),%rsi
mov    %rax,%rdi
call   1040 <bind_hook@plt>
lea    0xe97(%rip),%rsi
mov    %rbx,%rdi
call   1040 <bind_hook@plt>
lea    0xea1(%rip),%rsi
mov    %rbx,%rdi
call   1040 <bind_hook@plt>
lea    0xea9(%rip),%rsi
mov    %rbx,%rdi
call   1040 <bind_hook@plt>
lea    0xeb2(%rip),%rsi
mov    %rbx,%rdi
call   1040 <bind_hook@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1070 <dlclose@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xea0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
pop    %rbx
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@.str = [22 x i8] c"glgrab_glXSwapBuffers\00"
@.str.1 = [25 x i8] c"glgrab_glXDestroyContext\00"
@.str.2 = [23 x i8] c"glgrab_glXCreateWindow\00"
@.str.3 = [24 x i8] c"glgrab_glXDestroyWindow\00"
@.str.4 = [22 x i8] c"glgrab_XDestroyWindow\00"
@stderr = global i32 0
@.str.5 = [39 x i8] c"glgrab: failed to dlopen() myself: %s\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @init, i8* null }]
define void @init() {
%1 = alloca i8*
%2 = load i32, i32* @RTLD_LAZY
%3 = call i8* @dlopen(i32* null, i32 %2)
store i8* %3, i8** %1
%4 = load i8*, i8** %1
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %19
6: ; preds = %0
%7 = load i8*, i8** %1
%8 = call i32 @bind_hook(i8* %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = load i8*, i8** %1
%10 = call i32 @bind_hook(i8* %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%11 = load i8*, i8** %1
%12 = call i32 @bind_hook(i8* %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%13 = load i8*, i8** %1
%14 = call i32 @bind_hook(i8* %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%15 = load i8*, i8** %1
%16 = call i32 @bind_hook(i8* %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%17 = load i8*, i8** %1
%18 = call i32 @dlclose(i8* %17)
br label %23
19: ; preds = %0
%20 = load i32, i32* @stderr
%21 = call i8* (...) @dlerror()
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %19, %6
ret void
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
@.str = [22 x i8] c"glgrab_glXSwapBuffers\00"
@.str.1 = [25 x i8] c"glgrab_glXDestroyContext\00"
@.str.2 = [23 x i8] c"glgrab_glXCreateWindow\00"
@.str.3 = [24 x i8] c"glgrab_glXDestroyWindow\00"
@.str.4 = [22 x i8] c"glgrab_XDestroyWindow\00"
@stderr = global i32 0
@.str.5 = [39 x i8] c"glgrab: failed to dlopen() myself: %s\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @init, i8* null }]
define void @init() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i32* null, i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %11, label %4
4: ; preds = %0
%5 = tail call i32 @bind_hook(i8* %2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @bind_hook(i8* %2, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @bind_hook(i8* %2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%8 = tail call i32 @bind_hook(i8* %2, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%9 = tail call i32 @bind_hook(i8* %2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%10 = tail call i32 @dlclose(i8* %2)
br label %15
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = tail call i8* (...) @dlerror()
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %11, %4
ret void
}