#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exec_cmd(int, char **);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
pid_t popen_pipes(int ac, char *av[], int *infd, int *outfd, int *errfd) {
  int in_pipe[2] = {};
  int out_pipe[2] = {};
  int err_pipe[2] = {};
  pipe(in_pipe);
  pipe(out_pipe);
  pipe(err_pipe);
  pid_t pid;
  if ((pid = fork()) == -1) {
    perror("fork");
    return -1;
  } else if (pid == 0) {
    close(in_pipe[1]);
    dup2(in_pipe[0], 0);
    close(in_pipe[0]);
    close(out_pipe[0]);
    dup2(out_pipe[1], 1);
    close(out_pipe[1]);
    close(err_pipe[0]);
    dup2(err_pipe[1], 2);
    close(err_pipe[1]);
    exec_cmd(ac, av);
  }
  close(in_pipe[0]);
  close(out_pipe[1]);
  close(err_pipe[1]);
  *infd = in_pipe[1];
  *outfd = out_pipe[0];
  *errfd = err_pipe[0];
  return pid;
}


x86 assembly (clang, -O0):
<popen_pipes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rdi
call   1060 <pipe@plt>
lea    -0x38(%rbp),%rdi
call   1060 <pipe@plt>
lea    -0x40(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x44(%rbp)
cmp    $0xffffffff,%eax
jne    1201 <popen_pipes+0x91>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <popen_pipes+0x139>
cmpl   $0x0,-0x44(%rbp)
jne    126b <popen_pipes+0xfb>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%edi
call   1050 <close@plt>
mov    -0x34(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%edi
call   1050 <close@plt>
mov    -0x3c(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1070 <exec_cmd@plt>
jmp    1270 <popen_pipes+0x100>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x3c(%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)
mov    -0x40(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popen_pipes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %edi,%r13d
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
mov    %rsp,%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11d6 <popen_pipes+0x76>
cmp    $0xffffffff,%ebp
jne    123d <popen_pipes+0xdd>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
jmp    126b <popen_pipes+0x10b>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    %r13d,%edi
mov    %r12,%rsi
call   1060 <exec_cmd@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%eax
mov    %eax,(%rbx)
mov    0x8(%rsp),%eax
mov    %eax,(%r15)
mov    (%rsp),%eax
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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