#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 TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
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*/ *, int /*<<< orphan*/ *);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  fd_set rfds;
  fd_set master;
  int sockfd, newsockfd, portno;
  int count = 0;
  socklen_t clilen;
  char buffer[256];
  struct sockaddr_in serv_addr, cli_addr;
  int ret, text;
  if (argc < 2) {
    fprintf(stderr, "ERROR, no port provided\n");
    exit(1);
  }
  sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sockfd < 0) {
    error("ERROR opening socket");
  }
  FD_ZERO(&master);
  FD_SET(sockfd, &master);
  FD_ZERO(&rfds);
  FD_SET(sockfd, &rfds);
  bzero((char *)&serv_addr, sizeof(serv_addr));
  portno = atoi(argv[1]);
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR on binding");
  listen(sockfd, 5);
  while (1) {
    if (count % 1000 == 0)
      printf("======================= %d =======================\n", count);
    rfds = master;
    ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
    printf("Events = %d\n", ret);
    if (FD_ISSET(sockfd, &rfds)) {
      clilen = sizeof(cli_addr);
      newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
      printf("Newsockfd = %d\n", newsockfd);
      if (newsockfd < 0) {
        error("ERROR on accept");
      }
      FD_SET(newsockfd, &master);
    }
    for (int i = 0; i < FD_SETSIZE && ret > 0; ++i) {
      if (FD_ISSET(i, &rfds) && (i != sockfd)) {
        ret--;
        if (ret < 0) {
          error("ERROR reading from socket");
        } else {
          bzero(buffer, 256);
          text = recv(i, buffer, 255, 0);
          printf("Here is the message: %s\n", buffer);
          text = send(i, "I got your message\n", 18, 0);
          if (text < 0) {
            error("ERROR writing to socket");
          }
          for (int x = 0; x < FD_SETSIZE; ++x) {
            if (x != i && FD_ISSET(x, &master) && (x != sockfd)) {
              printf("send to monitor\n");
              text = send(x, buffer, 18, 0);
              if (text < 0) {
                error("ERROR writing to socket");
              }
            }
          }
        }
      }
    }
    count++;
  }
  close(newsockfd);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1259 <main+0x49>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    0x2d66(%rip),%rax
