#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closefrom(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char const *, char *const *);
int fileno(int /*<<< orphan*/ *);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setgid(int);
int /*<<< orphan*/ setgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ setuid(int);
int spawn(const char *path, char *const *argv, int uid, int gid, FILE *fpin,
          FILE *fpout, FILE *fperr) {
  int fdin, fdout, fderr, pid;
  if (!path || !argv)
    return -1;
  if (fpin)
    fdin = fileno(fpin);
  else
    fdin = open("/dev/null", O_RDONLY);
  if (fpout)
    fdout = fileno(fpout);
  else
    fdout = open("/dev/null", O_WRONLY);
  if (fperr)
    fderr = fileno(fperr);
  else
    fderr = open("/dev/null", O_WRONLY);
  if (fdin < 0 || fdout < 0 || fderr < 0)
    goto Fail;
  pid = fork();
  if (pid < 0)
    goto Fail;
  if (pid == 0) {
    dup2(fdin, 0);
    dup2(fdout, 1);
    dup2(fderr, 2);
    closefrom(3);
    setgroups(0, NULL);
    setgid(gid);
    setuid(uid);
    execv(path, argv);
    _exit(1);
  }
  if (!fpin)
    close(fdin);
  if (!fpout)
    close(fdout);
  if (!fperr)
    close(fderr);
  return pid;
Fail:
  if (!fpin)
    close(fdin);
  if (!fpout)
    close(fdout);
  if (!fperr)
    close(fderr);
  return -1;
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e8 <spawn+0x38>
cmpq   $0x0,-0x18(%rbp)
jne    11f4 <spawn+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ba <spawn+0x20a>
cmpq   $0x0,-0x28(%rbp)
je     1210 <spawn+0x60>
mov    -0x28(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,-0x34(%rbp)
jmp    1228 <spawn+0x78>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0xde0(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1244 <spawn+0x94>
mov    -0x30(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,-0x38(%rbp)
jmp    125c <spawn+0xac>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
lea    0xdac(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x38(%rbp)
cmpq   $0x0,0x10(%rbp)
je     1278 <spawn+0xc8>
mov    0x10(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,-0x3c(%rbp)
jmp    1290 <spawn+0xe0>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    0xd78(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     12ae <spawn+0xfe>
cmpl   $0x0,-0x38(%rbp)
jl     12ae <spawn+0xfe>
cmpl   $0x0,-0x3c(%rbp)
jge    12b3 <spawn+0x103>
jmp    137a <spawn+0x1ca>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    12cc <spawn+0x11c>
jmp    137a <spawn+0x1ca>
cmpl   $0x0,-0x40(%rbp)
jne    1336 <spawn+0x186>
mov    -0x34(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x38(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x3c(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    $0x3,%edi
call   1040 <closefrom@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <setgroups@plt>
mov    -0x20(%rbp),%edi
call   1090 <setgid@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <setuid@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <execv@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1349 <spawn+0x199>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x0,-0x30(%rbp)
jne    135c <spawn+0x1ac>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x0,0x10(%rbp)
jne    136f <spawn+0x1bf>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13ba <spawn+0x20a>
cmpq   $0x0,-0x28(%rbp)
jne    138d <spawn+0x1dd>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x0,-0x30(%rbp)
jne    13a0 <spawn+0x1f0>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x0,0x10(%rbp)
jne    13b3 <spawn+0x203>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     1295 <spawn+0xe5>
mov    %rsi,%r14
test   %rsi,%rsi
je     1295 <spawn+0xe5>
mov    %r9,%r12
mov    %r8,%rbx
mov    %rdi,%r13
mov    %ecx,0x10(%rsp)
mov    %edx,0x14(%rsp)
test   %r8,%r8
je     11f9 <spawn+0x49>
mov    %rbx,%rdi
call   1080 <fileno@plt>
jmp    120e <spawn+0x5e>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
lea    0xdf7(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
mov    0x50(%rsp),%r15
test   %r12,%r12
je     1224 <spawn+0x74>
mov    %r12,%rdi
call   1080 <fileno@plt>
jmp    1239 <spawn+0x89>
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
lea    0xdcc(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,0x8(%rsp)
test   %r15,%r15
je     124c <spawn+0x9c>
mov    %r15,%rdi
call   1080 <fileno@plt>
jmp    1261 <spawn+0xb1>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    0xda4(%rip),%rdi
call   10a0 <open@plt>
mov    0x8(%rsp),%ecx
or     %ebp,%ecx
mov    %eax,0xc(%rsp)
or     %eax,%ecx
js     12a7 <spawn+0xf7>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
js     12a7 <spawn+0xf7>
je     1300 <spawn+0x150>
mov    %eax,%r15d
test   %rbx,%rbx
je     12e1 <spawn+0x131>
test   %r12,%r12
je     12ed <spawn+0x13d>
cmpq   $0x0,0x50(%rsp)
je     12bf <spawn+0x10f>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %rbx,%rbx
je     12ca <spawn+0x11a>
test   %r12,%r12
je     12d6 <spawn+0x126>
mov    $0xffffffff,%r15d
cmpq   $0x0,0x50(%rsp)
jne    1295 <spawn+0xe5>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
jmp    1295 <spawn+0xe5>
mov    %ebp,%edi
call   1060 <close@plt>
test   %r12,%r12
jne    12b1 <spawn+0x101>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
jmp    12b1 <spawn+0x101>
mov    %ebp,%edi
call   1060 <close@plt>
test   %r12,%r12
jne    128d <spawn+0xdd>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
cmpq   $0x0,0x50(%rsp)
jne    1295 <spawn+0xe5>
jmp    12bf <spawn+0x10f>
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    0x8(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    $0x3,%edi
call   1040 <closefrom@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1070 <setgroups@plt>
mov    0x10(%rsp),%edi
call   1090 <setgid@plt>
mov    0x14(%rsp),%edi
call   10b0 <setuid@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   10c0 <execv@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define i32 @spawn(i8* %0, i8** %1, i32 %2, i32 %3, i32* %4, i32* %5, i32* %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i8* %0, i8** %9
store i8** %1, i8*** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i32* %4, i32** %13
store i32* %5, i32** %14
store i32* %6, i32** %15
%20 = load i8*, i8** %9
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %7
%23 = load i8**, i8*** %10
%24 = icmp ne i8** %23, null
br i1 %24, label %26, label %25
25: ; preds = %22, %7
store i32 -1, i32* %8
br label %127
26: ; preds = %22
%27 = load i32*, i32** %13
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32*, i32** %13
%31 = call i32 @fileno(i32* %30)
store i32 %31, i32* %16
br label %35
32: ; preds = %26
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %33)
store i32 %34, i32* %16
br label %35
35: ; preds = %32, %29
%36 = load i32*, i32** %14
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32*, i32** %14
%40 = call i32 @fileno(i32* %39)
store i32 %40, i32* %17
br label %44
41: ; preds = %35
%42 = load i32, i32* @O_WRONLY
%43 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %42)
store i32 %43, i32* %17
br label %44
44: ; preds = %41, %38
%45 = load i32*, i32** %15
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32*, i32** %15
%49 = call i32 @fileno(i32* %48)
store i32 %49, i32* %18
br label %53
50: ; preds = %44
%51 = load i32, i32* @O_WRONLY
%52 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %51)
store i32 %52, i32* %18
br label %53
53: ; preds = %50, %47
%54 = load i32, i32* %16
%55 = icmp slt i32 %54, 0
br i1 %55, label %62, label %56
56: ; preds = %53
%57 = load i32, i32* %17
%58 = icmp slt i32 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %56
%60 = load i32, i32* %18
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %59, %56, %53
br label %108
63: ; preds = %59
%64 = call i32 (...) @fork()
store i32 %64, i32* %19
%65 = load i32, i32* %19
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %63
br label %108
68: ; preds = %63
%69 = load i32, i32* %19
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %88
71: ; preds = %68
%72 = load i32, i32* %16
%73 = call i32 @dup2(i32 %72, i32 0)
%74 = load i32, i32* %17
%75 = call i32 @dup2(i32 %74, i32 1)
%76 = load i32, i32* %18
%77 = call i32 @dup2(i32 %76, i32 2)
%78 = call i32 @closefrom(i32 3)
%79 = call i32 @setgroups(i32 0, i32* null)
%80 = load i32, i32* %12
%81 = call i32 @setgid(i32 %80)
%82 = load i32, i32* %11
%83 = call i32 @setuid(i32 %82)
%84 = load i8*, i8** %9
%85 = load i8**, i8*** %10
%86 = call i32 @execv(i8* %84, i8** %85)
%87 = call i32 @_exit(i32 1)
unreachable
88: ; preds = %68
%89 = load i32*, i32** %13
%90 = icmp ne i32* %89, null
br i1 %90, label %94, label %91
91: ; preds = %88
%92 = load i32, i32* %16
%93 = call i32 @close(i32 %92)
br label %94
94: ; preds = %91, %88
%95 = load i32*, i32** %14
%96 = icmp ne i32* %95, null
br i1 %96, label %100, label %97
97: ; preds = %94
%98 = load i32, i32* %17
%99 = call i32 @close(i32 %98)
br label %100
100: ; preds = %97, %94
%101 = load i32*, i32** %15
%102 = icmp ne i32* %101, null
br i1 %102, label %106, label %103
103: ; preds = %100
%104 = load i32, i32* %18
%105 = call i32 @close(i32 %104)
br label %106
106: ; preds = %103, %100
%107 = load i32, i32* %19
store i32 %107, i32* %8
br label %127
108: ; preds = %67, %62
%109 = load i32*, i32** %13
%110 = icmp ne i32* %109, null
br i1 %110, label %114, label %111
111: ; preds = %108
%112 = load i32, i32* %16
%113 = call i32 @close(i32 %112)
br label %114
114: ; preds = %111, %108
%115 = load i32*, i32** %14
%116 = icmp ne i32* %115, null
br i1 %116, label %120, label %117
117: ; preds = %114
%118 = load i32, i32* %17
%119 = call i32 @close(i32 %118)
br label %120
120: ; preds = %117, %114
%121 = load i32*, i32** %15
%122 = icmp ne i32* %121, null
br i1 %122, label %126, label %123
123: ; preds = %120
%124 = load i32, i32* %18
%125 = call i32 @close(i32 %124)
br label %126
126: ; preds = %123, %120
store i32 -1, i32* %8
br label %127
127: ; preds = %126, %106, %25
%128 = load i32, i32* %8
ret i32 %128
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define i32 @spawn(i8* %0, i8** %1, i32 %2, i32 %3, i32* %4, i32* %5, i32* %6) {
%8 = icmp ne i8* %0, null
%9 = icmp ne i8** %1, null
%10 = and i1 %8, %9
br i1 %10, label %11, label %71
11: ; preds = %7
%12 = icmp eq i32* %4, null
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 @fileno(i32* %4)
br label %18
15: ; preds = %11
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %15, %13
%19 = phi i32 [ %14, %13 ], [ %17, %15 ]
%20 = icmp eq i32* %5, null
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @fileno(i32* %5)
br label %26
23: ; preds = %18
%24 = load i32, i32* @O_WRONLY
%25 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23, %21
%27 = phi i32 [ %22, %21 ], [ %25, %23 ]
%28 = icmp eq i32* %6, null
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = tail call i32 @fileno(i32* %6)
br label %34
31: ; preds = %26
%32 = load i32, i32* @O_WRONLY
%33 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %31, %29
%35 = phi i32 [ %30, %29 ], [ %33, %31 ]
%36 = or i32 %27, %19
%37 = or i32 %36, %35
%38 = icmp sgt i32 %37, -1
br i1 %38, label %39, label %61
39: ; preds = %34
%40 = tail call i32 (...) @fork()
%41 = icmp slt i32 %40, 0
br i1 %41, label %61, label %42
42: ; preds = %39
%43 = icmp eq i32 %40, 0
br i1 %43, label %44, label %54
44: ; preds = %42
%45 = tail call i32 @dup2(i32 %19, i32 0)
%46 = tail call i32 @dup2(i32 %27, i32 1)
%47 = tail call i32 @dup2(i32 %35, i32 2)
%48 = tail call i32 @closefrom(i32 3)
%49 = tail call i32 @setgroups(i32 0, i32* null)
%50 = tail call i32 @setgid(i32 %3)
%51 = tail call i32 @setuid(i32 %2)
%52 = tail call i32 @execv(i8* %0, i8** %1)
%53 = tail call i32 @_exit(i32 1)
unreachable
54: ; preds = %42
br i1 %12, label %55, label %57
55: ; preds = %54
%56 = tail call i32 @close(i32 %19)
br label %57
57: ; preds = %55, %54
br i1 %20, label %58, label %60
58: ; preds = %57
%59 = tail call i32 @close(i32 %27)
br label %60
60: ; preds = %58, %57
br i1 %28, label %68, label %71
61: ; preds = %39, %34
br i1 %12, label %62, label %64
62: ; preds = %61
%63 = tail call i32 @close(i32 %19)
br label %64
64: ; preds = %62, %61
br i1 %20, label %65, label %67
65: ; preds = %64
%66 = tail call i32 @close(i32 %27)
br label %67
67: ; preds = %65, %64
br i1 %28, label %68, label %71
68: ; preds = %67, %60
%69 = phi i32 [ %40, %60 ], [ -1, %67 ]
%70 = tail call i32 @close(i32 %35)
br label %71
71: ; preds = %68, %67, %60, %7
%72 = phi i32 [ -1, %7 ], [ %40, %60 ], [ -1, %67 ], [ %69, %68 ]
ret i32 %72
}