#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 list {
  int dummy;
};

/* Variables and functions */
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup2(int, int);
int errno;
int /*<<< orphan*/ execlp(char *, char *, char *, char const *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
struct list *list_from_fd(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ parent_write(int, struct list *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
struct list *pipe_readwrite(const char *cmd, struct list *l) {
  int parent_to_child[2], child_to_parent[2];
  if (pipe(parent_to_child) == -1 || pipe(child_to_parent) == -1) {
    close(parent_to_child[0]);
    close(parent_to_child[1]);
    close(child_to_parent[0]);
    close(child_to_parent[1]);
    return NULL;
  }
  switch (fork()) {
  case -1:
    return NULL;
  case 0:
    close(parent_to_child[1]);
    close(child_to_parent[0]);
    if (dup2(parent_to_child[0], STDIN_FILENO) == -1 ||
        dup2(child_to_parent[1], STDOUT_FILENO) == -1 ||
        dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {
      exit(-1);
    }
    execlp("sh", "sh", "-c", cmd, NULL);
    perror("exec()");
    exit(-1);
  default: {
    struct list *ret;
    int saveerrno;
    close(parent_to_child[0]);
    close(child_to_parent[1]);
    if (parent_write(parent_to_child[1], l))
      ret = NULL;
    else
      ret = list_from_fd(child_to_parent[0], NULL);
    saveerrno = errno;
    wait(NULL);
    errno = saveerrno;
    return ret;
  }
  }
}


x86 assembly (clang, -O0):
<pipe_readwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     11d4 <pipe_readwrite+0x34>
lea    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1201 <pipe_readwrite+0x61>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1346 <pipe_readwrite+0x1a6>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
sub    $0xffffffff,%eax
je     122b <pipe_readwrite+0x8b>
jmp    121b <pipe_readwrite+0x7b>
mov    -0x38(%rbp),%eax
test   %eax,%eax
je     1238 <pipe_readwrite+0x98>
jmp    12db <pipe_readwrite+0x13b>
movq   $0x0,-0x8(%rbp)
jmp    1346 <pipe_readwrite+0x1a6>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     129c <pipe_readwrite+0xfc>
mov    -0x24(%rbp),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     129c <pipe_readwrite+0xfc>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12a6 <pipe_readwrite+0x106>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rcx
lea    0xd4f(%rip),%rsi
lea    0xd4b(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1090 <execlp@plt>
lea    0xd3a(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <parent_write@plt>
cmp    $0x0,%eax
je     130d <pipe_readwrite+0x16d>
movq   $0x0,-0x30(%rbp)
jmp    131d <pipe_readwrite+0x17d>
mov    -0x28(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <list_from_fd@plt>
mov    %rax,-0x30(%rbp)
mov    0x2cb4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
mov    -0x34(%rbp),%ecx
mov    0x2c9c(%rip),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_readwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1206 <pipe_readwrite+0x66>
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1206 <pipe_readwrite+0x66>
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     1250 <pipe_readwrite+0xb0>
test   %eax,%eax
je     125e <pipe_readwrite+0xbe>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%edi
mov    %r15,%rsi
call   1030 <parent_write@plt>
test   %eax,%eax
je     122e <pipe_readwrite+0x8e>
xor    %ebx,%ebx
jmp    123c <pipe_readwrite+0x9c>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
xor    %ebx,%ebx
jmp    1250 <pipe_readwrite+0xb0>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   10a0 <list_from_fd@plt>
mov    %rax,%rbx
mov    0x2d95(%rip),%r14
mov    (%r14),%ebp
xor    %edi,%edi
call   10b0 <wait@plt>
mov    %ebp,(%r14)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     12b3 <pipe_readwrite+0x113>
mov    0xc(%rsp),%edi
mov    0x2d5e(%rip),%rbx
mov    (%rbx),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     12b3 <pipe_readwrite+0x113>
mov    (%rbx),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12bd <pipe_readwrite+0x11d>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xd3c(%rip),%rdi
lea    0xd38(%rip),%rdx
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   1090 <execlp@plt>
lea    0xd26(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.list = type { i32 }
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [7 x i8] c"exec()\00"
@errno = global i32 0
define %struct.list* @pipe_readwrite(i8* %0, %struct.list* %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = bitcast [2 x i32]* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %26
14: ; preds = %10, %2
%15 = load i32, i32* %7
%16 = call i32 @close(i32 %15)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
br label %72
26: ; preds = %10
%27 = call i32 (...) @fork()
switch i32 %27, label %55 [
i32 -1, label %72
i32 0, label %28
]
28: ; preds = %26
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %11
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %7
%35 = load i32, i32* @STDIN_FILENO
%36 = call i32 @dup2(i32 %34, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %49, label %38
38: ; preds = %28
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%40 = load i32, i32* %39
%41 = load i32, i32* @STDOUT_FILENO
%42 = call i32 @dup2(i32 %40, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %49, label %44
44: ; preds = %38
%45 = load i32, i32* @STDOUT_FILENO
%46 = load i32, i32* @STDERR_FILENO
%47 = call i32 @dup2(i32 %45, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %44, %38, %28
%50 = call i32 @exit(i32 -1)
unreachable
51: ; preds = %44
%52 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32* null)
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %26
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32 @parent_write(i32 %62, %struct.list* %1)
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %55
%66 = load i32, i32* %11
%67 = call %struct.list* @list_from_fd(i32 %66, i32* null)
br label %68
68: ; preds = %55, %65
%69 = phi %struct.list* [ %67, %65 ], [ null, %55 ]
%70 = load i32, i32* @errno
%71 = call i32 @wait(i32* null)
store i32 %70, i32* @errno
br label %72
72: ; preds = %26, %68, %14
%73 = phi %struct.list* [ null, %14 ], [ %69, %68 ], [ null, %26 ]
ret %struct.list* %73
}