#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 scalar_t__ pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
int errno;
int /*<<< orphan*/ execvp(char const *, char *const *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int execvpfw(const char *path, char *const argv[]) {
  int r;
  pid_t pid = fork();
  if (pid) {
    waitpid(pid, &r, 0);
    return WEXITSTATUS(r);
  } else {
    execvp(path, argv);
    fprintf(stderr, "Errno: %i\n", errno);
    exit(errno ? errno : -1);
  }
}


x86 assembly (clang, -O0):
<execvpfw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a3 <execvpfw+0x43>
mov    -0x20(%rbp),%rdi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x14(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <execvp@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
lea    0xe37(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e03(%rip),%rax
cmpl   $0x0,(%rax)
je     11ef <execvpfw+0x8f>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    11fc <execvpfw+0x9c>
mov    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11fc <execvpfw+0x9c>
mov    -0x24(%rbp),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<execvpfw>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     1196 <execvpfw+0x36>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rbx
mov    (%rbx),%edx
lea    0xe46(%rip),%rsi
call   1030 <fprintf@plt>
mov    (%rbx),%eax
cmp    $0x1,%eax
sbb    %edi,%edi
or     %eax,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [11 x i8] c"Errno: %i\0A\00"
@errno = global i32 0
define i32 @execvpfw(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %3
store i8** %1, i8*** %4
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i64, i64* %6
%12 = call i32 @waitpid(i64 %11, i32* %5, i32 0)
%13 = load i32, i32* %5
%14 = call i32 @WEXITSTATUS(i32 %13)
ret i32 %14
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = load i8**, i8*** %4
%18 = call i32 @execvp(i8* %16, i8** %17)
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @errno
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = load i32, i32* @errno
br label %27
26: ; preds = %15
br label %27
27: ; preds = %26, %24
%28 = phi i32 [ %25, %24 ], [ -1, %26 ]
%29 = call i32 @exit(i32 %28)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [11 x i8] c"Errno: %i\0A\00"
@errno = global i32 0
define i32 @execvpfw(i8* %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = call i32 @waitpid(i64 %5, i32* %3, i32 0)
%9 = load i32, i32* %3
%10 = call i32 @WEXITSTATUS(i32 %9)
ret i32 %10
11: ; preds = %2
%12 = tail call i32 @execvp(i8* %0, i8** %1)
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* @errno
%17 = icmp eq i32 %16, 0
%18 = select i1 %17, i32 -1, i32 %16
%19 = tail call i32 @exit(i32 %18)
unreachable
}