#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*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
scalar_t__ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ setlinebuf(int /*<<< orphan*/ *);
int vfork();
int start_child(char *cmd, FILE **readpipe, FILE **writepipe) {
  int childpid, pipe1[2], pipe2[2];
  if ((pipe(pipe1) < 0) || (pipe(pipe2) < 0)) {
    perror("pipe");
    exit(-1);
  }
  if ((childpid = vfork()) < 0) {
    perror("fork");
    exit(-1);
  } else if (childpid > 0) {
    close(pipe1[0]);
    close(pipe2[1]);
    *readpipe = fdopen(pipe2[0], "r");
    fcntl(pipe2[0], F_SETFL, O_NONBLOCK);
    *writepipe = fdopen(pipe1[1], "w");
    setlinebuf(*writepipe);
    return (childpid);
  } else {
    close(pipe1[1]);
    close(pipe2[0]);
    dup2(pipe1[0], 0);
    dup2(pipe2[1], 1);
    close(pipe1[0]);
    close(pipe2[1]);
    if (execlp(cmd, cmd, NULL) < 0)
      perror("execlp");
  }
  return (0);
}


x86 assembly (clang, -O0):
<start_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x2c(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jl     11da <start_child+0x3a>
lea    -0x34(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11f0 <start_child+0x50>
lea    0xe1f(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
call   10a0 <vfork@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1217 <start_child+0x77>
lea    0xdfd(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jle    1294 <start_child+0xf4>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%edi
lea    0xdcf(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x28(%rbp),%edi
lea    0xd9e(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <setlinebuf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1303 <start_child+0x163>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x30(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10c0 <execlp@plt>
cmp    $0x0,%rax
jge    12f2 <start_child+0x152>
lea    0xd21(%rip),%rdi
call   1090 <perror@plt>
jmp    12f7 <start_child+0x157>
jmp    12fc <start_child+0x15c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_child>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     12b5 <start_child+0x115>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     12b5 <start_child+0x115>
call   10a0 <vfork@plt>
test   %eax,%eax
js     12be <start_child+0x11e>
mov    %eax,%r14d
je     1248 <start_child+0xa8>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0xe02(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,(%rbx)
mov    0x8(%rsp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    0x14(%rsp),%edi
lea    0xdd6(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
call   1050 <setlinebuf@plt>
jmp    1299 <start_child+0xf9>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
xor    %ebx,%ebx
mov    %ebx,%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    %ebx,%edx
mov    %rbp,%rdi
mov    %rbp,%rsi
call   10c0 <execlp@plt>
test   %rax,%rax
js     12a7 <start_child+0x107>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd60(%rip),%rdi
call   1090 <perror@plt>
jmp    1299 <start_child+0xf9>
lea    0xd44(%rip),%rdi
jmp    12c5 <start_child+0x125>
lea    0xd40(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

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

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