#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 /*<<< orphan*/ execvpe(char *, char **, char **);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  char *envs[] = {"NAME1=value1", "NAME2=value2", (char *)0};
  pid_t cpid;
  if ((cpid = fork()) == 0) {
    execvpe(argv[1], &argv[1], envs);
  } else if (cpid > 0) {
    int status;
    wait(&status);
    printf("Child status is %d\n", WEXITSTATUS(status));
  } else {
    perror("fork() failed");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c83(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c80(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c7d(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11ca <main+0x6a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
lea    -0x30(%rbp),%rdx
call   1030 <execvpe@plt>
jmp    120a <main+0xaa>
cmpq   $0x0,-0x38(%rbp)
jle    11f9 <main+0x99>
lea    -0x3c(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x3c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
jmp    1205 <main+0xa5>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
jmp    120a <main+0xaa>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    0x2ca1(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0x2c85(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11b1 <main+0x51>
jle    11ce <main+0x6e>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe72(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    11c6 <main+0x66>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
lea    0x10(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <execvpe@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
jmp    11c6 <main+0x66>

LLVM IR (-O0):
@.str = [13 x i8] c"NAME1=value1\00"
@.str.1 = [13 x i8] c"NAME2=value2\00"
@__const.main.envs = [3 x i8*] [i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [20 x i8] c"Child status is %d\0A\00"
@.str.3 = [14 x i8] c"fork() failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [3 x i8*]
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [3 x i8*]* %6 to i8*
%10 = call i64 (...) @fork()
store i64 %10, i64* %7
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
%19 = call i32 @execvpe(i8* %15, i8** %17, i8** %18)
br label %31
20: ; preds = %2
%21 = load i64, i64* %7
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = call i32 @wait(i32* %8)
%25 = load i32, i32* %8
%26 = call i32 @WEXITSTATUS(i32 %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %30
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
br label %31
31: ; preds = %30, %12
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str = [13 x i8] c"NAME1=value1\00"
@.str.1 = [13 x i8] c"NAME2=value2\00"
@__const.main.envs = [3 x i8*] [i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [20 x i8] c"Child status is %d\0A\00"
@.str.3 = [14 x i8] c"fork() failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [3 x i8*]
%4 = alloca i32
%5 = bitcast [3 x i8*]* %3 to i8*
%6 = tail call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%12 = call i32 @execvpe(i8* %10, i8** %9, i8** %11)
br label %23
13: ; preds = %2
%14 = icmp sgt i64 %6, 0
br i1 %14, label %15, label %21
15: ; preds = %13
%16 = bitcast i32* %4 to i8*
%17 = call i32 @wait(i32* %4)
%18 = load i32, i32* %4
%19 = call i32 @WEXITSTATUS(i32 %18)
%20 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %23
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %15, %21, %8
ret i32 0
}