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

/* Variables and functions */
int 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 atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int make_socket(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(int argc, char **argv) {
  int sock;
  fd_set active_fd_set, read_fd_set;
  int i;
  struct sockaddr_in clientname;
  size_t size;
  int port;
  if (argc != 2) {
    fprintf(stderr, "Use: %s <port_number>\n", argv[0]);
    exit(1);
  }
  port = atoi(argv[1]);
  sock = make_socket(port);
  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);
            fprintf(stderr, "[conection closed by client]\n");
          }
        }
      }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1248 <main+0x48>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   10b0 <make_socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    1294 <main+0x94>
lea    0xd96(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
cmp    $0x0,%rax
jge    12f0 <main+0xf0>
lea    0xd41(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2cd7(%rip),%rcx
cmp    (%rcx),%eax
jge    140c <main+0x20c>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13f9 <main+0x1f9>
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    13b2 <main+0x1b2>
movq   $0x8,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    136a <main+0x16a>
lea    0xcce(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    0x2c6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
lea    0xc92(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1120 <FD_SET@plt>
jmp    13f4 <main+0x1f4>
mov    -0x20(%rbp),%edi
call   1100 <read_from_client@plt>
cmp    $0x0,%rax
jge    13ef <main+0x1ef>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10f0 <FD_CLR@plt>
mov    0x2c01(%rip),%rax
mov    (%rax),%edi
lea    0xc6e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    13f4 <main+0x1f4>
jmp    13f9 <main+0x1f9>
jmp    13fe <main+0x1fe>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12f7 <main+0xf7>
jmp    12a9 <main+0xa9>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    1243 <main+0x43>
mov    0x8(%rsi),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   10b0 <make_socket@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    $0x1,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    1267 <main+0x67>
lea    0xdd9(%rip),%rdi
jmp    13e4 <main+0x1e4>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdaa(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1120 <FD_SET@plt>
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
mov    0x2d4d(%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    12af <main+0xaf>
lea    0xd74(%rip),%rdi
jmp    13e4 <main+0x1e4>
lea    0x4(%rsp),%r13
jmp    12db <main+0xdb>
cs 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     12a3 <main+0xa3>
mov    (%rbx),%edi
test   %edi,%edi
jle    12c0 <main+0xc0>
xor    %r15d,%r15d
jmp    131b <main+0x11b>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
call   1100 <read_from_client@plt>
test   %rax,%rax
js     13ae <main+0x1ae>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15d
mov    (%rbx),%edi
cmp    %edi,%r15d
jge    12c0 <main+0xc0>
mov    %r15d,%edi
mov    %r13,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1310 <main+0x110>
cmp    %r15d,%r14d
jne    12f0 <main+0xf0>
movq   $0x8,0x10(%rsp)
mov    %r14d,%edi
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   10d0 <accept@plt>
test   %eax,%eax
js     13dd <main+0x1dd>
mov    %eax,%r12d
mov    %r14d,%ebx
mov    %r13,%r14
mov    0x2c7d(%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    0x2c4d(%rip),%rbx
lea    0xc9a(%rip),%rsi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r12d,%edi
mov    %rsp,%rsi
call   1120 <FD_SET@plt>
jmp    1310 <main+0x110>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    %rsp,%rsi
call   10f0 <FD_CLR@plt>
mov    0x2c18(%rip),%rax
mov    (%rax),%edi
lea    0xc85(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1310 <main+0x110>
lea    0xc41(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2be0(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Use: %s <port_number>\0A\00"
@.str.1 = [7 x i8] c"listen\00"
@EXIT_FAILURE = global i32 0
@FD_SETSIZE = global i32 0
@.str.2 = [7 x i8] c"select\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [42 x i8] c"[Server: connect from host %s, port %hd]\0A\00"
@.str.5 = [30 x i8] c"[conection closed by client]\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = call i32 @make_socket(i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = call i64 @listen(i32 %30, i32 1)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %23
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %23
%38 = call i32 @FD_ZERO(i32* %7)
%39 = load i32, i32* %6
%40 = call i32 @FD_SET(i32 %39, i32* %7)
br label %41
41: ; preds = %37, %101
%42 = load i32, i32* %7
store i32 %42, i32* %8
%43 = load i32, i32* @FD_SETSIZE
%44 = call i64 @select(i32 %43, i32* %8, i32* null, i32* null, i32* null)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %41
store i32 0, i32* %9
br label %51
51: ; preds = %98, %50
%52 = load i32, i32* %9
%53 = load i32, i32* @FD_SETSIZE
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %101
55: ; preds = %51
%56 = load i32, i32* %9
%57 = call i64 @FD_ISSET(i32 %56, i32* %8)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %97
59: ; preds = %55
%60 = load i32, i32* %9
%61 = load i32, i32* %6
%62 = icmp eq i32 %60, %61
br i1 %62, label %63, label %84
63: ; preds = %59
store i64 8, i64* %11
%64 = load i32, i32* %6
%65 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%66 = call i32 @accept(i32 %64, %struct.sockaddr* %65, i64* %11)
store i32 %66, i32* %13
%67 = load i32, i32* %13
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %63
%70 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
73: ; preds = %63
%74 = load i32, i32* @stderr
%75 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%76 = load i32, i32* %75
%77 = call i8* @inet_ntoa(i32 %76)
%78 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%79 = load i32, i32* %78
%80 = call i32 @ntohs(i32 %79)
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %77, i32 %80)
%82 = load i32, i32* %13
%83 = call i32 @FD_SET(i32 %82, i32* %7)
br label %96
84: ; preds = %59
%85 = load i32, i32* %9
%86 = call i64 @read_from_client(i32 %85)
%87 = icmp slt i64 %86, 0
br i1 %87, label %88, label %95
88: ; preds = %84
%89 = load i32, i32* %9
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* %9
%92 = call i32 @FD_CLR(i32 %91, i32* %7)
%93 = load i32, i32* @stderr
%94 = call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
br label %95
95: ; preds = %88, %84
br label %96
96: ; preds = %95, %73
br label %97
97: ; preds = %96, %55
br label %98
98: ; preds = %97
%99 = load i32, i32* %9
%100 = add nsw i32 %99, 1
store i32 %100, i32* %9
br label %51
101: ; preds = %51
br label %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Use: %s <port_number>\0A\00"
@.str.1 = [7 x i8] c"listen\00"
@EXIT_FAILURE = global i32 0
@FD_SETSIZE = global i32 0
@.str.2 = [7 x i8] c"select\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [42 x i8] c"[Server: connect from host %s, port %hd]\0A\00"
@.str.5 = [30 x i8] c"[conection closed by client]\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i64
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i64* %6 to i8*
%11 = icmp eq i32 %0, 2
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = tail call i32 @make_socket(i32 %20)
%22 = tail call i64 @listen(i32 %21, i32 1)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %17
%29 = call i32 @FD_ZERO(i32* %3)
%30 = call i32 @FD_SET(i32 %21, i32* %3)
%31 = load i32, i32* %3
store i32 %31, i32* %4
%32 = load i32, i32* @FD_SETSIZE
%33 = call i64 @select(i32 %32, i32* %4, i32* null, i32* null, i32* null)
%34 = icmp slt i64 %33, 0
br i1 %34, label %47, label %35
35: ; preds = %28
%36 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
br label %44
39: ; preds = %80, %44
%40 = phi i32 [ %45, %44 ], [ %82, %80 ]
%41 = load i32, i32* %3
store i32 %41, i32* %4
%42 = call i64 @select(i32 %40, i32* %4, i32* null, i32* null, i32* null)
%43 = icmp slt i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %35, %39
%45 = load i32, i32* @FD_SETSIZE
%46 = icmp sgt i32 %45, 0
br i1 %46, label %51, label %39
47: ; preds = %39, %28
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %44, %80
%52 = phi i32 [ %81, %80 ], [ 0, %44 ]
%53 = call i64 @FD_ISSET(i32 %52, i32* %4)
%54 = icmp eq i64 %53, 0
br i1 %54, label %80, label %55
55: ; preds = %51
%56 = icmp eq i32 %52, %21
br i1 %56, label %57, label %72
57: ; preds = %55
store i64 8, i64* %6
%58 = call i32 @accept(i32 %21, %struct.sockaddr* %36, i64* %6)
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %57
%65 = load i32, i32* @stderr
%66 = load i32, i32* %37
%67 = call i8* @inet_ntoa(i32 %66)
%68 = load i32, i32* %38
%69 = call i32 @ntohs(i32 %68)
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %67, i32 %69)
%71 = call i32 @FD_SET(i32 %58, i32* %3)
br label %80
72: ; preds = %55
%73 = call i64 @read_from_client(i32 %52)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %72
%76 = call i32 @close(i32 %52)
%77 = call i32 @FD_CLR(i32 %52, i32* %3)
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
br label %80
80: ; preds = %51, %72, %75, %64
%81 = add nuw nsw i32 %52, 1
%82 = load i32, i32* @FD_SETSIZE
%83 = icmp slt i32 %81, %82
br i1 %83, label %51, label %39
}