#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 {
  int 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_select(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);
  while (1) {
    int fd;
    int nread;
    int result;
    optfds = readfds;
    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 10000000;
    FD_ZERO(&readfds);
    if ((result = select(fd_maxsize + 1, &optfds, (fd_set *)0, (fd_set *)0,
                         &timeout)) < 0) {
      die("Failed select socket");
    }
    printf("select result: %d\n", result);
    for (fd = 0; fd < fd_maxsize + 1; fd++) {
      printf("for FD: %d\n", fd);
      if (FD_ISSET(fd, &optfds)) {
        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 & fd_maxsize: %d\n", clientsock);
        } 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_select>:
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    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x58(%rbp)
movl   $0x989680,-0x60(%rbp)
lea    -0x34(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x40(%rbp),%edi
add    $0x1,%edi
lea    -0x38(%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
jge    1239 <tm_select+0x79>
lea    0xdcc(%rip),%rdi
call   10b0 <die@plt>
mov    -0x4c(%rbp),%esi
lea    0xdd2(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    -0x40(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jae    1372 <tm_select+0x1b2>
mov    -0x44(%rbp),%esi
lea    0xdbe(%rip),%rdi
call   1040 <printf@plt>
mov    -0x44(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     135f <tm_select+0x19f>
mov    -0x44(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    12ff <tm_select+0x13f>
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    12d9 <tm_select+0x119>
lea    0xd60(%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
lea    0xd62(%rip),%rdi
call   1040 <printf@plt>
jmp    135a <tm_select+0x19a>
mov    -0x44(%rbp),%edi
mov    0x2cdf(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1060 <ioctl@plt>
cmpl   $0x0,-0x48(%rbp)
jne    1346 <tm_select+0x186>
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    0xd3b(%rip),%rdi
call   1040 <printf@plt>
jmp    1355 <tm_select+0x195>
mov    -0x44(%rbp),%esi
lea    0xd35(%rip),%rdi
call   1040 <printf@plt>
jmp    135a <tm_select+0x19a>
jmp    135f <tm_select+0x19f>
jmp    1364 <tm_select+0x1a4>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    124f <tm_select+0x8f>
jmp    11e9 <tm_select+0x29>

x86 assembly (clang, -O2):
<tm_select>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10e0 <FD_SET@plt>
lea    0x14(%rsp),%r15
lea    0xe35(%rip),%r13
lea    0x30(%rsp),%r12
mov    %ebx,%ebp
jmp    1205 <tm_select+0x45>
nopl   0x0(%rax)
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%eax
mov    %eax,0x14(%rsp)
movq   $0x0,0x28(%rsp)
movl   $0x989680,0x20(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <FD_ZERO@plt>
lea    0x1(%rbp),%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x20(%rsp),%r8
call   1090 <select@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    124f <tm_select+0x8f>
lea    0xdb6(%rip),%rdi
call   10b0 <die@plt>
lea    0xdbf(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
cmp    $0xffffffff,%ebp
je     1200 <tm_select+0x40>
xor    %r14d,%r14d
jmp    12d0 <tm_select+0x110>
nopl   0x0(%rax,%rax,1)
movl   $0x4,0x10(%rsp)
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    %r12,%rdi
call   10a0 <getdate@plt>
mov    %ebx,%edi
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1316 <tm_select+0x156>
mov    %ebp,%edi
lea    0x8(%rsp),%rsi
call   10e0 <FD_SET@plt>
lea    0xd9e(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
add    $0x1,%r14d
lea    0x1(%rbp),%eax
cmp    %eax,%r14d
jae    1205 <tm_select+0x45>
mov    %r13,%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    %r14d,%edi
mov    %r15,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12c0 <tm_select+0x100>
cmp    %r14d,%ebx
je     1270 <tm_select+0xb0>
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
lea    0xc(%rsp),%rdx
call   1060 <ioctl@plt>
cmpl   $0x0,0xc(%rsp)
je     1324 <tm_select+0x164>
lea    0xd71(%rip),%rdi
jmp    1340 <tm_select+0x180>
lea    0xd17(%rip),%rdi
call   10b0 <die@plt>
jmp    12a6 <tm_select+0xe6>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %r14d,%edi
lea    0x8(%rsp),%rsi
call   10d0 <FD_CLR@plt>
lea    0xd37(%rip),%rdi
mov    %r14d,%esi
jmp    12bb <tm_select+0xfb>

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

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