#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 O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char const *, int, ...);
int /*<<< orphan*/ stderr;
void spawnc(char **cmd, const char *in, const char *out) {
  if (fork() == 0) {
    int outfd = 0, infd = 0;
    if (out) {
      outfd = open(out, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
      close(1);
      close(2);
      dup2(outfd, 1);
      dup2(outfd, 2);
      close(outfd);
    }
    if (in) {
      infd = open(in, O_RDONLY);
      close(0);
      dup2(infd, 0);
      close(infd);
    }
    execvp(cmd[0], cmd);
    fprintf(stderr, "FAILED TO EXECVP\n");
  }
}


x86 assembly (clang, -O0):
<spawnc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    126f <spawnc+0x10f>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     120f <spawnc+0xaf>
mov    -0x18(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
mov    0x2e03(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x10(%rbp)
je     124a <spawnc+0xea>
mov    -0x10(%rbp),%rdi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <execvp@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
call   1050 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawnc>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     1180 <spawnc+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret
test   %rbx,%rbx
je     11f1 <spawnc+0x91>
mov    0x2e1c(%rip),%rax
mov    0x2e65(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e52(%rip),%rax
or     (%rax),%esi
mov    0x2e11(%rip),%rax
mov    0x2e22(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %r15,%r15
je     1222 <spawnc+0xc2>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1040 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1070 <execvp@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <fprintf@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"FAILED TO EXECVP\0A\00"
define void @spawnc(i8** %0, i8* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %53
11: ; preds = %3
store i32 0, i32* %7
store i32 0, i32* %8
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %33
14: ; preds = %11
%15 = load i8*, i8** %6
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = call i32 (i8*, i32, ...) @open(i8* %15, i32 %20, i32 %23)
store i32 %24, i32* %7
%25 = call i32 @close(i32 1)
%26 = call i32 @close(i32 2)
%27 = load i32, i32* %7
%28 = call i32 @dup2(i32 %27, i32 1)
%29 = load i32, i32* %7
%30 = call i32 @dup2(i32 %29, i32 2)
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %14, %11
%34 = load i8*, i8** %5
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %45
36: ; preds = %33
%37 = load i8*, i8** %5
%38 = load i32, i32* @O_RDONLY
%39 = call i32 (i8*, i32, ...) @open(i8* %37, i32 %38)
store i32 %39, i32* %8
%40 = call i32 @close(i32 0)
%41 = load i32, i32* %8
%42 = call i32 @dup2(i32 %41, i32 0)
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %36, %33
%46 = load i8**, i8*** %4
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = load i8**, i8*** %4
%50 = call i32 @execvp(i8* %48, i8** %49)
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %53
53: ; preds = %45, %3
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"FAILED TO EXECVP\0A\00"
define void @spawnc(i8** %0, i8* %1, i8* %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %36
6: ; preds = %3
%7 = icmp eq i8* %2, null
br i1 %7, label %23, label %8
8: ; preds = %6
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = tail call i32 (i8*, i32, ...) @open(i8* %2, i32 %13, i32 %16)
%18 = tail call i32 @close(i32 1)
%19 = tail call i32 @close(i32 2)
%20 = tail call i32 @dup2(i32 %17, i32 1)
%21 = tail call i32 @dup2(i32 %17, i32 2)
%22 = tail call i32 @close(i32 %17)
br label %23
23: ; preds = %8, %6
%24 = icmp eq i8* %1, null
br i1 %24, label %31, label %25
25: ; preds = %23
%26 = load i32, i32* @O_RDONLY
%27 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %26)
%28 = tail call i32 @close(i32 0)
%29 = tail call i32 @dup2(i32 %27, i32 0)
%30 = tail call i32 @close(i32 %27)
br label %31
31: ; preds = %25, %23
%32 = load i8*, i8** %0
%33 = tail call i32 @execvp(i8* %32, i8** %0)
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %31, %3
ret void
}