#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 dummy;
};
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 /*<<< orphan*/ server_address;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ client_address;

/* 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 FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int server_sockfd, client_sockfd;
  int server_len, client_len;
  struct sockaddr_in server_address;
  struct sockaddr_in client_address;
  int result;
  fd_set readfds, testfds;
  server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  server_address.sin_family = AF_INET;
  server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  server_address.sin_port = htons(9734);
  server_len = sizeof(server_address);
  bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
  listen(server_sockfd, 5);
  FD_ZERO(&readfds);
  FD_SET(server_sockfd, &readfds);
  while (1) {
    char ch;
    int fd;
    int nread;
    testfds = readfds;
    printf("server waiting\n");
    result = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0,
                    (struct timeval *)0);
    if (result < 1) {
      perror("server5");
      exit(1);
    }
    printf("FD_SETSIZE:%d fd:%d\n", FD_SETSIZE, fd);
    for (fd = 0; fd < 10; fd++) {
      printf("fd:%d\n", fd);
      if (FD_ISSET(fd, &testfds)) {
        printf("serer_sockfd:%d fd:%d\n", server_sockfd, fd);
        if (fd == server_sockfd) {
          client_len = sizeof(client_address);
          client_sockfd = accept(
              server_sockfd, (struct sockaddr *)&client_address, &client_len);
          FD_SET(client_sockfd, &readfds);
          printf("adding client on fd %d\n", client_sockfd);
        } else {
          ioctl(fd, FIONREAD, &nread);
          if (nread == 0) {
            close(fd);
            FD_CLR(fd, &readfds);
            printf("removing client on fd %d\n", fd);
          } else {
            read(fd, &ch, 1);
            printf("serving client on fd %d\n", fd);
            ch++;
            write(fd, &ch, 1);
          }
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x2606,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
movl   $0xc,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   10e0 <bind@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
lea    -0x38(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1130 <FD_SET@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
lea    -0x3c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10c0 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
jge    1301 <main+0xe1>
lea    0xd1e(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
mov    -0x44(%rbp),%edx
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x44(%rbp)
cmpl   $0xa,-0x44(%rbp)
jge    1450 <main+0x230>
mov    -0x44(%rbp),%esi
lea    0xcf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x44(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     143d <main+0x21d>
mov    -0x8(%rbp),%esi
mov    -0x44(%rbp),%edx
lea    0xccd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x44(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    13af <main+0x18f>
movl   $0xc,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1100 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1130 <FD_SET@plt>
mov    -0xc(%rbp),%esi
lea    0xca1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1438 <main+0x218>
mov    -0x44(%rbp),%edi
mov    0x2c2f(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1080 <ioctl@plt>
cmpl   $0x0,-0x48(%rbp)
jne    13f8 <main+0x1d8>
mov    -0x44(%rbp),%edi
call   1090 <close@plt>
mov    -0x44(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1110 <FD_CLR@plt>
mov    -0x44(%rbp),%esi
lea    0xc70(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1433 <main+0x213>
mov    -0x44(%rbp),%edi
lea    -0x3d(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
mov    -0x44(%rbp),%esi
lea    0xc63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x3d(%rbp),%al
add    $0x1,%al
mov    %al,-0x3d(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x3d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1438 <main+0x218>
jmp    143d <main+0x21d>
jmp    1442 <main+0x222>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1322 <main+0x102>
jmp    12ad <main+0x8d>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e2a(%rip),%r14
mov    (%r14),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x2606,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10c0 <FD_SET@plt>
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
lea    0xdcc(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [16 x i8] c"server waiting\0A\00"
@FD_SETSIZE = global i32 0
@.str.1 = [8 x i8] c"server5\00"
@.str.2 = [21 x i8] c"FD_SETSIZE:%d fd:%d\0A\00"
@.str.3 = [7 x i8] c"fd:%d\0A\00"
@.str.4 = [23 x i8] c"serer_sockfd:%d fd:%d\0A\00"
@.str.5 = [24 x i8] c"adding client on fd %d\0A\00"
@FIONREAD = global i32 0
@.str.6 = [26 x i8] c"removing client on fd %d\0A\00"
@.str.7 = [25 x i8] c"serving client on fd %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %1
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %2
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = call i32 @htons(i32 9734)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %23, i32* %24
store i32 12, i32* %4
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%27 = load i32, i32* %4
%28 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 %27)
%29 = load i32, i32* %2
%30 = call i32 @listen(i32 %29, i32 5)
%31 = call i32 @FD_ZERO(i32* %9)
%32 = load i32, i32* %2
%33 = call i32 @FD_SET(i32 %32, i32* %9)
br label %34
34: ; preds = %0, %100
%35 = load i32, i32* %9
store i32 %35, i32* %10
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @FD_SETSIZE
%38 = call i32 @select(i32 %37, i32* %10, i32* null, i32* null, %struct.timeval* null)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 1
br i1 %40, label %41, label %44
41: ; preds = %34
%42 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %34
%45 = load i32, i32* @FD_SETSIZE
%46 = load i32, i32* %12
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %45, i32 %46)
store i32 0, i32* %12
br label %48
48: ; preds = %97, %44
%49 = load i32, i32* %12
%50 = icmp slt i32 %49, 10
br i1 %50, label %51, label %100
51: ; preds = %48
%52 = load i32, i32* %12
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %12
%55 = call i64 @FD_ISSET(i32 %54, i32* %10)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %96
57: ; preds = %51
%58 = load i32, i32* %2
%59 = load i32, i32* %12
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %58, i32 %59)
%61 = load i32, i32* %12
%62 = load i32, i32* %2
%63 = icmp eq i32 %61, %62
br i1 %63, label %64, label %72
64: ; preds = %57
store i32 12, i32* %5
%65 = load i32, i32* %2
%66 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%67 = call i32 @accept(i32 %65, %struct.sockaddr* %66, i32* %5)
store i32 %67, i32* %3
%68 = load i32, i32* %3
%69 = call i32 @FD_SET(i32 %68, i32* %9)
%70 = load i32, i32* %3
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %70)
br label %95
72: ; preds = %57
%73 = load i32, i32* %12
%74 = load i32, i32* @FIONREAD
%75 = call i32 @ioctl(i32 %73, i32 %74, i32* %13)
%76 = load i32, i32* %13
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %85
78: ; preds = %72
%79 = load i32, i32* %12
%80 = call i32 @close(i32 %79)
%81 = load i32, i32* %12
%82 = call i32 @FD_CLR(i32 %81, i32* %9)
%83 = load i32, i32* %12
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %83)
br label %94
85: ; preds = %72
%86 = load i32, i32* %12
%87 = call i32 @read(i32 %86, i8* %11, i32 1)
%88 = load i32, i32* %12
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i32 %88)
%90 = load i8, i8* %11
%91 = add i8 %90, 1
store i8 %91, i8* %11
%92 = load i32, i32* %12
%93 = call i32 @write(i32 %92, i8* %11, i32 1)
br label %94
94: ; preds = %85, %78
br label %95
95: ; preds = %94, %64
br label %96
96: ; preds = %95, %51
br label %97
97: ; preds = %96
%98 = load i32, i32* %12
%99 = add nsw i32 %98, 1
store i32 %99, i32* %12
br label %48
100: ; preds = %48
br label %34
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [8 x i8] c"server5\00"
@FIONREAD = global i32 0
@str = [15 x i8] c"server waiting\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = tail call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 9734)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i32 @bind(i32 %9, %struct.sockaddr* %17, i32 12)
%19 = call i32 @listen(i32 %9, i32 5)
%20 = call i32 @FD_ZERO(i32* %2)
%21 = call i32 @FD_SET(i32 %9, i32* %2)
%22 = load i32, i32* %2
store i32 %22, i32* %3
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @FD_SETSIZE
%25 = call i32 @select(i32 %24, i32* %3, i32* null, i32* null, %struct.timeval* null)
%26 = icmp slt i32 %25, 1
%27 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
}