#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*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ setsid();
int write(int, char *, int);
void do_forward(void) {
  char buf[4096], *b;
  int r, rr;
  setsid();
  while ((r = read(0, buf, sizeof(buf))) > 0) {
    b = buf;
    while (r > 0) {
      rr = write(1, b, r);
      if (rr == -1) {
        perror("write");
        rr = r;
      }
      r -= rr;
    }
  }
  _exit(0);
}


x86 assembly (clang, -O0):
<do_forward>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    $0x0,%al
call   1050 <setsid@plt>
lea    -0x1000(%rbp),%rsi
xor    %edi,%edi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x100c(%rbp)
cmp    $0x0,%eax
jle    11ff <do_forward+0xaf>
lea    -0x1000(%rbp),%rax
mov    %rax,-0x1008(%rbp)
cmpl   $0x0,-0x100c(%rbp)
jle    11fa <do_forward+0xaa>
mov    -0x1008(%rbp),%rsi
mov    -0x100c(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    %eax,-0x1010(%rbp)
cmpl   $0xffffffff,-0x1010(%rbp)
jne    11e1 <do_forward+0x91>
lea    0xe30(%rip),%rdi
call   1070 <perror@plt>
mov    -0x100c(%rbp),%eax
mov    %eax,-0x1010(%rbp)
mov    -0x1010(%rbp),%ecx
mov    -0x100c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x100c(%rbp)
jmp    1192 <do_forward+0x42>
jmp    1162 <do_forward+0x12>
xor    %edi,%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<do_forward>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
xor    %eax,%eax
call   1050 <setsid@plt>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11cb <do_forward+0x7b>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xe7f(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     11bf <do_forward+0x6f>
sub    %eax,%ebp
jg     1190 <do_forward+0x40>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1190 <do_forward+0x40>
jmp    11cb <do_forward+0x7b>
mov    %r14,%rdi
call   1070 <perror@plt>
mov    %ebp,%eax
jmp    11a4 <do_forward+0x54>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define void @do_forward() {
%1 = alloca [4096 x i8]
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (...) @setsid()
br label %6
6: ; preds = %28, %0
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%8 = call i32 @read(i32 0, i8* %7, i32 4096)
store i32 %8, i32* %3
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %29
10: ; preds = %6
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
store i8* %11, i8** %2
br label %12
12: ; preds = %24, %10
%13 = load i32, i32* %3
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %12
%16 = load i8*, i8** %2
%17 = load i32, i32* %3
%18 = call i32 @write(i32 1, i8* %16, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* %3
store i32 %23, i32* %4
br label %24
24: ; preds = %21, %15
%25 = load i32, i32* %4
%26 = load i32, i32* %3
%27 = sub nsw i32 %26, %25
store i32 %27, i32* %3
br label %12
28: ; preds = %12
br label %6
29: ; preds = %6
%30 = call i32 @_exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define void @do_forward() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @setsid()
%4 = call i32 @read(i32 0, i8* %2, i32 4096)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %9, label %21
6: ; preds = %15
%7 = call i32 @read(i32 0, i8* %2, i32 4096)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %19, label %21
9: ; preds = %0, %19
%10 = phi i32 [ %20, %19 ], [ %4, %0 ]
%11 = call i32 @write(i32 1, i8* %2, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = phi i32 [ %10, %13 ], [ %11, %9 ]
%17 = sub nsw i32 %10, %16
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %6
19: ; preds = %15, %6
%20 = phi i32 [ %17, %15 ], [ %7, %6 ]
br label %9
21: ; preds = %6, %0
%22 = tail call i32 @_exit(i32 0)
unreachable
}