#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 RTLD_GLOBAL;
int RTLD_NOW;
char *dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int stub1(char *);
int stub2(char *);
int stub3(char *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  void *handle;
  if (argc < 3) {
    usage(argv[0]);
    return 1;
  }
  if (argv[1][0] == 'G') {
    handle = dlopen("./.libs/libtest_dbi_plugin.so", RTLD_NOW | RTLD_GLOBAL);
  } else if (argv[1][0] == 'N') {
    handle = dlopen("./.libs/libtest_dbi_plugin.so", RTLD_NOW);
  } else {
    usage(argv[0]);
    return 1;
  }
  if (!handle) {
    printf("Failed to load test_dbi_plugin.so");
    return 1;
  }
  int (*init_db)(char *);
  char *error;
  *(void **)(&init_db) = dlsym(handle, "init_db");
  if ((error = dlerror()) != NULL) {
    fprintf(stderr, "%s\n", error);
    return 1;
  }
  int ndrivers = (*init_db)(argv[2]);
  if (ndrivers < 1) {
    fprintf(stderr,
            "Either you have no drivers in %s , or there was a problem loading "
            "them.\n",
            argv[2]);
    return 1;
  }
  fprintf(stdout, "num drivers = %d\n", (*init_db)(argv[2]));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1198 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    130f <main+0x1af>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x47,%eax
jne    11d3 <main+0x73>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
or     (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
jmp    1222 <main+0xc2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x4e,%eax
jne    1205 <main+0xa5>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    0xe09(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
jmp    121d <main+0xbd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    130f <main+0x1af>
jmp    1222 <main+0xc2>
cmpq   $0x0,-0x18(%rbp)
jne    1245 <main+0xe5>
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130f <main+0x1af>
mov    -0x18(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1080 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1295 <main+0x135>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130f <main+0x1af>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    0x10(%rcx),%rdi
call   *%rax
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jge    12db <main+0x17b>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130f <main+0x1af>
mov    0x2ce6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    0x10(%rcx),%rdi
call   *%rax
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xd94(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    118e <main+0x2e>
mov    0x8(%rbx),%rax
mov    (%rax),%al
cmp    $0x4e,%al
je     11a2 <main+0x42>
cmp    $0x47,%al
jne    118e <main+0x2e>
mov    0x2e5f(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
jmp    11ab <main+0x4b>
mov    (%rbx),%rdi
call   1070 <usage@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     11f6 <main+0x96>
lea    0xe7d(%rip),%rsi
mov    %rax,%rdi
call   1080 <dlsym@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     1204 <main+0xa4>
mov    0x2def(%rip),%rcx
mov    (%rcx),%edi
lea    0xe5e(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1196 <main+0x36>
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
jmp    1196 <main+0x36>
mov    0x10(%rbx),%rdi
call   *%r14
test   %eax,%eax
jle    1238 <main+0xd8>
mov    0x2db2(%rip),%rax
mov    (%rax),%ebp
mov    0x10(%rbx),%rdi
call   *%r14
lea    0xe6f(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    119b <main+0x3b>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xe00(%rip),%rsi
jmp    11ed <main+0x8d>

LLVM IR (-O0):
@.str = [30 x i8] c"./.libs/libtest_dbi_plugin.so\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [34 x i8] c"Failed to load test_dbi_plugin.so\00"
@.str.2 = [8 x i8] c"init_db\00"
@stderr = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [73 x i8] c"Either you have no drivers in %s , or there was a problem loading them.\0A\00"
@stdout = global i32 0
@.str.5 = [18 x i8] c"num drivers = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32 (i8*)*
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @usage(i8* %15)
store i32 1, i32* %3
br label %84
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 71
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = load i32, i32* @RTLD_NOW
%27 = load i32, i32* @RTLD_GLOBAL
%28 = or i32 %26, %27
%29 = call i8* @dlopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %28)
store i8* %29, i8** %6
br label %47
30: ; preds = %17
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 78
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = load i32, i32* @RTLD_NOW
%40 = call i8* @dlopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %39)
store i8* %40, i8** %6
br label %46
41: ; preds = %30
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 0
%44 = load i8*, i8** %43
%45 = call i32 @usage(i8* %44)
store i32 1, i32* %3
br label %84
46: ; preds = %38
br label %47
47: ; preds = %46, %25
%48 = load i8*, i8** %6
%49 = icmp ne i8* %48, null
br i1 %49, label %52, label %50
50: ; preds = %47
%51 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %84
52: ; preds = %47
%53 = load i8*, i8** %6
%54 = call i8* @dlsym(i8* %53, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%55 = bitcast i32 (i8*)** %7 to i8**
store i8* %54, i8** %55
%56 = call i8* (...) @dlerror()
store i8* %56, i8** %8
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %62
58: ; preds = %52
%59 = load i32, i32* @stderr
%60 = load i8*, i8** %8
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %60)
store i32 1, i32* %3
br label %84
62: ; preds = %52
%63 = load i32 (i8*)*, i32 (i8*)** %7
%64 = load i8**, i8*** %5
%65 = getelementptr inbounds i8*, i8** %64, i64 2
%66 = load i8*, i8** %65
%67 = call i32 %63(i8* %66)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = icmp slt i32 %68, 1
br i1 %69, label %70, label %76
70: ; preds = %62
%71 = load i32, i32* @stderr
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 2
%74 = load i8*, i8** %73
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.4, i64 0, i64 0), i8* %74)
store i32 1, i32* %3
br label %84
76: ; preds = %62
%77 = load i32, i32* @stdout
%78 = load i32 (i8*)*, i32 (i8*)** %7
%79 = load i8**, i8*** %5
%80 = getelementptr inbounds i8*, i8** %79, i64 2
%81 = load i8*, i8** %80
%82 = call i32 %78(i8* %81)
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %82)
store i32 0, i32* %3
br label %84
84: ; preds = %76, %70, %58, %50, %41, %12
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
@.str = [30 x i8] c"./.libs/libtest_dbi_plugin.so\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [34 x i8] c"Failed to load test_dbi_plugin.so\00"
@.str.2 = [8 x i8] c"init_db\00"
@stderr = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [73 x i8] c"Either you have no drivers in %s , or there was a problem loading them.\0A\00"
@stdout = global i32 0
@.str.5 = [18 x i8] c"num drivers = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @usage(i8* %5)
br label %48
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i8, i8* %9
switch i8 %10, label %17 [
i8 71, label %11
i8 78, label %15
]
11: ; preds = %7
%12 = load i32, i32* @RTLD_NOW
%13 = load i32, i32* @RTLD_GLOBAL
%14 = or i32 %13, %12
br label %20
15: ; preds = %7
%16 = load i32, i32* @RTLD_NOW
br label %20
17: ; preds = %7
%18 = load i8*, i8** %1
%19 = tail call i32 @usage(i8* %18)
br label %48
20: ; preds = %15, %11
%21 = phi i32 [ %16, %15 ], [ %14, %11 ]
%22 = tail call i8* @dlopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %48
26: ; preds = %20
%27 = tail call i8* @dlsym(i8* %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%28 = bitcast i8* %27 to i32 (i8*)*
%29 = tail call i8* (...) @dlerror()
%30 = icmp eq i8* %29, null
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %29)
br label %48
34: ; preds = %26
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = tail call i32 %28(i8* %36)
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %35
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.4, i64 0, i64 0), i8* %41)
br label %48
43: ; preds = %34
%44 = load i32, i32* @stdout
%45 = load i8*, i8** %35
%46 = tail call i32 %28(i8* %45)
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %31, %43, %39, %24, %17, %4
%49 = phi i32 [ 1, %4 ], [ 1, %24 ], [ 1, %17 ], [ 1, %31 ], [ 1, %39 ], [ 0, %43 ]
ret i32 %49
}