#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ error_exit(int, char *, ...);
int execve(char const *, char **, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int pipe(int *);
void start_script(const char *filename, int *rfd, int *wfd, pid_t *pid) {
  int fds[2] = {0};
  if (pipe(fds) == -1)
    error_exit(1, "error creating pipe\n");
  *pid = fork();
  if (*pid == -1)
    error_exit(1, "error forking\n");
  if (*pid == 0) {
    char *no_arguments[] = {NULL};
    close(0);
    dup(fds[0]);
    close(1);
    dup(fds[1]);
    close(2);
    open("/dev/null", O_RDWR);
    if (execve(filename, no_arguments, no_arguments) == -1)
      error_exit(1, "execve of %s failed\n", filename);
    exit(1);
  }
  *rfd = fds[0];
  *wfd = fds[1];
}


x86 assembly (clang, -O0):
<start_script>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11dd <start_script+0x4d>
mov    $0x1,%edi
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1070 <error_exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    120c <start_script+0x7c>
mov    $0x1,%edi
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1070 <error_exit@plt>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    12a4 <start_script+0x114>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1030 <dup@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
lea    0xdc0(%rip),%rdi
call   1090 <open@plt>
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1080 <execve@plt>
cmp    $0xffffffff,%eax
jne    129a <start_script+0x10a>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1070 <error_exit@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_script>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11c2 <start_script+0x42>
lea    0xe4a(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1060 <error_exit@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
jne    11e5 <start_script+0x65>
lea    0xe3e(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1060 <error_exit@plt>
mov    (%rbx),%eax
test   %eax,%eax
je     1204 <start_script+0x84>
mov    0x8(%rsp),%eax
mov    %eax,(%r12)
mov    0xc(%rsp),%eax
mov    %eax,(%r15)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
movq   $0x0,0x10(%rsp)
xor    %edi,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1030 <dup@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    0xdda(%rip),%rdi
call   1080 <open@plt>
lea    0x10(%rsp),%rdx
mov    %r14,%rdi
mov    %rdx,%rsi
call   1070 <execve@plt>
cmp    $0xffffffff,%eax
jne    127a <start_script+0xfa>
lea    0xdc3(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <error_exit@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"error creating pipe\0A\00"
@.str.1 = [15 x i8] c"error forking\0A\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [21 x i8] c"execve of %s failed\0A\00"
define void @start_script(i8* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca [2 x i32]
%10 = alloca [1 x i8*]
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%11 = bitcast [2 x i32]* %9 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = call i32 (i32, i8*, ...) @error_exit(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %4
%18 = call i32 (...) @fork()
%19 = load i32*, i32** %8
store i32 %18, i32* %19
%20 = load i32*, i32** %8
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 (i32, i8*, ...) @error_exit(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
%26 = load i32*, i32** %8
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %52
29: ; preds = %25
%30 = bitcast [1 x i8*]* %10 to i8*
%31 = call i32 @close(i32 0)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @dup(i32 %33)
%35 = call i32 @close(i32 1)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @dup(i32 %37)
%39 = call i32 @close(i32 2)
%40 = load i32, i32* @O_RDWR
%41 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = load i8*, i8** %5
%43 = getelementptr inbounds [1 x i8*], [1 x i8*]* %10, i64 0, i64 0
%44 = getelementptr inbounds [1 x i8*], [1 x i8*]* %10, i64 0, i64 0
%45 = call i32 @execve(i8* %42, i8** %43, i8** %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %29
%48 = load i8*, i8** %5
%49 = call i32 (i32, i8*, ...) @error_exit(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %47, %29
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %25
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%54 = load i32, i32* %53
%55 = load i32*, i32** %6
store i32 %54, i32* %55
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%57 = load i32, i32* %56
%58 = load i32*, i32** %7
store i32 %57, i32* %58
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"error creating pipe\0A\00"
@.str.1 = [15 x i8] c"error forking\0A\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [21 x i8] c"execve of %s failed\0A\00"
define void @start_script(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2, i32* nocapture %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to [2 x i32]*
%7 = alloca [1 x i8*]
%8 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%9 = bitcast i64* %5 to i32*
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = call i32 (i32, i8*, ...) @error_exit(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %4
%15 = call i32 (...) @fork()
store i32 %15, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 (i32, i8*, ...) @error_exit(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %3
br label %20
20: ; preds = %17, %14
%21 = phi i32 [ %19, %17 ], [ %15, %14 ]
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %43
23: ; preds = %20
%24 = bitcast [1 x i8*]* %7 to i8*
%25 = bitcast [1 x i8*]* %7 to i64*
store i64 0, i64* %25
%26 = call i32 @close(i32 0)
%27 = load i32, i32* %9
%28 = call i32 @dup(i32 %27)
%29 = call i32 @close(i32 1)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @dup(i32 %31)
%33 = call i32 @close(i32 2)
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds [1 x i8*], [1 x i8*]* %7, i64 0, i64 0
%37 = call i32 @execve(i8* %0, i8** %36, i8** %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %23
%40 = call i32 (i32, i8*, ...) @error_exit(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %41
41: ; preds = %39, %23
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %20
%44 = load i32, i32* %9
store i32 %44, i32* %1
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%46 = load i32, i32* %45
store i32 %46, i32* %2
ret void
}