#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 */

/* Type definitions */
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ currtime;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
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*/ FIONREAD;
int accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ getdate(char *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, ...);
int select(unsigned int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
void tm_select3(int serversock, unsigned int max_client) {
  int clientsock;
  struct sockaddr_in client_addr;
  char currtime[32];
  fd_set readfds, optfds;
  unsigned clientlen, fd_maxsize;
  fd_maxsize = serversock;
  FD_ZERO(&readfds);
  FD_SET(serversock, &readfds);
  printf("server sock:%d\n", serversock);
  while (1) {
    int fd;
    int nread;
    int result;
    optfds = readfds;
    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 0;
    FD_ZERO(&readfds);
    FD_SET(serversock, &readfds);
    if ((result = select(fd_maxsize + 1, &readfds, (fd_set *)0, (fd_set *)0,
                         &timeout)) <= 0) {
      continue;
    }
    printf("select result: %d\n", result);
    for (fd = 0; fd < fd_maxsize + 1; fd++) {
      printf("for FD: %d\n", fd);
      if (FD_ISSET(fd, &readfds)) {
        if (fd < 3) {
          continue;
        }
        if (fd == serversock) {
          clientlen = sizeof(client_addr);
          memset(currtime, 0, sizeof(currtime));
          getdate(currtime);
          if ((clientsock = accept(serversock, (struct sockaddr *)&client_addr,
                                   &clientlen)) < 0) {
            die("Failed to accept client connection");
          }
          FD_SET(clientsock, &readfds);
          fd_maxsize = clientsock;
          printf("accept new fd %d & fd_maxsize: %d\n", clientsock, fd_maxsize);
        } else {
          ioctl(fd, FIONREAD, &nread);
          if (nread != 0) {
            close(fd);
            FD_CLR(fd, &readfds);
            printf("FD_CLR fd %d\n", fd);
          } else {
            printf("handle client %d\n", fd);
          }
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<tm_select3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x34(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x4(%rbp),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
lea    -0x34(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x40(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x60(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jg     1250 <tm_select3+0x90>
jmp    11fa <tm_select3+0x3a>
mov    -0x4c(%rbp),%esi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    -0x40(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jae    13a5 <tm_select3+0x1e5>
mov    -0x44(%rbp),%esi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x44(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1392 <tm_select3+0x1d2>
cmpl   $0x3,-0x44(%rbp)
jge    12af <tm_select3+0xef>
jmp    1397 <tm_select3+0x1d7>
mov    -0x44(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    132e <tm_select3+0x16e>
movl   $0x4,-0x3c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x30(%rbp),%rdi
call   10a0 <getdate@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1303 <tm_select3+0x143>
lea    0xd31(%rip),%rdi
call   10b0 <die@plt>
mov    -0xc(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x40(%rbp),%edx
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    138d <tm_select3+0x1cd>
mov    -0x44(%rbp),%edi
mov    0x2cb0(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1060 <ioctl@plt>
cmpl   $0x0,-0x48(%rbp)
je     1377 <tm_select3+0x1b7>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
mov    -0x44(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10d0 <FD_CLR@plt>
mov    -0x44(%rbp),%esi
lea    0xd0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1388 <tm_select3+0x1c8>
mov    -0x44(%rbp),%esi
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    138d <tm_select3+0x1cd>
jmp    1392 <tm_select3+0x1d2>
jmp    1397 <tm_select3+0x1d7>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1268 <tm_select3+0xa8>
jmp    11fa <tm_select3+0x3a>

x86 assembly (clang, -O2):
<tm_select3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10e0 <FD_SET@plt>
lea    0xe22(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe35(%rip),%r12
lea    0x20(%rsp),%r13
mov    %ebp,%r15d
jmp    1216 <tm_select3+0x56>
nopl   0x0(%rax,%rax,1)
mov    $0xffffffff,%r15d
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10e0 <FD_SET@plt>
lea    0x1(%r15),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   1090 <select@plt>
test   %eax,%eax
jle    1216 <tm_select3+0x56>
lea    0xdd0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xffffffff,%r15d
je     1210 <tm_select3+0x50>
xor    %r14d,%r14d
jmp    12db <tm_select3+0x11b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movl   $0x4,0x4(%rsp)
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    %r13,%rdi
call   10a0 <getdate@plt>
mov    %ebp,%edi
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   10c0 <accept@plt>
mov    %eax,%r15d
test   %eax,%eax
js     133d <tm_select3+0x17d>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10e0 <FD_SET@plt>
lea    0xda5(%rip),%rdi
mov    %r15d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r14d
lea    0x1(%r15),%eax
cmp    %eax,%r14d
jae    1216 <tm_select3+0x56>
mov    %r12,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12ca <tm_select3+0x10a>
cmp    $0x3,%r14d
jb     12ca <tm_select3+0x10a>
cmp    %r14d,%ebp
je     1270 <tm_select3+0xb0>
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rsp,%rdx
call   1060 <ioctl@plt>
cmpl   $0x0,(%rsp)
je     134e <tm_select3+0x18e>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   10d0 <FD_CLR@plt>
lea    0xd4a(%rip),%rdi
jmp    1355 <tm_select3+0x195>
lea    0xcfb(%rip),%rdi
call   10b0 <die@plt>
jmp    12ab <tm_select3+0xeb>
lea    0xd3e(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12ca <tm_select3+0x10a>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"server sock:%d\0A\00"
@.str.1 = [19 x i8] c"select result: %d\0A\00"
@.str.2 = [12 x i8] c"for FD: %d\0A\00"
@.str.3 = [35 x i8] c"Failed to accept client connection\00"
@.str.4 = [35 x i8] c"accept new fd %d & fd_maxsize: %d\0A\00"
@FIONREAD = global i32 0
@.str.5 = [14 x i8] c"FD_CLR fd %d\0A\00"
@.str.6 = [18 x i8] c"handle client %d\0A\00"
define void @tm_select3(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca [32 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.timeval
store i32 %0, i32* %3
store i32 %1, i32* %4
%16 = load i32, i32* %3
store i32 %16, i32* %11
%17 = call i32 @FD_ZERO(i32* %8)
%18 = load i32, i32* %3
%19 = call i32 @FD_SET(i32 %18, i32* %8)
%20 = load i32, i32* %3
%21 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %2, %33, %96
%23 = load i32, i32* %8
store i32 %23, i32* %9
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
store i64 0, i64* %25
%26 = call i32 @FD_ZERO(i32* %8)
%27 = load i32, i32* %3
%28 = call i32 @FD_SET(i32 %27, i32* %8)
%29 = load i32, i32* %11
%30 = add i32 %29, 1
%31 = call i32 @select(i32 %30, i32* %8, i32* null, i32* null, %struct.timeval* %15)
store i32 %31, i32* %14
%32 = icmp sle i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %22
br label %22
34: ; preds = %22
%35 = load i32, i32* %14
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %35)
store i32 0, i32* %12
br label %37
37: ; preds = %93, %34
%38 = load i32, i32* %12
%39 = load i32, i32* %11
%40 = add i32 %39, 1
%41 = icmp ult i32 %38, %40
br i1 %41, label %42, label %96
42: ; preds = %37
%43 = load i32, i32* %12
%44 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %12
%46 = call i64 @FD_ISSET(i32 %45, i32* %8)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %92
48: ; preds = %42
%49 = load i32, i32* %12
%50 = icmp slt i32 %49, 3
br i1 %50, label %51, label %52
51: ; preds = %48
br label %93
52: ; preds = %48
%53 = load i32, i32* %12
%54 = load i32, i32* %3
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %74
56: ; preds = %52
store i32 4, i32* %10
%57 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%58 = call i32 @memset(i8* %57, i32 0, i32 32)
%59 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%60 = call i32 @getdate(i8* %59)
%61 = load i32, i32* %3
%62 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%63 = call i32 @accept(i32 %61, %struct.sockaddr* %62, i32* %10)
store i32 %63, i32* %5
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %56
%66 = call i32 @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %67
67: ; preds = %65, %56
%68 = load i32, i32* %5
%69 = call i32 @FD_SET(i32 %68, i32* %8)
%70 = load i32, i32* %5
store i32 %70, i32* %11
%71 = load i32, i32* %5
%72 = load i32, i32* %11
%73 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %71, i32 %72)
br label %91
74: ; preds = %52
%75 = load i32, i32* %12
%76 = load i32, i32* @FIONREAD
%77 = call i32 @ioctl(i32 %75, i32 %76, i32* %13)
%78 = load i32, i32* %13
%79 = icmp ne i32 %78, 0
br i1 %79, label %80, label %87
80: ; preds = %74
%81 = load i32, i32* %12
%82 = call i32 @close(i32 %81)
%83 = load i32, i32* %12
%84 = call i32 @FD_CLR(i32 %83, i32* %8)
%85 = load i32, i32* %12
%86 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %85)
br label %90
87: ; preds = %74
%88 = load i32, i32* %12
%89 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %88)
br label %90
90: ; preds = %87, %80
br label %91
91: ; preds = %90, %67
br label %92
92: ; preds = %91, %42
br label %93
93: ; preds = %92, %51
%94 = load i32, i32* %12
%95 = add nsw i32 %94, 1
store i32 %95, i32* %12
br label %37
96: ; preds = %37
br label %22
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"server sock:%d\0A\00"
@.str.1 = [19 x i8] c"select result: %d\0A\00"
@.str.2 = [12 x i8] c"for FD: %d\0A\00"
@.str.3 = [35 x i8] c"Failed to accept client connection\00"
@.str.4 = [35 x i8] c"accept new fd %d & fd_maxsize: %d\0A\00"
@FIONREAD = global i32 0
@.str.5 = [14 x i8] c"FD_CLR fd %d\0A\00"
@.str.6 = [18 x i8] c"handle client %d\0A\00"
define void @tm_select3(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [32 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%11 = bitcast i32* %5 to i8*
%12 = bitcast i32* %6 to i8*
%13 = call i32 @FD_ZERO(i32* %5)
%14 = call i32 @FD_SET(i32 %0, i32* %5)
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%16 = bitcast i32* %7 to i8*
%17 = bitcast %struct.timeval* %8 to i8*
%18 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%19 = bitcast %struct.timeval* %8 to i8*
br label %20
20: ; preds = %66, %2
%21 = phi i32 [ %0, %2 ], [ %67, %66 ]
%22 = call i32 @FD_ZERO(i32* %5)
%23 = call i32 @FD_SET(i32 %0, i32* %5)
%24 = add i32 %21, 1
%25 = call i32 @select(i32 %24, i32* %5, i32* null, i32* null, %struct.timeval* %8)
%26 = icmp slt i32 %25, 1
br i1 %26, label %66, label %27
27: ; preds = %20
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%29 = icmp eq i32 %21, -1
br i1 %29, label %66, label %30
30: ; preds = %27, %61
%31 = phi i32 [ %62, %61 ], [ %21, %27 ]
%32 = phi i32 [ %63, %61 ], [ 0, %27 ]
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i64 @FD_ISSET(i32 %32, i32* %5)
%35 = icmp eq i64 %34, 0
%36 = icmp ult i32 %32, 3
%37 = or i1 %35, %36
br i1 %37, label %61, label %38
38: ; preds = %30
%39 = icmp eq i32 %32, %0
br i1 %39, label %40, label %50
40: ; preds = %38
store i32 4, i32* %6
%41 = call i32 @memset(i8* %10, i32 0, i32 32)
%42 = call i32 @getdate(i8* %10)
%43 = call i32 @accept(i32 %0, %struct.sockaddr* %18, i32* %6)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %40
%48 = call i32 @FD_SET(i32 %43, i32* %5)
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %43, i32 %43)
br label %61
50: ; preds = %38
%51 = load i32, i32* @FIONREAD
%52 = call i32 @ioctl(i32 %32, i32 %51, i32* %7)
%53 = load i32, i32* %7
%54 = icmp eq i32 %53, 0
br i1 %54, label %59, label %55
55: ; preds = %50
%56 = call i32 @close(i32 %32)
%57 = call i32 @FD_CLR(i32 %32, i32* %5)
%58 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %32)
br label %61
59: ; preds = %50
%60 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %32)
br label %61
61: ; preds = %30, %55, %59, %47
%62 = phi i32 [ %31, %30 ], [ %43, %47 ], [ %31, %55 ], [ %31, %59 ]
%63 = add nuw nsw i32 %32, 1
%64 = add i32 %62, 1
%65 = icmp ult i32 %63, %64
br i1 %65, label %30, label %66
66: ; preds = %61, %27, %20
%67 = phi i32 [ %21, %20 ], [ -1, %27 ], [ %62, %61 ]
br label %20
}