#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;
void *dlopen(char *, int);
scalar_t__ dlsym(void *, char *);
int stub1(char const *, char **, char **);
int execve(const char *filename, char *const argv[], char *const envp[]) {
  void *handle = dlopen("/lib64/libc.so.6", RTLD_NOW | RTLD_LOCAL);
  if (handle == NULL)
    handle = dlopen("/lib/libc.so.6", RTLD_NOW | RTLD_LOCAL);
  static int (*func)(const char *, char **, char **);
  func = (int (*)(const char *, char **, char **))dlsym(handle, "execve");
  return (*func)(filename, (char **)argv, (char **)envp);
}


x86 assembly (clang, -O0):
<execve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%esi
mov    0x2e8c(%rip),%rax
or     (%rax),%esi
lea    0xeb3(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1183 <execve+0x63>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
lea    0xe97(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1040 <dlsym@plt>
mov    %rax,0x2ea6(%rip)
mov    0x2e9f(%rip),%rax
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   *%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execve>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2eaf(%rip),%r12
mov    0x2e90(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
lea    0xeb1(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
jne    116d <execve+0x4d>
mov    0x0(%r13),%esi
or     (%r12),%esi
lea    0xea9(%rip),%rdi
call   1030 <dlopen@plt>
lea    0xeac(%rip),%rsi
mov    %rax,%rdi
call   1040 <dlsym@plt>
mov    %rax,0x2ebd(%rip)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    *%rax

LLVM IR (-O0):
@.str = [17 x i8] c"/lib64/libc.so.6\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.1 = [15 x i8] c"/lib/libc.so.6\00"
@execve.func = internal global i32 (i8*, i8**, i8**)* null
@.str.2 = [7 x i8] c"execve\00"
define i32 @execve(i8* %0, i8** %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
%8 = load i32, i32* @RTLD_NOW
%9 = load i32, i32* @RTLD_LOCAL
%10 = or i32 %8, %9
%11 = call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i32, i32* @RTLD_NOW
%16 = load i32, i32* @RTLD_LOCAL
%17 = or i32 %15, %16
%18 = call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i8* %18, i8** %7
br label %19
19: ; preds = %14, %3
%20 = load i8*, i8** %7
%21 = call i64 @dlsym(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%22 = inttoptr i64 %21 to i32 (i8*, i8**, i8**)*
store i32 (i8*, i8**, i8**)* %22, i32 (i8*, i8**, i8**)** @execve.func
%23 = load i32 (i8*, i8**, i8**)*, i32 (i8*, i8**, i8**)** @execve.func
%24 = load i8*, i8** %4
%25 = load i8**, i8*** %5
%26 = load i8**, i8*** %6
%27 = call i32 %23(i8* %24, i8** %25, i8** %26)
ret i32 %27
}

LLVM IR (-O2):
@.str = [17 x i8] c"/lib64/libc.so.6\00"
@RTLD_NOW = global i32 0
@RTLD_LOCAL = global i32 0
@.str.1 = [15 x i8] c"/lib/libc.so.6\00"
@execve.func = internal unnamed_addr global i32 (i8*, i8**, i8**)* null
@.str.2 = [7 x i8] c"execve\00"
define i32 @execve(i8* %0, i8** %1, i8** %2) {
%4 = load i32, i32* @RTLD_NOW
%5 = load i32, i32* @RTLD_LOCAL
%6 = or i32 %5, %4
%7 = tail call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = load i32, i32* @RTLD_NOW
%11 = load i32, i32* @RTLD_LOCAL
%12 = or i32 %11, %10
%13 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %9, %3
%15 = phi i8* [ %13, %9 ], [ %7, %3 ]
%16 = tail call i64 @dlsym(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%17 = inttoptr i64 %16 to i32 (i8*, i8**, i8**)*
store i32 (i8*, i8**, i8**)* %17, i32 (i8*, i8**, i8**)** @execve.func
%18 = tail call i32 %17(i8* %0, i8** %1, i8** %2)
ret i32 %18
}