#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 **);
int execv(const char *filename, char *const argv[]) {
  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 **);
  func = (int (*)(const char *, char **))dlsym(handle, "execv");
  return (*func)(filename, (char **)argv);
}


x86 assembly (clang, -O0):
<execv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2eb1(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
lea    0xeb7(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117f <execv+0x5f>
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1030 <dlopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1040 <dlsym@plt>
mov    %rax,0x2eaa(%rip)
mov    0x2ea3(%rip),%rax
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   *%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execv>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eb3(%rip),%r15
mov    0x2e94(%rip),%r12
mov    (%r12),%esi
or     (%r15),%esi
lea    0xeb6(%rip),%rdi
call   1030 <dlopen@plt>
test   %rax,%rax
jne    1167 <execv+0x47>
mov    (%r12),%esi
or     (%r15),%esi
lea    0xeaf(%rip),%rdi
call   1030 <dlopen@plt>
lea    0xeb2(%rip),%rsi
mov    %rax,%rdi
call   1040 <dlsym@plt>
mov    %rax,0x2ec3(%rip)
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
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"
@execv.func = internal global i32 (i8*, i8**)* null
@.str.2 = [6 x i8] c"execv\00"
define i32 @execv(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i8*
store i8* %0, i8** %3
store i8** %1, i8*** %4
%6 = load i32, i32* @RTLD_NOW
%7 = load i32, i32* @RTLD_LOCAL
%8 = or i32 %6, %7
%9 = call i8* @dlopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @RTLD_NOW
%14 = load i32, i32* @RTLD_LOCAL
%15 = or i32 %13, %14
%16 = call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i8* %16, i8** %5
br label %17
17: ; preds = %12, %2
%18 = load i8*, i8** %5
%19 = call i64 @dlsym(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%20 = inttoptr i64 %19 to i32 (i8*, i8**)*
store i32 (i8*, i8**)* %20, i32 (i8*, i8**)** @execv.func
%21 = load i32 (i8*, i8**)*, i32 (i8*, i8**)** @execv.func
%22 = load i8*, i8** %3
%23 = load i8**, i8*** %4
%24 = call i32 %21(i8* %22, i8** %23)
ret i32 %24
}

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