mov    (%rax),%edx
call   1130 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1292 <main+0x82>
lea    0xd8c(%rip),%rdi
call   10d0 <error@plt>
lea    -0x18(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1120 <FD_SET@plt>
lea    -0x14(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1120 <FD_SET@plt>
lea    -0x140(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x138(%rbp)
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x13c(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x140(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    1333 <main+0x123>
lea    0xd00(%rip),%rdi
call   10d0 <error@plt>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
mov    -0x28(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1365 <main+0x155>
mov    -0x28(%rbp),%esi
lea    0xce1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2c46(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10a0 <select@plt>
mov    %eax,-0x154(%rbp)
mov    -0x154(%rbp),%esi
lea    0xcd8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1408 <main+0x1f8>
movl   $0xc,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x150(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xca1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
jge    13fc <main+0x1ec>
lea    0xc99(%rip),%rdi
call   10d0 <error@plt>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1120 <FD_SET@plt>
movl   $0x0,-0x15c(%rbp)
mov    -0x15c(%rbp),%ecx
xor    %eax,%eax
mov    0x2b97(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x161(%rbp)
jge    143f <main+0x22f>
cmpl   $0x0,-0x154(%rbp)
setg   %al
mov    %al,-0x161(%rbp)
mov    -0x161(%rbp),%al
test   $0x1,%al
jne    1452 <main+0x242>
jmp    1604 <main+0x3f4>
mov    -0x15c(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     15eb <main+0x3db>
mov    -0x15c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     15eb <main+0x3db>
mov    -0x154(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x154(%rbp)
cmpl   $0x0,-0x154(%rbp)
jge    14a7 <main+0x297>
lea    0xc03(%rip),%rdi
call   10d0 <error@plt>
jmp    15e6 <main+0x3d6>
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
mov    -0x15c(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x158(%rbp)
lea    -0x130(%rbp),%rsi
lea    0xbd5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x15c(%rbp),%edi
lea    0xbda(%rip),%rsi
mov    $0x12,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x158(%rbp)
cmpl   $0x0,-0x158(%rbp)
jge    1524 <main+0x314>
lea    0xbc8(%rip),%rdi
call   10d0 <error@plt>
movl   $0x0,-0x160(%rbp)
mov    -0x160(%rbp),%eax
mov    0x2a7d(%rip),%rcx
cmp    (%rcx),%eax
jge    15e1 <main+0x3d1>
mov    -0x160(%rbp),%eax
cmp    -0x15c(%rbp),%eax
je     15c8 <main+0x3b8>
mov    -0x160(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     15c8 <main+0x3b8>
mov    -0x160(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     15c8 <main+0x3b8>
lea    0xb7b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x160(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x12,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x158(%rbp)
cmpl   $0x0,-0x158(%rbp)
jge    15c3 <main+0x3b3>
lea    0xb29(%rip),%rdi
call   10d0 <error@plt>
jmp    15c8 <main+0x3b8>
jmp    15cd <main+0x3bd>
mov    -0x160(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x160(%rbp)
jmp    152e <main+0x31e>
jmp    15e6 <main+0x3d6>
jmp    15eb <main+0x3db>
jmp    15f0 <main+0x3e0>
mov    -0x15c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x15c(%rbp)
jmp    1412 <main+0x202>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1340 <main+0x130>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
cmp    $0x1,%edi
jle    153b <main+0x31b>
mov    %rsi,%r14
mov    0x2da4(%rip),%r15
mov    (%r15),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
call   1140 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1270 <main+0x50>
lea    0xdae(%rip),%rdi
call   10e0 <error@plt>
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   10a0 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1130 <FD_SET@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   10a0 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1130 <FD_SET@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   10f0 <bzero@plt>
mov    0x8(%r14),%rdi
call   1110 <atoi@plt>
mov    (%r15),%ecx
mov    %ecx,0x18(%rsp)
mov    0x2d1b(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x14(%rsp)
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
test   %rax,%rax
jns    12f6 <main+0xd6>
lea    0xd3d(%rip),%rdi
call   10e0 <error@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10c0 <listen@plt>
mov    0x2caf(%rip),%r13
mov    %rsp,%rbp
xor    %r14d,%r14d
jmp    132c <main+0x10c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rbp
mov    0x8(%rsp),%r14d
add    $0x1,%r14d
imul   $0x26e978d5,%r14d,%eax
ror    $0x3,%eax
cmp    $0x418937,%eax
ja     134e <main+0x12e>
lea    0xcfb(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r14d,0x8(%rsp)
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
mov    0x0(%r13),%edi
mov    %rbp,%r14
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
mov    %eax,%r15d
lea    0xcf9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
lea    0x4(%rsp),%rbp
je     13e2 <main+0x1c2>
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1100 <accept@plt>
mov    %eax,%r14d
lea    0xcc6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
test   %r14d,%r14d
jns    13d4 <main+0x1b4>
lea    0xcc1(%rip),%rdi
call   10e0 <error@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1130 <FD_SET@plt>
mov    %rsp,%r14
cmpl   $0x0,0x0(%r13)
jle    1320 <main+0x100>
test   %r15d,%r15d
jle    1320 <main+0x100>
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1500 <main+0x2e0>
cmp    %ebx,%r12d
je     1500 <main+0x2e0>
mov    %r14,%rbp
lea    0x30(%rsp),%r14
mov    %r14,%rdi
mov    $0x100,%esi
call   10f0 <bzero@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xc55(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12d,%edi
lea    0xc5a(%rip),%rsi
mov    $0x12,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
test   %eax,%eax
jns    147b <main+0x25b>
lea    0xc57(%rip),%rdi
call   10e0 <error@plt>
add    $0xffffffff,%r15d
mov    0x0(%r13),%eax
test   %eax,%eax
jle    1515 <main+0x2f5>
xor    %r14d,%r14d
lea    0x4(%rsp),%rbp
jmp    14ad <main+0x28d>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r14d
mov    0x0(%r13),%eax
cmp    %eax,%r14d
jge    1510 <main+0x2f0>
cmp    %r14d,%r12d
je     14a0 <main+0x280>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     14a0 <main+0x280>
cmp    %r14d,%ebx
je     14a0 <main+0x280>
lea    0xc17(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
lea    0x30(%rsp),%rsi
mov    $0x12,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
test   %eax,%eax
jns    14a0 <main+0x280>
lea    0xbdb(%rip),%rdi
call   10e0 <error@plt>
jmp    14a0 <main+0x280>
nopl   0x0(%rax)
mov    0x0(%r13),%eax
jmp    1520 <main+0x300>
cs nopw 0x0(%rax,%rax,1)
mov    %rsp,%r14
jmp    1520 <main+0x300>
mov    %rbp,%r14
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12d
cmp    %eax,%r12d
jge    1320 <main+0x100>
test   %r15d,%r15d
jg     1400 <main+0x1e0>
jmp    1320 <main+0x100>
mov    0x2a86(%rip),%rax
mov    (%rax),%edi
lea    0xab5(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"ERROR, no port provided\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@.str.3 = [52 x i8] c"======================= %d =======================\0A\00"
@FD_SETSIZE = global i32 0
@.str.4 = [13 x i8] c"Events = %d\0A\00"
@.str.5 = [16 x i8] c"Newsockfd = %d\0A\00"
@.str.6 = [16 x i8] c"ERROR on accept\00"
@.str.7 = [26 x i8] c"ERROR reading from socket\00"
@.str.8 = [25 x i8] c"Here is the message: %s\0A\00"
@.str.9 = [20 x i8] c"I got your message\0A\00"
@.str.10 = [24 x i8] c"ERROR writing to socket\00"
@.str.11 = [17 x i8] c"send to monitor\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 i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [256 x i8]
%14 = alloca %struct.sockaddr_in
%15 = alloca %struct.sockaddr_in
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 2
br i1 %21, label %22, label %26
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = load i32, i32* @IPPROTO_TCP
%30 = call i32 @socket(i32 %27, i32 %28, i32 %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %26
%36 = call i32 @FD_ZERO(i32* %7)
%37 = load i32, i32* %8
%38 = call i32 @FD_SET(i32 %37, i32* %7)
%39 = call i32 @FD_ZERO(i32* %6)
%40 = load i32, i32* %8
%41 = call i32 @FD_SET(i32 %40, i32* %6)
%42 = bitcast %struct.sockaddr_in* %14 to i8*
%43 = call i32 @bzero(i8* %42, i32 12)
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 @atoi(i8* %46)
store i32 %47, i32* %10
%48 = load i32, i32* @AF_INET
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
store i32 %48, i32* %49
%50 = load i32, i32* @INADDR_ANY
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
store i32 %50, i32* %52
%53 = load i32, i32* %10
%54 = call i32 @htons(i32 %53)
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
store i32 %54, i32* %55
%56 = load i32, i32* %8
%57 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%58 = call i64 @bind(i32 %56, %struct.sockaddr* %57, i32 12)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %35
%61 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %62
62: ; preds = %60, %35
%63 = load i32, i32* %8
%64 = call i32 @listen(i32 %63, i32 5)
br label %65
65: ; preds = %62, %170
%66 = load i32, i32* %11
%67 = srem i32 %66, 1000
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %65
%70 = load i32, i32* %11
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %70)
br label %72
72: ; preds = %69, %65
%73 = load i32, i32* %7
store i32 %73, i32* %6
%74 = load i32, i32* @FD_SETSIZE
%75 = call i32 @select(i32 %74, i32* %6, i32* null, i32* null, i32* null)
store i32 %75, i32* %16
%76 = load i32, i32* %16
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %76)
%78 = load i32, i32* %8
%79 = call i64 @FD_ISSET(i32 %78, i32* %6)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %94
81: ; preds = %72
store i32 12, i32* %12
%82 = load i32, i32* %8
%83 = bitcast %struct.sockaddr_in* %15 to %struct.sockaddr*
%84 = call i32 @accept(i32 %82, %struct.sockaddr* %83, i32* %12)
store i32 %84, i32* %9
%85 = load i32, i32* %9
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %85)
%87 = load i32, i32* %9
%88 = icmp slt i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %81
%90 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %91
91: ; preds = %89, %81
%92 = load i32, i32* %9
%93 = call i32 @FD_SET(i32 %92, i32* %7)
br label %94
94: ; preds = %91, %72
store i32 0, i32* %18
br label %95
95: ; preds = %167, %94
%96 = load i32, i32* %18
%97 = load i32, i32* @FD_SETSIZE
%98 = icmp slt i32 %96, %97
br i1 %98, label %99, label %102
99: ; preds = %95
%100 = load i32, i32* %16
%101 = icmp sgt i32 %100, 0
br label %102
102: ; preds = %99, %95
%103 = phi i1 [ false, %95 ], [ %101, %99 ]
br i1 %103, label %104, label %170
104: ; preds = %102
%105 = load i32, i32* %18
%106 = call i64 @FD_ISSET(i32 %105, i32* %6)
%107 = icmp ne i64 %106, 0
br i1 %107, label %108, label %166
108: ; preds = %104
%109 = load i32, i32* %18
%110 = load i32, i32* %8
%111 = icmp ne i32 %109, %110
br i1 %111, label %112, label %166
112: ; preds = %108
%113 = load i32, i32* %16
%114 = add nsw i32 %113, -1
store i32 %114, i32* %16
%115 = load i32, i32* %16
%116 = icmp slt i32 %115, 0
br i1 %116, label %117, label %119
117: ; preds = %112
%118 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
br label %165
119: ; preds = %112
%120 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%121 = call i32 @bzero(i8* %120, i32 256)
%122 = load i32, i32* %18
%123 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%124 = call i32 @recv(i32 %122, i8* %123, i32 255, i32 0)
store i32 %124, i32* %17
%125 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%126 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* %125)
%127 = load i32, i32* %18
%128 = call i32 @send(i32 %127, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 18, i32 0)
store i32 %128, i32* %17
%129 = load i32, i32* %17
%130 = icmp slt i32 %129, 0
br i1 %130, label %131, label %133
131: ; preds = %119
%132 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %133
133: ; preds = %131, %119
store i32 0, i32* %19
br label %134
134: ; preds = %161, %133
%135 = load i32, i32* %19
%136 = load i32, i32* @FD_SETSIZE
%137 = icmp slt i32 %135, %136
br i1 %137, label %138, label %164
138: ; preds = %134
%139 = load i32, i32* %19
%140 = load i32, i32* %18
%141 = icmp ne i32 %139, %140
br i1 %141, label %142, label %160
142: ; preds = %138
%143 = load i32, i32* %19
%144 = call i64 @FD_ISSET(i32 %143, i32* %7)
%145 = icmp ne i64 %144, 0
br i1 %145, label %146, label %160
146: ; preds = %142
%147 = load i32, i32* %19
%148 = load i32, i32* %8
%149 = icmp ne i32 %147, %148
br i1 %149, label %150, label %160
150: ; preds = %146
%151 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.11, i64 0, i64 0))
%152 = load i32, i32* %19
%153 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%154 = call i32 @send(i32 %152, i8* %153, i32 18, i32 0)
store i32 %154, i32* %17
%155 = load i32, i32* %17
%156 = icmp slt i32 %155, 0
br i1 %156, label %157, label %159
157: ; preds = %150
%158 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %159
159: ; preds = %157, %150
br label %160
160: ; preds = %159, %146, %142, %138
br label %161
161: ; preds = %160
%162 = load i32, i32* %19
%163 = add nsw i32 %162, 1
store i32 %163, i32* %19
br label %134
164: ; preds = %134
br label %165
165: ; preds = %164, %117
br label %166
166: ; preds = %165, %108, %104
br label %167
167: ; preds = %166
%168 = load i32, i32* %18
%169 = add nsw i32 %168, 1
store i32 %169, i32* %18
br label %95
170: ; preds = %102
%171 = load i32, i32* %11
%172 = add nsw i32 %171, 1
store i32 %172, i32* %11
br label %65
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"ERROR, no port provided\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@.str.3 = [52 x i8] c"======================= %d =======================\0A\00"
@FD_SETSIZE = global i32 0
@.str.4 = [13 x i8] c"Events = %d\0A\00"
@.str.5 = [16 x i8] c"Newsockfd = %d\0A\00"
@.str.6 = [16 x i8] c"ERROR on accept\00"
@.str.8 = [25 x i8] c"Here is the message: %s\0A\00"
@.str.9 = [20 x i8] c"I got your message\0A\00"
@.str.10 = [24 x i8] c"ERROR writing to socket\00"
@str = [16 x i8] c"send to monitor\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = bitcast %struct.sockaddr_in* %7 to i8*
%14 = bitcast %struct.sockaddr_in* %8 to i8*
%15 = icmp slt i32 %0, 2
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = load i32, i32* @IPPROTO_TCP
%24 = tail call i32 @socket(i32 %21, i32 %22, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = call i32 @FD_ZERO(i32* %4)
%30 = call i32 @FD_SET(i32 %24, i32* %4)
%31 = call i32 @FD_ZERO(i32* %3)
%32 = call i32 @FD_SET(i32 %24, i32* %3)
%33 = call i32 @bzero(i8* %13, i32 12)
%34 = getelementptr inbounds i8*, i8** %1, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 2
store i32 %37, i32* %38
%39 = load i32, i32* @INADDR_ANY
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1, i32 0
store i32 %39, i32* %40
%41 = call i32 @htons(i32 %36)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
store i32 %41, i32* %42
%43 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%44 = call i64 @bind(i32 %24, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %28
%47 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %28
%49 = call i32 @listen(i32 %24, i32 5)
%50 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
br label %51
51: ; preds = %77, %48
%52 = phi i32 [ 0, %48 ], [ %78, %77 ]
%53 = urem i32 %52, 1000
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %52)
br label %57
57: ; preds = %55, %51
%58 = load i32, i32* %4
store i32 %58, i32* %3
%59 = load i32, i32* @FD_SETSIZE
%60 = call i32 @select(i32 %59, i32* %3, i32* null, i32* null, i32* null)
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %60)
%62 = call i64 @FD_ISSET(i32 %24, i32* %3)
%63 = icmp eq i64 %62, 0
br i1 %63, label %72, label %64
64: ; preds = %57
store i32 12, i32* %5
%65 = call i32 @accept(i32 %24, %struct.sockaddr* %50, i32* %5)
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %65)
%67 = icmp slt i32 %65, 0
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %70
70: ; preds = %68, %64
%71 = call i32 @FD_SET(i32 %65, i32* %4)
br label %72
72: ; preds = %70, %57
%73 = load i32, i32* @FD_SETSIZE
%74 = icmp sgt i32 %73, 0
%75 = icmp sgt i32 %60, 0
%76 = and i1 %74, %75
br i1 %76, label %79, label %77
77: ; preds = %118, %72
%78 = add nuw nsw i32 %52, 1
br label %51
79: ; preds = %72, %118
%80 = phi i32 [ %121, %118 ], [ 0, %72 ]
%81 = phi i32 [ %120, %118 ], [ %60, %72 ]
%82 = call i64 @FD_ISSET(i32 %80, i32* %3)
%83 = icmp eq i64 %82, 0
%84 = icmp eq i32 %80, %24
%85 = select i1 %83, i1 true, i1 %84
br i1 %85, label %86, label %88
86: ; preds = %79
%87 = load i32, i32* @FD_SETSIZE
br label %118
88: ; preds = %79
%89 = add nsw i32 %81, -1
%90 = call i32 @bzero(i8* %12, i32 256)
%91 = call i32 @recv(i32 %80, i8* %12, i32 255, i32 0)
%92 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* %12)
%93 = call i32 @send(i32 %80, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 18, i32 0)
%94 = icmp slt i32 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %88
%96 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %97
97: ; preds = %95, %88
%98 = load i32, i32* @FD_SETSIZE
%99 = icmp sgt i32 %98, 0
br i1 %99, label %100, label %118
100: ; preds = %97, %114
%101 = phi i32 [ %115, %114 ], [ 0, %97 ]
%102 = icmp eq i32 %101, %80
br i1 %102, label %114, label %103
103: ; preds = %100
%104 = call i64 @FD_ISSET(i32 %101, i32* %4)
%105 = icmp eq i64 %104, 0
%106 = icmp eq i32 %101, %24
%107 = select i1 %105, i1 true, i1 %106
br i1 %107, label %114, label %108
108: ; preds = %103
%109 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%110 = call i32 @send(i32 %101, i8* %12, i32 18, i32 0)
%111 = icmp slt i32 %110, 0
br i1 %111, label %112, label %114
112: ; preds = %108
%113 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %114
114: ; preds = %100, %103, %112, %108
%115 = add nuw nsw i32 %101, 1
%116 = load i32, i32* @FD_SETSIZE
%117 = icmp slt i32 %115, %116
br i1 %117, label %100, label %118
118: ; preds = %114, %86, %97
%119 = phi i32 [ %87, %86 ], [ %98, %97 ], [ %116, %114 ]
%120 = phi i32 [ %81, %86 ], [ %89, %97 ], [ %89, %114 ]
%121 = add nuw nsw i32 %80, 1
%122 = icmp slt i32 %121, %119
%123 = icmp sgt i32 %120, 0
%124 = select i1 %122, i1 %123, i1 false
br i1 %124, label %79, label %77
}