#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_addr;
  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*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *,
                           int /*<<< orphan*/);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read_from_client(int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
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)) {
        if (i == sock) {
          int new;
          size = sizeof(clientname);
          new = accept(sock, (struct sockaddr *)&clientname, &size);
          if (new < 0) {
            perror("accept");
            exit(EXIT_FAILURE);
          }
          fprintf(stderr, "Server: connect from host %s, port %hd.\n",
                  inet_ntoa(clientname.sin_addr), 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    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x15b3,%edi
call   10b0 <make_socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    123d <main+0x4d>
lea    0xdd6(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
lea    -0xc(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
cmp    $0x0,%rax
jge    1299 <main+0xa9>
lea    0xd81(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2d2e(%rip),%rcx
cmp    (%rcx),%eax
jge    139c <main+0x1ac>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1389 <main+0x199>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1359 <main+0x169>
movq   $0x8,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1313 <main+0x123>
lea    0xd0e(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xcd2(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x2c(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1110 <FD_SET@plt>
jmp    1384 <main+0x194>
mov    -0x14(%rbp),%edi
call   10f0 <read_from_client@plt>
cmp    $0x0,%rax
jge    137f <main+0x18f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10e0 <FD_CLR@plt>
jmp    1384 <main+0x194>
jmp    1389 <main+0x199>
jmp    138e <main+0x19e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12a0 <main+0xb0>
jmp    1252 <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   10b0 <make_socket@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    $0x1,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    1228 <main+0x38>
lea    0xddd(%rip),%rdi
jmp    1387 <main+0x197>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1110 <FD_SET@plt>
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
mov    0x2d8c(%rip),%rbx
mov    (%rbx),%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
jns    1270 <main+0x80>
lea    0xd9c(%rip),%rdi
jmp    1387 <main+0x197>
lea    0x4(%rsp),%r13
jmp    129b <main+0xab>
nopw   0x0(%rax,%rax,1)
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
js     1264 <main+0x74>
mov    (%rbx),%edi
test   %edi,%edi
jle    1280 <main+0x90>
xor    %r15d,%r15d
jmp    12db <main+0xeb>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
call   10f0 <read_from_client@plt>
test   %rax,%rax
js     1368 <main+0x178>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15d
mov    (%rbx),%edi
cmp    %edi,%r15d
jge    1280 <main+0x90>
mov    %r15d,%edi
mov    %r13,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12d0 <main+0xe0>
cmp    %r15d,%r14d
jne    12b0 <main+0xc0>
movq   $0x8,0x10(%rsp)
mov    %r14d,%edi
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   10d0 <accept@plt>
test   %eax,%eax
js     1380 <main+0x190>
mov    %eax,%r12d
mov    %r14d,%ebx
mov    %r13,%r14
mov    0x2cc1(%rip),%rax
mov    (%rax),%r13d
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0xc(%rsp),%edi
call   10a0 <ntohs@plt>
mov    %r13d,%edi
mov    %r14,%r13
mov    %ebx,%r14d
mov    0x2c91(%rip),%rbx
lea    0xcc7(%rip),%rsi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1060 <fprintf@plt>
mov    %r12d,%edi
mov    %rsp,%rsi
call   1110 <FD_SET@plt>
jmp    12d0 <main+0xe0>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    %rsp,%rsi
call   10e0 <FD_CLR@plt>
jmp    12d0 <main+0xe0>
lea    0xc87(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c3d(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, 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 = [7 x i8] c"accept\00"
@stderr = global i32 0
@.str.3 = [41 x i8] c"Server: connect from host %s, port %hd.\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 %35, 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
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
br label %32
27: ; preds = %66, %32
%28 = phi i32 [ %33, %32 ], [ %68, %66 ]
%29 = load i32, i32* %1
store i32 %29, i32* %2
%30 = call i64 @select(i32 %28, i32* %2, i32* null, i32* null, i32* null)
%31 = icmp slt i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %23, %27
%33 = load i32, i32* @FD_SETSIZE
%34 = icmp sgt i32 %33, 0
br i1 %34, label %39, label %27
35: ; preds = %27, %16
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %32, %66
%40 = phi i32 [ %67, %66 ], [ 0, %32 ]
%41 = call i64 @FD_ISSET(i32 %40, i32* %2)
%42 = icmp eq i64 %41, 0
br i1 %42, label %66, label %43
43: ; preds = %39
%44 = icmp eq i32 %40, %9
br i1 %44, label %45, label %60
45: ; preds = %43
store i64 8, 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.2, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %45
%53 = load i32, i32* @stderr
%54 = load i32, i32* %25
%55 = call i8* @inet_ntoa(i32 %54)
%56 = load i32, i32* %26
%57 = call i32 @ntohs(i32 %56)
%58 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %55, i32 %57)
%59 = call i32 @FD_SET(i32 %46, i32* %1)
br label %66
60: ; preds = %43
%61 = call i64 @read_from_client(i32 %40)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = call i32 @close(i32 %40)
%65 = call i32 @FD_CLR(i32 %40, i32* %1)
br label %66
66: ; preds = %39, %60, %63, %52
%67 = add nuw nsw i32 %40, 1
%68 = load i32, i32* @FD_SETSIZE
%69 = icmp slt i32 %67, %68
br i1 %69, label %39, label %27
}