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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ FD_CLR(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, int);
void write_stdin_all_sockets(int *sockets, size_t n, fd_set *all_read_set) {
  char buf[1024];
  size_t sum = 0;
  while (true) {
    int r = read(STDIN_FILENO, buf, 1024);
    if (r < 0) {
      if (errno == EWOULDBLOCK || errno == EAGAIN) {
        break;
      }
      perror("read stdin");
      exit(1);
    }
    if (r == 0) {
      fprintf(stderr, "stdin: pipeline closed.\n");
      FD_CLR(0, all_read_set);
      break;
    }
    for (size_t i = 0; i < n; i++) {
      if (sockets[i] >= 0) {
        if (write(sockets[i], buf, r) < 0 && errno != EAGAIN &&
            errno != EWOULDBLOCK) {
          perror("write socket");
          sockets[i] = -1;
        }
      }
    }
    sum += r;
  }
  if (sum > 0) {
    fprintf(stderr, "%lu bytes read from stdin.\n", sum);
  }
}


x86 assembly (clang, -O0):
<write_stdin_all_sockets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x428(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jge    11fe <write_stdin_all_sockets+0x9e>
mov    0x2e22(%rip),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
je     11e3 <write_stdin_all_sockets+0x83>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    11e8 <write_stdin_all_sockets+0x88>
jmp    131a <write_stdin_all_sockets+0x1ba>
lea    0xe11(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x42c(%rbp)
jne    1232 <write_stdin_all_sockets+0xd2>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rsi
xor    %edi,%edi
call   1070 <FD_CLR@plt>
jmp    131a <write_stdin_all_sockets+0x1ba>
movq   $0x0,-0x438(%rbp)
mov    -0x438(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1300 <write_stdin_all_sockets+0x1a0>
mov    -0x8(%rbp),%rax
mov    -0x438(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jl     12e4 <write_stdin_all_sockets+0x184>
mov    -0x8(%rbp),%rax
mov    -0x438(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x420(%rbp),%rsi
mov    -0x42c(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12df <write_stdin_all_sockets+0x17f>
mov    0x2d44(%rip),%rax
mov    (%rax),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
je     12df <write_stdin_all_sockets+0x17f>
mov    0x2d2a(%rip),%rax
mov    (%rax),%rax
mov    0x2d18(%rip),%rcx
cmp    (%rcx),%rax
je     12df <write_stdin_all_sockets+0x17f>
lea    0xd5c(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%rax
mov    -0x438(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
jmp    12e4 <write_stdin_all_sockets+0x184>
jmp    12e9 <write_stdin_all_sockets+0x189>
mov    -0x438(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x438(%rbp)
jmp    123d <write_stdin_all_sockets+0xdd>
movslq -0x42c(%rbp),%rax
add    -0x428(%rbp),%rax
mov    %rax,-0x428(%rbp)
jmp    1182 <write_stdin_all_sockets+0x22>
cmpq   $0x0,-0x428(%rbp)
jbe    1346 <write_stdin_all_sockets+0x1e6>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
mov    -0x428(%rbp),%rdx
lea    0xcf2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_stdin_all_sockets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1248 <write_stdin_all_sockets+0xe8>
mov    %eax,%ebp
mov    %r14,0x8(%rsp)
xor    %r15d,%r15d
lea    0x10(%rsp),%r13
jmp    11d2 <write_stdin_all_sockets+0x72>
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rax
add    %rax,%r15
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     124b <write_stdin_all_sockets+0xeb>
test   %ebp,%ebp
je     129d <write_stdin_all_sockets+0x13d>
test   %rbx,%rbx
je     11b0 <write_stdin_all_sockets+0x50>
xor    %r14d,%r14d
jmp    11f9 <write_stdin_all_sockets+0x99>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r14
cmp    %r14,%rbx
je     11b0 <write_stdin_all_sockets+0x50>
mov    (%r12,%r14,4),%edi
test   %edi,%edi
js     11f0 <write_stdin_all_sockets+0x90>
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11f0 <write_stdin_all_sockets+0x90>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%rax
je     11f0 <write_stdin_all_sockets+0x90>
mov    0x2da3(%rip),%rcx
cmp    (%rcx),%rax
je     11f0 <write_stdin_all_sockets+0x90>
lea    0xdeb(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,(%r12,%r14,4)
ff
jmp    11f0 <write_stdin_all_sockets+0x90>
xor    %r15d,%r15d
mov    0x2d86(%rip),%rax
mov    (%rax),%rax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%rax
je     126d <write_stdin_all_sockets+0x10d>
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%rax
jne    12d7 <write_stdin_all_sockets+0x177>
test   %r15,%r15
je     12c5 <write_stdin_all_sockets+0x165>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fprintf@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xd5e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %edi,%edi
mov    0x8(%rsp),%rsi
call   1070 <FD_CLR@plt>
test   %r15,%r15
jne    1272 <write_stdin_all_sockets+0x112>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd22(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str = [11 x i8] c"read stdin\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"stdin: pipeline closed.\0A\00"
@.str.2 = [13 x i8] c"write socket\00"
@.str.3 = [28 x i8] c"%lu bytes read from stdin.\0A\00"
define void @write_stdin_all_sockets(i32* %0, i64 %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca [1024 x i8]
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
store i32* %0, i32** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
store i64 0, i64* %8
br label %11
11: ; preds = %3, %75
%12 = load i32, i32* @STDIN_FILENO
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 1024)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @EWOULDBLOCK
%20 = icmp eq i64 %18, %19
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @EAGAIN
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21, %17
br label %80
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %11
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32*, i32** %6
%36 = call i32 @FD_CLR(i32 0, i32* %35)
br label %80
37: ; preds = %29
store i64 0, i64* %10
br label %38
38: ; preds = %72, %37
%39 = load i64, i64* %10
%40 = load i64, i64* %5
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %75
42: ; preds = %38
%43 = load i32*, i32** %4
%44 = load i64, i64* %10
%45 = getelementptr inbounds i32, i32* %43, i64 %44
%46 = load i32, i32* %45
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %71
48: ; preds = %42
%49 = load i32*, i32** %4
%50 = load i64, i64* %10
%51 = getelementptr inbounds i32, i32* %49, i64 %50
%52 = load i32, i32* %51
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%54 = load i32, i32* %9
%55 = call i64 @write(i32 %52, i8* %53, i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %70
57: ; preds = %48
%58 = load i64, i64* @errno
%59 = load i64, i64* @EAGAIN
%60 = icmp ne i64 %58, %59
br i1 %60, label %61, label %70
61: ; preds = %57
%62 = load i64, i64* @errno
%63 = load i64, i64* @EWOULDBLOCK
%64 = icmp ne i64 %62, %63
br i1 %64, label %65, label %70
65: ; preds = %61
%66 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%67 = load i32*, i32** %4
%68 = load i64, i64* %10
%69 = getelementptr inbounds i32, i32* %67, i64 %68
store i32 -1, i32* %69
br label %70
70: ; preds = %65, %61, %57, %48
br label %71
71: ; preds = %70, %42
br label %72
72: ; preds = %71
%73 = load i64, i64* %10
%74 = add i64 %73, 1
store i64 %74, i64* %10
br label %38
75: ; preds = %38
%76 = load i32, i32* %9
%77 = sext i32 %76 to i64
%78 = load i64, i64* %8
%79 = add i64 %78, %77
store i64 %79, i64* %8
br label %11
80: ; preds = %32, %25
%81 = load i64, i64* %8
%82 = icmp ugt i64 %81, 0
br i1 %82, label %83, label %87
83: ; preds = %80
%84 = load i32, i32* @stderr
%85 = load i64, i64* %8
%86 = call i32 (i32, i8*, ...) @fprintf(i32 %84, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 %85)
br label %87
87: ; preds = %83, %80
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str = [11 x i8] c"read stdin\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"stdin: pipeline closed.\0A\00"
@.str.2 = [13 x i8] c"write socket\00"
@.str.3 = [28 x i8] c"%lu bytes read from stdin.\0A\00"
define void @write_stdin_all_sockets(i32* nocapture %0, i64 %1, i32* %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @STDIN_FILENO
%7 = call i32 @read(i32 %6, i8* %5, i32 1024)
%8 = icmp slt i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = icmp eq i64 %1, 0
br label %22
11: ; preds = %51, %3
%12 = phi i64 [ 0, %3 ], [ %53, %51 ]
%13 = load i64, i64* @errno
%14 = load i64, i64* @EWOULDBLOCK
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %57, label %19
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %9, %51
%23 = phi i32 [ %7, %9 ], [ %55, %51 ]
%24 = phi i64 [ 0, %9 ], [ %53, %51 ]
%25 = icmp eq i32 %23, 0
br i1 %25, label %27, label %26
26: ; preds = %22
br i1 %10, label %51, label %31
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%30 = tail call i32 @FD_CLR(i32 0, i32* %2)
br label %57
31: ; preds = %26, %48
%32 = phi i64 [ %49, %48 ], [ 0, %26 ]
%33 = getelementptr inbounds i32, i32* %0, i64 %32
%34 = load i32, i32* %33
%35 = icmp sgt i32 %34, -1
br i1 %35, label %36, label %48
36: ; preds = %31
%37 = call i64 @write(i32 %34, i8* %5, i32 %23)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = load i64, i64* @errno
%41 = load i64, i64* @EAGAIN
%42 = icmp eq i64 %40, %41
%43 = load i64, i64* @EWOULDBLOCK
%44 = icmp eq i64 %40, %43
%45 = select i1 %42, i1 true, i1 %44
br i1 %45, label %48, label %46
46: ; preds = %39
%47 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %33
br label %48
48: ; preds = %31, %46, %39, %36
%49 = add nuw i64 %32, 1
%50 = icmp eq i64 %49, %1
br i1 %50, label %51, label %31
51: ; preds = %48, %26
%52 = sext i32 %23 to i64
%53 = add i64 %24, %52
%54 = load i32, i32* @STDIN_FILENO
%55 = call i32 @read(i32 %54, i8* %5, i32 1024)
%56 = icmp slt i32 %55, 0
br i1 %56, label %11, label %22
57: ; preds = %27, %11
%58 = phi i64 [ %24, %27 ], [ %12, %11 ]
%59 = icmp eq i64 %58, 0
br i1 %59, label %63, label %60
60: ; preds = %57
%61 = load i32, i32* @stderr
%62 = tail call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 %58)
br label %63
63: ; preds = %60, %57
ret void
}