#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int orig_execv(char const *, char *const *);
int orig_execve(char const *, char *const *, char *const *);
int orig_execvp(char const *, char *const *);
int orig_execvpe(char const *, char *const *, char *const *);
int /*<<< orphan*/ stderr;
int wrapper(const int __type, const char *__path, char *const __argv[],
            char *const __envp[]) {
  char *argv = __argv[0];
  int i = 0;
  while (argv != NULL) {
    fprintf(stderr, "%s: argv[%d]: [[%s]]\n", __path, i++, argv);
    argv = __argv[i];
  }
  switch (__type) {
  case 1:
    return orig_execv(__path, __argv);
  case 2:
    return orig_execvp(__path, __argv);
  case 3:
    return orig_execve(__path, __argv, __envp);
  case 4:
    return orig_execvpe(__path, __argv, __envp);
  default:
    fprintf(
        stderr,
        "execvhack.so: invalid given function type (%d); error in source code.",
        __type);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<wrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d3 <wrapper+0x73>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%r8
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
jmp    1189 <wrapper+0x29>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x3,%eax
ja     1258 <wrapper+0xf8>
mov    -0x38(%rbp),%rax
lea    0xe0d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <orig_execv@plt>
mov    %eax,-0x4(%rbp)
jmp    127c <wrapper+0x11c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <orig_execvp@plt>
mov    %eax,-0x4(%rbp)
jmp    127c <wrapper+0x11c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <orig_execve@plt>
mov    %eax,-0x4(%rbp)
jmp    127c <wrapper+0x11c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <orig_execvpe@plt>
mov    %eax,-0x4(%rbp)
jmp    127c <wrapper+0x11c>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wrapper>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x8(%rsp)
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,0x10(%rsp)
mov    (%rdx),%r8
test   %r8,%r8
je     11c3 <wrapper+0x63>
mov    0x2e53(%rip),%r14
lea    0xe7c(%rip),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%edi
lea    0x1(%rbp),%r12
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%r15,%rbp,8),%r8
mov    %r12,%rbp
test   %r8,%r8
jne    11a0 <wrapper+0x40>
mov    0x10(%rsp),%rdx
lea    -0x1(%rdx),%eax
cmp    $0x3,%eax
ja     124e <wrapper+0xee>
lea    0xe29(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    %rbx,%rdi
mov    %r15,%rsi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <orig_execv@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    0x8(%rsp),%rdx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <orig_execve@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <orig_execvp@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    0x8(%rsp),%rdx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <orig_execvpe@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"%s: argv[%d]: [[%s]]\0A\00"
@.str.1 = [70 x i8] c"execvhack.so: invalid given function type (%d); error in source code.\00"
define i32 @wrapper(i32 %0, i8* %1, i8** %2, i8** %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8** %2, i8*** %8
store i8** %3, i8*** %9
%12 = load i8**, i8*** %8
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
store i8* %14, i8** %10
store i32 0, i32* %11
br label %15
15: ; preds = %18, %4
%16 = load i8*, i8** %10
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %30
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %7
%21 = load i32, i32* %11
%22 = add nsw i32 %21, 1
store i32 %22, i32* %11
%23 = load i8*, i8** %10
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21, i8* %23)
%25 = load i8**, i8*** %8
%26 = load i32, i32* %11
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %25, i64 %27
%29 = load i8*, i8** %28
store i8* %29, i8** %10
br label %15
30: ; preds = %15
%31 = load i32, i32* %6
switch i32 %31, label %50 [
i32 1, label %32
i32 2, label %36
i32 3, label %40
i32 4, label %45
]
32: ; preds = %30
%33 = load i8*, i8** %7
%34 = load i8**, i8*** %8
%35 = call i32 @orig_execv(i8* %33, i8** %34)
store i32 %35, i32* %5
br label %55
36: ; preds = %30
%37 = load i8*, i8** %7
%38 = load i8**, i8*** %8
%39 = call i32 @orig_execvp(i8* %37, i8** %38)
store i32 %39, i32* %5
br label %55
40: ; preds = %30
%41 = load i8*, i8** %7
%42 = load i8**, i8*** %8
%43 = load i8**, i8*** %9
%44 = call i32 @orig_execve(i8* %41, i8** %42, i8** %43)
store i32 %44, i32* %5
br label %55
45: ; preds = %30
%46 = load i8*, i8** %7
%47 = load i8**, i8*** %8
%48 = load i8**, i8*** %9
%49 = call i32 @orig_execvpe(i8* %46, i8** %47, i8** %48)
store i32 %49, i32* %5
br label %55
50: ; preds = %30
%51 = load i32, i32* @stderr
%52 = load i32, i32* %6
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0), i32 %52)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %45, %40, %36, %32
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"%s: argv[%d]: [[%s]]\0A\00"
@.str.1 = [70 x i8] c"execvhack.so: invalid given function type (%d); error in source code.\00"
define i32 @wrapper(i32 %0, i8* %1, i8** %2, i8** %3) {
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %17, label %7
7: ; preds = %4, %7
%8 = phi i64 [ %11, %7 ], [ 0, %4 ]
%9 = phi i8* [ %15, %7 ], [ %5, %4 ]
%10 = load i32, i32* @stderr
%11 = add nuw i64 %8, 1
%12 = trunc i64 %8 to i32
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %12, i8* %9)
%14 = getelementptr inbounds i8*, i8** %2, i64 %11
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %7
17: ; preds = %7, %4
switch i32 %0, label %26 [
i32 1, label %18
i32 2, label %20
i32 3, label %22
i32 4, label %24
]
18: ; preds = %17
%19 = tail call i32 @orig_execv(i8* %1, i8** %2)
br label %30
20: ; preds = %17
%21 = tail call i32 @orig_execvp(i8* %1, i8** %2)
br label %30
22: ; preds = %17
%23 = tail call i32 @orig_execve(i8* %1, i8** %2, i8** %3)
br label %30
24: ; preds = %17
%25 = tail call i32 @orig_execvpe(i8* %1, i8** %2, i8** %3)
br label %30
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %24, %22, %20, %18
%31 = phi i32 [ %25, %24 ], [ %23, %22 ], [ %21, %20 ], [ %19, %18 ]
ret i32 %31
}