#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;
  int 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 ssize_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
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*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close_socket(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int maxfd;
  fd_set master;
  fd_set readfds;
  int status;
  struct timeval tv;
  int sock, client_sock;
  ssize_t readret;
  ssize_t sendret;
  struct sockaddr_in addr;
  char buf[4096];
  fprintf(stdout, "----- Echo Server -----\n");
  if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "Failed creating socket.\n");
    return EXIT_FAILURE;
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(9999);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr))) {
    close_socket(sock);
    fprintf(stderr, "Failed binding socket.\n");
    return EXIT_FAILURE;
  }
  if (listen(sock, SOMAXCONN)) {
    close_socket(sock);
    fprintf(stderr, "Error listening on socket.\n");
    return EXIT_FAILURE;
  }
  FD_ZERO(&master);
  FD_ZERO(&readfds);
  FD_SET(sock, &master);
  maxfd = sock;
  tv.tv_sec = 3;
  tv.tv_usec = 500000;
  for (;;) {
    readfds = master;
    status = select(maxfd + 1, &readfds, NULL, NULL, &tv);
    if (status == -1) {
      perror("select");
      exit(EXIT_FAILURE);
    } else if (status == 0) {
    } else {
      for (int i = 0; i <= maxfd; i++) {
        if (FD_ISSET(i, &readfds)) {
          if (i == sock) {
            client_sock = accept(sock, NULL, NULL);
            if (client_sock == -1) {
              close_socket(sock);
              FD_CLR(sock, &master);
              fprintf(stderr, "Server Accepting sock error");
            } else {
              FD_SET(client_sock, &master);
              if (client_sock > maxfd) {
                maxfd = client_sock;
              }
            }
          } else {
            readret = recv(i, buf, 4096, 0);
            if (readret == -1) {
              close_socket(i);
              FD_CLR(i, &master);
              fprintf(stderr, "Server Reading data error");
            } else if (readret == 0) {
              close_socket(i);
              FD_CLR(i, &master);
            } else {
              sendret = send(i, buf, readret, 0);
              if (sendret == 0) {
                close_socket(i);
                FD_CLR(i, &master);
              } else if (sendret != readret) {
                close_socket(i);
                FD_CLR(i, &master);
                fprintf(stderr, "Server Error sending to client\n");
              }
            }
          }
        }
      }
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1279 <main+0x79>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    15ad <main+0x3ad>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,-0x44(%rbp)
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
je     12e7 <main+0xe7>
mov    -0x2c(%rbp),%edi
call   10a0 <close_socket@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    15ad <main+0x3ad>
mov    -0x2c(%rbp),%edi
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
je     1330 <main+0x130>
mov    -0x2c(%rbp),%edi
call   10a0 <close_socket@plt>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
lea    0xd30(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2c8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    15ad <main+0x3ad>
lea    -0x18(%rbp),%rdi
call   1080 <FD_ZERO@plt>
lea    -0x1c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0x3,-0x28(%rbp)
movl   $0x7a120,-0x24(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    13a9 <main+0x1a9>
lea    0xcd0(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c0e(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jne    13b8 <main+0x1b8>
jmp    15a3 <main+0x3a3>
movl   $0x0,-0x1054(%rbp)
mov    -0x1054(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     159e <main+0x39e>
mov    -0x1054(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1585 <main+0x385>
mov    -0x1054(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jne    146b <main+0x26b>
mov    -0x2c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10e0 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1443 <main+0x243>
mov    -0x2c(%rbp),%edi
call   10a0 <close_socket@plt>
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10f0 <FD_CLR@plt>
mov    0x2ba0(%rip),%rax
mov    (%rax),%edi
lea    0xc34(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1466 <main+0x266>
mov    -0x30(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1461 <main+0x261>
mov    -0x30(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1466 <main+0x266>
jmp    1580 <main+0x380>
mov    -0x1054(%rbp),%edi
lea    -0x1050(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    14c5 <main+0x2c5>
mov    -0x1054(%rbp),%edi
call   10a0 <close_socket@plt>
mov    -0x1054(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10f0 <FD_CLR@plt>
mov    0x2b1e(%rip),%rax
mov    (%rax),%edi
lea    0xbce(%rip),%rsi
call   1070 <fprintf@plt>
jmp    157b <main+0x37b>
cmpl   $0x0,-0x34(%rbp)
jne    14ee <main+0x2ee>
mov    -0x1054(%rbp),%edi
call   10a0 <close_socket@plt>
mov    -0x1054(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10f0 <FD_CLR@plt>
jmp    1576 <main+0x376>
mov    -0x1054(%rbp),%edi
lea    -0x1050(%rbp),%rsi
mov    -0x34(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    1531 <main+0x331>
mov    -0x1054(%rbp),%edi
call   10a0 <close_socket@plt>
mov    -0x1054(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10f0 <FD_CLR@plt>
jmp    1571 <main+0x371>
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
je     156c <main+0x36c>
mov    -0x1054(%rbp),%edi
call   10a0 <close_socket@plt>
mov    -0x1054(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10f0 <FD_CLR@plt>
mov    0x2a72(%rip),%rax
mov    (%rax),%edi
lea    0xb3c(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1571 <main+0x371>
jmp    1576 <main+0x376>
jmp    157b <main+0x37b>
jmp    1580 <main+0x380>
jmp    1585 <main+0x385>
jmp    158a <main+0x38a>
mov    -0x1054(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1054(%rbp)
jmp    13c2 <main+0x1c2>
jmp    15a3 <main+0x3a3>
jmp    15a8 <main+0x3a8>
jmp    1362 <main+0x162>
mov    -0x4(%rbp),%eax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
cmp    $0xffffffff,%eax
je     129d <main+0x9d>
mov    %eax,%ebx
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
je     12af <main+0xaf>
mov    %ebx,%edi
call   10a0 <close_socket@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd97(%rip),%rsi
jmp    12db <main+0xdb>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
lea    0xd6c(%rip),%rsi
jmp    12db <main+0xdb>
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10b0 <listen@plt>
test   %rax,%rax
je     12fb <main+0xfb>
mov    %ebx,%edi
call   10a0 <close_socket@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1110 <FD_SET@plt>
movabs $0x7a12000000003,%rax
mov    %rax,0x18(%rsp)
lea    0x18(%rsp),%r12
mov    %ebx,%r14d
lea    0x1(%r14),%ebp
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1090 <select@plt>
test   %eax,%eax
je     1340 <main+0x140>
cmp    $0xffffffff,%eax
je     1488 <main+0x288>
test   %r14d,%r14d
js     1340 <main+0x140>
xor    %r13d,%r13d
jmp    139c <main+0x19c>
mov    %r13d,%edi
call   10a0 <close_socket@plt>
mov    %r13d,%edi
mov    %rsp,%rsi
call   10f0 <FD_CLR@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0x1(%r13),%eax
cmp    %r14d,%r13d
mov    %eax,%r13d
jge    1334 <main+0x134>
mov    %r13d,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1390 <main+0x190>
cmp    %r13d,%ebx
jne    13d6 <main+0x1d6>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
cmp    $0xffffffff,%eax
je     141a <main+0x21a>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rsp,%rsi
call   1110 <FD_SET@plt>
cmp    %r14d,%ebp
cmovg  %ebp,%r14d
jmp    1390 <main+0x190>
mov    %r13d,%edi
lea    0x20(%rsp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     136f <main+0x16f>
mov    %eax,%ebp
mov    %r13d,%edi
cmp    $0xffffffff,%eax
jne    143d <main+0x23d>
call   10a0 <close_socket@plt>
mov    %r13d,%edi
mov    %rsp,%rsi
call   10f0 <FD_CLR@plt>
mov    0x2bc1(%rip),%rax
mov    (%rax),%edi
lea    0xc71(%rip),%rsi
jmp    147e <main+0x27e>
mov    %ebx,%edi
call   10a0 <close_socket@plt>
mov    %ebx,%edi
mov    %rsp,%rsi
call   10f0 <FD_CLR@plt>
mov    0x2b9e(%rip),%rax
mov    (%rax),%edi
lea    0xc32(%rip),%rsi
jmp    147e <main+0x27e>
lea    0x20(%rsp),%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %eax,%eax
je     136f <main+0x16f>
cmp    %ebp,%eax
je     1390 <main+0x190>
mov    %r13d,%edi
call   10a0 <close_socket@plt>
mov    %r13d,%edi
mov    %rsp,%rsi
call   10f0 <FD_CLR@plt>
mov    0x2b5b(%rip),%rax
mov    (%rax),%edi
lea    0xc25(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1390 <main+0x190>
lea    0xbd7(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2b15(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>

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

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stdout = global i32 0
@.str = [25 x i8] c"----- Echo Server -----\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed creating socket.\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [24 x i8] c"Failed binding socket.\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [28 x i8] c"Error listening on socket.\0A\00"
@.str.4 = [7 x i8] c"select\00"
@.str.5 = [28 x i8] c"Server Accepting sock error\00"
@.str.6 = [26 x i8] c"Server Reading data error\00"
@.str.7 = [32 x i8] c"Server Error sending to client\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca %struct.sockaddr_in
%7 = alloca [4096 x i8]
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast %struct.timeval* %5 to i8*
%11 = bitcast %struct.sockaddr_in* %6 to i8*
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* @stdout
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @PF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %109
22: ; preds = %2
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %23, i32* %24
%25 = tail call i32 @htons(i32 9999)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%30 = call i64 @bind(i32 %17, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %22
%33 = call i32 @close_socket(i32 %17)
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %109
36: ; preds = %22
%37 = load i32, i32* @SOMAXCONN
%38 = call i64 @listen(i32 %17, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = call i32 @close_socket(i32 %17)
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %109
44: ; preds = %36
%45 = call i32 @FD_ZERO(i32* %3)
%46 = call i32 @FD_ZERO(i32* %4)
%47 = call i32 @FD_SET(i32 %17, i32* %3)
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 3, i32* %48
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i32 500000, i32* %49
br label %50
50: ; preds = %105, %44
%51 = phi i32 [ %17, %44 ], [ %106, %105 ]
%52 = add nsw i32 %51, 1
%53 = icmp slt i32 %51, 0
br label %54
54: ; preds = %58, %50
%55 = load i32, i32* %3
store i32 %55, i32* %4
%56 = call i32 @select(i32 %52, i32* %4, i32* null, i32* null, %struct.timeval* %5)
switch i32 %56, label %57 [
i32 -1, label %59
i32 0, label %58
]
57: ; preds = %54
br i1 %53, label %58, label %63
58: ; preds = %57, %54
br label %54
59: ; preds = %54
%60 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %57, %105
%64 = phi i32 [ %107, %105 ], [ 0, %57 ]
%65 = phi i32 [ %106, %105 ], [ %51, %57 ]
%66 = call i64 @FD_ISSET(i32 %64, i32* %4)
%67 = icmp eq i64 %66, 0
br i1 %67, label %105, label %68
68: ; preds = %63
%69 = icmp eq i32 %64, %17
br i1 %69, label %70, label %82
70: ; preds = %68
%71 = call i32 @accept(i32 %17, i32* null, i32* null)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %78
73: ; preds = %70
%74 = call i32 @close_socket(i32 %17)
%75 = call i32 @FD_CLR(i32 %17, i32* %3)
%76 = load i32, i32* @stderr
%77 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %105
78: ; preds = %70
%79 = call i32 @FD_SET(i32 %71, i32* %3)
%80 = icmp sgt i32 %71, %65
%81 = select i1 %80, i32 %71, i32 %65
br label %105
82: ; preds = %68
%83 = call i32 @recv(i32 %64, i8* %12, i32 4096, i32 0)
switch i32 %83, label %92 [
i32 -1, label %84
i32 0, label %89
]
84: ; preds = %82
%85 = call i32 @close_socket(i32 %64)
%86 = call i32 @FD_CLR(i32 %64, i32* %3)
%87 = load i32, i32* @stderr
%88 = call i32 @fprintf(i32 %87, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %105
89: ; preds = %82
%90 = call i32 @close_socket(i32 %64)
%91 = call i32 @FD_CLR(i32 %64, i32* %3)
br label %105
92: ; preds = %82
%93 = call i32 @send(i32 %64, i8* %12, i32 %83, i32 0)
%94 = icmp eq i32 %93, 0
br i1 %94, label %95, label %98
95: ; preds = %92
%96 = call i32 @close_socket(i32 %64)
%97 = call i32 @FD_CLR(i32 %64, i32* %3)
br label %105
98: ; preds = %92
%99 = icmp eq i32 %93, %83
br i1 %99, label %105, label %100
100: ; preds = %98
%101 = call i32 @close_socket(i32 %64)
%102 = call i32 @FD_CLR(i32 %64, i32* %3)
%103 = load i32, i32* @stderr
%104 = call i32 @fprintf(i32 %103, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
br label %105
105: ; preds = %78, %63, %84, %95, %100, %98, %89, %73
%106 = phi i32 [ %65, %73 ], [ %65, %84 ], [ %65, %89 ], [ %65, %95 ], [ %65, %100 ], [ %65, %98 ], [ %65, %63 ], [ %81, %78 ]
%107 = add nuw nsw i32 %64, 1
%108 = icmp slt i32 %64, %106
br i1 %108, label %63, label %50
109: ; preds = %40, %32, %19
%110 = load i32, i32* @EXIT_FAILURE
ret i32 %110
}