#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 */
struct stat {
  int st_mode;
};

/* Variables and functions */
int S_IEXEC;
void *dlsym_libc(char *);
char *const *newargv(char const *, char *const *);
char const *observe_handler();
char *const *reset_LD_PRELOAD(char *const *);
scalar_t__ stat(char const *, struct stat *);
int execve(const char *path, char *const argv[], char *const envp[]) {
  int (*actual)(const char *path, char *const argv[], char *const envp[]);
  *(void **)(&actual) = dlsym_libc("execve");
  if (actual == NULL)
    return -1;
  {
    struct stat st;
    if (stat(path, &st) != 0 || (st.st_mode & S_IEXEC) == 0)
      return -1;
  };
  ;
  ;
  ;
  if (actual(observe_handler(), newargv(path, argv), reset_LD_PRELOAD(envp)) ==
      0)
    return 0;
  ;
  ;
  return -1;
}


x86 assembly (clang, -O0):
<execve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe95(%rip),%rdi
call   1070 <dlsym_libc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118b <execve+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <execve+0xcd>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    11b7 <execve+0x67>
mov    -0x30(%rbp),%eax
mov    0x2e34(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11c3 <execve+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <execve+0xcd>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1060 <observe_handler@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <newargv@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <reset_LD_PRELOAD@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
mov    -0x38(%rbp),%rax
call   *%rax
cmp    $0x0,%eax
jne    1216 <execve+0xc6>
movl   $0x0,-0x4(%rbp)
jmp    121d <execve+0xcd>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execve>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe95(%rip),%rdi
call   1070 <dlsym_libc@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11ca <execve+0x7a>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %r12,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    11ca <execve+0x7a>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rsp)
je     11ca <execve+0x7a>
xor    %eax,%eax
call   1060 <observe_handler@plt>
mov    %rax,%r13
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <newargv@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <reset_LD_PRELOAD@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
call   *%rbx
neg    %eax
sbb    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"execve\00"
@S_IEXEC = global i32 0
define i32 @execve(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32 (i8*, i8**, i8**)*
%9 = alloca %struct.stat
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%10 = call i8* @dlsym_libc(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = bitcast i32 (i8*, i8**, i8**)** %8 to i8**
store i8* %10, i8** %11
%12 = load i32 (i8*, i8**, i8**)*, i32 (i8*, i8**, i8**)** %8
%13 = icmp eq i32 (i8*, i8**, i8**)* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %38
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i64 @stat(i8* %16, %struct.stat* %9)
%18 = icmp ne i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @S_IEXEC
%23 = and i32 %21, %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19, %15
store i32 -1, i32* %4
br label %38
26: ; preds = %19
%27 = load i32 (i8*, i8**, i8**)*, i32 (i8*, i8**, i8**)** %8
%28 = call i8* (...) @observe_handler()
%29 = load i8*, i8** %5
%30 = load i8**, i8*** %6
%31 = call i8** @newargv(i8* %29, i8** %30)
%32 = load i8**, i8*** %7
%33 = call i8** @reset_LD_PRELOAD(i8** %32)
%34 = call i32 %27(i8* %28, i8** %31, i8** %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %26
store i32 0, i32* %4
br label %38
37: ; preds = %26
store i32 -1, i32* %4
br label %38
38: ; preds = %37, %36, %25, %14
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"execve\00"
@S_IEXEC = global i32 0
define i32 @execve(i8* %0, i8** %1, i8** %2) {
%4 = alloca %struct.stat
%5 = tail call i8* @dlsym_libc(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = bitcast i8* %5 to i32 (i8*, i8**, i8**)*
%7 = icmp eq i8* %5, null
br i1 %7, label %26, label %8
8: ; preds = %3
%9 = bitcast %struct.stat* %4 to i8*
%10 = call i64 @stat(i8* %0, %struct.stat* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @S_IEXEC
%16 = and i32 %15, %14
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %8, %12
br label %26
19: ; preds = %12
%20 = call i8* (...) @observe_handler()
%21 = call i8** @newargv(i8* %0, i8** %1)
%22 = call i8** @reset_LD_PRELOAD(i8** %2)
%23 = call i32 %6(i8* %20, i8** %21, i8** %22)
%24 = icmp ne i32 %23, 0
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %19, %18, %3
%27 = phi i32 [ -1, %18 ], [ -1, %3 ], [ %25, %19 ]
ret i32 %27
}