#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 timeval {
  int member_0;
  int /*<<< orphan*/ member_1;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ printf(char *, int, int);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                  int /*<<< orphan*/, struct timeval *);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
void servlet(int server) {
  fd_set set;
  int maxfd = server;
  int ceiling = 0;
  FD_ZERO(&set);
  FD_SET(server, &set);
  for (;;) {
    struct timeval timeout = {2, 0};
    if (select(maxfd + 1, &set, 0, 0, &timeout) > 0) {
      if (FD_ISSET(server, &set)) {
        if (ceiling < 5) {
          int client = accept(server, 0, 0);
          if (maxfd < client)
            maxfd = client;
          FD_SET(client, &set);
          ceiling++;
          printf("select process #%d: %d connected\n", getpid(), ceiling);
        }
      } else {
        int i;
        for (i = 0; i < maxfd + 1; i++) {
          if (FD_ISSET(i, &set)) {
            char buffer[1024];
            int bytes;
            bytes = recv(i, buffer, sizeof(buffer), 0);
            if (bytes < 0) {
              close(i);
              FD_CLR(i, &set);
              ceiling--;
              printf("select process #%d: %d connected\n", getpid(), ceiling);
            } else
              send(i, buffer, bytes, 0);
          }
        }
      }
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<servlet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    0xe19(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x18(%rbp),%r8
mov    %ecx,%edx
call   10a0 <select@plt>
cmp    $0x0,%rax
jle    1349 <servlet+0x199>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     127f <servlet+0xcf>
cmpl   $0x5,-0x10(%rbp)
jge    127a <servlet+0xca>
mov    -0x4(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   10b0 <accept@plt>
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    124d <servlet+0x9d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xd93(%rip),%rdi
call   1070 <printf@plt>
jmp    1344 <servlet+0x194>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    133f <servlet+0x18f>
mov    -0x20(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     132c <servlet+0x17c>
mov    -0x20(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    1310 <servlet+0x160>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   10c0 <FD_CLR@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xd02(%rip),%rdi
call   1070 <printf@plt>
jmp    1327 <servlet+0x177>
mov    -0x20(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    132c <servlet+0x17c>
jmp    1331 <servlet+0x181>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1286 <servlet+0xd6>
jmp    1344 <servlet+0x194>
jmp    1349 <servlet+0x199>
jmp    11e0 <servlet+0x30>

x86 assembly (clang, -O2):
<servlet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %edi,%r12d
lea    0x1c(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
lea    0x20(%rsp),%r13
xor    %ebp,%ebp
mov    %r12d,%r15d
mov    %r12d,0xc(%rsp)
jmp    1200 <servlet+0x50>
mov    0xc(%rsp),%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movq   $0x2,0x10(%rsp)
lea    0x1(%r15),%r14d
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   10a0 <select@plt>
test   %rax,%rax
jle    1200 <servlet+0x50>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1280 <servlet+0xd0>
cmp    $0x4,%ebp
jg     1200 <servlet+0x50>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
cmp    %eax,%r15d
cmovl  %eax,%r15d
mov    %eax,%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
add    $0x1,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd97(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1070 <printf@plt>
jmp    1200 <servlet+0x50>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %r15d,%r15d
js     1200 <servlet+0x50>
xor    %r12d,%r12d
jmp    12a9 <servlet+0xf9>
xchg   %ax,%ax
mov    %r13,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
add    $0x1,%r12d
cmp    %r12d,%r14d
je     11ed <servlet+0x3d>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     129c <servlet+0xec>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %r12d,%edi
test   %eax,%eax
jns    1290 <servlet+0xe0>
call   1080 <close@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10c0 <FD_CLR@plt>
add    $0xffffffff,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd0d(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1070 <printf@plt>
jmp    129c <servlet+0xec>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.servlet.timeout = %struct.timeval { i32 2, i32 0 }
@.str = [34 x i8] c"select process #%d: %d connected\0A\00"
define void @servlet(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32
store i32 %0, i32* %2
%11 = load i32, i32* %2
store i32 %11, i32* %4
store i32 0, i32* %5
%12 = call i32 @FD_ZERO(i32* %3)
%13 = load i32, i32* %2
%14 = call i32 @FD_SET(i32 %13, i32* %3)
br label %15
15: ; preds = %83, %1
%16 = bitcast %struct.timeval* %6 to i8*
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 1
%19 = call i64 @select(i32 %18, i32* %3, i32 0, i32 0, %struct.timeval* %6)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %83
21: ; preds = %15
%22 = load i32, i32* %2
%23 = call i64 @FD_ISSET(i32 %22, i32* %3)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %45
25: ; preds = %21
%26 = load i32, i32* %5
%27 = icmp slt i32 %26, 5
br i1 %27, label %28, label %44
28: ; preds = %25
%29 = load i32, i32* %2
%30 = call i32 @accept(i32 %29, i32 0, i32 0)
store i32 %30, i32* %7
%31 = load i32, i32* %4
%32 = load i32, i32* %7
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = load i32, i32* %7
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %28
%37 = load i32, i32* %7
%38 = call i32 @FD_SET(i32 %37, i32* %3)
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
%41 = call i32 (...) @getpid()
%42 = load i32, i32* %5
%43 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %41, i32 %42)
br label %44
44: ; preds = %36, %25
br label %82
45: ; preds = %21
store i32 0, i32* %8
br label %46
46: ; preds = %78, %45
%47 = load i32, i32* %8
%48 = load i32, i32* %4
%49 = add nsw i32 %48, 1
%50 = icmp slt i32 %47, %49
br i1 %50, label %51, label %81
51: ; preds = %46
%52 = load i32, i32* %8
%53 = call i64 @FD_ISSET(i32 %52, i32* %3)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %77
55: ; preds = %51
%56 = load i32, i32* %8
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%58 = call i32 @recv(i32 %56, i8* %57, i32 1024, i32 0)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %71
61: ; preds = %55
%62 = load i32, i32* %8
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %8
%65 = call i32 @FD_CLR(i32 %64, i32* %3)
%66 = load i32, i32* %5
%67 = add nsw i32 %66, -1
store i32 %67, i32* %5
%68 = call i32 (...) @getpid()
%69 = load i32, i32* %5
%70 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %68, i32 %69)
br label %76
71: ; preds = %55
%72 = load i32, i32* %8
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%74 = load i32, i32* %10
%75 = call i32 @send(i32 %72, i8* %73, i32 %74, i32 0)
br label %76
76: ; preds = %71, %61
br label %77
77: ; preds = %76, %51
br label %78
78: ; preds = %77
%79 = load i32, i32* %8
%80 = add nsw i32 %79, 1
store i32 %80, i32* %8
br label %46
81: ; preds = %46
br label %82
82: ; preds = %81, %44
br label %83
83: ; preds = %82, %15
br label %15
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [34 x i8] c"select process #%d: %d connected\0A\00"
define void @servlet(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timeval*
%5 = alloca [1024 x i8]
%6 = bitcast i32* %2 to i8*
%7 = call i32 @FD_ZERO(i32* %2)
%8 = call i32 @FD_SET(i32 %0, i32* %2)
%9 = bitcast i64* %3 to i8*
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %11
11: ; preds = %54, %1
%12 = phi i32 [ 0, %1 ], [ %55, %54 ]
%13 = phi i32 [ %0, %1 ], [ %56, %54 ]
store i64 2, i64* %3
%14 = add nsw i32 %13, 1
%15 = call i64 @select(i32 %14, i32* %2, i32 0, i32 0, %struct.timeval* %4)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %54
17: ; preds = %11
%18 = call i64 @FD_ISSET(i32 %0, i32* %2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = icmp slt i32 %13, 0
br i1 %21, label %54, label %32
22: ; preds = %17
%23 = icmp slt i32 %12, 5
br i1 %23, label %24, label %54
24: ; preds = %22
%25 = call i32 @accept(i32 %0, i32 0, i32 0)
%26 = icmp slt i32 %13, %25
%27 = select i1 %26, i32 %25, i32 %13
%28 = call i32 @FD_SET(i32 %25, i32* %2)
%29 = add nsw i32 %12, 1
%30 = call i32 (...) @getpid()
%31 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %29)
br label %54
32: ; preds = %20, %50
%33 = phi i32 [ %52, %50 ], [ 0, %20 ]
%34 = phi i32 [ %51, %50 ], [ %12, %20 ]
%35 = call i64 @FD_ISSET(i32 %33, i32* %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %50, label %37
37: ; preds = %32
%38 = call i32 @recv(i32 %33, i8* %10, i32 1024, i32 0)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %37
%41 = call i32 @close(i32 %33)
%42 = call i32 @FD_CLR(i32 %33, i32* %2)
%43 = add nsw i32 %34, -1
%44 = call i32 (...) @getpid()
%45 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %44, i32 %43)
br label %48
46: ; preds = %37
%47 = call i32 @send(i32 %33, i8* %10, i32 %38, i32 0)
br label %48
48: ; preds = %46, %40
%49 = phi i32 [ %43, %40 ], [ %34, %46 ]
br label %50
50: ; preds = %32, %48
%51 = phi i32 [ %49, %48 ], [ %34, %32 ]
%52 = add nuw i32 %33, 1
%53 = icmp eq i32 %33, %13
br i1 %53, label %54, label %32
54: ; preds = %50, %20, %24, %22, %11
%55 = phi i32 [ %29, %24 ], [ %12, %22 ], [ %12, %11 ], [ %12, %20 ], [ %51, %50 ]
%56 = phi i32 [ %27, %24 ], [ %13, %22 ], [ %13, %11 ], [ %13, %20 ], [ %13, %50 ]
br label %11
}