#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, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ const *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int read_sockets_to_files(int *sockets, size_t n, fd_set const *fdset,
                          int *res_files, fd_set *all_read_set) {
  char buf[1024];
  int nclosed = 0;
  for (size_t i = 0; i < n; i++) {
    int s = sockets[i];
    if (s == -1) {
      continue;
    }
    if (!(FD_ISSET(s, fdset))) {
      continue;
    }
    size_t sum = 0;
    while (true) {
      int r;
      r = read(s, buf, 1024);
      if (r < 0) {
        if (errno == EWOULDBLOCK || errno == EAGAIN) {
          break;
        }
        perror("read socket");
        sockets[i] = -1;
        break;
      }
      if (r == 0) {
        fprintf(stderr, "Sock %d: connection closed.\n", s);
        FD_CLR(s, all_read_set);
        sockets[i] = -1;
        nclosed++;
        break;
      }
      write(res_files[i], buf, r);
      sum += r;
    }
    if (sum > 0) {
      fprintf(stderr, "Sock %d: %lu bytes received.\n", s, sum);
    }
  }
  return nclosed;
}


x86 assembly (clang, -O0):
<read_sockets_to_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x434(%rbp)
movq   $0x0,-0x440(%rbp)
mov    -0x440(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1360 <read_sockets_to_files+0x200>
mov    -0x8(%rbp),%rax
mov    -0x440(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x444(%rbp)
cmpl   $0xffffffff,-0x444(%rbp)
jne    11cb <read_sockets_to_files+0x6b>
jmp    1349 <read_sockets_to_files+0x1e9>
mov    -0x444(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    11e8 <read_sockets_to_files+0x88>
jmp    1349 <read_sockets_to_files+0x1e9>
movq   $0x0,-0x450(%rbp)
mov    -0x444(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x454(%rbp)
cmpl   $0x0,-0x454(%rbp)
jge    1279 <read_sockets_to_files+0x119>
mov    0x2db4(%rip),%rax
mov    (%rax),%rax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%rax
je     1251 <read_sockets_to_files+0xf1>
mov    0x2d9a(%rip),%rax
mov    (%rax),%rax
mov    0x2d78(%rip),%rcx
cmp    (%rcx),%rax
jne    1256 <read_sockets_to_files+0xf6>
jmp    1312 <read_sockets_to_files+0x1b2>
lea    0xda3(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%rax
mov    -0x440(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
jmp    1312 <read_sockets_to_files+0x1b2>
cmpl   $0x0,-0x454(%rbp)
jne    12d8 <read_sockets_to_files+0x178>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
mov    -0x444(%rbp),%edx
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x444(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1080 <FD_CLR@plt>
mov    -0x8(%rbp),%rax
mov    -0x440(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    1312 <read_sockets_to_files+0x1b2>
mov    -0x20(%rbp),%rax
mov    -0x440(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x430(%rbp),%rsi
mov    -0x454(%rbp),%edx
call   1030 <write@plt>
movslq -0x454(%rbp),%rax
add    -0x450(%rbp),%rax
mov    %rax,-0x450(%rbp)
jmp    11f3 <read_sockets_to_files+0x93>
cmpq   $0x0,-0x450(%rbp)
jbe    1344 <read_sockets_to_files+0x1e4>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
mov    -0x444(%rbp),%edx
mov    -0x450(%rbp),%rcx
lea    0xcec(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1349 <read_sockets_to_files+0x1e9>
mov    -0x440(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x440(%rbp)
jmp    1194 <read_sockets_to_files+0x34>
mov    -0x434(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sockets_to_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    %r8,0x20(%rsp)
mov    %rdx,0x28(%rsp)
test   %rsi,%rsi
je     12f2 <read_sockets_to_files+0x192>
mov    %rcx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x30(%rsp),%rbp
movl   $0x0,0xc(%rsp)
xor    %r13d,%r13d
mov    %rsi,0x18(%rsp)
mov    %rdi,0x10(%rsp)
jmp    11c2 <read_sockets_to_files+0x62>
nopl   0x0(%rax)
mov    0x18(%rsp),%r15
add    $0x1,%r13
cmp    %r15,%r13
je     12fa <read_sockets_to_files+0x19a>
mov    (%r12,%r13,4),%r14d
cmp    $0xffffffff,%r14d
je     11b5 <read_sockets_to_files+0x55>
mov    %r14d,%edi
mov    0x28(%rsp),%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     11b5 <read_sockets_to_files+0x55>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1237 <read_sockets_to_files+0xd7>
mov    %eax,%r12d
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
test   %r12d,%r12d
je     128e <read_sockets_to_files+0x12e>
mov    (%rbx,%r13,4),%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
mov    %r12d,%eax
add    %rax,%r15
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1200 <read_sockets_to_files+0xa0>
jmp    123a <read_sockets_to_files+0xda>
xor    %r15d,%r15d
mov    0x2d97(%rip),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
je     127f <read_sockets_to_files+0x11f>
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%rax
mov    0x10(%rsp),%r12
je     1284 <read_sockets_to_files+0x124>
lea    0xd98(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,(%r12,%r13,4)
ff
test   %r15,%r15
jne    12d0 <read_sockets_to_files+0x170>
jmp    11b0 <read_sockets_to_files+0x50>
mov    0x10(%rsp),%r12
test   %r15,%r15
jne    12d0 <read_sockets_to_files+0x170>
jmp    11b0 <read_sockets_to_files+0x50>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%edi
mov    0x20(%rsp),%rsi
call   1080 <FD_CLR@plt>
mov    0x10(%rsp),%r12
movl   $0xffffffff,(%r12,%r13,4)
ff
addl   $0x1,0xc(%rsp)
test   %r15,%r15
je     11b0 <read_sockets_to_files+0x50>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
lea    0xd49(%rip),%rsi
mov    %r14d,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11b0 <read_sockets_to_files+0x50>
movl   $0x0,0xc(%rsp)
mov    0xc(%rsp),%eax
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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