#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_LAZY;
int check_int32(void *, char *, int);
int check_int8(void *, char *, int);
char *dlerror();
void *dlopen(char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  void *handle = dlopen(argv[1], RTLD_LAZY | RTLD_GLOBAL);
  if (!handle) {
    fprintf(stderr, "ERROR: failed to open the shared library: %s\n",
            dlerror());
    return 2;
  }
  int ret = 0;
  ret = check_int8(handle, "DidInitRun", 1);
  if (ret != 0) {
    return ret;
  }
  ret = check_int8(handle, "DidMainRun", 0);
  if (ret != 0) {
    return ret;
  }
  ret = check_int32(handle, "FromPkg", 1024);
  if (ret != 0) {
    return ret;
  }
  printf("PASS\n");
  return 0;
}


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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
or     (%rax),%esi
call   1060 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d5 <main+0x75>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe3c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1273 <main+0x113>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe47(%rip),%rsi
mov    $0x1,%edx
call   1080 <check_int8@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1209 <main+0xa9>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1273 <main+0x113>
mov    -0x18(%rbp),%rdi
lea    0xe25(%rip),%rsi
xor    %edx,%edx
call   1080 <check_int8@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1233 <main+0xd3>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1273 <main+0x113>
mov    -0x18(%rbp),%rdi
lea    0xe06(%rip),%rsi
mov    $0x400,%edx
call   1040 <check_int32@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1260 <main+0x100>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1273 <main+0x113>
lea    0xde5(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x8(%rsi),%rdi
mov    0x2e64(%rip),%rax
mov    0x2e75(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <dlopen@plt>
test   %rax,%rax
je     119e <main+0x3e>
mov    %rax,%rbx
lea    0xea3(%rip),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
call   1080 <check_int8@plt>
test   %eax,%eax
je     11c6 <main+0x66>
pop    %rbx
ret
mov    0x2e33(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe4b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x2,%eax
pop    %rbx
ret
lea    0xe6c(%rip),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <check_int8@plt>
test   %eax,%eax
jne    119c <main+0x3c>
lea    0xe62(%rip),%rsi
mov    %rbx,%rdi
mov    $0x400,%edx
call   1050 <check_int32@plt>
test   %eax,%eax
jne    119c <main+0x3c>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@RTLD_GLOBAL = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"ERROR: failed to open the shared library: %s\0A\00"
@.str.1 = [11 x i8] c"DidInitRun\00"
@.str.2 = [11 x i8] c"DidMainRun\00"
@.str.3 = [8 x i8] c"FromPkg\00"
@.str.4 = [6 x i8] c"PASS\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @RTLD_LAZY
%12 = load i32, i32* @RTLD_GLOBAL
%13 = or i32 %11, %12
%14 = call i8* @dlopen(i8* %10, i32 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i8* (...) @dlerror()
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 2, i32* %3
br label %44
21: ; preds = %2
store i32 0, i32* %7
%22 = load i8*, i8** %6
%23 = call i32 @check_int8(i8* %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load i32, i32* %7
store i32 %27, i32* %3
br label %44
28: ; preds = %21
%29 = load i8*, i8** %6
%30 = call i32 @check_int8(i8* %29, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 0)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = load i32, i32* %7
store i32 %34, i32* %3
br label %44
35: ; preds = %28
%36 = load i8*, i8** %6
%37 = call i32 @check_int32(i8* %36, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 1024)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = load i32, i32* %7
store i32 %41, i32* %3
br label %44
42: ; preds = %35
%43 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %44
44: ; preds = %42, %40, %33, %26, %17
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
@RTLD_GLOBAL = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"ERROR: failed to open the shared library: %s\0A\00"
@.str.1 = [11 x i8] c"DidInitRun\00"
@.str.2 = [11 x i8] c"DidMainRun\00"
@.str.3 = [8 x i8] c"FromPkg\00"
@str = [5 x i8] c"PASS\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i32, i32* @RTLD_LAZY
%6 = load i32, i32* @RTLD_GLOBAL
%7 = or i32 %6, %5
%8 = tail call i8* @dlopen(i8* %4, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i8* (...) @dlerror()
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %25
14: ; preds = %2
%15 = tail call i32 @check_int8(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 1)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = tail call i32 @check_int8(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = tail call i32 @check_int32(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 1024)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %14, %17, %20, %10
%26 = phi i32 [ 2, %10 ], [ 0, %23 ], [ %15, %14 ], [ %18, %17 ], [ %21, %20 ]
ret i32 %26
}