#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 */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int execvp(char *, char **);
int fork();
int spawn_proc(int in, int out, char ***cmd) {
  pid_t pid;
  if ((pid = fork()) == 0) {
    if (in != 0) {
      dup2(in, 0);
      close(in);
    }
    if (out != 1) {
      dup2(out, 1);
      close(out);
    }
    return execvp(*cmd[0], *cmd);
  }
  return pid;
}


x86 assembly (clang, -O0):
<spawn_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    11be <spawn_proc+0x7e>
cmpl   $0x0,-0x8(%rbp)
je     1181 <spawn_proc+0x41>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x1,-0xc(%rbp)
je     11a0 <spawn_proc+0x60>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1050 <execvp@plt>
mov    %eax,-0x4(%rbp)
jmp    11c4 <spawn_proc+0x84>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_proc>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     115b <spawn_proc+0x1b>
pop    %rbx
pop    %r14
pop    %rbp
ret
test   %ebx,%ebx
je     116f <spawn_proc+0x2f>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0x1,%ebp
je     1187 <spawn_proc+0x47>
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    (%r14),%rsi
mov    (%rsi),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <execvp@plt>

LLVM IR (-O0):
define i32 @spawn_proc(i32 %0, i32 %1, i8*** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8***
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8*** %2, i8**** %7
%9 = call i32 (...) @fork()
store i32 %9, i32* %8
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %35
11: ; preds = %3
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i32, i32* %5
%16 = call i32 @dup2(i32 %15, i32 0)
%17 = load i32, i32* %5
%18 = call i32 @close(i32 %17)
br label %19
19: ; preds = %14, %11
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i32, i32* %6
%24 = call i32 @dup2(i32 %23, i32 1)
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %22, %19
%28 = load i8***, i8**** %7
%29 = getelementptr inbounds i8**, i8*** %28, i64 0
%30 = load i8**, i8*** %29
%31 = load i8*, i8** %30
%32 = load i8***, i8**** %7
%33 = load i8**, i8*** %32
%34 = call i32 @execvp(i8* %31, i8** %33)
store i32 %34, i32* %4
br label %37
35: ; preds = %3
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %27
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @spawn_proc(i32 %0, i32 %1, i8*** nocapture %2) {
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %20
6: ; preds = %3
%7 = icmp eq i32 %0, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = tail call i32 @dup2(i32 %0, i32 0)
%10 = tail call i32 @close(i32 %0)
br label %11
11: ; preds = %8, %6
%12 = icmp eq i32 %1, 1
br i1 %12, label %16, label %13
13: ; preds = %11
%14 = tail call i32 @dup2(i32 %1, i32 1)
%15 = tail call i32 @close(i32 %1)
br label %16
16: ; preds = %13, %11
%17 = load i8**, i8*** %2
%18 = load i8*, i8** %17
%19 = tail call i32 @execvp(i8* %18, i8** %17)
br label %20
20: ; preds = %3, %16
%21 = phi i32 [ %19, %16 ], [ %4, %3 ]
ret i32 %21
}