#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int errno;
int execve(char const *, char *const *, char *const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int execute(const char *filename, const char *argv[], const char *env[]) {
  pid_t pid;
  int status = 0;
  pid = fork();
  if (!pid) {
    int result = execve(filename, (char *const *)argv, (char *const *)env);
    result = errno;
    if (result > 0) {
      result = -result;
    }
    exit(-1);
  } else {
    waitpid(pid, &status, 0);
    if (WIFEXITED(status))
      return WEXITSTATUS(status);
    else
      return -1;
  }
}


x86 assembly (clang, -O0):
<execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11cb <execute+0x6b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <execve@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11c1 <execute+0x61>
xor    %eax,%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x28(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11fb <execute+0x9b>
mov    -0x28(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    1202 <execute+0xa2>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11bb <execute+0x5b>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     11ac <execute+0x4c>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
jmp    11b1 <execute+0x51>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <execve@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
define i32 @execute(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 0, i32* %9
%11 = call i32 (...) @fork()
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 0
br i1 %13, label %27, label %14
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i8**, i8*** %6
%17 = load i8**, i8*** %7
%18 = call i32 @execve(i8* %15, i8** %16, i8** %17)
store i32 %18, i32* %10
%19 = load i32, i32* @errno
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i32, i32* %10
%24 = sub nsw i32 0, %23
store i32 %24, i32* %10
br label %25
25: ; preds = %22, %14
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %3
%28 = load i32, i32* %8
%29 = call i32 @waitpid(i32 %28, i32* %9, i32 0)
%30 = load i32, i32* %9
%31 = call i64 @WIFEXITED(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i32, i32* %9
%35 = call i32 @WEXITSTATUS(i32 %34)
store i32 %35, i32* %4
br label %37
36: ; preds = %27
store i32 -1, i32* %4
br label %37
37: ; preds = %36, %33
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @execute(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%6 = tail call i32 (...) @fork()
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 @execve(i8* %0, i8** %1, i8** %2)
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %3
%12 = call i32 @waitpid(i32 %6, i32* %4, i32 0)
%13 = load i32, i32* %4
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* %4
%18 = call i32 @WEXITSTATUS(i32 %17)
br label %19
19: ; preds = %11, %16
%20 = phi i32 [ %18, %16 ], [ -1, %11 ]
ret i32 %20
}