#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 read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int start_downstream(int ptmaster) {
  int ret = _ensure(120, fork());
  if (ret == 0) {
    char buf[1024];
    while (1) {
      char *bufptr = buf;
      int size = read(ptmaster, bufptr, sizeof(buf));
      if (size == 0)
        break;
      while (size > 0) {
        int rc = _ensure(128, 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    $0x78,%edi
call   1060 <_ensure@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    123b <start_downstream+0xeb>
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,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jne    11bd <start_downstream+0x6d>
jmp    1234 <start_downstream+0xe4>
jmp    11c2 <start_downstream+0x72>
cmpl   $0x0,-0x41c(%rbp)
jle    122f <start_downstream+0xdf>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    -0x418(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,%esi
mov    $0x80,%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    11c2 <start_downstream+0x72>
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    $0x78,%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>
test   %eax,%eax
jne    11a7 <start_downstream+0x57>
xor    %edi,%edi
call   1050 <exit@plt>
mov    %eax,%ebp
mov    0x2e40(%rip),%r12
jmp    11d6 <start_downstream+0x86>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11a0 <start_downstream+0x50>
test   %ebp,%ebp
jle    11c0 <start_downstream+0x70>
mov    %r14,%rbx
nopl   (%rax)
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    $0x80,%edi
mov    %eax,%esi
call   1060 <_ensure@plt>
cltq
add    %rax,%rbx
sub    %eax,%ebp
jg     11e0 <start_downstream+0x90>
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 120, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %41
12: ; preds = %1
br label %13
13: ; preds = %12, %38
%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)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
br label %39
21: ; preds = %13
br label %22
22: ; preds = %25, %21
%23 = load i32, i32* %6
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %22
%26 = load i32, i32* @STDOUT_FILENO
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = call i32 @write(i32 %26, i8* %27, i32 %28)
%30 = call i32 @_ensure(i32 128, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = load i8*, i8** %5
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %5
%35 = load i32, i32* %7
%36 = load i32, i32* %6
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %6
br label %22
38: ; preds = %22
br label %13
39: ; preds = %20
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %1
%42 = load i32, i32* %3
ret i32 %42
}

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 120, i32 %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %28
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 = icmp eq i32 %8, 0
br i1 %9, label %26, label %13
10: ; preds = %16, %13
%11 = call i32 @read(i32 %0, i8* %7, i32 1024)
%12 = icmp eq i32 %11, 0
br i1 %12, label %26, label %13
13: ; preds = %6, %10
%14 = phi i32 [ %11, %10 ], [ %8, %6 ]
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %10
16: ; preds = %13, %16
%17 = phi i8* [ %23, %16 ], [ %7, %13 ]
%18 = phi i32 [ %24, %16 ], [ %14, %13 ]
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @write(i32 %19, i8* %17, i32 %18)
%21 = tail call i32 @_ensure(i32 128, i32 %20)
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %17, i64 %22
%24 = sub nsw i32 %18, %21
%25 = icmp sgt i32 %24, 0
br i1 %25, label %16, label %10
26: ; preds = %10, %6
%27 = tail call i32 @exit(i32 0)
unreachable
28: ; preds = %1
ret i32 %4
}