#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 */
typedef int /*<<< orphan*/ libdevice;

/* Variables and functions */
int RTLD_GLOBAL;
int RTLD_NOW;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char const *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int ret = 0;
  const char *libpocl = "$ORIGIN"
                        "/../../lib/CL/libpocl."
                        "so";
  char libdevice[4096] = "";
  if (argc > 1)
    snprintf(libdevice, sizeof(libdevice),
             "$ORIGIN"
             "/../../lib/CL/devices/%s/libpocl-devices-%s.so",
             argv[1], argv[1]);
  void *handle_libpocl = dlopen(libpocl, RTLD_NOW | RTLD_GLOBAL);
  if (!handle_libpocl) {
    fprintf(stderr, "dlopen(%s, RTLD_NOW | RTLD_GLOBAL) failed: %s\n", libpocl,
            dlerror());
    ret = 1;
  }
  if (ret == 0 && argc > 1) {
    void *handle_device = dlopen(libdevice, RTLD_NOW);
    if (!handle_device) {
      fprintf(stderr, "dlopen(%s, RTLD_NOW) failed: %s\n", libdevice,
              dlerror());
      ret = 1;
    }
    if (handle_device)
      dlclose(handle_device);
  }
  if (handle_libpocl)
    dlclose(handle_libpocl);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe79(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
cmpl   $0x1,-0x8(%rbp)
jle    11d0 <main+0x70>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8
mov    $0x1000,%esi
lea    0xe55(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    0x2e04(%rip),%rax
or     (%rax),%esi
call   1060 <dlopen@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    1244 <main+0xe4>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x103c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x103c(%rbp),%edi
mov    -0x1038(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    12e8 <main+0x188>
cmpl   $0x1,-0x8(%rbp)
jle    12e8 <main+0x188>
lea    -0x1020(%rbp),%rdi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
call   1060 <dlopen@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jne    12c9 <main+0x169>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x104c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x104c(%rbp),%edi
mov    -0x1048(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc8(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
cmpq   $0x0,-0x1030(%rbp)
je     12e3 <main+0x183>
mov    -0x1030(%rbp),%rdi
call   1080 <dlclose@plt>
jmp    12e8 <main+0x188>
cmpq   $0x0,-0x1028(%rbp)
je     1302 <main+0x1a2>
mov    -0x1028(%rbp),%rdi
call   1080 <dlclose@plt>
mov    -0x14(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1050 <memset@plt>
cmp    $0x2,%ebp
jl     11a1 <main+0x41>
mov    0x8(%rbx),%rcx
lea    0xe8f(%rip),%rdx
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rcx,%r8
call   1040 <snprintf@plt>
mov    0x2e38(%rip),%r15
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
or     (%r15),%esi
lea    0xe45(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     11e9 <main+0x89>
mov    %rax,%r14
xor    %ebx,%ebx
cmp    $0x2,%ebp
jl     1241 <main+0xe1>
mov    (%r15),%esi
mov    %rsp,%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     1218 <main+0xb8>
mov    %rax,%rdi
call   1080 <dlclose@plt>
jmp    1241 <main+0xe1>
mov    0x2de0(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe56(%rip),%rsi
lea    0xdf9(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    $0x1,%ebx
jmp    1249 <main+0xe9>
mov    0x2db1(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe56(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    $0x1,%ebx
mov    %r14,%rdi
call   1080 <dlclose@plt>
mov    %ebx,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"$ORIGIN/../../lib/CL/libpocl.so\00"
@.str.1 = [54 x i8] c"$ORIGIN/../../lib/CL/devices/%s/libpocl-devices-%s.so\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@stderr = global i32 0
@.str.2 = [47 x i8] c"dlopen(%s, RTLD_NOW | RTLD_GLOBAL) failed: %s\0A\00"
@.str.3 = [33 x i8] c"dlopen(%s, RTLD_NOW) failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [4096 x i8]
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = bitcast [4096 x i8]* %8 to i8*
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %23
14: ; preds = %2
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @snprintf(i8* %15, i32 4096, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %21)
br label %23
23: ; preds = %14, %2
%24 = load i8*, i8** %7
%25 = load i32, i32* @RTLD_NOW
%26 = load i32, i32* @RTLD_GLOBAL
%27 = or i32 %25, %26
%28 = call i8* @dlopen(i8* %24, i32 %27)
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = icmp ne i8* %29, null
br i1 %30, label %36, label %31
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %7
%34 = call i8* (...) @dlerror()
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* %34)
store i32 1, i32* %6
br label %36
36: ; preds = %31, %23
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %60
39: ; preds = %36
%40 = load i32, i32* %4
%41 = icmp sgt i32 %40, 1
br i1 %41, label %42, label %60
42: ; preds = %39
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%44 = load i32, i32* @RTLD_NOW
%45 = call i8* @dlopen(i8* %43, i32 %44)
store i8* %45, i8** %10
%46 = load i8*, i8** %10
%47 = icmp ne i8* %46, null
br i1 %47, label %53, label %48
48: ; preds = %42
%49 = load i32, i32* @stderr
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%51 = call i8* (...) @dlerror()
%52 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %50, i8* %51)
store i32 1, i32* %6
br label %53
53: ; preds = %48, %42
%54 = load i8*, i8** %10
%55 = icmp ne i8* %54, null
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i8*, i8** %10
%58 = call i32 @dlclose(i8* %57)
br label %59
59: ; preds = %56, %53
br label %60
60: ; preds = %59, %39, %36
%61 = load i8*, i8** %9
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i8*, i8** %9
%65 = call i32 @dlclose(i8* %64)
br label %66
66: ; preds = %63, %60
%67 = load i32, i32* %6
ret i32 %67
}

LLVM IR (-O2):
@.str = [32 x i8] c"$ORIGIN/../../lib/CL/libpocl.so\00"
@.str.1 = [54 x i8] c"$ORIGIN/../../lib/CL/devices/%s/libpocl-devices-%s.so\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@stderr = global i32 0
@.str.2 = [47 x i8] c"dlopen(%s, RTLD_NOW | RTLD_GLOBAL) failed: %s\0A\00"
@.str.3 = [33 x i8] c"dlopen(%s, RTLD_NOW) failed: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i32 @snprintf(i8* %4, i32 4096, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %8)
br label %10
10: ; preds = %6, %2
%11 = load i32, i32* @RTLD_NOW
%12 = load i32, i32* @RTLD_GLOBAL
%13 = or i32 %12, %11
%14 = call i8* @dlopen(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %27, label %16
16: ; preds = %10
br i1 %5, label %17, label %31
17: ; preds = %16
%18 = load i32, i32* @RTLD_NOW
%19 = call i8* @dlopen(i8* %4, i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i8* (...) @dlerror()
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %4, i8* %23)
br label %31
25: ; preds = %17
%26 = call i32 @dlclose(i8* %19)
br label %31
27: ; preds = %10
%28 = load i32, i32* @stderr
%29 = call i8* (...) @dlerror()
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %29)
br label %34
31: ; preds = %21, %25, %16
%32 = phi i32 [ 1, %21 ], [ 0, %25 ], [ 0, %16 ]
%33 = call i32 @dlclose(i8* %14)
br label %34
34: ; preds = %27, %31
%35 = phi i32 [ %32, %31 ], [ 1, %27 ]
ret i32 %35
}