#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ RTLD_NOW;
char *dlerror();
scalar_t__ dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int i;
  struct stat st;
  char buf[PATH_MAX];
  for (i = 1; i < argc; i++) {
    if (dlopen(argv[i], RTLD_NOW)) {
      fprintf(stdout, "dlopen() of \"%s\" succeeded.\n", argv[i]);
    } else {
      snprintf(buf, sizeof(buf), "./%s", argv[i]);
      if ((stat(buf, &st) == 0) && dlopen(buf, RTLD_NOW)) {
        fprintf(stdout,
                "dlopen() of \"./%s\" "
                "succeeded.\n",
                argv[i]);
      } else {
        fprintf(stdout,
                "dlopen() of \"%s\" failed: "
                "%s\n",
                argv[i], dlerror());
        return 1;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12d8 <main+0x188>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
cmp    $0x0,%rax
je     11f5 <main+0xa5>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12c5 <main+0x175>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xe0b(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    1272 <main+0x122>
mov    -0x38(%rbp),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
cmp    $0x0,%rax
je     1272 <main+0x122>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12c0 <main+0x170>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12e6 <main+0x196>
jmp    12c5 <main+0x175>
jmp    12ca <main+0x17a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119d <main+0x4d>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,-0x30(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
mov    %rax,-0x38(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
xor    %r12d,%r12d
cmp    $0x2,%edi
jl     125a <main+0x10a>
mov    %rsi,%rbx
mov    %edi,%r14d
mov    $0x1,%r13d
xor    %r12d,%r12d
jmp    11c1 <main+0x71>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    (%rbx,%r13,8),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%r13
cmp    %r13,%r14
je     125a <main+0x10a>
mov    (%rbx,%r13,8),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
lea    0xe26(%rip),%rsi
test   %rax,%rax
jne    11a0 <main+0x50>
mov    (%rbx,%r13,8),%rcx
mov    %r15,%rdi
mov    -0x38(%rbp),%rsi
lea    0xe2c(%rip),%rdx
call   1040 <snprintf@plt>
mov    %r15,%rdi
lea    -0x40(%rbp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    1228 <main+0xd8>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1050 <dlopen@plt>
lea    0xe03(%rip),%rsi
test   %rax,%rax
jne    11a0 <main+0x50>
mov    0x2da1(%rip),%rax
mov    (%rax),%r14d
mov    (%rbx,%r13,8),%rbx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xdfd(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%r12d
mov    -0x30(%rbp),%rsp
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@RTLD_NOW = global i32 0
@stdout = global i32 0
@.str = [29 x i8] c"dlopen() of \22%s\22 succeeded.\0A\00"
@.str.1 = [5 x i8] c"./%s\00"
@.str.2 = [31 x i8] c"dlopen() of \22./%s\22 succeeded.\0A\00"
@.str.3 = [29 x i8] c"dlopen() of \22%s\22 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 %struct.stat
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
store i32 1, i32* %6
br label %15
15: ; preds = %69, %2
%16 = load i32, i32* %6
%17 = load i32, i32* %4
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %72
19: ; preds = %15
%20 = load i8**, i8*** %5
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = load i32, i32* @RTLD_NOW
%26 = call i64 @dlopen(i8* %24, i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %19
%29 = load i32, i32* @stdout
%30 = load i8**, i8*** %5
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8*, i8** %30, i64 %32
%34 = load i8*, i8** %33
%35 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %34)
br label %68
36: ; preds = %19
%37 = trunc i64 %12 to i32
%38 = load i8**, i8*** %5
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = call i32 @snprintf(i8* %14, i32 %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = call i64 @stat(i8* %14, %struct.stat* %7)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %58
46: ; preds = %36
%47 = load i32, i32* @RTLD_NOW
%48 = call i64 @dlopen(i8* %14, i32 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %58
50: ; preds = %46
%51 = load i32, i32* @stdout
%52 = load i8**, i8*** %5
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8*, i8** %52, i64 %54
%56 = load i8*, i8** %55
%57 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %56)
br label %67
58: ; preds = %46, %36
%59 = load i32, i32* @stdout
%60 = load i8**, i8*** %5
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8*, i8** %60, i64 %62
%64 = load i8*, i8** %63
%65 = call i8* (...) @dlerror()
%66 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %64, i8* %65)
store i32 1, i32* %3
store i32 1, i32* %10
br label %73
67: ; preds = %50
br label %68
68: ; preds = %67, %28
br label %69
69: ; preds = %68
%70 = load i32, i32* %6
%71 = add nsw i32 %70, 1
store i32 %71, i32* %6
br label %15
72: ; preds = %15
store i32 0, i32* %3
store i32 1, i32* %10
br label %73
73: ; preds = %72, %58
%74 = load i8*, i8** %8
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@RTLD_NOW = global i32 0
@stdout = global i32 0
@.str = [29 x i8] c"dlopen() of \22%s\22 succeeded.\0A\00"
@.str.1 = [5 x i8] c"./%s\00"
@.str.2 = [31 x i8] c"dlopen() of \22./%s\22 succeeded.\0A\00"
@.str.3 = [29 x i8] c"dlopen() of \22%s\22 failed: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %40
10: ; preds = %2
%11 = zext i32 %0 to i64
br label %12
12: ; preds = %10, %33
%13 = phi i64 [ 1, %10 ], [ %38, %33 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 %13
%15 = load i8*, i8** %14
%16 = load i32, i32* @RTLD_NOW
%17 = call i64 @dlopen(i8* %15, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %12
%20 = load i8*, i8** %14
%21 = call i32 @snprintf(i8* %8, i32 %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i64 @stat(i8* %8, %struct.stat* %3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @RTLD_NOW
%26 = call i64 @dlopen(i8* %8, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %24, %19
%29 = load i32, i32* @stdout
%30 = load i8*, i8** %14
%31 = call i8* (...) @dlerror()
%32 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %30, i8* %31)
br label %40
33: ; preds = %24, %12
%34 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), %12 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), %24 ]
%35 = load i32, i32* @stdout
%36 = load i8*, i8** %14
%37 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* %34, i8* %36)
%38 = add nuw nsw i64 %13, 1
%39 = icmp eq i64 %38, %11
br i1 %39, label %40, label %12
40: ; preds = %33, %2, %28
%41 = phi i32 [ 1, %28 ], [ 0, %2 ], [ 0, %33 ]
ret i32 %41
}