#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 dirent {
  scalar_t__ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *strndup(char *, int);
int /*<<< orphan*/ *strstr(scalar_t__, char *);
void do_analysis() {
  DIR *dir;
  struct dirent *dent;
  void (*zxd)(void) = NULL;
  dir = opendir("./lib/");
  while ((dent = readdir(dir)) != NULL) {
    if (strstr(dent->d_name, "so") != NULL) {
      char *err;
      char sname[256];
      char *ssname;
      ssname = strndup((char *)dent->d_name, 256);
      sprintf(sname, "./lib/%s", ssname);
      printf("%s\n", sname);
      void *handle = dlopen(sname, RTLD_LAZY);
      if (handle == NULL) {
        printf("dlopen failed:%s\n", dlerror());
        return;
      }
      dlerror();
      *(void **)&zxd = dlsym(handle, "zxdate");
      if ((err = dlerror()) != NULL) {
        printf("dlsym failed\n");
        perror(err);
        return;
      }
      if (zxd != NULL)
        zxd();
      dlclose(handle);
    }
  }
}


x86 assembly (clang, -O0):
<do_analysis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movq   $0x0,-0x18(%rbp)
lea    0xe36(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1303 <do_analysis+0x153>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe0f(%rip),%rsi
call   10d0 <strstr@plt>
cmp    $0x0,%rax
je     12fe <do_analysis+0x14e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x100,%esi
call   1060 <strndup@plt>
mov    %rax,-0x128(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x128(%rbp),%rdx
lea    0xdd6(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x120(%rbp),%rsi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
call   1070 <dlopen@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x0,-0x130(%rbp)
jne    1295 <do_analysis+0xe5>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1303 <do_analysis+0x153>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x130(%rbp),%rdi
lea    0xd7b(%rip),%rsi
call   10a0 <dlsym@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12e4 <do_analysis+0x134>
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1090 <perror@plt>
jmp    1303 <do_analysis+0x153>
cmpq   $0x0,-0x18(%rbp)
je     12f2 <do_analysis+0x142>
call   *-0x18(%rbp)
mov    -0x130(%rbp),%rdi
call   10c0 <dlclose@plt>
jmp    11d3 <do_analysis+0x23>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_analysis>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
lea    0xe28(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12b5 <do_analysis+0xf5>
mov    %rax,%rbp
lea    0xe0c(%rip),%r14
mov    %rsp,%r12
jmp    121c <do_analysis+0x5c>
mov    %rbp,%rdi
call   10d0 <dlclose@plt>
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12b5 <do_analysis+0xf5>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
call   10e0 <strstr@plt>
test   %rax,%rax
je     1208 <do_analysis+0x48>
mov    0x0(%rbp),%rdi
mov    $0x100,%esi
call   1070 <strndup@plt>
mov    %r12,%rdi
lea    0xdc5(%rip),%rsi
mov    %rax,%rdx
call   10c0 <sprintf@plt>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1080 <dlopen@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rbp,%rbp
je     12a4 <do_analysis+0xe4>
mov    %rbp,%rdi
lea    0xda6(%rip),%rsi
call   10b0 <dlsym@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    12c7 <do_analysis+0x107>
test   %r15,%r15
je     1200 <do_analysis+0x40>
call   *%r15
jmp    1200 <do_analysis+0x40>
lea    0xd68(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r13
lea    0xd5b(%rip),%rdi
call   1040 <puts@plt>
mov    %r13,%rdi
call   10a0 <perror@plt>
jmp    12b5 <do_analysis+0xf5>

LLVM IR (-O0):
%struct.dirent = type { i64 }
@.str = [7 x i8] c"./lib/\00"
@.str.1 = [3 x i8] c"so\00"
@.str.2 = [9 x i8] c"./lib/%s\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@RTLD_LAZY = global i32 0
@.str.4 = [18 x i8] c"dlopen failed:%s\0A\00"
@.str.5 = [7 x i8] c"zxdate\00"
@.str.6 = [14 x i8] c"dlsym failed\0A\00"
define void @do_analysis() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca void ()*
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = alloca i8*
%7 = alloca i8*
store void ()* null, void ()** %3
%8 = call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %1
br label %9
9: ; preds = %57, %0
%10 = load i32*, i32** %1
%11 = call %struct.dirent* @readdir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %2
%12 = icmp ne %struct.dirent* %11, null
br i1 %12, label %13, label %58
13: ; preds = %9
%14 = load %struct.dirent*, %struct.dirent** %2
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i32 0, i32 0
%16 = load i64, i64* %15
%17 = call i32* @strstr(i64 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %57
19: ; preds = %13
%20 = load %struct.dirent*, %struct.dirent** %2
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i8*
%24 = call i8* @strndup(i8* %23, i32 256)
store i8* %24, i8** %6
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%26 = load i8*, i8** %6
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%31 = load i32, i32* @RTLD_LAZY
%32 = call i8* @dlopen(i8* %30, i32 %31)
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %19
%36 = call i8* (...) @dlerror()
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %58
38: ; preds = %19
%39 = call i8* (...) @dlerror()
%40 = load i8*, i8** %7
%41 = call i8* @dlsym(i8* %40, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%42 = bitcast void ()** %3 to i8**
store i8* %41, i8** %42
%43 = call i8* (...) @dlerror()
store i8* %43, i8** %4
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %49
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%47 = load i8*, i8** %4
%48 = call i32 @perror(i8* %47)
br label %58
49: ; preds = %38
%50 = load void ()*, void ()** %3
%51 = icmp ne void ()* %50, null
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = load void ()*, void ()** %3
br label %54
54: ; preds = %52, %49
%55 = load i8*, i8** %7
%56 = call i32 @dlclose(i8* %55)
br label %57
57: ; preds = %54, %13
br label %9
58: ; preds = %35, %45, %9
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i64 }
@.str = [7 x i8] c"./lib/\00"
@.str.1 = [3 x i8] c"so\00"
@.str.2 = [9 x i8] c"./lib/%s\00"
@RTLD_LAZY = global i32 0
@.str.4 = [18 x i8] c"dlopen failed:%s\0A\00"
@.str.5 = [7 x i8] c"zxdate\00"
@str = [13 x i8] c"dlsym failed\00"
define void @do_analysis() {
%1 = alloca [256 x i8]
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%3 = call %struct.dirent* @readdir(i32* %2)
%4 = icmp eq %struct.dirent* %3, null
br i1 %4, label %42, label %5
5: ; preds = %0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
br label %7
7: ; preds = %5, %39
%8 = phi %struct.dirent* [ %3, %5 ], [ %40, %39 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%10 = load i64, i64* %9
%11 = call i32* @strstr(i64 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %39, label %13
13: ; preds = %7
%14 = load i64, i64* %9
%15 = inttoptr i64 %14 to i8*
%16 = call i8* @strndup(i8* %15, i32 256)
%17 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = call i32 @puts(i8* %6)
%19 = load i32, i32* @RTLD_LAZY
%20 = call i8* @dlopen(i8* %6, i32 %19)
%21 = icmp eq i8* %20, null
%22 = call i8* (...) @dlerror()
br i1 %21, label %23, label %25
23: ; preds = %13
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %22)
br label %38
25: ; preds = %13
%26 = call i8* @dlsym(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%27 = bitcast i8* %26 to void ()*
%28 = call i8* (...) @dlerror()
%29 = icmp eq i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%32 = call i32 @perror(i8* %28)
br label %38
33: ; preds = %25
%34 = icmp eq i8* %26, null
br i1 %34, label %36, label %35
35: ; preds = %33
br label %36
36: ; preds = %35, %33
%37 = call i32 @dlclose(i8* %20)
br label %39
38: ; preds = %30, %23
br label %42
39: ; preds = %36, %7
%40 = call %struct.dirent* @readdir(i32* %2)
%41 = icmp eq %struct.dirent* %40, null
br i1 %41, label %42, label %7
42: ; preds = %39, %0, %38
ret void
}