#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 sockaddr_in {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int make_socket(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read_from_client(int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(void) {
  int sock;
  fd_set active_fd_set, read_fd_set;
  int i;
  struct sockaddr_in clientname;
  socklen_t size;
  sock = make_socket(5557);
  if (listen(sock, 1) < 0) {
    perror("listen");
    exit(EXIT_FAILURE);
  }
  FD_ZERO(&active_fd_set);
  FD_SET(sock, &active_fd_set);
  while (1) {
    read_fd_set = active_fd_set;
    if (select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) {
      perror("select");
      exit(EXIT_FAILURE);
    }
    for (i = 0; i < FD_SETSIZE; ++i)
      if (FD_ISSET(i, &read_fd_set)) {
        if (i == sock) {
          int new;
          size = sizeof clientname;
          new = accept(sock, NULL, NULL);
          if (new < 0) {
            perror("accept");
            exit(EXIT_FAILURE);
          }
          printf("Server: connection received\n");
          FD_SET(new, &active_fd_set);
        } else {
          if (read_from_client(i) < 0) {
            close(i);
            FD_CLR(i, &active_fd_set);
          }
        }
      }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x15b5,%edi
call   1090 <make_socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    121d <main+0x4d>
lea    0xdf6(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
lea    -0xc(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
cmp    $0x0,%rax
jge    1279 <main+0xa9>
lea    0xda1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2d56(%rip),%rcx
cmp    (%rcx),%eax
jge    1351 <main+0x181>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     133e <main+0x16e>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    130e <main+0x13e>
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12f1 <main+0x121>
lea    0xd30(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
lea    0xd1d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10f0 <FD_SET@plt>
jmp    1339 <main+0x169>
mov    -0x14(%rbp),%edi
call   10d0 <read_from_client@plt>
cmp    $0x0,%rax
jge    1334 <main+0x164>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10c0 <FD_CLR@plt>
jmp    1339 <main+0x169>
jmp    133e <main+0x16e>
jmp    1343 <main+0x173>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1280 <main+0xb0>
jmp    1232 <main+0x62>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x15b5,%edi
call   1090 <make_socket@plt>
mov    %eax,%r13d
mov    %eax,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
test   %rax,%rax
jns    1205 <main+0x35>
lea    0xe00(%rip),%rdi
jmp    1302 <main+0x132>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   10f0 <FD_SET@plt>
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
mov    0x2db5(%rip),%r15
mov    (%r15),%edi
mov    %rsp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
test   %rax,%rax
jns    124e <main+0x7e>
lea    0xdbe(%rip),%rdi
jmp    1302 <main+0x132>
mov    %rsp,%r14
lea    0x4(%rsp),%r12
jmp    127b <main+0xab>
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
test   %rax,%rax
js     1242 <main+0x72>
mov    (%r15),%edi
test   %edi,%edi
jle    1260 <main+0x90>
xor    %ebp,%ebp
jmp    12aa <main+0xda>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
call   10d0 <read_from_client@plt>
test   %rax,%rax
js     12e8 <main+0x118>
nopl   0x0(%rax)
add    $0x1,%ebp
mov    (%r15),%edi
cmp    %edi,%ebp
jge    1260 <main+0x90>
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12a0 <main+0xd0>
cmp    %ebp,%r13d
jne    1290 <main+0xc0>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
test   %eax,%eax
js     12fb <main+0x12b>
mov    %eax,%ebx
lea    0xd3e(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   10f0 <FD_SET@plt>
jmp    12a0 <main+0xd0>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebp,%edi
mov    %r12,%rsi
call   10c0 <FD_CLR@plt>
jmp    12a0 <main+0xd0>
lea    0xd0c(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
@.str = [7 x i8] c"listen\00"
@EXIT_FAILURE = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [7 x i8] c"accept\00"
@.str.3 = [29 x i8] c"Server: connection received\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 @make_socket(i32 5557)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = call i64 @listen(i32 %10, i32 1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %0
%18 = call i32 @FD_ZERO(i32* %3)
%19 = load i32, i32* %2
%20 = call i32 @FD_SET(i32 %19, i32* %3)
br label %21
21: ; preds = %17, %71
%22 = load i32, i32* %3
store i32 %22, i32* %4
%23 = load i32, i32* @FD_SETSIZE
%24 = call i64 @select(i32 %23, i32* %4, i32* null, i32* null, i32* null)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %21
store i32 0, i32* %5
br label %31
31: ; preds = %68, %30
%32 = load i32, i32* %5
%33 = load i32, i32* @FD_SETSIZE
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %71
35: ; preds = %31
%36 = load i32, i32* %5
%37 = call i64 @FD_ISSET(i32 %36, i32* %4)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %67
39: ; preds = %35
%40 = load i32, i32* %5
%41 = load i32, i32* %2
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %56
43: ; preds = %39
store i32 4, i32* %7
%44 = load i32, i32* %2
%45 = call i32 @accept(i32 %44, i32* null, i32* null)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %43
%53 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32, i32* %8
%55 = call i32 @FD_SET(i32 %54, i32* %3)
br label %66
56: ; preds = %39
%57 = load i32, i32* %5
%58 = call i64 @read_from_client(i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %56
%61 = load i32, i32* %5
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %5
%64 = call i32 @FD_CLR(i32 %63, i32* %3)
br label %65
65: ; preds = %60, %56
br label %66
66: ; preds = %65, %52
br label %67
67: ; preds = %66, %35
br label %68
68: ; preds = %67
%69 = load i32, i32* %5
%70 = add nsw i32 %69, 1
store i32 %70, i32* %5
br label %31
71: ; preds = %31
br label %21
}

LLVM IR (-O2):
@.str = [7 x i8] c"listen\00"
@EXIT_FAILURE = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [7 x i8] c"accept\00"
@str = [28 x i8] c"Server: connection received\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @make_socket(i32 5557)
%6 = tail call i64 @listen(i32 %5, i32 1)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = call i32 @FD_ZERO(i32* %1)
%14 = call i32 @FD_SET(i32 %5, i32* %1)
%15 = load i32, i32* %1
store i32 %15, i32* %2
%16 = load i32, i32* @FD_SETSIZE
%17 = call i64 @select(i32 %16, i32* %2, i32* null, i32* null, i32* null)
%18 = icmp slt i64 %17, 0
br i1 %18, label %27, label %24
19: ; preds = %53, %24
%20 = phi i32 [ %25, %24 ], [ %55, %53 ]
%21 = load i32, i32* %1
store i32 %21, i32* %2
%22 = call i64 @select(i32 %20, i32* %2, i32* null, i32* null, i32* null)
%23 = icmp slt i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %12, %19
%25 = load i32, i32* @FD_SETSIZE
%26 = icmp sgt i32 %25, 0
br i1 %26, label %31, label %19
27: ; preds = %19, %12
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %24, %53
%32 = phi i32 [ %54, %53 ], [ 0, %24 ]
%33 = call i64 @FD_ISSET(i32 %32, i32* %2)
%34 = icmp eq i64 %33, 0
br i1 %34, label %53, label %35
35: ; preds = %31
%36 = icmp eq i32 %32, %5
br i1 %36, label %37, label %47
37: ; preds = %35
%38 = call i32 @accept(i32 %5, i32* null, i32* null)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %37
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%46 = call i32 @FD_SET(i32 %38, i32* %1)
br label %53
47: ; preds = %35
%48 = call i64 @read_from_client(i32 %32)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = call i32 @close(i32 %32)
%52 = call i32 @FD_CLR(i32 %32, i32* %1)
br label %53
53: ; preds = %31, %47, %50, %44
%54 = add nuw nsw i32 %32, 1
%55 = load i32, i32* @FD_SETSIZE
%56 = icmp slt i32 %54, %55
br i1 %56, label %31, label %19
}