#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;
};

/* Variables and functions */
int POLLIN;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void handle_connection(struct pollfd *connfds, int num) {
  int i, n;
  char buf[1024];
  memset(buf, 0, 1024);
  for (i = 1; i <= num; i++) {
    if (connfds[i].fd < 0)
      continue;
    if (connfds[i].revents & POLLIN) {
      n = read(connfds[i].fd, buf, 1024);
      if (n == 0) {
        close(connfds[i].fd);
        connfds[i].fd = -1;
        continue;
      }
      write(STDOUT_FILENO, buf, n);
      write(connfds[i].fd, buf, n);
    }
  }
}


x86 assembly (clang, -O0):
<handle_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
movl   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     123f <handle_connection+0xff>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,8)
jge    118f <handle_connection+0x4f>
jmp    1231 <handle_connection+0xf1>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%eax
mov    0x2e3e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     122c <handle_connection+0xec>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11fa <handle_connection+0xba>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,8)
jmp    1231 <handle_connection+0xf1>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%edi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
jmp    1231 <handle_connection+0xf1>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    116c <handle_connection+0x2c>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r14d
mov    %rdi,%r12
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
test   %r14d,%r14d
jle    11e3 <handle_connection+0xa3>
add    $0x1,%r14d
mov    $0x1,%ebx
mov    0x2e65(%rip),%r13
mov    %rsp,%r15
jmp    11ac <handle_connection+0x6c>
mov    %eax,%ebp
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    (%r12,%rbx,8),%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
je     11e3 <handle_connection+0xa3>
mov    (%r12,%rbx,8),%edi
test   %edi,%edi
js     11a3 <handle_connection+0x63>
mov    0x0(%r13),%eax
test   %eax,0x4(%r12,%rbx,8)
je     11a3 <handle_connection+0x63>
mov    %r15,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    1180 <handle_connection+0x40>
mov    (%r12,%rbx,8),%edi
call   1050 <close@plt>
movl   $0xffffffff,(%r12,%rbx,8)
ff
jmp    11a3 <handle_connection+0x63>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@STDOUT_FILENO = global i32 0
define void @handle_connection(%struct.pollfd* %0, i32 %1) {
%3 = alloca %struct.pollfd*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
store %struct.pollfd* %0, %struct.pollfd** %3
store i32 %1, i32* %4
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 1024)
store i32 1, i32* %5
br label %10
10: ; preds = %72, %2
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = icmp sle i32 %11, %12
br i1 %13, label %14, label %75
14: ; preds = %10
%15 = load %struct.pollfd*, %struct.pollfd** %3
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i64 %17
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %14
br label %72
23: ; preds = %14
%24 = load %struct.pollfd*, %struct.pollfd** %3
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %24, i64 %26
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = load i32, i32* @POLLIN
%31 = and i32 %29, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %71
33: ; preds = %23
%34 = load %struct.pollfd*, %struct.pollfd** %3
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %34, i64 %36
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 1024)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %57
44: ; preds = %33
%45 = load %struct.pollfd*, %struct.pollfd** %3
%46 = load i32, i32* %5
%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
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = load %struct.pollfd*, %struct.pollfd** %3
%53 = load i32, i32* %5
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %52, i64 %54
%56 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %55, i32 0, i32 0
store i32 -1, i32* %56
br label %72
57: ; preds = %33
%58 = load i32, i32* @STDOUT_FILENO
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%60 = load i32, i32* %6
%61 = call i32 @write(i32 %58, i8* %59, i32 %60)
%62 = load %struct.pollfd*, %struct.pollfd** %3
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %62, i64 %64
%66 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%69 = load i32, i32* %6
%70 = call i32 @write(i32 %67, i8* %68, i32 %69)
br label %71
71: ; preds = %57, %23
br label %72
72: ; preds = %71, %44, %22
%73 = load i32, i32* %5
%74 = add nsw i32 %73, 1
store i32 %74, i32* %5
br label %10
75: ; preds = %10
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@STDOUT_FILENO = global i32 0
define void @handle_connection(%struct.pollfd* nocapture %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 1024)
%6 = icmp slt i32 %1, 1
br i1 %6, label %35, label %7
7: ; preds = %2
%8 = add nuw i32 %1, 1
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %7, %32
%11 = phi i64 [ 1, %7 ], [ %33, %32 ]
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 %11, i32 0
%13 = load i32, i32* %12
%14 = icmp slt i32 %13, 0
br i1 %14, label %32, label %15
15: ; preds = %10
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 %11, i32 1
%17 = load i32, i32* %16
%18 = load i32, i32* @POLLIN
%19 = and i32 %18, %17
%20 = icmp eq i32 %19, 0
br i1 %20, label %32, label %21
21: ; preds = %15
%22 = call i32 @read(i32 %13, i8* %4, i32 1024)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* %12
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %12
br label %32
27: ; preds = %21
%28 = load i32, i32* @STDOUT_FILENO
%29 = call i32 @write(i32 %28, i8* %4, i32 %22)
%30 = load i32, i32* %12
%31 = call i32 @write(i32 %30, i8* %4, i32 %22)
br label %32
32: ; preds = %15, %27, %10, %24
%33 = add nuw nsw i64 %11, 1
%34 = icmp eq i64 %33, %9
br i1 %34, label %35, label %10
35: ; preds = %32, %2
ret void
}