#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*/ ENOMEM;
char **clone_env(char *const *, size_t *, size_t *, size_t *, size_t *,
                 size_t *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free_env(char **, size_t, size_t, size_t, size_t, size_t);
int /*<<< orphan*/ init_execvpe();
int orig_execvpe(char const *, char *const *, char **);
int /*<<< orphan*/ report_exec(char const *);
int my_execvpe(char const *prog, char *const *argv, char *const *env) {
  int retval = -1;
  char **new_env = NULL;
  size_t ldp_pos, dil_pos, dffns_pos, lmtf_pos, lmpl_pos;
  init_execvpe();
  report_exec(prog);
  new_env =
      clone_env(env, &ldp_pos, &dil_pos, &dffns_pos, &lmtf_pos, &lmpl_pos);
  if (new_env == NULL) {
    errno = ENOMEM;
    goto error;
  }
  retval = orig_execvpe(prog, argv, new_env);
error:
  free_env(new_env, ldp_pos, dil_pos, dffns_pos, lmtf_pos, lmpl_pos);
  return retval;
}


x86 assembly (clang, -O0):
<my_execvpe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1060 <init_execvpe@plt>
mov    -0x8(%rbp),%rdi
call   1070 <report_exec@plt>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
lea    -0x48(%rbp),%r8
lea    -0x50(%rbp),%r9
call   1030 <clone_env@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <my_execvpe+0x76>
mov    0x2e2a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e19(%rip),%rax
mov    %ecx,(%rax)
jmp    11da <my_execvpe+0x8a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <orig_execvpe@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x48(%rbp),%r8
mov    -0x50(%rbp),%r9
call   1050 <free_env@plt>
mov    -0x1c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_execvpe>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1060 <init_execvpe@plt>
mov    %r15,%rdi
call   1070 <report_exec@plt>
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rsp,%r9
mov    %rbx,%rdi
call   1030 <clone_env@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ab <my_execvpe+0x5b>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <orig_execvpe@plt>
mov    %eax,%ebp
jmp    11c2 <my_execvpe+0x72>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    0x2e1d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    0x20(%rsp),%rsi
mov    0x18(%rsp),%rdx
mov    0x10(%rsp),%rcx
mov    0x8(%rsp),%r8
mov    (%rsp),%r9
mov    %rbx,%rdi
call   1050 <free_env@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @my_execvpe(i8* %0, i8** %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
store i32 -1, i32* %7
store i8** null, i8*** %8
%14 = call i32 (...) @init_execvpe()
%15 = load i8*, i8** %4
%16 = call i32 @report_exec(i8* %15)
%17 = load i8**, i8*** %6
%18 = call i8** @clone_env(i8** %17, i64* %9, i64* %10, i64* %11, i64* %12, i64* %13)
store i8** %18, i8*** %8
%19 = load i8**, i8*** %8
%20 = icmp eq i8** %19, null
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = load i32, i32* @ENOMEM
store i32 %22, i32* @errno
br label %28
23: ; preds = %3
%24 = load i8*, i8** %4
%25 = load i8**, i8*** %5
%26 = load i8**, i8*** %8
%27 = call i32 @orig_execvpe(i8* %24, i8** %25, i8** %26)
store i32 %27, i32* %7
br label %28
28: ; preds = %23, %21
%29 = load i8**, i8*** %8
%30 = load i64, i64* %9
%31 = load i64, i64* %10
%32 = load i64, i64* %11
%33 = load i64, i64* %12
%34 = load i64, i64* %13
%35 = call i32 @free_env(i8** %29, i64 %30, i64 %31, i64 %32, i64 %33, i64 %34)
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @my_execvpe(i8* %0, i8** %1, i8** %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = bitcast i64* %4 to i8*
%10 = bitcast i64* %5 to i8*
%11 = bitcast i64* %6 to i8*
%12 = bitcast i64* %7 to i8*
%13 = bitcast i64* %8 to i8*
%14 = tail call i32 (...) @init_execvpe()
%15 = tail call i32 @report_exec(i8* %0)
%16 = call i8** @clone_env(i8** %2, i64* %4, i64* %5, i64* %6, i64* %7, i64* %8)
%17 = icmp eq i8** %16, null
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i32, i32* @ENOMEM
store i32 %19, i32* @errno
br label %22
20: ; preds = %3
%21 = call i32 @orig_execvpe(i8* %0, i8** %1, i8** %16)
br label %22
22: ; preds = %20, %18
%23 = phi i32 [ -1, %18 ], [ %21, %20 ]
%24 = load i64, i64* %4
%25 = load i64, i64* %5
%26 = load i64, i64* %6
%27 = load i64, i64* %7
%28 = load i64, i64* %8
%29 = call i32 @free_env(i8** %16, i64 %24, i64 %25, i64 %26, i64 %27, i64 %28)
ret i32 %23
}