#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*/ 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 accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int select(unsigned int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
void tm_select2(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;
    clientlen = sizeof(client_addr);
    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);
    if (serversock != -1 && FD_ISSET(serversock, &readfds)) {
      if ((clientsock = accept(serversock, (struct sockaddr *)&client_addr,
                               &clientlen)) < 0) {
        perror("accept");
        continue;
      }
    }
    fd_maxsize = clientsock;
    FD_CLR(clientsock, &readfds);
    FD_CLR(serversock, &readfds);
    close(clientsock);
  }
}


x86 assembly (clang, -O0):
<tm_select2>:
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   1060 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x4(%rbp),%esi
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
movl   $0x4,-0x3c(%rbp)
lea    -0x34(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10b0 <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   1070 <select@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jg     1225 <tm_select2+0x95>
jmp    11c8 <tm_select2+0x38>
mov    -0x4c(%rbp),%esi
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0xffffffff,-0x4(%rbp)
je     1286 <tm_select2+0xf6>
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1286 <tm_select2+0xf6>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1281 <tm_select2+0xf1>
lea    0xdac(%rip),%rdi
call   1080 <perror@plt>
jmp    11c8 <tm_select2+0x38>
jmp    1286 <tm_select2+0xf6>
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10a0 <FD_CLR@plt>
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10a0 <FD_CLR@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    11c8 <tm_select2+0x38>

x86 assembly (clang, -O2):
<tm_select2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10b0 <FD_SET@plt>
lea    0xe52(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0x10(%rsp),%r15
lea    0xe4f(%rip),%r12
mov    %ebx,%ebp
jmp    1200 <tm_select2+0x70>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%edi
mov    %r14,%rsi
call   10a0 <FD_CLR@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10a0 <FD_CLR@plt>
mov    %r13d,%edi
call   1050 <close@plt>
mov    %r13d,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x4,0x4(%rsp)
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10b0 <FD_SET@plt>
lea    0x1(%rbp),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1070 <select@plt>
test   %eax,%eax
jle    1200 <tm_select2+0x70>
mov    %r12,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
cmp    $0xffffffff,%ebx
je     11e0 <tm_select2+0x50>
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11e0 <tm_select2+0x50>
mov    %ebx,%edi
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1090 <accept@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    11e0 <tm_select2+0x50>
lea    0xdba(%rip),%rdi
call   1080 <perror@plt>
jmp    1200 <tm_select2+0x70>

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 = [7 x i8] c"accept\00"
define void @tm_select2(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 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %2, %33, %48, %51
%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
store i32 4, i32* %10
%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 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %3
%38 = icmp ne i32 %37, -1
br i1 %38, label %39, label %51
39: ; preds = %34
%40 = load i32, i32* %3
%41 = call i64 @FD_ISSET(i32 %40, i32* %8)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = load i32, i32* %3
%45 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%46 = call i32 @accept(i32 %44, %struct.sockaddr* %45, i32* %10)
store i32 %46, i32* %5
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %22
50: ; preds = %43
br label %51
51: ; preds = %50, %39, %34
%52 = load i32, i32* %5
store i32 %52, i32* %11
%53 = load i32, i32* %5
%54 = call i32 @FD_CLR(i32 %53, i32* %8)
%55 = load i32, i32* %3
%56 = call i32 @FD_CLR(i32 %55, i32* %8)
%57 = load i32, i32* %5
%58 = call i32 @close(i32 %57)
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 = [7 x i8] c"accept\00"
define void @tm_select2(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = call i32 @FD_ZERO(i32* %4)
%11 = call i32 @FD_SET(i32 %0, i32* %4)
%12 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%13 = bitcast %struct.timeval* %6 to i8*
%14 = icmp eq i32 %0, -1
%15 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%16 = bitcast %struct.timeval* %6 to i8*
br label %17
17: ; preds = %40, %2
%18 = phi i32 [ %0, %2 ], [ %41, %40 ]
%19 = phi i32 [ undef, %2 ], [ %42, %40 ]
store i32 4, i32* %5
%20 = call i32 @FD_ZERO(i32* %4)
%21 = call i32 @FD_SET(i32 %0, i32* %4)
%22 = add i32 %18, 1
%23 = call i32 @select(i32 %22, i32* %4, i32* null, i32* null, %struct.timeval* %6)
%24 = icmp slt i32 %23, 1
br i1 %24, label %40, label %25
25: ; preds = %17
%26 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br i1 %14, label %35, label %27
27: ; preds = %25
%28 = call i64 @FD_ISSET(i32 %0, i32* %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %27
%31 = call i32 @accept(i32 %0, %struct.sockaddr* %15, i32* %5)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %40
35: ; preds = %30, %27, %25
%36 = phi i32 [ %31, %30 ], [ %19, %27 ], [ %19, %25 ]
%37 = call i32 @FD_CLR(i32 %36, i32* %4)
%38 = call i32 @FD_CLR(i32 %0, i32* %4)
%39 = call i32 @close(i32 %36)
br label %40
40: ; preds = %17, %35, %33
%41 = phi i32 [ %18, %33 ], [ %36, %35 ], [ %18, %17 ]
%42 = phi i32 [ %31, %33 ], [ %36, %35 ], [ %19, %17 ]
br label %17
}