#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 uint16_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ clientname;

/* 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, struct sockaddr *, size_t *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read_from_client(int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(void) {
  extern int make_socket(uint16_t port);
  int sock;
  fd_set active_fd_set, read_fd_set;
  int i;
  struct sockaddr_in clientname;
  size_t size;
  sock = make_socket(5555);
  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)) {
        printf("adding fd: %d\n", i);
        if (i == sock) {
          int new;
          size = sizeof(clientname);
          new = accept(sock, (struct sockaddr *)&clientname, &size);
          if (new < 0) {
            perror("accept");
            exit(EXIT_FAILURE);
          }
          printf("Server: connect from host , port %d.\n",
                 ntohs(clientname.sin_port));
          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    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x15b3,%edi
call   10a0 <make_socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    122d <main+0x4d>
lea    0xde6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    -0xc(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d91(%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    1289 <main+0xa9>
lea    0xd91(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2d46(%rip),%rcx
cmp    (%rcx),%eax
jge    137c <main+0x19c>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1369 <main+0x189>
mov    -0x14(%rbp),%esi
lea    0xd4c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1339 <main+0x159>
movq   $0x4,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1312 <main+0x132>
lea    0xd1e(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1090 <ntohs@plt>
mov    %eax,%esi
lea    0xd01(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1100 <FD_SET@plt>
jmp    1364 <main+0x184>
mov    -0x14(%rbp),%edi
call   10e0 <read_from_client@plt>
cmp    $0x0,%rax
jge    135f <main+0x17f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10d0 <FD_CLR@plt>
jmp    1364 <main+0x184>
jmp    1369 <main+0x189>
jmp    136e <main+0x18e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1290 <main+0xb0>
jmp    1242 <main+0x62>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x15b3,%edi
call   10a0 <make_socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
test   %rax,%rax
jns    1217 <main+0x37>
lea    0xdee(%rip),%rdi
jmp    133f <main+0x15f>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1100 <FD_SET@plt>
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
mov    0x2da6(%rip),%r12
mov    (%r12),%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
test   %rax,%rax
jns    1260 <main+0x80>
lea    0xdac(%rip),%rdi
jmp    133f <main+0x15f>
lea    0x4(%rsp),%r14
lea    0xda2(%rip),%r15
jmp    128b <main+0xab>
xchg   %ax,%ax
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
test   %rax,%rax
js     1254 <main+0x74>
mov    (%r12),%edi
test   %edi,%edi
jle    1270 <main+0x90>
xor    %r13d,%r13d
jmp    12bd <main+0xdd>
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
call   10e0 <read_from_client@plt>
test   %rax,%rax
js     1320 <main+0x140>
nopl   (%rax)
add    $0x1,%r13d
mov    (%r12),%edi
cmp    %edi,%r13d
jge    1270 <main+0x90>
mov    %r13d,%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12b0 <main+0xd0>
mov    %r15,%rdi
mov    %r13d,%esi
call   1040 <printf@plt>
cmp    %r13d,%ebx
jne    12a0 <main+0xc0>
movq   $0x4,0x10(%rsp)
mov    %ebx,%edi
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   10c0 <accept@plt>
test   %eax,%eax
js     1338 <main+0x158>
mov    %eax,%ebp
mov    0x8(%rsp),%edi
call   1090 <ntohs@plt>
lea    0xd17(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rsp,%rsi
call   1100 <FD_SET@plt>
jmp    12b0 <main+0xd0>
mov    %r13d,%edi
call   1050 <close@plt>
mov    %r13d,%edi
mov    %rsp,%rsi
call   10d0 <FD_CLR@plt>
jmp    12b0 <main+0xd0>
lea    0xcde(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = 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 = [15 x i8] c"adding fd: %d\0A\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [38 x i8] c"Server: connect from host , port %d.\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 i64
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 @make_socket(i32 5555)
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, %77
%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 = %74, %30
%32 = load i32, i32* %5
%33 = load i32, i32* @FD_SETSIZE
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %77
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 %73
39: ; preds = %35
%40 = load i32, i32* %5
%41 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %5
%43 = load i32, i32* %2
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %62
45: ; preds = %39
store i64 4, i64* %7
%46 = load i32, i32* %2
%47 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%48 = call i32 @accept(i32 %46, %struct.sockaddr* %47, i64* %7)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %45
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @ntohs(i32 %57)
%59 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %8
%61 = call i32 @FD_SET(i32 %60, i32* %3)
br label %72
62: ; preds = %39
%63 = load i32, i32* %5
%64 = call i64 @read_from_client(i32 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %62
%67 = load i32, i32* %5
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %5
%70 = call i32 @FD_CLR(i32 %69, i32* %3)
br label %71
71: ; preds = %66, %62
br label %72
72: ; preds = %71, %55
br label %73
73: ; preds = %72, %35
br label %74
74: ; preds = %73
%75 = load i32, i32* %5
%76 = add nsw i32 %75, 1
store i32 %76, i32* %5
br label %31
77: ; preds = %31
br label %21
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = 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 = [15 x i8] c"adding fd: %d\0A\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [38 x i8] c"Server: connect from host , port %d.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i64
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i64* %4 to i8*
%9 = tail call i32 @make_socket(i32 5555)
%10 = tail call i64 @listen(i32 %9, i32 1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = call i32 @FD_ZERO(i32* %1)
%18 = call i32 @FD_SET(i32 %9, i32* %1)
%19 = load i32, i32* %1
store i32 %19, i32* %2
%20 = load i32, i32* @FD_SETSIZE
%21 = call i64 @select(i32 %20, i32* %2, i32* null, i32* null, i32* null)
%22 = icmp slt i64 %21, 0
br i1 %22, label %34, label %23
23: ; preds = %16
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
br label %31
26: ; preds = %63, %31
%27 = phi i32 [ %32, %31 ], [ %65, %63 ]
%28 = load i32, i32* %1
store i32 %28, i32* %2
%29 = call i64 @select(i32 %27, i32* %2, i32* null, i32* null, i32* null)
%30 = icmp slt i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %23, %26
%32 = load i32, i32* @FD_SETSIZE
%33 = icmp sgt i32 %32, 0
br i1 %33, label %38, label %26
34: ; preds = %26, %16
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %31, %63
%39 = phi i32 [ %64, %63 ], [ 0, %31 ]
%40 = call i64 @FD_ISSET(i32 %39, i32* %2)
%41 = icmp eq i64 %40, 0
br i1 %41, label %63, label %42
42: ; preds = %38
%43 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%44 = icmp eq i32 %39, %9
br i1 %44, label %45, label %57
45: ; preds = %42
store i64 4, i64* %4
%46 = call i32 @accept(i32 %9, %struct.sockaddr* %24, i64* %4)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %45
%53 = load i32, i32* %25
%54 = call i32 @ntohs(i32 %53)
%55 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %54)
%56 = call i32 @FD_SET(i32 %46, i32* %1)
br label %63
57: ; preds = %42
%58 = call i64 @read_from_client(i32 %39)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = call i32 @close(i32 %39)
%62 = call i32 @FD_CLR(i32 %39, i32* %1)
br label %63
63: ; preds = %38, %57, %60, %52
%64 = add nuw nsw i32 %39, 1
%65 = load i32, i32* @FD_SETSIZE
%66 = icmp slt i32 %64, %65
br i1 %66, label %38, label %26
}