#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 /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ parent_write(int, struct list *);
int pipe(int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int system(char const *);
int /*<<< orphan*/ wait(int *);
int pipe_write(const char *cmd, struct list *l, int close_out) {
  int fds[2];
  if (pipe(fds) == -1)
    return -1;
  switch (fork()) {
  case -1:
    return -1;
  case 0: {
    if (close_out) {
      freopen("/dev/null", "w", stdout);
      freopen("/dev/null", "w", stderr);
    }
    close(fds[1]);
    if (dup2(fds[0], STDIN_FILENO) == -1)
      exit(-1);
    exit(system(cmd));
  }
  default: {
    int ret;
    close(fds[0]);
    parent_write(fds[1], l);
    wait(&ret);
    return ret;
  }
  }
}


x86 assembly (clang, -O0):
<pipe_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x24(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11c1 <pipe_write+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <pipe_write+0x108>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     11eb <pipe_write+0x5b>
jmp    11db <pipe_write+0x4b>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     11f7 <pipe_write+0x67>
jmp    1275 <pipe_write+0xe5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <pipe_write+0x108>
cmpl   $0x0,-0x1c(%rbp)
je     1239 <pipe_write+0xa9>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
lea    0xdef(%rip),%rdi
lea    0xdf2(%rip),%rsi
call   1060 <freopen@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
lea    0xdd3(%rip),%rdi
lea    0xdd6(%rip),%rsi
call   1060 <freopen@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x24(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1265 <pipe_write+0xd5>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <system@plt>
mov    %eax,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <parent_write@plt>
lea    -0x28(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_write>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x4(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     11e4 <pipe_write+0x54>
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     11e9 <pipe_write+0x59>
test   %eax,%eax
je     11f2 <pipe_write+0x62>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
call   1030 <parent_write@plt>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
mov    0xc(%rsp),%eax
jmp    11e9 <pipe_write+0x59>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
test   %ebp,%ebp
je     122c <pipe_write+0x9c>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xdfa(%rip),%rbx
lea    0xdfd(%rip),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <freopen@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <freopen@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1256 <pipe_write+0xc6>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    %r14,%rdi
call   1040 <system@plt>
mov    %eax,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.list = type { i32 }
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
@STDIN_FILENO = global i32 0
define i32 @pipe_write(i8* %0, %struct.list* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.list*
%7 = alloca i32
%8 = alloca [2 x i32]
%9 = alloca i32
store i8* %0, i8** %5
store %struct.list* %1, %struct.list** %6
store i32 %2, i32* %7
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %50
14: ; preds = %3
%15 = call i32 (...) @fork()
switch i32 %15, label %40 [
i32 -1, label %16
i32 0, label %17
]
16: ; preds = %14
store i32 -1, i32* %4
br label %50
17: ; preds = %14
%18 = load i32, i32* %7
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i32, i32* @stdout
%22 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @stderr
%24 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %20, %17
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%30 = load i32, i32* %29
%31 = load i32, i32* @STDIN_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %25
%37 = load i8*, i8** %5
%38 = call i32 @system(i8* %37)
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %14
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%45 = load i32, i32* %44
%46 = load %struct.list*, %struct.list** %6
%47 = call i32 @parent_write(i32 %45, %struct.list* %46)
%48 = call i32 @wait(i32* %9)
%49 = load i32, i32* %9
store i32 %49, i32* %4
br label %50
50: ; preds = %40, %16, %13
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
%struct.list = type { i32 }
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
@STDIN_FILENO = global i32 0
define i32 @pipe_write(i8* nocapture %0, %struct.list* %1, i32 %2) {
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = bitcast [2 x i32]* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %41, label %10
10: ; preds = %3
%11 = call i32 (...) @fork()
switch i32 %11, label %32 [
i32 -1, label %41
i32 0, label %12
]
12: ; preds = %10
%13 = icmp eq i32 %2, 0
br i1 %13, label %19, label %14
14: ; preds = %12
%15 = load i32, i32* @stdout
%16 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @stderr
%18 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %14, %12
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %7
%24 = load i32, i32* @STDIN_FILENO
%25 = call i32 @dup2(i32 %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %19
%30 = call i32 @system(i8* %0)
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %10
%33 = bitcast i32* %5 to i8*
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @parent_write(i32 %37, %struct.list* %1)
%39 = call i32 @wait(i32* %5)
%40 = load i32, i32* %5
br label %41
41: ; preds = %10, %3, %32
%42 = phi i32 [ %40, %32 ], [ -1, %3 ], [ %11, %10 ]
ret i32 %42
}