#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 EXIT_SUCCESS;
int /*<<< orphan*/ RTLD_NOW;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char const *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, const char *argv[]) {
  if (argc != 2) {
    printf("Usage: ./dlopentest <path_to_pam_module>\n");
    exit(1);
  }
  void *libhandle = dlopen(argv[1], RTLD_NOW);
  if (libhandle == NULL) {
    printf("%s\n", dlerror());
    exit(1);
  }
  void *sym = dlsym(libhandle, "pam_sm_authenticate");
  if (sym == NULL) {
    printf("%s\n", dlerror());
    exit(1);
  }
  sym = dlsym(libhandle, "pam_sm_setcred");
  if (sym == NULL) {
    printf("%s\n", dlerror());
    exit(1);
  }
  dlclose(libhandle);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1198 <main+0x38>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11df <main+0x7f>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1220 <main+0xc0>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1261 <main+0x101>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1080 <dlclose@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    11c7 <main+0x67>
mov    0x8(%rsi),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
test   %rax,%rax
je     11bb <main+0x5b>
mov    %rax,%rbx
lea    0xe79(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11bb <main+0x5b>
lea    0xe79(%rip),%rsi
mov    %rbx,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11bb <main+0x5b>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    %rax,%rdi
jmp    11ce <main+0x6e>
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"Usage: ./dlopentest <path_to_pam_module>\0A\00"
@RTLD_NOW = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [20 x i8] c"pam_sm_authenticate\00"
@.str.3 = [15 x i8] c"pam_sm_setcred\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @RTLD_NOW
%18 = call i8* @dlopen(i8* %16, i32 %17)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = call i8* (...) @dlerror()
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %13
%26 = load i8*, i8** %6
%27 = call i8* @dlsym(i8* %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = call i8* (...) @dlerror()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %25
%35 = load i8*, i8** %6
%36 = call i8* @dlsym(i8* %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = call i8* (...) @dlerror()
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %34
%44 = load i8*, i8** %6
%45 = call i32 @dlclose(i8* %44)
%46 = load i32, i32* @EXIT_SUCCESS
ret i32 %46
}

LLVM IR (-O2):
@RTLD_NOW = global i32 0
@.str.2 = [20 x i8] c"pam_sm_authenticate\00"
@.str.3 = [15 x i8] c"pam_sm_setcred\00"
@EXIT_SUCCESS = global i32 0
@str = [41 x i8] c"Usage: ./dlopentest <path_to_pam_module>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @RTLD_NOW
%11 = tail call i8* @dlopen(i8* %9, i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = tail call i8* (...) @dlerror()
%15 = tail call i32 @puts(i8* dereferenceable(1) %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %7
%18 = tail call i8* @dlsym(i8* %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = tail call i8* (...) @dlerror()
%22 = tail call i32 @puts(i8* dereferenceable(1) %21)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = tail call i8* @dlsym(i8* %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = tail call i8* (...) @dlerror()
%29 = tail call i32 @puts(i8* dereferenceable(1) %28)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = tail call i32 @dlclose(i8* %11)
%33 = load i32, i32* @EXIT_SUCCESS
ret i32 %33
}