#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;
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
void *get_curl_lib() {
  char *libs[] = {"libcurl.so.4", "libcurl.so", "libcurl-gnutls.so.4",
                  "libcurl.so.3", NULL};
  static void *h = NULL;
  if (h)
    return h;
  char **p = libs;
  while (*p) {
    h = dlopen(*p, RTLD_LAZY);
    if (h) {
      return h;
    }
    p++;
  }
  printf("Failed to open Curl library.\n");
  return NULL;
}


x86 assembly (clang, -O0):
<get_curl_lib>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    0xeb1(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xeb3(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xeb3(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xebc(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,0x2ec4(%rip)
je     1192 <get_curl_lib+0x62>
mov    0x2eb7(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1207 <get_curl_lib+0xd7>
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmpq   $0x0,(%rax)
je     11f3 <get_curl_lib+0xc3>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,0x2e7c(%rip)
cmpq   $0x0,0x2e74(%rip)
je     11e2 <get_curl_lib+0xb2>
mov    0x2e67(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1207 <get_curl_lib+0xd7>
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
jmp    119a <get_curl_lib+0x6a>
lea    0xe3f(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_curl_lib>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movq   $0x0,0x20(%rsp)
lea    0xec9(%rip),%rdi
mov    %rdi,(%rsp)
lea    0xecb(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xeca(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xed2(%rip),%rax
mov    %rax,0x18(%rsp)
mov    0x2ed2(%rip),%rax
test   %rax,%rax
jne    11ae <get_curl_lib+0x8e>
lea    0x8(%rsp),%rbx
mov    0x2e69(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    (%r14),%esi
call   1040 <dlopen@plt>
mov    %rax,0x2ea9(%rip)
test   %rax,%rax
jne    11ae <get_curl_lib+0x8e>
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
jne    1180 <get_curl_lib+0x60>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"libcurl.so.4\00"
@.str.1 = [11 x i8] c"libcurl.so\00"
@.str.2 = [20 x i8] c"libcurl-gnutls.so.4\00"
@.str.3 = [13 x i8] c"libcurl.so.3\00"
@get_curl_lib.h = internal global i8* null
@RTLD_LAZY = global i32 0
@.str.4 = [30 x i8] c"Failed to open Curl library.\0A\00"
define i8* @get_curl_lib() {
%1 = alloca i8*
%2 = alloca [5 x i8*]
%3 = alloca i8**
%4 = bitcast [5 x i8*]* %2 to i8*
%5 = bitcast i8* %4 to [5 x i8*]*
%6 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i32 0, i32 0
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0), i8** %6
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i32 0, i32 1
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i32 0, i32 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i32 0, i32 2
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i32 0, i32 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i32 0, i32 3
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i32 0, i32 0), i8** %9
%10 = load i8*, i8** @get_curl_lib.h
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = load i8*, i8** @get_curl_lib.h
store i8* %13, i8** %1
br label %34
14: ; preds = %0
%15 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 0
store i8** %15, i8*** %3
br label %16
16: ; preds = %29, %14
%17 = load i8**, i8*** %3
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %32
20: ; preds = %16
%21 = load i8**, i8*** %3
%22 = load i8*, i8** %21
%23 = load i32, i32* @RTLD_LAZY
%24 = call i8* @dlopen(i8* %22, i32 %23)
store i8* %24, i8** @get_curl_lib.h
%25 = load i8*, i8** @get_curl_lib.h
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = load i8*, i8** @get_curl_lib.h
store i8* %28, i8** %1
br label %34
29: ; preds = %20
%30 = load i8**, i8*** %3
%31 = getelementptr inbounds i8*, i8** %30, i32 1
store i8** %31, i8*** %3
br label %16
32: ; preds = %16
%33 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i8* null, i8** %1
br label %34
34: ; preds = %32, %27, %12
%35 = load i8*, i8** %1
ret i8* %35
}

LLVM IR (-O2):
@.str = [13 x i8] c"libcurl.so.4\00"
@.str.1 = [11 x i8] c"libcurl.so\00"
@.str.2 = [20 x i8] c"libcurl-gnutls.so.4\00"
@.str.3 = [13 x i8] c"libcurl.so.3\00"
@get_curl_lib.h = internal unnamed_addr global i8* null
@RTLD_LAZY = global i32 0
@str = [29 x i8] c"Failed to open Curl library.\00"
define i8* @get_curl_lib() {
%1 = alloca [5 x i8*]
%2 = bitcast [5 x i8*]* %1 to i8*
%3 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 4
%4 = bitcast i8** %3 to i64*
store i64 0, i64* %4
%5 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 0
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 1
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 2
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 3
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %8
%9 = load i8*, i8** @get_curl_lib.h
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %23
11: ; preds = %14
%12 = load i8*, i8** %20
%13 = icmp eq i8* %12, null
br i1 %13, label %21, label %14
14: ; preds = %0, %11
%15 = phi i8* [ %12, %11 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %0 ]
%16 = phi i8** [ %20, %11 ], [ %5, %0 ]
%17 = load i32, i32* @RTLD_LAZY
%18 = tail call i8* @dlopen(i8* %15, i32 %17)
store i8* %18, i8** @get_curl_lib.h
%19 = icmp eq i8* %18, null
%20 = getelementptr inbounds i8*, i8** %16, i64 1
br i1 %19, label %11, label %23
21: ; preds = %11
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %14, %21, %0
%24 = phi i8* [ %9, %0 ], [ null, %21 ], [ %18, %14 ]
ret i8* %24
}