#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 /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int pipe_child(int fd, int orig_fd) {
  const char *fd_info = (orig_fd == 1) ? "out" : "err";
  char buf[4096];
  int r;
  while ((r = read(fd, buf, sizeof(buf))) >= 0) {
    int w;
    int len;
    len = r;
    do {
      w = write(orig_fd, buf, len);
      if (w < 0) {
        fprintf(stderr,
                "unable to write to std%s: "
                "%s\n",
                fd_info, strerror(errno));
        exit(1);
      }
      len -= w;
    } while (len > 0);
  }
  fprintf(stderr, "unable to read from std%s: %s\n", fd_info, strerror(errno));
  exit(1);
}


x86 assembly (clang, -O0):
<pipe_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edx
lea    0xe99(%rip),%rax
lea    0xe8e(%rip),%rcx
cmp    $0x1,%edx
cmove  %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
jl     1248 <pipe_child+0xf8>
mov    -0x1014(%rbp),%eax
mov    %eax,-0x101c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x101c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    1222 <pipe_child+0xd2>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x102c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x102c(%rbp),%edi
mov    -0x1028(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf5(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x1018(%rbp),%ecx
mov    -0x101c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jg     11ac <pipe_child+0x5c>
jmp    117d <pipe_child+0x2d>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x103c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x103c(%rbp),%edi
mov    -0x1038(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<pipe_child>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %esi,%ebp
mov    %edi,%r15d
cmp    $0x1,%esi
lea    0xe92(%rip),%rax
lea    0xe8f(%rip),%r14
cmove  %rax,%r14
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11ba <pipe_child+0x6a>
mov    %eax,%ebx
mov    %rsp,%r12
nop
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11da <pipe_child+0x8a>
sub    %eax,%ebx
jg     1190 <pipe_child+0x40>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1190 <pipe_child+0x40>
mov    0x2e1f(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe4e(%rip),%rsi
jmp    11f8 <pipe_child+0xa8>
mov    0x2dff(%rip),%rax
mov    (%rax),%ebx
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe10(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"out\00"
@.str.1 = [4 x i8] c"err\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"unable to write to std%s: %s\0A\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"unable to read from std%s: %s\0A\00"
define i32 @pipe_child(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 1
%12 = zext i1 %11 to i64
%13 = select i1 %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
store i8* %13, i8** %5
br label %14
14: ; preds = %42, %2
%15 = load i32, i32* %3
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 4096)
store i32 %17, i32* %7
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %43
19: ; preds = %14
%20 = load i32, i32* %7
store i32 %20, i32* %9
br label %21
21: ; preds = %39, %19
%22 = load i32, i32* %4
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%24 = load i32, i32* %9
%25 = call i32 @write(i32 %22, i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %5
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %30, i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %21
%36 = load i32, i32* %8
%37 = load i32, i32* %9
%38 = sub nsw i32 %37, %36
store i32 %38, i32* %9
br label %39
39: ; preds = %35
%40 = load i32, i32* %9
%41 = icmp sgt i32 %40, 0
br i1 %41, label %21, label %42
42: ; preds = %39
br label %14
43: ; preds = %14
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %5
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %45, i8* %47)
%49 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [4 x i8] c"out\00"
@.str.1 = [4 x i8] c"err\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"unable to write to std%s: %s\0A\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"unable to read from std%s: %s\0A\00"
define i32 @pipe_child(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = icmp eq i32 %1, 1
%5 = select i1 %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = call i32 @read(i32 %0, i8* %6, i32 4096)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %12, label %27
9: ; preds = %22
%10 = call i32 @read(i32 %0, i8* %6, i32 4096)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %25, label %27
12: ; preds = %2, %25
%13 = phi i32 [ %26, %25 ], [ %7, %2 ]
%14 = call i32 @write(i32 %1, i8* %6, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = sub nsw i32 %13, %14
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %9
25: ; preds = %22, %9
%26 = phi i32 [ %23, %22 ], [ %10, %9 ]
br label %12
27: ; preds = %9, %2
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %30)
%32 = tail call i32 @exit(i32 1)
unreachable
}