#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 */
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void handle_connection(int *connfds, int num, fd_set *prset, fd_set *pallset) {
  int i, n;
  char buf[1024];
  memset(buf, 0, 1024);
  for (i = 0; i <= num; i++) {
    if (connfds[i] < 0)
      continue;
    if (FD_ISSET(connfds[i], prset)) {
      n = read(connfds[i], buf, 1024);
      if (n == 0) {
        close(connfds[i]);
        FD_CLR(connfds[i], pallset);
        connfds[i] = -1;
        continue;
      }
      printf("read msg is: ");
      write(STDOUT_FILENO, buf, n);
      write(connfds[i], buf, n);
    }
  }
}


x86 assembly (clang, -O0):
<handle_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     1297 <handle_connection+0x127>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jge    11c7 <handle_connection+0x57>
jmp    1289 <handle_connection+0x119>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1284 <handle_connection+0x114>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1246 <handle_connection+0xd6>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x20(%rbp),%rsi
call   1090 <FD_CLR@plt>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
jmp    1289 <handle_connection+0x119>
lea    0xdb3(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
jmp    1289 <handle_connection+0x119>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a4 <handle_connection+0x34>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rcx,0x8(%rsp)
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
test   %r12d,%r12d
js     1243 <handle_connection+0xd3>
add    $0x1,%r12d
xor    %r14d,%r14d
lea    0x10(%rsp),%r13
jmp    11f8 <handle_connection+0x88>
nopw   0x0(%rax,%rax,1)
mov    %eax,%ebp
lea    0xe37(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    (%rbx,%r14,4),%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
add    $0x1,%r14
cmp    %r14,%r12
je     1243 <handle_connection+0xd3>
mov    (%rbx,%r14,4),%edi
test   %edi,%edi
js     11ef <handle_connection+0x7f>
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11ef <handle_connection+0x7f>
mov    (%rbx,%r14,4),%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jne    11c0 <handle_connection+0x50>
mov    (%rbx,%r14,4),%edi
call   1070 <close@plt>
mov    (%rbx,%r14,4),%edi
mov    0x8(%rsp),%rsi
call   1090 <FD_CLR@plt>
movl   $0xffffffff,(%rbx,%r14,4)
ff
jmp    11ef <handle_connection+0x7f>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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