#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*/ STDOUT_FILENO;
int _ensure(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int start_downstream(int down) {
  int ret = _ensure(113, fork());
  if (ret == 0) {
    char buf[1024];
    while (1) {
      char *bufptr = buf;
      int size = _ensure(118, read(down, bufptr, sizeof(buf)));
      if (size == 0)
        break;
      while (size > 0) {
        int rc = _ensure(121, write(STDOUT_FILENO, bufptr, size));
        bufptr += rc;
        size -= rc;
      }
    }
    exit(0);
  }
  return ret;
}


x86 assembly (clang, -O0):
<start_downstream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,%esi
mov    $0x71,%edi
call   1060 <_ensure@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1247 <start_downstream+0xf7>
jmp    1183 <start_downstream+0x33>
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x418(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%esi
mov    $0x76,%edi
call   1060 <_ensure@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jne    11c9 <start_downstream+0x79>
jmp    1240 <start_downstream+0xf0>
jmp    11ce <start_downstream+0x7e>
cmpl   $0x0,-0x41c(%rbp)
jle    123b <start_downstream+0xeb>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    -0x418(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,%esi
mov    $0x79,%edi
call   1060 <_ensure@plt>
mov    %eax,-0x420(%rbp)
mov    -0x420(%rbp),%ecx
mov    -0x418(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x418(%rbp)
mov    -0x420(%rbp),%ecx
mov    -0x41c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x41c(%rbp)
jmp    11ce <start_downstream+0x7e>
jmp    1183 <start_downstream+0x33>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_downstream>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %edi,%r15d
xor    %eax,%eax
call   1070 <fork@plt>
mov    $0x71,%edi
mov    %eax,%esi
call   1060 <_ensure@plt>
test   %eax,%eax
je     1189 <start_downstream+0x39>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%r14
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    $0x76,%edi
mov    %eax,%esi
call   1060 <_ensure@plt>
test   %eax,%eax
jne    11b3 <start_downstream+0x63>
xor    %edi,%edi
call   1050 <exit@plt>
mov    %eax,%ebp
mov    0x2e34(%rip),%r12
jmp    11e2 <start_downstream+0x92>
xchg   %ax,%ax
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    $0x76,%edi
mov    %eax,%esi
call   1060 <_ensure@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11ac <start_downstream+0x5c>
test   %ebp,%ebp
jle    11c0 <start_downstream+0x70>
mov    %r14,%rbx
nopl   0x0(%rax)
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    $0x79,%edi
mov    %eax,%esi
call   1060 <_ensure@plt>
cltq
add    %rax,%rbx
sub    %eax,%ebp
jg     11f0 <start_downstream+0xa0>
jmp    11c0 <start_downstream+0x70>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
define i32 @start_downstream(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = call i32 (...) @fork()
%9 = call i32 @_ensure(i32 113, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %42
12: ; preds = %1
br label %13
13: ; preds = %12, %39
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8* %14, i8** %5
%15 = load i32, i32* %2
%16 = load i8*, i8** %5
%17 = call i32 @read(i32 %15, i8* %16, i32 1024)
%18 = call i32 @_ensure(i32 118, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %13
br label %40
22: ; preds = %13
br label %23
23: ; preds = %26, %22
%24 = load i32, i32* %6
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %23
%27 = load i32, i32* @STDOUT_FILENO
%28 = load i8*, i8** %5
%29 = load i32, i32* %6
%30 = call i32 @write(i32 %27, i8* %28, i32 %29)
%31 = call i32 @_ensure(i32 121, i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = load i8*, i8** %5
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8* %35, i8** %5
%36 = load i32, i32* %7
%37 = load i32, i32* %6
%38 = sub nsw i32 %37, %36
store i32 %38, i32* %6
br label %23
39: ; preds = %23
br label %13
40: ; preds = %21
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %1
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
define i32 @start_downstream(i32 %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32 (...) @fork()
%4 = tail call i32 @_ensure(i32 113, i32 %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %30
6: ; preds = %1
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%8 = call i32 @read(i32 %0, i8* %7, i32 1024)
%9 = tail call i32 @_ensure(i32 118, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %28, label %15
11: ; preds = %18, %15
%12 = call i32 @read(i32 %0, i8* %7, i32 1024)
%13 = tail call i32 @_ensure(i32 118, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %6, %11
%16 = phi i32 [ %13, %11 ], [ %9, %6 ]
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %11
18: ; preds = %15, %18
%19 = phi i8* [ %25, %18 ], [ %7, %15 ]
%20 = phi i32 [ %26, %18 ], [ %16, %15 ]
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i32 @write(i32 %21, i8* %19, i32 %20)
%23 = tail call i32 @_ensure(i32 121, i32 %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %19, i64 %24
%26 = sub nsw i32 %20, %23
%27 = icmp sgt i32 %26, 0
br i1 %27, label %18, label %11
28: ; preds = %11, %6
%29 = tail call i32 @exit(i32 0)
unreachable
30: ; preds = %1
ret i32 %4
}