#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;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ is_emulated_32bit();
int /*<<< orphan*/ snprintf(char *, int, char *);
int /*<<< orphan*/ stderr;
void *get_xlib_handle() {
  void *ret_handle = NULL;
  char library[(1024) + 1];
  if (is_emulated_32bit() == 0) {
    snprintf(library, (1024), "/usr/lib/libX11.so.6");
  } else {
    snprintf(library, (1024), "/usr/lib32/libX11.so.6");
  }
  ret_handle = dlopen(library, RTLD_LAZY);
  if (ret_handle == NULL) {
    fprintf(stderr, "Failed to dlopen %s\n", library);
    fprintf(stderr, "dlerror says: %s\n", dlerror());
  }
  return ret_handle;
}


x86 assembly (clang, -O0):
<get_xlib_handle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movq   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1070 <is_emulated_32bit@plt>
cmp    $0x0,%rax
jne    1191 <get_xlib_handle+0x41>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
lea    0xe79(%rip),%rdx
call   1040 <snprintf@plt>
jmp    11a9 <get_xlib_handle+0x59>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
lea    0xe71(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x410(%rbp),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1214 <get_xlib_handle+0xc4>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xe48(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x414(%rbp),%edi
mov    %rax,%rdx
lea    0xe32(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_xlib_handle>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
xor    %eax,%eax
call   1070 <is_emulated_32bit@plt>
test   %rax,%rax
lea    0xe96(%rip),%rax
lea    0xea4(%rip),%rdx
cmove  %rax,%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1040 <snprintf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <dlopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d2 <get_xlib_handle+0x82>
mov    0x2e3b(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe7d(%rip),%rsi
mov    %rsp,%rdx
call   1060 <fprintf@plt>
mov    0x0(%rbp),%ebp
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe79(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/usr/lib/libX11.so.6\00"
@.str.1 = [23 x i8] c"/usr/lib32/libX11.so.6\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.2 = [21 x i8] c"Failed to dlopen %s\0A\00"
@.str.3 = [18 x i8] c"dlerror says: %s\0A\00"
define i8* @get_xlib_handle() {
%1 = alloca i8*
%2 = alloca [1025 x i8]
store i8* null, i8** %1
%3 = call i64 (...) @is_emulated_32bit()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %11
8: ; preds = %0
%9 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 1024, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %8, %5
%12 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%13 = load i32, i32* @RTLD_LAZY
%14 = call i8* @dlopen(i8* %12, i32 %13)
store i8* %14, i8** %1
%15 = load i8*, i8** %1
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %24
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @stderr
%22 = call i8* (...) @dlerror()
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %17, %11
%25 = load i8*, i8** %1
ret i8* %25
}

LLVM IR (-O2):
@.str = [21 x i8] c"/usr/lib/libX11.so.6\00"
@.str.1 = [23 x i8] c"/usr/lib32/libX11.so.6\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.2 = [21 x i8] c"Failed to dlopen %s\0A\00"
@.str.3 = [18 x i8] c"dlerror says: %s\0A\00"
define i8* @get_xlib_handle() {
%1 = alloca [1025 x i8]
%2 = getelementptr inbounds [1025 x i8], [1025 x i8]* %1, i64 0, i64 0
%3 = tail call i64 (...) @is_emulated_32bit()
%4 = icmp eq i64 %3, 0
%5 = select i1 %4, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0)
%6 = call i32 @snprintf(i8* %2, i32 1024, i8* %5)
%7 = load i32, i32* @RTLD_LAZY
%8 = call i8* @dlopen(i8* %2, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%13 = load i32, i32* @stderr
%14 = call i8* (...) @dlerror()
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %10, %0
ret i8* %8
}