#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*/ EXIT_FAILURE;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ execl(char *, int /*<<< orphan*/, char *, char const *,
                         int /*<<< orphan*/ *);
int fork();
char *get_shell();
int /*<<< orphan*/ last_component(char *);
scalar_t__ pipe(int *);
int spawn_process(const char *cmd, int *in, int *out) {
  int in_fds[2], out_fds[2];
  if (pipe(in_fds) < 0 || pipe(out_fds) < 0) {
    error(EXIT_FAILURE, errno, "pipe error");
  }
  int pid = fork();
  if (pid < 0) {
    error(EXIT_FAILURE, errno, "fork error");
  }
  if (pid == 0) {
    close(in_fds[0]);
    close(out_fds[1]);
    if (dup2(out_fds[0], STDIN_FILENO) < 0) {
      error(EXIT_FAILURE, errno, "dup2 error");
    }
    if (dup2(in_fds[1], STDOUT_FILENO) < 0) {
      error(EXIT_FAILURE, errno, "dup2 error");
    }
    close(out_fds[0]);
    char *shell = get_shell();
    execl(shell, last_component(shell), "-c", cmd, NULL);
    error(EXIT_FAILURE, errno, "execl error");
  }
  close(in_fds[1]);
  close(out_fds[0]);
  *in = in_fds[0];
  *out = out_fds[1];
  return pid;
}


x86 assembly (clang, -O0):
<spawn_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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     11ba <spawn_process+0x3a>
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11d8 <spawn_process+0x58>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdx
call   1070 <error@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    120a <spawn_process+0x8a>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdx
call   1070 <error@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    12f3 <spawn_process+0x173>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    125d <spawn_process+0xdd>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    0xdbe(%rip),%rdx
call   1070 <error@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    1296 <spawn_process+0x116>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
lea    0xd85(%rip),%rdx
call   1070 <error@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1080 <get_shell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <last_component@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
lea    0xd56(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1090 <execl@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
lea    0xd36(%rip),%rdx
call   1070 <error@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_process>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     11b1 <spawn_process+0x31>
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jns    11cf <spawn_process+0x4f>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdx
call   1070 <error@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     129b <spawn_process+0x11b>
jne    12b9 <spawn_process+0x139>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
test   %rax,%rax
jns    122c <spawn_process+0xac>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    0xdef(%rip),%rdx
call   1070 <error@plt>
mov    0xc(%rsp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
test   %rax,%rax
jns    1261 <spawn_process+0xe1>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
lea    0xdba(%rip),%rdx
call   1070 <error@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1080 <get_shell@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <last_component@plt>
lea    0xd9f(%rip),%rdx
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r15,%rcx
xor    %r8d,%r8d
call   1090 <execl@plt>
lea    0xd8b(%rip),%rdx
jmp    12a2 <spawn_process+0x122>
lea    0xd69(%rip),%rdx
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
call   1070 <error@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%r12)
mov    0x4(%rsp),%eax
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [11 x i8] c"fork error\00"
@STDIN_FILENO = global i32 0
@.str.2 = [11 x i8] c"dup2 error\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [12 x i8] c"execl error\00"
define i32 @spawn_process(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]
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%12 = call i64 @pipe(i32* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%16 = call i64 @pipe(i32* %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14, %3
%19 = load i32, i32* @EXIT_FAILURE
%20 = load i32, i32* @errno
%21 = call i32 @error(i32 %19, i32 %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %18, %14
%23 = call i32 (...) @fork()
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @EXIT_FAILURE
%28 = load i32, i32* @errno
%29 = call i32 @error(i32 %27, i32 %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %26, %22
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %72
33: ; preds = %30
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%41 = load i32, i32* %40
%42 = load i32, i32* @STDIN_FILENO
%43 = call i64 @dup2(i32 %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %33
%46 = load i32, i32* @EXIT_FAILURE
%47 = load i32, i32* @errno
%48 = call i32 @error(i32 %46, i32 %47, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %45, %33
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%51 = load i32, i32* %50
%52 = load i32, i32* @STDOUT_FILENO
%53 = call i64 @dup2(i32 %51, i32 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %49
%56 = load i32, i32* @EXIT_FAILURE
%57 = load i32, i32* @errno
%58 = call i32 @error(i32 %56, i32 %57, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %55, %49
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = call i8* (...) @get_shell()
store i8* %63, i8** %10
%64 = load i8*, i8** %10
%65 = load i8*, i8** %10
%66 = call i32 @last_component(i8* %65)
%67 = load i8*, i8** %4
%68 = call i32 @execl(i8* %64, i32 %66, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %67, i32* null)
%69 = load i32, i32* @EXIT_FAILURE
%70 = load i32, i32* @errno
%71 = call i32 @error(i32 %69, i32 %70, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %72
72: ; preds = %59, %30
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%74 = load i32, i32* %73
%75 = call i32 @close(i32 %74)
%76 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%77 = load i32, i32* %76
%78 = call i32 @close(i32 %77)
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%80 = load i32, i32* %79
%81 = load i32*, i32** %5
store i32 %80, i32* %81
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%83 = load i32, i32* %82
%84 = load i32*, i32** %6
store i32 %83, i32* %84
%85 = load i32, i32* %9
ret i32 %85
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [11 x i8] c"fork error\00"
@STDIN_FILENO = global i32 0
@.str.2 = [11 x i8] c"dup2 error\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [12 x i8] c"execl error\00"
define i32 @spawn_process(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 %19
15: ; preds = %11, %3
%16 = load i32, i32* @EXIT_FAILURE
%17 = load i32, i32* @errno
%18 = call i32 @error(i32 %16, i32 %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %15, %11
%20 = call i32 (...) @fork()
%21 = icmp slt i32 %20, 0
br i1 %21, label %55, label %22
22: ; preds = %19
%23 = icmp eq i32 %20, 0
br i1 %23, label %24, label %60
24: ; preds = %22
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%31 = load i32, i32* %30
%32 = load i32, i32* @STDIN_FILENO
%33 = call i64 @dup2(i32 %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %24
%36 = load i32, i32* @EXIT_FAILURE
%37 = load i32, i32* @errno
%38 = call i32 @error(i32 %36, i32 %37, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %35, %24
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%41 = load i32, i32* %40
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i64 @dup2(i32 %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %39
%46 = load i32, i32* @EXIT_FAILURE
%47 = load i32, i32* @errno
%48 = call i32 @error(i32 %46, i32 %47, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %45, %39
%50 = load i32, i32* %30
%51 = call i32 @close(i32 %50)
%52 = call i8* (...) @get_shell()
%53 = call i32 @last_component(i8* %52)
%54 = call i32 @execl(i8* %52, i32 %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32* null)
br label %55
55: ; preds = %19, %49
%56 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), %49 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %19 ]
%57 = load i32, i32* @EXIT_FAILURE
%58 = load i32, i32* @errno
%59 = call i32 @error(i32 %57, i32 %58, i8* %56)
br label %60
60: ; preds = %55, %22
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %8
store i32 %67, i32* %1
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%69 = load i32, i32* %68
store i32 %69, i32* %2
ret i32 %20
}