#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 */
struct CmdGroup {
  int dummy;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ pipe(int *);
pid_t middlecmd(struct CmdGroup *cmdgroup, int *writepipe, int *readpipe) {
  pid_t pid;
  if (pipe(writepipe) == 0) {
    pid = fork();
    if (pid == 0) {
      dup2(readpipe[0], STDIN_FILENO);
      close(readpipe[0]);
      close(readpipe[1]);
      dup2(writepipe[1], STDOUT_FILENO);
      close(writepipe[1]);
      close(writepipe[0]);
    }
    return pid;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<middlecmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jne    11e1 <middlecmd+0xa1>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d4 <middlecmd+0x94>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e9 <middlecmd+0xa9>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<middlecmd>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1050 <pipe@plt>
mov    %rax,%rcx
mov    $0xffffffffffffffff,%rax
test   %rcx,%rcx
jne    11b1 <middlecmd+0x71>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jne    11b1 <middlecmd+0x71>
mov    (%r14),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    (%r14),%edi
call   1040 <close@plt>
mov    0x4(%r14),%edi
call   1040 <close@plt>
mov    0x4(%rbx),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x4(%rbx),%edi
call   1040 <close@plt>
mov    (%rbx),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.CmdGroup = type { i32 }
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i64 @middlecmd(%struct.CmdGroup* %0, i32* %1, i32* %2) {
%4 = alloca i64
%5 = alloca %struct.CmdGroup*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
store %struct.CmdGroup* %0, %struct.CmdGroup** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%9 = load i32*, i32** %6
%10 = call i64 @pipe(i32* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %45
12: ; preds = %3
%13 = call i64 (...) @fork()
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %43
16: ; preds = %12
%17 = load i32*, i32** %7
%18 = getelementptr inbounds i32, i32* %17, i64 0
%19 = load i32, i32* %18
%20 = load i32, i32* @STDIN_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = load i32*, i32** %7
%23 = getelementptr inbounds i32, i32* %22, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32*, i32** %7
%27 = getelementptr inbounds i32, i32* %26, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = load i32*, i32** %6
%31 = getelementptr inbounds i32, i32* %30, i64 1
%32 = load i32, i32* %31
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i32 @dup2(i32 %32, i32 %33)
%35 = load i32*, i32** %6
%36 = getelementptr inbounds i32, i32* %35, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i32*, i32** %6
%40 = getelementptr inbounds i32, i32* %39, i64 0
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %16, %12
%44 = load i64, i64* %8
store i64 %44, i64* %4
br label %46
45: ; preds = %3
store i64 -1, i64* %4
br label %46
46: ; preds = %45, %43
%47 = load i64, i64* %4
ret i64 %47
}

LLVM IR (-O2):
%struct.CmdGroup = type { i32 }
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i64 @middlecmd(%struct.CmdGroup* nocapture readnone %0, i32* %1, i32* nocapture %2) {
%4 = tail call i64 @pipe(i32* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %26
6: ; preds = %3
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %6
%10 = load i32, i32* %2
%11 = load i32, i32* @STDIN_FILENO
%12 = tail call i32 @dup2(i32 %10, i32 %11)
%13 = load i32, i32* %2
%14 = tail call i32 @close(i32 %13)
%15 = getelementptr inbounds i32, i32* %2, i64 1
%16 = load i32, i32* %15
%17 = tail call i32 @close(i32 %16)
%18 = getelementptr inbounds i32, i32* %1, i64 1
%19 = load i32, i32* %18
%20 = load i32, i32* @STDOUT_FILENO
%21 = tail call i32 @dup2(i32 %19, i32 %20)
%22 = load i32, i32* %18
%23 = tail call i32 @close(i32 %22)
%24 = load i32, i32* %1
%25 = tail call i32 @close(i32 %24)
br label %26
26: ; preds = %3, %6, %9
%27 = phi i64 [ 0, %9 ], [ %7, %6 ], [ -1, %3 ]
ret i64 %27
}