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

/* Variables and functions */
scalar_t__ ECHILD;
int ENOENT;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int posix_spawn(int *, char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                char **, char **);
int posix_spawnp(int *, char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                 char **, char **);
int /*<<< orphan*/ printf(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int error, status;
  pid_t pid, waitres;
  char *myargs[4];
  char *myenv[2] = {"answer=42", NULL};
  while (waitpid(-1, NULL, 0) != -1)
    ;
  assert(errno == ECHILD);
  myargs[0] = "sh";
  myargs[1] = "-c";
  myargs[2] = "exit $answer";
  myargs[3] = NULL;
  error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv);
  assert(error == 0);
  waitres = waitpid(pid, &status, 0);
  assert(waitres == pid);
  assert(WIFEXITED(status) && WEXITSTATUS(status) == 42);
  myargs[0] = "/var/empty/nonexistent";
  myargs[1] = NULL;
  error = posix_spawn(&pid, myargs[0], NULL, NULL, myargs, myenv);
  if (error == 0) {
    waitres = waitpid(pid, &status, 0);
    assert(waitres == pid);
    assert(WIFEXITED(status) && WEXITSTATUS(status) == 127);
  } else {
    assert(error == ENOENT);
    waitres = waitpid(-1, NULL, 0);
    assert(waitres == -1 && errno == ECHILD);
  }
  printf("PASS posix_spawn()\n");
  printf("PASS posix_spawnp()\n");
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c63(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c60(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11ba <main+0x4a>
jmp    119c <main+0x2c>
mov    0x2e17(%rip),%rax
mov    (%rax),%rax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
lea    0xe28(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe20(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe18(%rip),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x40(%rbp),%rsi
lea    -0x40(%rbp),%r8
lea    -0x50(%rbp),%r9
lea    -0x1c(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <posix_spawnp@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x18(%rbp),%edi
call   1090 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x51(%rbp)
je     1283 <main+0x113>
mov    -0x18(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x2a,%eax
sete   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
lea    0xd86(%rip),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x40(%rbp),%rsi
lea    -0x40(%rbp),%r8
lea    -0x50(%rbp),%r9
lea    -0x1c(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <posix_spawn@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    132d <main+0x1bd>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x18(%rbp),%edi
call   1090 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x52(%rbp)
je     131b <main+0x1ab>
mov    -0x18(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x7f,%eax
sete   %al
mov    %al,-0x52(%rbp)
mov    -0x52(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
jmp    138f <main+0x21f>
mov    -0x14(%rbp),%eax
mov    0x2c89(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x20(%rbp)
mov    %al,-0x53(%rbp)
jne    1382 <main+0x212>
mov    0x2c69(%rip),%rax
mov    (%rax),%rax
mov    0x2c57(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x53(%rbp)
mov    -0x53(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
lea    0xc9e(%rip),%rdi
call   1030 <printf@plt>
lea    0xca6(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
movaps 0x2c70(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1190 <main+0x20>
mov    0x2e2e(%rip),%r14
mov    (%r14),%rax
mov    0x2e1c(%rip),%r15
xor    %edi,%edi
cmp    (%r15),%rax
sete   %dil
call   1040 <assert@plt>
lea    0xe41(%rip),%rsi
mov    %rsi,0x10(%rsp)
lea    0xe38(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xe2f(%rip),%rax
mov    %rax,0x20(%rsp)
movq   $0x0,0x28(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%r8
lea    0x30(%rsp),%r9
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <posix_spawnp@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    0x8(%rsp),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
xor    %edi,%edi
cmp    0x8(%rsp),%eax
sete   %dil
call   1040 <assert@plt>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     1254 <main+0xe4>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
xor    %ebx,%ebx
cmp    $0x2a,%eax
sete   %bl
mov    %ebx,%edi
call   1040 <assert@plt>
lea    0xdbb(%rip),%rsi
mov    %rsi,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%r8
lea    0x30(%rsp),%r9
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <posix_spawn@plt>
test   %eax,%eax
je     12c1 <main+0x151>
mov    0x2d2d(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%eax
sete   %dil
call   1040 <assert@plt>
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
sete   %al
mov    (%r14),%rcx
cmp    (%r15),%rcx
sete   %bl
and    %al,%bl
jmp    12ff <main+0x18f>
mov    0x8(%rsp),%edi
lea    0xc(%rsp),%rsi
xor    %ebx,%ebx
xor    %edx,%edx
call   1070 <waitpid@plt>
xor    %edi,%edi
cmp    0x8(%rsp),%eax
sete   %dil
call   1040 <assert@plt>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     12ff <main+0x18f>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x7f,%eax
sete   %bl
movzbl %bl,%edi
call   1040 <assert@plt>
lea    0xd26(%rip),%rdi
call   1030 <puts@plt>
lea    0xd2d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"answer=42\00"
@__const.main.myenv = [2 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0), i8* null]
@errno = global i64 0
@ECHILD = global i64 0
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@.str.3 = [13 x i8] c"exit $answer\00"
@.str.4 = [23 x i8] c"/var/empty/nonexistent\00"
@ENOENT = global i32 0
@.str.5 = [20 x i8] c"PASS posix_spawn()\0A\00"
@.str.6 = [21 x i8] c"PASS posix_spawnp()\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4 x i8*]
%11 = alloca [2 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [2 x i8*]* %11 to i8*
br label %13
13: ; preds = %16, %2
%14 = call i32 @waitpid(i32 -1, i32* null, i32 0)
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %13
br label %13
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @ECHILD
%20 = icmp eq i64 %18, %19
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %23
%24 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %24
%25 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %25
%26 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 3
store i8* null, i8** %26
%27 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
%28 = load i8*, i8** %27
%29 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
%30 = getelementptr inbounds [2 x i8*], [2 x i8*]* %11, i64 0, i64 0
%31 = call i32 @posix_spawnp(i32* %8, i8* %28, i32* null, i32* null, i8** %29, i8** %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i32, i32* %8
%37 = call i32 @waitpid(i32 %36, i32* %7, i32 0)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = load i32, i32* %8
%40 = icmp eq i32 %38, %39
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = load i32, i32* %7
%44 = call i64 @WIFEXITED(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %17
%47 = load i32, i32* %7
%48 = call i32 @WEXITSTATUS(i32 %47)
%49 = icmp eq i32 %48, 42
br label %50
50: ; preds = %46, %17
%51 = phi i1 [ false, %17 ], [ %49, %46 ]
%52 = zext i1 %51 to i32
%53 = call i32 @assert(i32 %52)
%54 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8** %54
%55 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 1
store i8* null, i8** %55
%56 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
%57 = load i8*, i8** %56
%58 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
%59 = getelementptr inbounds [2 x i8*], [2 x i8*]* %11, i64 0, i64 0
%60 = call i32 @posix_spawn(i32* %8, i8* %57, i32* null, i32* null, i8** %58, i8** %59)
store i32 %60, i32* %6
%61 = load i32, i32* %6
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %82
63: ; preds = %50
%64 = load i32, i32* %8
%65 = call i32 @waitpid(i32 %64, i32* %7, i32 0)
store i32 %65, i32* %9
%66 = load i32, i32* %9
%67 = load i32, i32* %8
%68 = icmp eq i32 %66, %67
%69 = zext i1 %68 to i32
%70 = call i32 @assert(i32 %69)
%71 = load i32, i32* %7
%72 = call i64 @WIFEXITED(i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %63
%75 = load i32, i32* %7
%76 = call i32 @WEXITSTATUS(i32 %75)
%77 = icmp eq i32 %76, 127
br label %78
78: ; preds = %74, %63
%79 = phi i1 [ false, %63 ], [ %77, %74 ]
%80 = zext i1 %79 to i32
%81 = call i32 @assert(i32 %80)
br label %99
82: ; preds = %50
%83 = load i32, i32* %6
%84 = load i32, i32* @ENOENT
%85 = icmp eq i32 %83, %84
%86 = zext i1 %85 to i32
%87 = call i32 @assert(i32 %86)
%88 = call i32 @waitpid(i32 -1, i32* null, i32 0)
store i32 %88, i32* %9
%89 = load i32, i32* %9
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %95
91: ; preds = %82
%92 = load i64, i64* @errno
%93 = load i64, i64* @ECHILD
%94 = icmp eq i64 %92, %93
br label %95
95: ; preds = %91, %82
%96 = phi i1 [ false, %82 ], [ %94, %91 ]
%97 = zext i1 %96 to i32
%98 = call i32 @assert(i32 %97)
br label %99
99: ; preds = %95, %78
%100 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%101 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"answer=42\00"
@__const.main.myenv = [2 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0), i8* null]
@errno = global i64 0
@ECHILD = global i64 0
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@.str.3 = [13 x i8] c"exit $answer\00"
@.str.4 = [23 x i8] c"/var/empty/nonexistent\00"
@ENOENT = global i32 0
@str = [19 x i8] c"PASS posix_spawn()\00"
@str.7 = [20 x i8] c"PASS posix_spawnp()\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4 x i8*]
%6 = alloca [2 x i8*]
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast [4 x i8*]* %5 to i8*
%10 = bitcast [2 x i8*]* %6 to i8*
br label %11
11: ; preds = %11, %2
%12 = tail call i32 @waitpid(i32 -1, i32* null, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %11
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @ECHILD
%17 = icmp eq i64 %15, %16
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %20
%21 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %21
%22 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %22
%23 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 3
store i8* null, i8** %23
%24 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
%25 = call i32 @posix_spawnp(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* null, i32* null, i8** %20, i8** %24)
%26 = icmp eq i32 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i32, i32* %4
%30 = call i32 @waitpid(i32 %29, i32* %3, i32 0)
%31 = load i32, i32* %4
%32 = icmp eq i32 %30, %31
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i32, i32* %3
%36 = call i64 @WIFEXITED(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %14
%39 = load i32, i32* %3
%40 = call i32 @WEXITSTATUS(i32 %39)
%41 = icmp eq i32 %40, 42
%42 = zext i1 %41 to i32
br label %43
43: ; preds = %38, %14
%44 = phi i32 [ 0, %14 ], [ %42, %38 ]
%45 = call i32 @assert(i32 %44)
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8** %20
store i8* null, i8** %21
%46 = call i32 @posix_spawn(i32* %4, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32* null, i32* null, i8** %20, i8** %24)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %62
48: ; preds = %43
%49 = load i32, i32* %4
%50 = call i32 @waitpid(i32 %49, i32* %3, i32 0)
%51 = load i32, i32* %4
%52 = icmp eq i32 %50, %51
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = load i32, i32* %3
%56 = call i64 @WIFEXITED(i32 %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %73, label %58
58: ; preds = %48
%59 = load i32, i32* %3
%60 = call i32 @WEXITSTATUS(i32 %59)
%61 = icmp eq i32 %60, 127
br label %73
62: ; preds = %43
%63 = load i32, i32* @ENOENT
%64 = icmp eq i32 %46, %63
%65 = zext i1 %64 to i32
%66 = call i32 @assert(i32 %65)
%67 = call i32 @waitpid(i32 -1, i32* null, i32 0)
%68 = icmp eq i32 %67, -1
%69 = load i64, i64* @errno
%70 = load i64, i64* @ECHILD
%71 = icmp eq i64 %69, %70
%72 = select i1 %68, i1 %71, i1 false
br label %73
73: ; preds = %48, %58, %62
%74 = phi i1 [ %72, %62 ], [ false, %48 ], [ %61, %58 ]
%75 = zext i1 %74 to i32
%76 = call i32 @assert(i32 %75)
%77 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%78 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}