#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 */

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
scalar_t__ pipe(int *);
void spawn(char **argv, int *in_fd, int *out_fd) {
  int in_fds[2], out_fds[2];
  if (pipe(in_fds) < 0 || pipe(out_fds) < 0)
    err(1, "pipe");
  switch (fork()) {
  case -1:
    err(1, "fork");
  case 0:
    if (dup2(in_fds[0], STDIN_FILENO) < 0 ||
        dup2(out_fds[1], STDOUT_FILENO) < 0)
      err(1, NULL);
    (void)close(in_fds[0]);
    (void)close(in_fds[1]);
    (void)close(out_fds[0]);
    (void)close(out_fds[1]);
    if (execvp(argv[0], argv) < 0)
      err(1, "failed to execute %s", argv[0]);
  default:
    (void)close(in_fds[0]);
    (void)close(out_fds[1]);
    *in_fd = in_fds[1];
    *out_fd = out_fds[0];
  }
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jl     119a <spawn+0x3a>
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11ad <spawn+0x4d>
mov    $0x1,%edi
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     11d7 <spawn+0x77>
jmp    11c7 <spawn+0x67>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     11ea <spawn+0x8a>
jmp    1286 <spawn+0x126>
mov    $0x1,%edi
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x20(%rbp),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%eax
jl     121e <spawn+0xbe>
mov    -0x24(%rbp),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%eax
jge    122e <spawn+0xce>
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <execvp@plt>
cmp    $0x0,%eax
jge    1281 <spawn+0x121>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    $0x1,%edi
lea    0xd90(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
jmp    1286 <spawn+0x126>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     118e <spawn+0x2e>
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jns    11a1 <spawn+0x41>
lea    0xe6b(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1060 <err@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11c8 <spawn+0x68>
cmp    $0xffffffff,%eax
jne    124a <spawn+0xea>
lea    0xe49(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1060 <err@plt>
mov    0x8(%rsp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
test   %eax,%eax
js     11f4 <spawn+0x94>
mov    0x4(%rsp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
test   %eax,%eax
jns    1202 <spawn+0xa2>
mov    $0x1,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1060 <err@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1070 <execvp@plt>
test   %eax,%eax
jns    124a <spawn+0xea>
mov    (%rbx),%rdx
lea    0xdcc(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1060 <err@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%eax
mov    %eax,(%r15)
mov    (%rsp),%eax
mov    %eax,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [21 x i8] c"failed to execute %s\00"
define void @spawn(i8** %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca [2 x i32]
%5 = alloca [2 x i32]
%6 = bitcast [2 x i32]* %4 to i8*
%7 = bitcast [2 x i32]* %5 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %3
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%13 = call i64 @pipe(i32* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11, %3
%16 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = call i32 (...) @fork()
switch i32 %18, label %52 [
i32 -1, label %19
i32 0, label %21
]
19: ; preds = %17
%20 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %17, %19
%22 = load i32, i32* %8
%23 = load i32, i32* @STDIN_FILENO
%24 = call i32 @dup2(i32 %22, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %21
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%28 = load i32, i32* %27
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @dup2(i32 %28, i32 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26, %21
%33 = call i32 (i32, i8*, ...) @err(i32 1, i8* null)
br label %34
34: ; preds = %32, %26
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %0
%47 = call i32 @execvp(i8* %46, i8** %0)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %34
%50 = load i8*, i8** %0
%51 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %34, %49, %17
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%59 = load i32, i32* %58
store i32 %59, i32* %1
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%61 = load i32, i32* %60
store i32 %61, i32* %2
ret void
}