#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 /*<<< orphan*/ close(int);
int execve(char const *, char *const *, char *const *);
scalar_t__ fork();
int pipe(int *);
int /*<<< orphan*/ remap_pipe_stdin_stdout(int, int);
int spawnve(const char *path, char *const argv[], char *const envp[],
            int pipes[2]) {
  int pushpipes[2];
  int pullpipes[2];
  int status;
  status = pipe(pushpipes);
  if (status < 0) {
    return (status);
  }
  status = pipe(pullpipes);
  if (status < 0) {
    return (status);
  }
  pid_t result = fork();
  if (result < 0) {
    return (-1);
  }
  if (result == 0) {
    close(pushpipes[1]);
    close(pullpipes[0]);
    remap_pipe_stdin_stdout(pushpipes[0], pullpipes[1]);
    status = execve(path, argv, envp);
    if (status < 0) {
      return (status);
    }
  } else {
    pipes[0] = pullpipes[0];
    close(pullpipes[1]);
    pipes[1] = pushpipes[1];
    close(pushpipes[0]);
  }
  return (0);
}


x86 assembly (clang, -O0):
<spawnve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <pipe@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1189 <spawnve+0x39>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <spawnve+0xfa>
lea    -0x38(%rbp),%rdi
call   1040 <pipe@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11aa <spawnve+0x5a>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <spawnve+0xfa>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jge    11cc <spawnve+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <spawnve+0xfa>
cmpq   $0x0,-0x48(%rbp)
jne    1220 <spawnve+0xd0>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x38(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%edi
mov    -0x34(%rbp),%esi
call   1060 <remap_pipe_stdin_stdout@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <execve@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    121b <spawnve+0xcb>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <spawnve+0xfa>
jmp    1243 <spawnve+0xf3>
mov    -0x38(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x30(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawnve>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x10(%rsp),%rdi
call   1040 <pipe@plt>
test   %eax,%eax
js     11ec <spawnve+0x9c>
lea    0x8(%rsp),%rdi
call   1040 <pipe@plt>
test   %eax,%eax
js     11ec <spawnve+0x9c>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11b2 <spawnve+0x62>
je     11b9 <spawnve+0x69>
mov    0x8(%rsp),%eax
mov    %eax,(%rbx)
mov    0xc(%rsp),%edi
call   1030 <close@plt>
mov    0x14(%rsp),%eax
mov    %eax,0x4(%rbx)
mov    0x10(%rsp),%edi
call   1030 <close@plt>
jmp    11ea <spawnve+0x9a>
mov    $0xffffffff,%eax
jmp    11ec <spawnve+0x9c>
mov    0x14(%rsp),%edi
call   1030 <close@plt>
mov    0x8(%rsp),%edi
call   1030 <close@plt>
mov    0x10(%rsp),%edi
mov    0xc(%rsp),%esi
call   1060 <remap_pipe_stdin_stdout@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <execve@plt>
test   %eax,%eax
js     11ec <spawnve+0x9c>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @spawnve(i8* %0, i8** %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca [2 x i32]
%11 = alloca [2 x i32]
%12 = alloca i32
%13 = alloca i64
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i8** %2, i8*** %8
store i32* %3, i32** %9
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
store i32 %15, i32* %12
%16 = load i32, i32* %12
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = load i32, i32* %12
store i32 %19, i32* %5
br label %72
20: ; preds = %4
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%22 = call i32 @pipe(i32* %21)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = load i32, i32* %12
store i32 %26, i32* %5
br label %72
27: ; preds = %20
%28 = call i64 (...) @fork()
store i64 %28, i64* %13
%29 = load i64, i64* %13
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %5
br label %72
32: ; preds = %27
%33 = load i64, i64* %13
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %56
35: ; preds = %32
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @remap_pipe_stdin_stdout(i32 %43, i32 %45)
%47 = load i8*, i8** %6
%48 = load i8**, i8*** %7
%49 = load i8**, i8*** %8
%50 = call i32 @execve(i8* %47, i8** %48, i8** %49)
store i32 %50, i32* %12
%51 = load i32, i32* %12
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %35
%54 = load i32, i32* %12
store i32 %54, i32* %5
br label %72
55: ; preds = %35
br label %71
56: ; preds = %32
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%58 = load i32, i32* %57
%59 = load i32*, i32** %9
%60 = getelementptr inbounds i32, i32* %59, i64 0
store i32 %58, i32* %60
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%65 = load i32, i32* %64
%66 = load i32*, i32** %9
%67 = getelementptr inbounds i32, i32* %66, i64 1
store i32 %65, i32* %67
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%69 = load i32, i32* %68
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %56, %55
store i32 0, i32* %5
br label %72
72: ; preds = %71, %53, %31, %25, %18
%73 = load i32, i32* %5
ret i32 %73
}

LLVM IR (-O2):
define i32 @spawnve(i8* %0, i8** %1, i8** %2, i32* nocapture writeonly %3) {
%5 = alloca [2 x i32]
%6 = alloca [2 x i32]
%7 = bitcast [2 x i32]* %5 to i8*
%8 = bitcast [2 x i32]* %6 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %44, label %12
12: ; preds = %4
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%14 = call i32 @pipe(i32* %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %44, label %16
16: ; preds = %12
%17 = call i64 (...) @fork()
%18 = icmp slt i64 %17, 0
br i1 %18, label %44, label %19
19: ; preds = %16
%20 = icmp eq i64 %17, 0
br i1 %20, label %21, label %33
21: ; preds = %19
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %13
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %9
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @remap_pipe_stdin_stdout(i32 %27, i32 %29)
%31 = call i32 @execve(i8* %0, i8** %1, i8** %2)
%32 = icmp slt i32 %31, 0
br i1 %32, label %44, label %43
33: ; preds = %19
%34 = load i32, i32* %13
store i32 %34, i32* %3
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds i32, i32* %3, i64 1
store i32 %39, i32* %40
%41 = load i32, i32* %9
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %21, %33
br label %44
44: ; preds = %43, %16, %21, %12, %4
%45 = phi i32 [ %10, %4 ], [ %14, %12 ], [ 0, %43 ], [ -1, %16 ], [ %31, %21 ]
ret i32 %45
}