#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 STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int execvp(char *, char **);
int fork();
int spawn_proc(int in, int out, char **command) {
  pid_t pid;
  if ((pid = fork()) == 0) {
    if (in != STDIN_FILENO) {
      dup2(in, STDIN_FILENO);
      close(in);
    }
    if (out != STDOUT_FILENO) {
      dup2(out, STDOUT_FILENO);
      close(out);
    }
    return execvp(command[0], command);
  }
  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    11d3 <spawn_proc+0x93>
mov    -0x8(%rbp),%eax
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%eax
je     1190 <spawn_proc+0x50>
mov    -0x8(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%eax
je     11bb <spawn_proc+0x7b>
mov    -0xc(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <execvp@plt>
mov    %eax,-0x4(%rbp)
jmp    11d9 <spawn_proc+0x99>
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
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
cmp    %ebx,%esi
je     1176 <spawn_proc+0x36>
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
cmp    %ebp,%esi
je     1191 <spawn_proc+0x51>
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <execvp@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
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 %37
11: ; preds = %3
%12 = load i32, i32* %5
%13 = load i32, i32* @STDIN_FILENO
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i32, i32* %5
%17 = load i32, i32* @STDIN_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = load i32, i32* %5
%20 = call i32 @close(i32 %19)
br label %21
21: ; preds = %15, %11
%22 = load i32, i32* %6
%23 = load i32, i32* @STDOUT_FILENO
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i32, i32* %6
%27 = load i32, i32* @STDOUT_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
br label %31
31: ; preds = %25, %21
%32 = load i8**, i8*** %7
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = load i8**, i8*** %7
%36 = call i32 @execvp(i8* %34, i8** %35)
store i32 %36, i32* %4
br label %39
37: ; preds = %3
%38 = load i32, i32* %8
store i32 %38, i32* %4
br label %39
39: ; preds = %37, %31
%40 = load i32, i32* %4
ret i32 %40
}

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