#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_LOCAL;
int RTLD_NOW;
int /*<<< orphan*/ dlclose(void *);
void *dlopen(char *, int);
void *dlsym(void *, char *);
int dynegg(char *cmd) {
  void *h, *f;
  int (*func)(char *), r;
  h = dlopen("/fat-dragon/lib/resh"
             "/eggbasket.so",
             RTLD_NOW | RTLD_LOCAL);
  if (h == NULL)
    return (0);
  f = dlsym(h, "tryegg");
  if (f == NULL) {
    dlclose(h);
    return (0);
  }
  func = (int (*)(char *))f;
  r = func(cmd);
  dlclose(h);
  return (r);
}


x86 assembly (clang, -O0):
<dynegg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
mov    0x2e84(%rip),%rax
or     (%rax),%esi
lea    0xeab(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1175 <dynegg+0x45>
movl   $0x0,-0x4(%rbp)
jmp    11cd <dynegg+0x9d>
mov    -0x18(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1040 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a9 <dynegg+0x79>
mov    -0x18(%rbp),%rdi
call   1050 <dlclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cd <dynegg+0x9d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rdi
call   *%rax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <dlclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dynegg>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2eaa(%rip),%rax
mov    0x2e8b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xeb0(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
je     117a <dynegg+0x4a>
mov    %rax,%rbx
lea    0xebe(%rip),%rsi
mov    %rax,%rdi
call   1040 <dlsym@plt>
test   %rax,%rax
je     117e <dynegg+0x4e>
mov    %r14,%rdi
call   *%rax
mov    %eax,%ebp
jmp    1180 <dynegg+0x50>
xor    %ebp,%ebp
jmp    1188 <dynegg+0x58>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <dlclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"/fat-dragon/lib/resh/eggbasket.so\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.1 = [7 x i8] c"tryegg\00"
define i32 @dynegg(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32 (i8*)*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @RTLD_NOW
%9 = load i32, i32* @RTLD_LOCAL
%10 = or i32 %8, %9
%11 = call i8* @dlopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 0, i32* %2
br label %32
15: ; preds = %1
%16 = load i8*, i8** %4
%17 = call i8* @dlsym(i8* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = call i32 @dlclose(i8* %21)
store i32 0, i32* %2
br label %32
23: ; preds = %15
%24 = load i8*, i8** %5
%25 = bitcast i8* %24 to i32 (i8*)*
store i32 (i8*)* %25, i32 (i8*)** %6
%26 = load i32 (i8*)*, i32 (i8*)** %6
%27 = load i8*, i8** %3
%28 = call i32 %26(i8* %27)
store i32 %28, i32* %7
%29 = load i8*, i8** %4
%30 = call i32 @dlclose(i8* %29)
%31 = load i32, i32* %7
store i32 %31, i32* %2
br label %32
32: ; preds = %23, %20, %14
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [34 x i8] c"/fat-dragon/lib/resh/eggbasket.so\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.1 = [7 x i8] c"tryegg\00"
define i32 @dynegg(i8* %0) {
%2 = load i32, i32* @RTLD_NOW
%3 = load i32, i32* @RTLD_LOCAL
%4 = or i32 %3, %2
%5 = tail call i8* @dlopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %16, label %7
7: ; preds = %1
%8 = tail call i8* @dlsym(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = bitcast i8* %8 to i32 (i8*)*
%12 = tail call i32 %11(i8* %0)
br label %13
13: ; preds = %7, %10
%14 = phi i32 [ %12, %10 ], [ 0, %7 ]
%15 = tail call i32 @dlclose(i8* %5)
br label %16
16: ; preds = %13, %1
%17 = phi i32 [ 0, %1 ], [ %14, %13 ]
ret i32 %17
}