#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*/ server_addr;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ client_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PANIC(char *);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, void *);
int atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int send(int, char *, int, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  int server_fd;
  int client_fd;
  struct sockaddr_in server_addr;
  struct sockaddr_in client_addr;
  fd_set master_fds;
  fd_set read_fds;
  int fdmax;
  struct timeval tv;
  int port;
  int len;
  int i, j;
  char buf[256], ch;
  int nbytes;
  int yes = 1;
  int retval;
  if (argc > 1)
    port = atoi(argv[1]);
  else
    port = 9999;
  tv.tv_sec = 60;
  tv.tv_usec = 0;
  FD_ZERO(&master_fds);
  FD_ZERO(&read_fds);
  if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    PANIC("socket");
  if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
    PANIC("setsockopt");
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = INADDR_ANY;
  server_addr.sin_port = htons(port);
  if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) ==
      -1)
    PANIC("bind");
  if (listen(server_fd, 10) == -1)
    PANIC("listen");
  FD_SET(server_fd, &master_fds);
  FD_SET(0, &master_fds);
  fdmax = server_fd;
  for (;;) {
    read_fds = master_fds;
    retval = select(fdmax + 1, &read_fds, NULL, NULL, &tv);
    switch (retval) {
    case -1:
      perror("select");
      continue;
    case 0:
      printf("Time Out...");
      for (i = 3; i <= fdmax; i++) {
        if (FD_ISSET(i, &master_fds))
          close(i);
      }
      exit(0);
    }
    for (i = 0; i <= fdmax; i++) {
      if (FD_ISSET(i, &read_fds)) {
        if (i == server_fd) {
          len = sizeof(client_addr);
          if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr,
                                  (void *)&len)) == -1) {
            perror("accept");
            continue;
          } else {
            FD_SET(client_fd, &master_fds);
            if (client_fd > fdmax) {
              fdmax = client_fd;
            }
            printf("New connection from %s on socket %d\n",
                   inet_ntoa(client_addr.sin_addr), client_fd);
          }
        } else {
          if ((nbytes = read(i, buf, sizeof(buf))) > 0) {
            write(0, buf, nbytes);
            for (j = 0; j <= fdmax; j++) {
              if (FD_ISSET(j, &master_fds)) {
                if (j != server_fd && j != i && j != 0) {
                  if (send(j, buf, nbytes, 0) == -1) {
                    perror("send");
                  }
                }
              }
            }
          } else {
            perror("recv");
            close(i);
            FD_CLR(i, &master_fds);
          }
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x17c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    12a2 <main+0x42>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1140 <atoi@plt>
mov    %eax,-0x5c(%rbp)
jmp    12a9 <main+0x49>
movl   $0x270f,-0x5c(%rbp)
movl   $0x3c,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
lea    -0x3c(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
lea    -0x40(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1180 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12fb <main+0x9b>
lea    0xd0a(%rip),%rdi
call   10a0 <PANIC@plt>
mov    -0x14(%rbp),%edi
mov    0x2ce3(%rip),%rax
mov    (%rax),%esi
mov    0x2cb2(%rip),%rax
mov    (%rax),%edx
lea    -0x17c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1337 <main+0xd7>
lea    0xcd5(%rip),%rdi
call   10a0 <PANIC@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10b0 <memset@plt>
mov    0x2c92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2c7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x5c(%rbp),%edi
call   1070 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1110 <bind@plt>
cmp    $0xffffffff,%eax
jne    1390 <main+0x130>
lea    0xc87(%rip),%rdi
call   10a0 <PANIC@plt>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1100 <listen@plt>
cmp    $0xffffffff,%eax
jne    13b2 <main+0x152>
lea    0xc6a(%rip),%rdi
call   10a0 <PANIC@plt>
mov    -0x14(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1170 <FD_SET@plt>
xor    %edi,%edi
lea    -0x3c(%rbp),%rsi
call   1170 <FD_SET@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x44(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%rsi
lea    -0x58(%rbp),%r8
mov    %rcx,%rdx
call   10f0 <select@plt>
mov    %eax,-0x180(%rbp)
mov    -0x180(%rbp),%eax
mov    %eax,-0x184(%rbp)
sub    $0xffffffff,%eax
je     1422 <main+0x1c2>
jmp    140f <main+0x1af>
mov    -0x184(%rbp),%eax
test   %eax,%eax
je     1433 <main+0x1d3>
jmp    148c <main+0x22c>
lea    0xbf5(%rip),%rdi
call   1120 <perror@plt>
jmp    13cf <main+0x16f>
lea    0xbeb(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movl   $0x3,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x44(%rbp),%eax
jg     1485 <main+0x225>
mov    -0x64(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1472 <main+0x212>
mov    -0x64(%rbp),%edi
call   10c0 <close@plt>
jmp    1477 <main+0x217>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    1448 <main+0x1e8>
xor    %edi,%edi
call   1160 <exit@plt>
movl   $0x0,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x44(%rbp),%eax
jg     1641 <main+0x3e1>
mov    -0x64(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     162e <main+0x3ce>
mov    -0x64(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1534 <main+0x2d4>
movl   $0xc,-0x60(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x60(%rbp),%rdx
call   1130 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    14f5 <main+0x295>
lea    0xb46(%rip),%rdi
call   1120 <perror@plt>
jmp    1633 <main+0x3d3>
mov    -0x18(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1170 <FD_SET@plt>
mov    -0x18(%rbp),%eax
cmp    -0x44(%rbp),%eax
jle    1513 <main+0x2b3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,%rsi
mov    -0x18(%rbp),%edx
lea    0xb10(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    1629 <main+0x3c9>
mov    -0x64(%rbp),%edi
lea    -0x170(%rbp),%rsi
mov    $0x100,%edx
call   10d0 <read@plt>
mov    %eax,-0x178(%rbp)
cmp    $0x0,%eax
jle    1604 <main+0x3a4>
lea    -0x170(%rbp),%rsi
mov    -0x178(%rbp),%edx
xor    %edi,%edi
call   1040 <write@plt>
movl   $0x0,-0x68(%rbp)
mov    -0x68(%rbp),%eax
cmp    -0x44(%rbp),%eax
jg     15ff <main+0x39f>
mov    -0x68(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     15ec <main+0x38c>
mov    -0x68(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     15e7 <main+0x387>
mov    -0x68(%rbp),%eax
cmp    -0x64(%rbp),%eax
je     15e7 <main+0x387>
cmpl   $0x0,-0x68(%rbp)
je     15e7 <main+0x387>
mov    -0x68(%rbp),%edi
lea    -0x170(%rbp),%rsi
mov    -0x178(%rbp),%edx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
jne    15e2 <main+0x382>
lea    0xa80(%rip),%rdi
call   1120 <perror@plt>
jmp    15e7 <main+0x387>
jmp    15ec <main+0x38c>
jmp    15f1 <main+0x391>
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    1572 <main+0x312>
jmp    1624 <main+0x3c4>
lea    0xa57(%rip),%rdi
call   1120 <perror@plt>
mov    -0x64(%rbp),%edi
call   10c0 <close@plt>
mov    -0x64(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1150 <FD_CLR@plt>
jmp    1629 <main+0x3c9>
jmp    162e <main+0x3ce>
jmp    1633 <main+0x3d3>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    1493 <main+0x233>
jmp    13cf <main+0x16f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
movl   $0x1,0x8(%rsp)
mov    $0x270f,%r14d
cmp    $0x2,%edi
jl     1290 <main+0x30>
mov    0x8(%rsi),%rdi
call   1140 <atoi@plt>
mov    %eax,%r14d
movl   $0x3c,0x20(%rsp)
movq   $0x0,0x28(%rsp)
mov    %rsp,%rdi
call   10e0 <FD_ZERO@plt>
lea    0x4(%rsp),%rdi
call   10e0 <FD_ZERO@plt>
mov    0x2d26(%rip),%r15
mov    (%r15),%edi
mov    0x2d04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1180 <socket@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    12e1 <main+0x81>
lea    0xd24(%rip),%rdi
call   10a0 <PANIC@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%esi
mov    0x2ccf(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1317 <main+0xb7>
lea    0xcf5(%rip),%rdi
call   10a0 <PANIC@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10b0 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x2c9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1070 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1110 <bind@plt>
cmp    $0xffffffff,%eax
jne    136c <main+0x10c>
lea    0xcab(%rip),%rdi
call   10a0 <PANIC@plt>
mov    %r12d,%edi
mov    $0xa,%esi
call   1100 <listen@plt>
cmp    $0xffffffff,%eax
jne    138a <main+0x12a>
lea    0xc92(%rip),%rdi
call   10a0 <PANIC@plt>
mov    %rsp,%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
call   1170 <FD_SET@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1170 <FD_SET@plt>
lea    0x4(%rsp),%r14
mov    %r12d,%r15d
nopw   0x0(%rax,%rax,1)
lea    0x1(%r15),%ebp
lea    0x20(%rsp),%rbx
lea    0xc5e(%rip),%r13
jmp    13d8 <main+0x178>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
call   1120 <perror@plt>
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   10f0 <select@plt>
cmp    $0xffffffff,%eax
je     13d0 <main+0x170>
test   %eax,%eax
je     1555 <main+0x2f5>
test   %r15d,%r15d
js     13d8 <main+0x178>
xor    %r13d,%r13d
lea    0x40(%rsp),%rbp
jmp    1425 <main+0x1c5>
lea    0x4(%rsp),%r14
mov    %ebp,%r12d
lea    0x40(%rsp),%rbp
lea    0x1(%r13),%eax
cmp    %r15d,%r13d
mov    %eax,%r13d
jge    13b0 <main+0x150>
mov    %r13d,%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1419 <main+0x1b9>
cmp    %r12d,%r13d
jne    1490 <main+0x230>
movl   $0xc,0xc(%rsp)
mov    %r12d,%edi
lea    0x30(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1130 <accept@plt>
cmp    $0xffffffff,%eax
je     1520 <main+0x2c0>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rsp,%rsi
call   1170 <FD_SET@plt>
cmp    %r15d,%ebp
cmovg  %ebp,%r15d
mov    0x30(%rsp),%edi
call   1060 <inet_ntoa@plt>
lea    0xbb8(%rip),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <printf@plt>
jmp    1414 <main+0x1b4>
xchg   %ax,%ax
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
call   10d0 <read@plt>
test   %eax,%eax
jle    1531 <main+0x2d1>
mov    %eax,%ebx
xor    %edi,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %r15d,%r15d
js     1419 <main+0x1b9>
mov    %r12d,%ebp
lea    0x1(%r15),%r14d
xor    %r12d,%r12d
jmp    14dd <main+0x27d>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12d
cmp    %r12d,%r14d
je     140c <main+0x1ac>
mov    %r12d,%edi
mov    %rsp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     14d0 <main+0x270>
cmp    %r12d,%ebp
je     14d0 <main+0x270>
cmp    %r12d,%r13d
je     14d0 <main+0x270>
test   %r12d,%r12d
je     14d0 <main+0x270>
mov    %r12d,%edi
lea    0x40(%rsp),%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
jne    14d0 <main+0x270>
lea    0xb44(%rip),%rdi
call   1120 <perror@plt>
jmp    14d0 <main+0x270>
lea    0xb0a(%rip),%rdi
call   1120 <perror@plt>
jmp    1414 <main+0x1b4>
lea    0xb2a(%rip),%rdi
call   1120 <perror@plt>
mov    %r13d,%edi
call   10c0 <close@plt>
mov    %r13d,%edi
mov    %rsp,%rsi
call   1150 <FD_CLR@plt>
jmp    1419 <main+0x1b9>
lea    0xac9(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
cmp    $0x3,%r15d
jge    1570 <main+0x310>
xor    %edi,%edi
call   1160 <exit@plt>
neg    %r15d
mov    $0x3,%ebp
mov    %rsp,%rbx
jmp    1593 <main+0x333>
nopl   (%rax)
lea    (%r15,%rbp,1),%eax
add    $0x1,%eax
mov    %ebp,%ecx
add    $0x1,%ecx
mov    %ecx,%ebp
cmp    $0x1,%eax
je     1569 <main+0x309>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1580 <main+0x320>
mov    %ebp,%edi
call   10c0 <close@plt>
jmp    1580 <main+0x320>

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 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"select\00"
@.str.5 = [12 x i8] c"Time Out...\00"
@.str.6 = [7 x i8] c"accept\00"
@.str.7 = [37 x i8] c"New connection from %s on socket %d\0A\00"
@.str.8 = [5 x i8] c"send\00"
@.str.9 = [5 x i8] c"recv\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 %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca [256 x i8]
%19 = alloca i8
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %21
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 1
br i1 %24, label %25, label %30
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %14
br label %31
30: ; preds = %2
store i32 9999, i32* %14
br label %31
31: ; preds = %30, %25
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 60, i32* %32
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %33
%34 = call i32 @FD_ZERO(i32* %10)
%35 = call i32 @FD_ZERO(i32* %11)
%36 = load i32, i32* @AF_INET
%37 = load i32, i32* @SOCK_STREAM
%38 = call i32 @socket(i32 %36, i32 %37, i32 0)
store i32 %38, i32* %6
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = call i32 @PANIC(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %31
%43 = load i32, i32* %6
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEADDR
%46 = call i32 @setsockopt(i32 %43, i32 %44, i32 %45, i32* %21, i32 4)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @PANIC(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %42
%51 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%52 = load i32, i32* @AF_INET
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %52, i32* %53
%54 = load i32, i32* @INADDR_ANY
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
store i32 %54, i32* %56
%57 = load i32, i32* %14
%58 = call i32 @htons(i32 %57)
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %58, i32* %59
%60 = load i32, i32* %6
%61 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%62 = call i32 @bind(i32 %60, %struct.sockaddr* %61, i32 12)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %50
%65 = call i32 @PANIC(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %66
66: ; preds = %64, %50
%67 = load i32, i32* %6
%68 = call i32 @listen(i32 %67, i32 10)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @PANIC(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %72
72: ; preds = %70, %66
%73 = load i32, i32* %6
%74 = call i32 @FD_SET(i32 %73, i32* %10)
%75 = call i32 @FD_SET(i32 0, i32* %10)
%76 = load i32, i32* %6
store i32 %76, i32* %12
br label %77
77: ; preds = %196, %83, %72
%78 = load i32, i32* %10
store i32 %78, i32* %11
%79 = load i32, i32* %12
%80 = add nsw i32 %79, 1
%81 = call i32 @select(i32 %80, i32* %11, i32* null, i32* null, %struct.timeval* %13)
store i32 %81, i32* %22
%82 = load i32, i32* %22
switch i32 %82, label %104 [
i32 -1, label %83
i32 0, label %85
]
83: ; preds = %77
%84 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %77
85: ; preds = %77
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
store i32 3, i32* %16
br label %87
87: ; preds = %99, %85
%88 = load i32, i32* %16
%89 = load i32, i32* %12
%90 = icmp sle i32 %88, %89
br i1 %90, label %91, label %102
91: ; preds = %87
%92 = load i32, i32* %16
%93 = call i64 @FD_ISSET(i32 %92, i32* %10)
%94 = icmp ne i64 %93, 0
br i1 %94, label %95, label %98
95: ; preds = %91
%96 = load i32, i32* %16
%97 = call i32 @close(i32 %96)
br label %98
98: ; preds = %95, %91
br label %99
99: ; preds = %98
%100 = load i32, i32* %16
%101 = add nsw i32 %100, 1
store i32 %101, i32* %16
br label %87
102: ; preds = %87
%103 = call i32 @exit(i32 0)
unreachable
104: ; preds = %77
store i32 0, i32* %16
br label %105
105: ; preds = %193, %104
%106 = load i32, i32* %16
%107 = load i32, i32* %12
%108 = icmp sle i32 %106, %107
br i1 %108, label %109, label %196
109: ; preds = %105
%110 = load i32, i32* %16
%111 = call i64 @FD_ISSET(i32 %110, i32* %11)
%112 = icmp ne i64 %111, 0
br i1 %112, label %113, label %192
113: ; preds = %109
%114 = load i32, i32* %16
%115 = load i32, i32* %6
%116 = icmp eq i32 %114, %115
br i1 %116, label %117, label %141
117: ; preds = %113
store i32 12, i32* %15
%118 = load i32, i32* %6
%119 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%120 = bitcast i32* %15 to i8*
%121 = call i32 @accept(i32 %118, %struct.sockaddr* %119, i8* %120)
store i32 %121, i32* %7
%122 = icmp eq i32 %121, -1
br i1 %122, label %123, label %125
123: ; preds = %117
%124 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %193
125: ; preds = %117
%126 = load i32, i32* %7
%127 = call i32 @FD_SET(i32 %126, i32* %10)
%128 = load i32, i32* %7
%129 = load i32, i32* %12
%130 = icmp sgt i32 %128, %129
br i1 %130, label %131, label %133
131: ; preds = %125
%132 = load i32, i32* %7
store i32 %132, i32* %12
br label %133
133: ; preds = %131, %125
%134 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%135 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %134, i32 0, i32 0
%136 = load i32, i32* %135
%137 = call i8* @inet_ntoa(i32 %136)
%138 = load i32, i32* %7
%139 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i8* %137, i32 %138)
br label %140
140: ; preds = %133
br label %191
141: ; preds = %113
%142 = load i32, i32* %16
%143 = getelementptr inbounds [256 x i8], [256 x i8]* %18, i64 0, i64 0
%144 = call i32 @read(i32 %142, i8* %143, i32 256)
store i32 %144, i32* %20
%145 = icmp sgt i32 %144, 0
br i1 %145, label %146, label %184
146: ; preds = %141
%147 = getelementptr inbounds [256 x i8], [256 x i8]* %18, i64 0, i64 0
%148 = load i32, i32* %20
%149 = call i32 @write(i32 0, i8* %147, i32 %148)
store i32 0, i32* %17
br label %150
150: ; preds = %180, %146
%151 = load i32, i32* %17
%152 = load i32, i32* %12
%153 = icmp sle i32 %151, %152
br i1 %153, label %154, label %183
154: ; preds = %150
%155 = load i32, i32* %17
%156 = call i64 @FD_ISSET(i32 %155, i32* %10)
%157 = icmp ne i64 %156, 0
br i1 %157, label %158, label %179
158: ; preds = %154
%159 = load i32, i32* %17
%160 = load i32, i32* %6
%161 = icmp ne i32 %159, %160
br i1 %161, label %162, label %178
162: ; preds = %158
%163 = load i32, i32* %17
%164 = load i32, i32* %16
%165 = icmp ne i32 %163, %164
br i1 %165, label %166, label %178
166: ; preds = %162
%167 = load i32, i32* %17
%168 = icmp ne i32 %167, 0
br i1 %168, label %169, label %178
169: ; preds = %166
%170 = load i32, i32* %17
%171 = getelementptr inbounds [256 x i8], [256 x i8]* %18, i64 0, i64 0
%172 = load i32, i32* %20
%173 = call i32 @send(i32 %170, i8* %171, i32 %172, i32 0)
%174 = icmp eq i32 %173, -1
br i1 %174, label %175, label %177
175: ; preds = %169
%176 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %177
177: ; preds = %175, %169
br label %178
178: ; preds = %177, %166, %162, %158
br label %179
179: ; preds = %178, %154
br label %180
180: ; preds = %179
%181 = load i32, i32* %17
%182 = add nsw i32 %181, 1
store i32 %182, i32* %17
br label %150
183: ; preds = %150
br label %190
184: ; preds = %141
%185 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%186 = load i32, i32* %16
%187 = call i32 @close(i32 %186)
%188 = load i32, i32* %16
%189 = call i32 @FD_CLR(i32 %188, i32* %10)
br label %190
190: ; preds = %184, %183
br label %191
191: ; preds = %190, %140
br label %192
192: ; preds = %191, %109
br label %193
193: ; preds = %192, %123
%194 = load i32, i32* %16
%195 = add nsw i32 %194, 1
store i32 %195, i32* %16
br label %105
196: ; preds = %105
br label %77
}

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
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"select\00"
@.str.5 = [12 x i8] c"Time Out...\00"
@.str.6 = [7 x i8] c"accept\00"
@.str.7 = [37 x i8] c"New connection from %s on socket %d\0A\00"
@.str.8 = [5 x i8] c"send\00"
@.str.9 = [5 x i8] c"recv\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = bitcast %struct.sockaddr_in* %4 to i8*
%13 = bitcast i32* %5 to i8*
%14 = bitcast i32* %6 to i8*
%15 = bitcast %struct.timeval* %7 to i8*
%16 = bitcast i32* %8 to i8*
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%18 = bitcast i32* %10 to i8*
store i32 1, i32* %10
%19 = icmp sgt i32 %0, 1
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call i32 @atoi(i8* %22)
br label %24
24: ; preds = %2, %20
%25 = phi i32 [ %23, %20 ], [ 9999, %2 ]
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 60, i32* %26
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i64 0, i64* %27
%28 = call i32 @FD_ZERO(i32* %5)
%29 = call i32 @FD_ZERO(i32* %6)
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @PANIC(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %24
%37 = load i32, i32* @SOL_SOCKET
%38 = load i32, i32* @SO_REUSEADDR
%39 = call i32 @setsockopt(i32 %32, i32 %37, i32 %38, i32* %10, i32 4)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @PANIC(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %45, i32* %46
%47 = load i32, i32* @INADDR_ANY
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %47, i32* %48
%49 = call i32 @htons(i32 %25)
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %49, i32* %50
%51 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%52 = call i32 @bind(i32 %32, %struct.sockaddr* %51, i32 12)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %43
%55 = call i32 @PANIC(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %54, %43
%57 = call i32 @listen(i32 %32, i32 10)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @PANIC(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %59, %56
%62 = call i32 @FD_SET(i32 %32, i32* %5)
%63 = call i32 @FD_SET(i32 0, i32* %5)
%64 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %66
66: ; preds = %138, %61
%67 = phi i32 [ %32, %61 ], [ %139, %138 ]
%68 = add i32 %67, 1
%69 = icmp slt i32 %67, 0
br label %70
70: ; preds = %66, %74
br label %71
71: ; preds = %70, %75
%72 = load i32, i32* %5
store i32 %72, i32* %6
%73 = call i32 @select(i32 %68, i32* %6, i32* null, i32* null, %struct.timeval* %7)
switch i32 %73, label %74 [
i32 -1, label %75
i32 0, label %77
]
74: ; preds = %71
br i1 %69, label %70, label %91
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %71
77: ; preds = %71
%78 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%79 = icmp slt i32 %67, 3
br i1 %79, label %89, label %80
80: ; preds = %77, %86
%81 = phi i32 [ %87, %86 ], [ 3, %77 ]
%82 = call i64 @FD_ISSET(i32 %81, i32* %5)
%83 = icmp eq i64 %82, 0
br i1 %83, label %86, label %84
84: ; preds = %80
%85 = call i32 @close(i32 %81)
br label %86
86: ; preds = %80, %84
%87 = add nuw i32 %81, 1
%88 = icmp eq i32 %81, %67
br i1 %88, label %89, label %80
89: ; preds = %86, %77
%90 = call i32 @exit(i32 0)
unreachable
91: ; preds = %74, %138
%92 = phi i32 [ %139, %138 ], [ %67, %74 ]
%93 = phi i32 [ %140, %138 ], [ 0, %74 ]
%94 = call i64 @FD_ISSET(i32 %93, i32* %6)
%95 = icmp eq i64 %94, 0
br i1 %95, label %138, label %96
96: ; preds = %91
%97 = icmp eq i32 %93, %32
br i1 %97, label %98, label %110
98: ; preds = %96
store i32 12, i32* %8
%99 = call i32 @accept(i32 %32, %struct.sockaddr* %64, i8* %16)
%100 = icmp eq i32 %99, -1
br i1 %100, label %101, label %103
101: ; preds = %98
%102 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %138
103: ; preds = %98
%104 = call i32 @FD_SET(i32 %99, i32* %5)
%105 = icmp sgt i32 %99, %92
%106 = select i1 %105, i32 %99, i32 %92
%107 = load i32, i32* %65
%108 = call i8* @inet_ntoa(i32 %107)
%109 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i8* %108, i32 %99)
br label %138
110: ; preds = %96
%111 = call i32 @read(i32 %93, i8* %17, i32 256)
%112 = icmp sgt i32 %111, 0
br i1 %112, label %113, label %134
113: ; preds = %110
%114 = call i32 @write(i32 0, i8* %17, i32 %111)
%115 = icmp slt i32 %92, 0
br i1 %115, label %138, label %116
116: ; preds = %113, %131
%117 = phi i32 [ %132, %131 ], [ 0, %113 ]
%118 = call i64 @FD_ISSET(i32 %117, i32* %5)
%119 = icmp eq i64 %118, 0
%120 = icmp eq i32 %117, %32
%121 = select i1 %119, i1 true, i1 %120
br i1 %121, label %131, label %122
122: ; preds = %116
%123 = icmp ne i32 %117, %93
%124 = icmp ne i32 %117, 0
%125 = and i1 %123, %124
br i1 %125, label %126, label %131
126: ; preds = %122
%127 = call i32 @send(i32 %117, i8* %17, i32 %111, i32 0)
%128 = icmp eq i32 %127, -1
br i1 %128, label %129, label %131
129: ; preds = %126
%130 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %131
131: ; preds = %116, %126, %129, %122
%132 = add nuw i32 %117, 1
%133 = icmp eq i32 %117, %92
br i1 %133, label %138, label %116
134: ; preds = %110
%135 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%136 = call i32 @close(i32 %93)
%137 = call i32 @FD_CLR(i32 %93, i32* %5)
br label %138
138: ; preds = %131, %113, %91, %134, %103, %101
%139 = phi i32 [ %92, %101 ], [ %106, %103 ], [ %92, %134 ], [ %92, %91 ], [ %92, %113 ], [ %92, %131 ]
%140 = add nuw nsw i32 %93, 1
%141 = icmp slt i32 %93, %139
br i1 %141, label %91, label %66
}