#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 STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup2(int, int);
int errno;
int /*<<< orphan*/ exit(int);
int fork();
struct list *list_from_fd(int, int /*<<< orphan*/ *);
int pipe(int *);
int system(char const *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
struct list *pipe_read(const char *cmd) {
  int fds[2];
  if (pipe(fds) == -1)
    return NULL;
  switch (fork()) {
  case -1:
    return NULL;
  case 0:
    close(fds[0]);
    if (dup2(fds[1], STDOUT_FILENO) == -1)
      exit(-1);
    dup2(STDOUT_FILENO, STDERR_FILENO);
    exit(system(cmd));
  default: {
    struct list *l;
    int saveerrno;
    close(fds[1]);
    l = list_from_fd(fds[0], NULL);
    saveerrno = errno;
    wait(NULL);
    errno = saveerrno;
    return l;
  }
  }
}


x86 assembly (clang, -O0):
<pipe_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11ab <pipe_read+0x2b>
movq   $0x0,-0x8(%rbp)
jmp    1276 <pipe_read+0xf6>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     11d5 <pipe_read+0x55>
jmp    11c5 <pipe_read+0x45>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     11e2 <pipe_read+0x62>
jmp    1235 <pipe_read+0xb5>
movq   $0x0,-0x8(%rbp)
jmp    1276 <pipe_read+0xf6>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    120e <pipe_read+0x8e>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <list_from_fd@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    -0x24(%rbp),%ecx
mov    0x2d6c(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     11d7 <pipe_read+0x57>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11d7 <pipe_read+0x57>
test   %eax,%eax
je     11e3 <pipe_read+0x63>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1080 <list_from_fd@plt>
mov    %rax,%rbx
mov    0x2e0e(%rip),%r14
mov    (%r14),%ebp
xor    %edi,%edi
call   1090 <wait@plt>
mov    %ebp,(%r14)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2df9(%rip),%rbx
mov    (%rbx),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    120d <pipe_read+0x8d>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    (%rbx),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    %r14,%rdi
call   1030 <system@plt>
mov    %eax,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.list = type { i32 }
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@errno = global i32 0
define %struct.list* @pipe_read(i8* %0) {
%2 = alloca %struct.list*
%3 = alloca i8*
%4 = alloca [2 x i32]
%5 = alloca %struct.list*
%6 = alloca i32
store i8* %0, i8** %3
%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 %10, label %11
10: ; preds = %1
store %struct.list* null, %struct.list** %2
br label %43
11: ; preds = %1
%12 = call i32 (...) @fork()
switch i32 %12, label %32 [
i32 -1, label %13
i32 0, label %14
]
13: ; preds = %11
store %struct.list* null, %struct.list** %2
br label %43
14: ; preds = %11
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%19 = load i32, i32* %18
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %14
%26 = load i32, i32* @STDOUT_FILENO
%27 = load i32, i32* @STDERR_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = load i8*, i8** %3
%30 = call i32 @system(i8* %29)
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %11
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call %struct.list* @list_from_fd(i32 %37, i32* null)
store %struct.list* %38, %struct.list** %5
%39 = load i32, i32* @errno
store i32 %39, i32* %6
%40 = call i32 @wait(i32* null)
%41 = load i32, i32* %6
store i32 %41, i32* @errno
%42 = load %struct.list*, %struct.list** %5
store %struct.list* %42, %struct.list** %2
br label %43
43: ; preds = %32, %13, %10
%44 = load %struct.list*, %struct.list** %2
ret %struct.list* %44
}

LLVM IR (-O2):
%struct.list = type { i32 }
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@errno = global i32 0
define %struct.list* @pipe_read(i8* nocapture %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %33, label %7
7: ; preds = %1
%8 = call i32 (...) @fork()
switch i32 %8, label %25 [
i32 -1, label %33
i32 0, label %9
]
9: ; preds = %7
%10 = load i32, i32* %4
%11 = call i32 @close(i32 %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%13 = load i32, i32* %12
%14 = load i32, i32* @STDOUT_FILENO
%15 = call i32 @dup2(i32 %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %9
%20 = load i32, i32* @STDOUT_FILENO
%21 = load i32, i32* @STDERR_FILENO
%22 = call i32 @dup2(i32 %20, i32 %21)
%23 = call i32 @system(i8* %0)
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %7
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %4
%30 = call %struct.list* @list_from_fd(i32 %29, i32* null)
%31 = load i32, i32* @errno
%32 = call i32 @wait(i32* null)
store i32 %31, i32* @errno
br label %33
33: ; preds = %7, %1, %25
%34 = phi %struct.list* [ %30, %25 ], [ null, %1 ], [ null, %7 ]
ret %struct.list* %34
}