#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 */
struct pollfd {
  int fd;
  int revents;
};
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int POLLERR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
void recv_data(int fds, struct pollfd *set, int set_pos) {
  int sock;
  char buff[2048];
  int bytes;
  int i;
  for (i = 1; i <= set_pos; i++) {
    if ((sock = set[i].fd) < 0)
      continue;
    if (set[i].revents & (0x040 | POLLERR)) {
      bytes = read(sock, buff, sizeof(buff));
      if (bytes <= 0) {
        printf("client out\n");
        close(sock);
        set[i].fd = -1;
      } else {
        printf("read bytes: %d\n", bytes);
        if (write(sock, buff, bytes) == -1)
          perror("write data failed");
      }
      if (--fds <= 0)
        break;
    }
  }
}


x86 assembly (clang, -O0):
<recv_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x828(%rbp)
mov    -0x828(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     128f <recv_data+0x13f>
mov    -0x10(%rbp),%rax
movslq -0x828(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    119d <recv_data+0x4d>
jmp    127b <recv_data+0x12b>
mov    -0x10(%rbp),%rax
movslq -0x828(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%eax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%ecx
or     $0x40,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1276 <recv_data+0x126>
mov    -0x18(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
call   1060 <read@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0x0,-0x824(%rbp)
jg     1217 <recv_data+0xc7>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
movslq -0x828(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,8)
jmp    125a <recv_data+0x10a>
mov    -0x824(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    -0x824(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1255 <recv_data+0x105>
lea    0xdcc(%rip),%rdi
call   1070 <perror@plt>
jmp    125a <recv_data+0x10a>
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jg     1271 <recv_data+0x121>
jmp    128f <recv_data+0x13f>
jmp    1276 <recv_data+0x126>
jmp    127b <recv_data+0x12b>
mov    -0x828(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x828(%rbp)
jmp    116f <recv_data+0x1f>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
test   %edx,%edx
jle    1244 <recv_data+0xe4>
mov    %rsi,%rbx
mov    %edi,%ebp
add    $0x1,%edx
mov    $0x1,%r15d
lea    0x10(%rsp),%r14
mov    %rdx,0x8(%rsp)
jmp    11ad <recv_data+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r15
cmp    %r15,%rdx
je     1244 <recv_data+0xe4>
mov    (%rbx,%r15,8),%r12d
test   %r12d,%r12d
js     11a0 <recv_data+0x40>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
or     $0x40,%eax
test   %eax,0x4(%rbx,%r15,8)
je     11a0 <recv_data+0x40>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x800,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1211 <recv_data+0xb1>
mov    %eax,%r13d
lea    0xe19(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r13d,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    122d <recv_data+0xcd>
lea    0xe06(%rip),%rdi
call   1080 <perror@plt>
jmp    122d <recv_data+0xcd>
lea    0xe0a(%rip),%rdi
call   1030 <puts@plt>
mov    %r12d,%edi
call   1060 <close@plt>
movl   $0xffffffff,(%rbx,%r15,8)
ff
cmp    $0x2,%ebp
lea    0x10(%rsp),%r14
mov    0x8(%rsp),%rdx
jl     1244 <recv_data+0xe4>
add    $0xffffffff,%ebp
jmp    11a0 <recv_data+0x40>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLERR = global i32 0
@.str = [12 x i8] c"client out\0A\00"
@.str.1 = [16 x i8] c"read bytes: %d\0A\00"
@.str.2 = [18 x i8] c"write data failed\00"
define void @recv_data(i32 %0, %struct.pollfd* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.pollfd*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2048 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store %struct.pollfd* %1, %struct.pollfd** %5
store i32 %2, i32* %6
store i32 1, i32* %10
br label %11
11: ; preds = %68, %3
%12 = load i32, i32* %10
%13 = load i32, i32* %6
%14 = icmp sle i32 %12, %13
br i1 %14, label %15, label %71
15: ; preds = %11
%16 = load %struct.pollfd*, %struct.pollfd** %5
%17 = load i32, i32* %10
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i64 %18
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %19, i32 0, i32 0
%21 = load i32, i32* %20
store i32 %21, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %15
br label %68
24: ; preds = %15
%25 = load %struct.pollfd*, %struct.pollfd** %5
%26 = load i32, i32* %10
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %25, i64 %27
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i32, i32* @POLLERR
%32 = or i32 64, %31
%33 = and i32 %30, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %67
35: ; preds = %24
%36 = load i32, i32* %7
%37 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 2048)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp sle i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %35
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
%45 = load %struct.pollfd*, %struct.pollfd** %5
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %45, i64 %47
%49 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %48, i32 0, i32 0
store i32 -1, i32* %49
br label %61
50: ; preds = %35
%51 = load i32, i32* %9
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %7
%54 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%55 = load i32, i32* %9
%56 = call i32 @write(i32 %53, i8* %54, i32 %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %50
%59 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %60
60: ; preds = %58, %50
br label %61
61: ; preds = %60, %41
%62 = load i32, i32* %4
%63 = add nsw i32 %62, -1
store i32 %63, i32* %4
%64 = icmp sle i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %61
br label %71
66: ; preds = %61
br label %67
67: ; preds = %66, %24
br label %68
68: ; preds = %67, %23
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
store i32 %70, i32* %10
br label %11
71: ; preds = %65, %11
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLERR = global i32 0
@.str.1 = [16 x i8] c"read bytes: %d\0A\00"
@.str.2 = [18 x i8] c"write data failed\00"
@str = [11 x i8] c"client out\00"
define void @recv_data(i32 %0, %struct.pollfd* nocapture %1, i32 %2) {
%4 = alloca [2048 x i8]
%5 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%6 = icmp slt i32 %2, 1
br i1 %6, label %42, label %7
7: ; preds = %3
%8 = add nuw i32 %2, 1
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %7, %38
%11 = phi i64 [ 1, %7 ], [ %40, %38 ]
%12 = phi i32 [ %0, %7 ], [ %39, %38 ]
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 %11, i32 0
%14 = load i32, i32* %13
%15 = icmp slt i32 %14, 0
br i1 %15, label %38, label %16
16: ; preds = %10
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 %11, i32 1
%18 = load i32, i32* %17
%19 = load i32, i32* @POLLERR
%20 = or i32 %19, 64
%21 = and i32 %20, %18
%22 = icmp eq i32 %21, 0
br i1 %22, label %38, label %23
23: ; preds = %16
%24 = call i32 @read(i32 %14, i8* %5, i32 2048)
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%28 = tail call i32 @close(i32 %14)
store i32 -1, i32* %13
br label %35
29: ; preds = %23
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%31 = call i32 @write(i32 %14, i8* %5, i32 %24)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %29, %33, %26
%36 = add nsw i32 %12, -1
%37 = icmp slt i32 %12, 2
br i1 %37, label %42, label %38
38: ; preds = %16, %35, %10
%39 = phi i32 [ %12, %10 ], [ %36, %35 ], [ %12, %16 ]
%40 = add nuw nsw i64 %11, 1
%41 = icmp eq i64 %40, %9
br i1 %41, label %42, label %10
42: ; preds = %38, %35, %3
ret void
}