#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 ssize_t;
typedef int /*<<< orphan*/ line;

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ dprintf(int /*<<< orphan*/, char *, int, int, int);
int read_nonblock(int, char *, int);
int write_nonblock(int, char *, int);
int forward_to_many(int src, int *dests, size_t n) {
  char line[1024];
  ssize_t nr, nw;
  int i;
  while (nr = read_nonblock(src, line, sizeof(line)), nr > 0)
    for (i = 0; i < n; ++i)
      if (nw = write_nonblock(dests[i], line, nr), nw != nr)
        dprintf(STDERR_FILENO,
                "ERROR"
                " "
                "write(%d) %zi/%zi"
                "\n",
                dests[i], nw, nr);
  return nr;
}


x86 assembly (clang, -O0):
<forward_to_many>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read_nonblock@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    120e <forward_to_many+0xde>
movl   $0x0,-0x42c(%rbp)
movslq -0x42c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1209 <forward_to_many+0xd9>
mov    -0x10(%rbp),%rax
movslq -0x42c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write_nonblock@plt>
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%eax
cmp    -0x424(%rbp),%eax
je     11f0 <forward_to_many+0xc0>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x42c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x428(%rbp),%ecx
mov    -0x424(%rbp),%r8d
lea    0xe15(%rip),%rsi
call   1040 <dprintf@plt>
jmp    11f5 <forward_to_many+0xc5>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    1177 <forward_to_many+0x47>
jmp    1146 <forward_to_many+0x16>
mov    -0x424(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forward_to_many>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x10(%rsp),%rsi
mov    %edi,0xc(%rsp)
mov    $0x400,%edx
call   1050 <read_nonblock@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11d7 <forward_to_many+0xa7>
lea    0x10(%rsp),%r12
lea    0xe94(%rip),%r13
jmp    1187 <forward_to_many+0x57>
xchg   %ax,%ax
mov    0xc(%rsp),%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1050 <read_nonblock@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11d7 <forward_to_many+0xa7>
test   %r14,%r14
je     1170 <forward_to_many+0x40>
xor    %r15d,%r15d
jmp    11a9 <forward_to_many+0x79>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15
cmp    %r15,%r14
je     1170 <forward_to_many+0x40>
mov    (%rbx,%r15,4),%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <write_nonblock@plt>
cmp    %ebp,%eax
je     11a0 <forward_to_many+0x70>
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%edi
mov    (%rbx,%r15,4),%edx
mov    %r13,%rsi
mov    %eax,%ecx
mov    %ebp,%r8d
call   1040 <dprintf@plt>
jmp    11a0 <forward_to_many+0x70>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDERR_FILENO = global i32 0
@.str = [25 x i8] c"ERROR write(%d) %zi/%zi\0A\00"
define i32 @forward_to_many(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i64 %2, i64* %6
br label %11
11: ; preds = %49, %3
%12 = load i32, i32* %4
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%14 = call i32 @read_nonblock(i32 %12, i8* %13, i32 1024)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %50
17: ; preds = %11
store i32 0, i32* %10
br label %18
18: ; preds = %46, %17
%19 = load i32, i32* %10
%20 = sext i32 %19 to i64
%21 = load i64, i64* %6
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %49
23: ; preds = %18
%24 = load i32*, i32** %5
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* %8
%31 = call i32 @write_nonblock(i32 %28, i8* %29, i32 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = load i32, i32* %8
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %45
35: ; preds = %23
%36 = load i32, i32* @STDERR_FILENO
%37 = load i32*, i32** %5
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
%42 = load i32, i32* %9
%43 = load i32, i32* %8
%44 = call i32 @dprintf(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %41, i32 %42, i32 %43)
br label %45
45: ; preds = %35, %23
br label %46
46: ; preds = %45
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
br label %18
49: ; preds = %18
br label %11
50: ; preds = %11
%51 = load i32, i32* %8
ret i32 %51
}

LLVM IR (-O2):
@STDERR_FILENO = global i32 0
@.str = [25 x i8] c"ERROR write(%d) %zi/%zi\0A\00"
define i32 @forward_to_many(i32 %0, i32* nocapture %1, i64 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i32 @read_nonblock(i32 %0, i8* %5, i32 1024)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %28
8: ; preds = %3
%9 = icmp eq i64 %2, 0
br label %13
10: ; preds = %25, %13
%11 = call i32 @read_nonblock(i32 %0, i8* %5, i32 1024)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %8, %10
%14 = phi i32 [ %6, %8 ], [ %11, %10 ]
br i1 %9, label %10, label %15
15: ; preds = %13, %25
%16 = phi i64 [ %26, %25 ], [ 0, %13 ]
%17 = getelementptr inbounds i32, i32* %1, i64 %16
%18 = load i32, i32* %17
%19 = call i32 @write_nonblock(i32 %18, i8* %5, i32 %14)
%20 = icmp eq i32 %19, %14
br i1 %20, label %25, label %21
21: ; preds = %15
%22 = load i32, i32* @STDERR_FILENO
%23 = load i32, i32* %17
%24 = call i32 @dprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %19, i32 %14)
br label %25
25: ; preds = %15, %21
%26 = add nuw i64 %16, 1
%27 = icmp eq i64 %26, %2
br i1 %27, label %10, label %15
28: ; preds = %10, %3
%29 = phi i32 [ %6, %3 ], [ %11, %10 ]
ret i32 %29
}