#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    printf("Can't create socket.\n");
    return 1;
  }
  fcntl(sock, F_SETFL, (fcntl(sock, F_GETFL, 0)) | O_NONBLOCK);
  struct sockaddr_in address;
  address.sin_family = AF_INET;
  address.sin_port = htons(1337);
  address.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *)&address, sizeof(address)) < 0) {
    printf("Can't bind.\n");
    return 1;
  };
  int clients_arr[2];
  for (int i = 0; i < 2; i++)
    clients_arr[i] = -1;
  fd_set clients;
  int max_sock = 1;
  struct timeval timer;
  timer.tv_sec = 1;
  timer.tv_usec = 0;
  char buf[1024];
  listen(sock, 2);
  printf("Server started.\n");
  while (1) {
    int new_client = accept(sock, NULL, NULL);
    if (new_client >= 0) {
      int i = 0;
      while (i < 2 && clients_arr[i] != -1)
        i++;
      if (i < 2) {
        max_sock = (max_sock >= new_client + 1) ? max_sock : new_client + 1;
        clients_arr[i] = new_client;
        printf("Connected client %d.\n", i);
      } else {
        printf("There is no place for new clint.\n");
        shutdown(new_client, 2);
        close(new_client);
      }
    }
    FD_ZERO(&clients);
    for (int i = 0; i < 2; i++)
      if (clients_arr[i] != -1)
        FD_SET(clients_arr[i], &clients);
    int select_ret = select(max_sock, &clients, NULL, NULL, &timer);
    if (select_ret > 0) {
      for (int i = 0; i < 2; i++) {
        if (FD_ISSET(clients_arr[i], &clients) != 0) {
          int bytes_read = recv(clients_arr[i], buf, 1024, 0);
          if (bytes_read == 0) {
            printf("Disconnected client %d.\n", i);
            close(clients_arr[i]);
            clients_arr[i] = -1;
          } else {
            printf("Got message from client %d.\n", i);
            send(clients_arr[i], buf, bytes_read, 0);
          }
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1252 <main+0x52>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f2 <main+0x3f2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x460(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x45c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    -0x460(%rbp),%edi
mov    -0x45c(%rbp),%esi
mov    %eax,%edx
mov    0x2d4e(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x539,%edi
call   1070 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10a0 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <bind@plt>
cmp    $0x0,%rax
jge    12f8 <main+0xf8>
lea    0xd31(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f2 <main+0x3f2>
movl   $0x0,-0x24(%rbp)
cmpl   $0x2,-0x24(%rbp)
jge    1323 <main+0x123>
movslq -0x24(%rbp),%rax
movl   $0xffffffff,-0x20(%rbp,%rax,4)
ff
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12ff <main+0xff>
movl   $0x1,-0x2c(%rbp)
movl   $0x1,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   10e0 <listen@plt>
lea    0xcd6(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1100 <accept@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0x0,-0x444(%rbp)
jl     1470 <main+0x270>
movl   $0x0,-0x448(%rbp)
xor    %eax,%eax
cmpl   $0x2,-0x448(%rbp)
mov    %al,-0x461(%rbp)
jge    13aa <main+0x1aa>
movslq -0x448(%rbp),%rax
cmpl   $0xffffffff,-0x20(%rbp,%rax,4)
setne  %al
mov    %al,-0x461(%rbp)
mov    -0x461(%rbp),%al
test   $0x1,%al
jne    13bd <main+0x1bd>
jmp    13d1 <main+0x1d1>
mov    -0x448(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x448(%rbp)
jmp    1380 <main+0x180>
cmpl   $0x2,-0x448(%rbp)
jge    1442 <main+0x242>
mov    -0x2c(%rbp),%eax
mov    -0x444(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jl     1400 <main+0x200>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x468(%rbp)
jmp    140f <main+0x20f>
mov    -0x444(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x468(%rbp)
mov    -0x468(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x444(%rbp),%ecx
movslq -0x448(%rbp),%rax
mov    %ecx,-0x20(%rbp,%rax,4)
mov    -0x448(%rbp),%esi
lea    0xbfe(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    146b <main+0x26b>
lea    0xc01(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x444(%rbp),%edi
mov    $0x2,%esi
call   1060 <shutdown@plt>
mov    -0x444(%rbp),%edi
call   10b0 <close@plt>
jmp    1470 <main+0x270>
lea    -0x28(%rbp),%rdi
call   10c0 <FD_ZERO@plt>
movl   $0x0,-0x44c(%rbp)
cmpl   $0x2,-0x44c(%rbp)
jge    14cf <main+0x2cf>
movslq -0x44c(%rbp),%rax
cmpl   $0xffffffff,-0x20(%rbp,%rax,4)
je     14b6 <main+0x2b6>
movslq -0x44c(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
lea    -0x28(%rbp),%rsi
call   1110 <FD_SET@plt>
jmp    14bb <main+0x2bb>
mov    -0x44c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44c(%rbp)
jmp    1483 <main+0x283>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   10d0 <select@plt>
mov    %eax,-0x450(%rbp)
cmpl   $0x0,-0x450(%rbp)
jle    15ed <main+0x3ed>
movl   $0x0,-0x454(%rbp)
cmpl   $0x2,-0x454(%rbp)
jge    15e8 <main+0x3e8>
movslq -0x454(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
lea    -0x28(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     15cf <main+0x3cf>
movslq -0x454(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x458(%rbp)
cmpl   $0x0,-0x458(%rbp)
jne    1597 <main+0x397>
mov    -0x454(%rbp),%esi
lea    0xb00(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movslq -0x454(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
call   10b0 <close@plt>
movslq -0x454(%rbp),%rax
movl   $0xffffffff,-0x20(%rbp,%rax,4)
ff
jmp    15ca <main+0x3ca>
mov    -0x454(%rbp),%esi
lea    0xae1(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movslq -0x454(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
lea    -0x440(%rbp),%rsi
mov    -0x458(%rbp),%edx
xor    %ecx,%ecx
call   1080 <send@plt>
jmp    15cf <main+0x3cf>
jmp    15d4 <main+0x3d4>
mov    -0x454(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x454(%rbp)
jmp    1503 <main+0x303>
jmp    15ed <main+0x3ed>
jmp    1354 <main+0x154>
mov    -0x4(%rbp),%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    0x2dc0(%rip),%r14
mov    (%r14),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
test   %eax,%eax
js     1508 <main+0x2f8>
mov    %eax,%ebp
mov    0x2d7c(%rip),%rax
mov    (%rax),%ebx
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    0x2d79(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <fcntl@plt>
mov    (%r14),%eax
mov    %eax,0x28(%rsp)
mov    $0x539,%edi
call   1080 <htons@plt>
mov    %eax,0x24(%rsp)
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10b0 <htonl@plt>
mov    %eax,0x20(%rsp)
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1100 <bind@plt>
test   %rax,%rax
js     1511 <main+0x301>
movq   $0xffffffffffffffff,0x18(%rsp)
ff ff
movl   $0x1,0x30(%rsp)
movq   $0x0,0x38(%rsp)
mov    %ebp,%edi
mov    $0x2,%esi
call   10f0 <listen@plt>
lea    0xd6a(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,0x14(%rsp)
mov    $0xffffffff,%r15d
lea    0x10(%rsp),%rbx
mov    $0xffffffff,%r14d
mov    $0xffffffff,%r13d
mov    %ebp,0xc(%rsp)
jmp    1341 <main+0x131>
lea    0xd03(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r13d,%edi
call   10c0 <close@plt>
movl   $0xffffffff,0x1c(%rsp)
ff
mov    %r15d,%r14d
mov    $0xffffffff,%r13d
mov    0xc(%rsp),%ebp
lea    0x10(%rsp),%rbx
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1110 <accept@plt>
test   %eax,%eax
js     13c9 <main+0x1b9>
mov    %eax,%r12d
cmp    $0xffffffff,%r14d
je     1390 <main+0x180>
cmp    $0xffffffff,%r13d
je     1394 <main+0x184>
lea    0xcf2(%rip),%rdi
call   1040 <puts@plt>
mov    %r12d,%edi
mov    $0x2,%esi
call   1070 <shutdown@plt>
mov    %r12d,%edi
call   10c0 <close@plt>
mov    %rbx,%rdi
call   10d0 <FD_ZERO@plt>
jmp    13d7 <main+0x1c7>
xchg   %ax,%ax
xor    %esi,%esi
jmp    1399 <main+0x189>
mov    $0x1,%esi
mov    %esi,%eax
mov    %r12d,%ecx
add    $0x1,%ecx
mov    0x14(%rsp),%edx
cmp    %ecx,%edx
cmovl  %ecx,%edx
mov    %edx,0x14(%rsp)
mov    %r12d,0x18(%rsp,%rax,4)
lea    0xc46(%rip),%rdi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    0x18(%rsp),%r15d
mov    %r15d,%r14d
mov    %rbx,%rdi
call   10d0 <FD_ZERO@plt>
cmp    $0xffffffff,%r14d
je     13f0 <main+0x1e0>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1120 <FD_SET@plt>
jmp    13f6 <main+0x1e6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xffffffff,%r14d
mov    0x1c(%rsp),%r13d
cmp    $0xffffffff,%r13d
je     140c <main+0x1fc>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1120 <FD_SET@plt>
mov    0x14(%rsp),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x30(%rsp),%r8
call   10e0 <select@plt>
test   %eax,%eax
jle    1341 <main+0x131>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     14a6 <main+0x296>
mov    %r15d,%edi
lea    0x40(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     1477 <main+0x267>
mov    %eax,%ebp
lea    0xbd2(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1090 <send@plt>
jmp    149d <main+0x28d>
lea    0xb98(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r15d,%edi
call   10c0 <close@plt>
movl   $0xffffffff,0x18(%rsp)
ff
mov    $0xffffffff,%r15d
mov    0xc(%rsp),%ebp
lea    0x10(%rsp),%rbx
mov    %r13d,%edi
mov    %rbx,%rsi
call   1060 <FD_ISSET@plt>
mov    %r15d,%r14d
test   %rax,%rax
je     1341 <main+0x131>
mov    %r13d,%edi
lea    0x40(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     130c <main+0xfc>
mov    %eax,%ebp
lea    0xb4a(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1090 <send@plt>
mov    %r15d,%r14d
jmp    1338 <main+0x128>
lea    0xb7a(%rip),%rdi
jmp    1518 <main+0x308>
lea    0xb65(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x448,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Can't create socket.\0A\00"
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"Can't bind.\0A\00"
@.str.2 = [17 x i8] c"Server started.\0A\00"
@.str.3 = [22 x i8] c"Connected client %d.\0A\00"
@.str.4 = [34 x i8] c"There is no place for new clint.\0A\00"
@.str.5 = [25 x i8] c"Disconnected client %d.\0A\00"
@.str.6 = [29 x i8] c"Got message from client %d.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %1
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %185
23: ; preds = %0
%24 = load i32, i32* %2
%25 = load i32, i32* @F_SETFL
%26 = load i32, i32* %2
%27 = load i32, i32* @F_GETFL
%28 = call i32 @fcntl(i32 %26, i32 %27, i32 0)
%29 = load i32, i32* @O_NONBLOCK
%30 = or i32 %28, %29
%31 = call i32 @fcntl(i32 %24, i32 %25, i32 %30)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %32, i32* %33
%34 = call i32 @htons(i32 1337)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = call i32 @htonl(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %2
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i64 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %23
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %185
46: ; preds = %23
store i32 0, i32* %5
br label %47
47: ; preds = %54, %46
%48 = load i32, i32* %5
%49 = icmp slt i32 %48, 2
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i32, i32* %5
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %52
store i32 -1, i32* %53
br label %54
54: ; preds = %50
%55 = load i32, i32* %5
%56 = add nsw i32 %55, 1
store i32 %56, i32* %5
br label %47
57: ; preds = %47
store i32 1, i32* %7
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 1, i32* %58
%59 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %59
%60 = load i32, i32* %2
%61 = call i32 @listen(i32 %60, i32 2)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %57, %184
%64 = load i32, i32* %2
%65 = call i32 @accept(i32 %64, i32* null, i32* null)
store i32 %65, i32* %10
%66 = load i32, i32* %10
%67 = icmp sge i32 %66, 0
br i1 %67, label %68, label %111
68: ; preds = %63
store i32 0, i32* %11
br label %69
69: ; preds = %80, %68
%70 = load i32, i32* %11
%71 = icmp slt i32 %70, 2
br i1 %71, label %72, label %78
72: ; preds = %69
%73 = load i32, i32* %11
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %74
%76 = load i32, i32* %75
%77 = icmp ne i32 %76, -1
br label %78
78: ; preds = %72, %69
%79 = phi i1 [ false, %69 ], [ %77, %72 ]
br i1 %79, label %80, label %83
80: ; preds = %78
%81 = load i32, i32* %11
%82 = add nsw i32 %81, 1
store i32 %82, i32* %11
br label %69
83: ; preds = %78
%84 = load i32, i32* %11
%85 = icmp slt i32 %84, 2
br i1 %85, label %86, label %104
86: ; preds = %83
%87 = load i32, i32* %7
%88 = load i32, i32* %10
%89 = add nsw i32 %88, 1
%90 = icmp sge i32 %87, %89
br i1 %90, label %91, label %93
91: ; preds = %86
%92 = load i32, i32* %7
br label %96
93: ; preds = %86
%94 = load i32, i32* %10
%95 = add nsw i32 %94, 1
br label %96
96: ; preds = %93, %91
%97 = phi i32 [ %92, %91 ], [ %95, %93 ]
store i32 %97, i32* %7
%98 = load i32, i32* %10
%99 = load i32, i32* %11
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %100
store i32 %98, i32* %101
%102 = load i32, i32* %11
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %102)
br label %110
104: ; preds = %83
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%106 = load i32, i32* %10
%107 = call i32 @shutdown(i32 %106, i32 2)
%108 = load i32, i32* %10
%109 = call i32 @close(i32 %108)
br label %110
110: ; preds = %104, %96
br label %111
111: ; preds = %110, %63
%112 = call i32 @FD_ZERO(i32* %6)
store i32 0, i32* %12
br label %113
113: ; preds = %129, %111
%114 = load i32, i32* %12
%115 = icmp slt i32 %114, 2
br i1 %115, label %116, label %132
116: ; preds = %113
%117 = load i32, i32* %12
%118 = sext i32 %117 to i64
%119 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %118
%120 = load i32, i32* %119
%121 = icmp ne i32 %120, -1
br i1 %121, label %122, label %128
122: ; preds = %116
%123 = load i32, i32* %12
%124 = sext i32 %123 to i64
%125 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %124
%126 = load i32, i32* %125
%127 = call i32 @FD_SET(i32 %126, i32* %6)
br label %128
128: ; preds = %122, %116
br label %129
129: ; preds = %128
%130 = load i32, i32* %12
%131 = add nsw i32 %130, 1
store i32 %131, i32* %12
br label %113
132: ; preds = %113
%133 = load i32, i32* %7
%134 = call i32 @select(i32 %133, i32* %6, i32* null, i32* null, %struct.timeval* %8)
store i32 %134, i32* %13
%135 = load i32, i32* %13
%136 = icmp sgt i32 %135, 0
br i1 %136, label %137, label %184
137: ; preds = %132
store i32 0, i32* %14
br label %138
138: ; preds = %180, %137
%139 = load i32, i32* %14
%140 = icmp slt i32 %139, 2
br i1 %140, label %141, label %183
141: ; preds = %138
%142 = load i32, i32* %14
%143 = sext i32 %142 to i64
%144 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %143
%145 = load i32, i32* %144
%146 = call i64 @FD_ISSET(i32 %145, i32* %6)
%147 = icmp ne i64 %146, 0
br i1 %147, label %148, label %179
148: ; preds = %141
%149 = load i32, i32* %14
%150 = sext i32 %149 to i64
%151 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %150
%152 = load i32, i32* %151
%153 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%154 = call i32 @recv(i32 %152, i8* %153, i32 1024, i32 0)
store i32 %154, i32* %15
%155 = load i32, i32* %15
%156 = icmp eq i32 %155, 0
br i1 %156, label %157, label %168
157: ; preds = %148
%158 = load i32, i32* %14
%159 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %158)
%160 = load i32, i32* %14
%161 = sext i32 %160 to i64
%162 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %161
%163 = load i32, i32* %162
%164 = call i32 @close(i32 %163)
%165 = load i32, i32* %14
%166 = sext i32 %165 to i64
%167 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %166
store i32 -1, i32* %167
br label %178
168: ; preds = %148
%169 = load i32, i32* %14
%170 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %169)
%171 = load i32, i32* %14
%172 = sext i32 %171 to i64
%173 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %172
%174 = load i32, i32* %173
%175 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%176 = load i32, i32* %15
%177 = call i32 @send(i32 %174, i8* %175, i32 %176, i32 0)
br label %178
178: ; preds = %168, %157
br label %179
179: ; preds = %178, %141
br label %180
180: ; preds = %179
%181 = load i32, i32* %14
%182 = add nsw i32 %181, 1
store i32 %182, i32* %14
br label %138
183: ; preds = %138
br label %184
184: ; preds = %183, %132
br label %63
185: ; preds = %44, %21
%186 = load i32, i32* %1
ret i32 %186
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [22 x i8] c"Connected client %d.\0A\00"
@.str.5 = [25 x i8] c"Disconnected client %d.\0A\00"
@.str.6 = [29 x i8] c"Got message from client %d.\0A\00"
@str = [16 x i8] c"Server started.\00"
@str.7 = [33 x i8] c"There is no place for new clint.\00"
@str.8 = [12 x i8] c"Can't bind.\00"
@str.9 = [21 x i8] c"Can't create socket.\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i64
%3 = bitcast i64* %2 to [2 x i32]*
%4 = bitcast i64* %2 to i8*
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca [1024 x i8]
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
br label %121
14: ; preds = %0
%15 = load i32, i32* @F_SETFL
%16 = load i32, i32* @F_GETFL
%17 = tail call i32 @fcntl(i32 %10, i32 %16, i32 0)
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %17
%20 = tail call i32 @fcntl(i32 %10, i32 %15, i32 %19)
%21 = bitcast %struct.sockaddr_in* %1 to i8*
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %22, i32* %23
%24 = tail call i32 @htons(i32 1337)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = tail call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%30 = call i64 @bind(i32 %10, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %14
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0))
br label %121
34: ; preds = %14
store i64 -1, i64* %2
%35 = bitcast i32* %5 to i8*
%36 = bitcast %struct.timeval* %6 to i8*
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 1, i32* %37
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %38
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%40 = call i32 @listen(i32 %10, i32 2)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%42 = bitcast i64* %2 to i32*
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%44 = bitcast i64* %2 to i32*
br label %45
45: ; preds = %108, %34
%46 = phi i32 [ -1, %34 ], [ %109, %108 ]
%47 = phi i32 [ -1, %34 ], [ %110, %108 ]
%48 = phi i32 [ -1, %34 ], [ %111, %108 ]
%49 = phi i32 [ 1, %34 ], [ %85, %108 ]
%50 = call i32 @accept(i32 %10, i32* null, i32* null)
%51 = icmp sgt i32 %50, -1
br i1 %51, label %52, label %70
52: ; preds = %45
%53 = icmp eq i32 %47, -1
br i1 %53, label %61, label %54
54: ; preds = %52
%55 = icmp eq i32 %48, -1
br i1 %55, label %61, label %56
56: ; preds = %54
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.7, i64 0, i64 0))
%58 = call i32 @shutdown(i32 %50, i32 2)
%59 = call i32 @close(i32 %50)
%60 = call i32 @FD_ZERO(i32* %5)
br label %79
61: ; preds = %54, %52
%62 = phi i32 [ 0, %52 ], [ 1, %54 ]
%63 = zext i32 %62 to i64
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 %63
%65 = add nuw nsw i32 %50, 1
%66 = icmp slt i32 %49, %65
%67 = select i1 %66, i32 %65, i32 %49
store i32 %50, i32* %64
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %62)
%69 = load i32, i32* %42
br label %70
70: ; preds = %61, %45
%71 = phi i32 [ %46, %45 ], [ %69, %61 ]
%72 = phi i32 [ %47, %45 ], [ %69, %61 ]
%73 = phi i32 [ %49, %45 ], [ %67, %61 ]
%74 = call i32 @FD_ZERO(i32* %5)
%75 = icmp eq i32 %72, -1
br i1 %75, label %84, label %79
76: ; preds = %92
%77 = call i64 @FD_ISSET(i32 %87, i32* %5)
%78 = icmp eq i64 %77, 0
br i1 %78, label %104, label %95
79: ; preds = %56, %70
%80 = phi i32 [ %49, %56 ], [ %73, %70 ]
%81 = phi i32 [ %47, %56 ], [ %72, %70 ]
%82 = phi i32 [ %46, %56 ], [ %71, %70 ]
%83 = call i32 @FD_SET(i32 %81, i32* %5)
br label %84
84: ; preds = %70, %79
%85 = phi i32 [ %73, %70 ], [ %80, %79 ]
%86 = phi i32 [ -1, %70 ], [ %81, %79 ]
%87 = phi i32 [ %71, %70 ], [ %82, %79 ]
%88 = load i32, i32* %43
%89 = icmp eq i32 %88, -1
br i1 %89, label %92, label %90
90: ; preds = %84
%91 = call i32 @FD_SET(i32 %88, i32* %5)
br label %92
92: ; preds = %90, %84
%93 = call i32 @select(i32 %85, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%94 = icmp sgt i32 %93, 0
br i1 %94, label %76, label %108
95: ; preds = %76
%96 = call i32 @recv(i32 %87, i8* %39, i32 1024, i32 0)
%97 = icmp eq i32 %96, 0
br i1 %97, label %98, label %101
98: ; preds = %95
%99 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 0)
%100 = call i32 @close(i32 %87)
store i32 -1, i32* %44
br label %104
101: ; preds = %95
%102 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 0)
%103 = call i32 @send(i32 %87, i8* %39, i32 %96, i32 0)
br label %104
104: ; preds = %98, %101, %76
%105 = phi i32 [ -1, %98 ], [ %87, %101 ], [ %87, %76 ]
%106 = call i64 @FD_ISSET(i32 %88, i32* %5)
%107 = icmp eq i64 %106, 0
br i1 %107, label %108, label %112
108: ; preds = %104, %115, %118, %92
%109 = phi i32 [ %87, %92 ], [ %105, %118 ], [ %105, %115 ], [ %105, %104 ]
%110 = phi i32 [ %86, %92 ], [ %105, %118 ], [ %105, %115 ], [ %105, %104 ]
%111 = phi i32 [ %88, %92 ], [ -1, %118 ], [ %88, %115 ], [ %88, %104 ]
br label %45
112: ; preds = %104
%113 = call i32 @recv(i32 %88, i8* %39, i32 1024, i32 0)
%114 = icmp eq i32 %113, 0
br i1 %114, label %118, label %115
115: ; preds = %112
%116 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 1)
%117 = call i32 @send(i32 %88, i8* %39, i32 %113, i32 0)
br label %108
118: ; preds = %112
%119 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 1)
%120 = call i32 @close(i32 %88)
store i32 -1, i32* %43
br label %108
121: ; preds = %32, %12
ret i32 1
}