#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ do_exec(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fix_fds(int, int, int);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
void do_fork_exec(char *cmdline, int *pid, int *stdin_fd, int *stdout_fd,
                  int *stderr_fd) {
  int inpipe[2], outpipe[2], errpipe[2];
  if (pipe(inpipe) || pipe(outpipe) || (stderr_fd && pipe(errpipe))) {
    perror("pipe");
    exit(1);
  }
  switch (*pid = fork()) {
  case -1:
    perror("fork");
    exit(-1);
  case 0:
    if (stderr_fd) {
      fix_fds(inpipe[0], outpipe[1], errpipe[1]);
    } else
      fix_fds(inpipe[0], outpipe[1], 2);
    do_exec(cmdline);
    exit(-1);
  default:;
  }
  close(inpipe[0]);
  close(outpipe[1]);
  *stdin_fd = inpipe[1];
  *stdout_fd = outpipe[0];
  if (stderr_fd) {
    close(errpipe[1]);
    *stderr_fd = errpipe[0];
  }
}


x86 assembly (clang, -O0):
<do_fork_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jne    11d0 <do_fork_exec+0x60>
lea    -0x38(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jne    11d0 <do_fork_exec+0x60>
cmpq   $0x0,-0x28(%rbp)
je     11e6 <do_fork_exec+0x76>
lea    -0x40(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11e6 <do_fork_exec+0x76>
lea    0xe29(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
mov    %eax,%ecx
mov    %ecx,-0x44(%rbp)
sub    $0xffffffff,%eax
je     1216 <do_fork_exec+0xa6>
jmp    1206 <do_fork_exec+0x96>
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     122c <do_fork_exec+0xbc>
jmp    126d <do_fork_exec+0xfd>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
je     124a <do_fork_exec+0xda>
mov    -0x30(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    -0x3c(%rbp),%edx
call   1040 <fix_fds@plt>
jmp    125a <do_fork_exec+0xea>
mov    -0x30(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    $0x2,%edx
call   1040 <fix_fds@plt>
mov    -0x8(%rbp),%rdi
call   1030 <do_exec@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    1272 <do_fork_exec+0x102>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     12b0 <do_fork_exec+0x140>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_fork_exec>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8,%rbx
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r15
lea    0x18(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    1219 <do_fork_exec+0xa9>
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    1219 <do_fork_exec+0xa9>
test   %rbx,%rbx
je     11be <do_fork_exec+0x4e>
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    1219 <do_fork_exec+0xa9>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,0x0(%r13)
test   %eax,%eax
je     122f <do_fork_exec+0xbf>
cmp    $0xffffffff,%eax
je     125c <do_fork_exec+0xec>
mov    0x18(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    0x1c(%rsp),%eax
mov    %eax,(%r12)
mov    0x10(%rsp),%eax
mov    %eax,(%r14)
test   %rbx,%rbx
je     120b <do_fork_exec+0x9b>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%rbx)
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xde0(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x18(%rsp),%edi
mov    0x14(%rsp),%esi
mov    $0x2,%edx
test   %rbx,%rbx
je     1245 <do_fork_exec+0xd5>
mov    0xc(%rsp),%edx
call   1040 <fix_fds@plt>
mov    %r15,%rdi
call   1030 <do_exec@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xda2(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
define void @do_fork_exec(i8* %0, i32* %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca [2 x i32]
%12 = alloca [2 x i32]
%13 = alloca [2 x i32]
store i8* %0, i8** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %28, label %17
17: ; preds = %5
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%19 = call i64 @pipe(i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %17
%22 = load i32*, i32** %10
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%26 = call i64 @pipe(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24, %17, %5
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %24, %21
%32 = call i32 (...) @fork()
%33 = load i32*, i32** %7
store i32 %32, i32* %33
switch i32 %32, label %58 [
i32 -1, label %34
i32 0, label %37
]
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %31
%38 = load i32*, i32** %10
%39 = icmp ne i32* %38, null
br i1 %39, label %40, label %48
40: ; preds = %37
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @fix_fds(i32 %42, i32 %44, i32 %46)
br label %54
48: ; preds = %37
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%50 = load i32, i32* %49
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @fix_fds(i32 %50, i32 %52, i32 2)
br label %54
54: ; preds = %48, %40
%55 = load i8*, i8** %6
%56 = call i32 @do_exec(i8* %55)
%57 = call i32 @exit(i32 -1)
unreachable
58: ; preds = %31
br label %59
59: ; preds = %58
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%67 = load i32, i32* %66
%68 = load i32*, i32** %8
store i32 %67, i32* %68
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%70 = load i32, i32* %69
%71 = load i32*, i32** %9
store i32 %70, i32* %71
%72 = load i32*, i32** %10
%73 = icmp ne i32* %72, null
br i1 %73, label %74, label %81
74: ; preds = %59
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 1
%76 = load i32, i32* %75
%77 = call i32 @close(i32 %76)
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%79 = load i32, i32* %78
%80 = load i32*, i32** %10
store i32 %79, i32* %80
br label %81
81: ; preds = %74, %59
ret void
}

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