#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 */
typedef scalar_t__ time_t;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ clitaddr;

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ connect_process(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
scalar_t__ time(int /*<<< orphan*/);
int dtserv_run(int listenfd) {
  int connfd;
  struct sockaddr_in clitaddr;
  socklen_t addrlen;
  time_t oldsec, cursec;
  int count = 0;
  fd_set rfds;
  int ret;
  struct timeval tv;
  oldsec = time(0);
  for (;;) {
    FD_ZERO(&rfds);
    FD_SET(listenfd, &rfds);
    tv.tv_sec = 2;
    tv.tv_usec = 0;
    ret = select(listenfd + 1, &rfds, NULL, NULL, &tv);
    if (ret == -1) {
      error(0, errno, "select");
      continue;
    } else if (ret == 0)
      continue;
    if (!FD_ISSET(listenfd, &rfds))
      continue;
    addrlen = sizeof(clitaddr);
    connfd = accept(listenfd, (struct sockaddr *)&clitaddr, &addrlen);
    if (connfd == -1) {
      error(0, errno, "accept");
      continue;
    }
    connect_process(listenfd, connfd);
    count++;
    cursec = time(0);
    if (cursec > oldsec) {
      printf("last sec qps: %d\n", count);
      count = 0;
      oldsec = cursec;
    }
  }
}


x86 assembly (clang, -O0):
<dtserv_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x2c(%rbp)
xor    %edi,%edi
call   1060 <time@plt>
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10b0 <FD_SET@plt>
movl   $0x2,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1214 <dtserv_run+0x84>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xdf6(%rip),%rdx
call   1090 <error@plt>
jmp    11ad <dtserv_run+0x1d>
cmpl   $0x0,-0x34(%rbp)
jne    1223 <dtserv_run+0x93>
jmp    11ad <dtserv_run+0x1d>
jmp    1228 <dtserv_run+0x98>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1242 <dtserv_run+0xb2>
jmp    11ad <dtserv_run+0x1d>
movl   $0x4,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1282 <dtserv_run+0xf2>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xd8f(%rip),%rdx
call   1090 <error@plt>
jmp    11ad <dtserv_run+0x1d>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <connect_process@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
xor    %edi,%edi
call   1060 <time@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jle    12cd <dtserv_run+0x13d>
mov    -0x2c(%rbp),%esi
lea    0xd55(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ad <dtserv_run+0x1d>

x86 assembly (clang, -O2):
<dtserv_run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebx
xor    %edi,%edi
call   1060 <time@plt>
mov    %rax,0x18(%rsp)
lea    0x1(%rbx),%r14d
lea    0x24(%rsp),%rbp
lea    0x28(%rsp),%r15
lea    0xe3f(%rip),%r13
mov    0x2e18(%rip),%r12
nopl   0x0(%rax,%rax,1)
movl   $0x0,0x8(%rsp)
jmp    1210 <dtserv_run+0x80>
nopw   0x0(%rax,%rax,1)
movl   $0x4,0x14(%rsp)
mov    %ebx,%edi
lea    0x20(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   10a0 <accept@plt>
lea    0xe07(%rip),%rdx
cmp    $0xffffffff,%eax
jne    1261 <dtserv_run+0xd1>
mov    (%r12),%esi
xor    %edi,%edi
call   1090 <error@plt>
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
movl   $0x2,0x28(%rsp)
movq   $0x0,0x30(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1080 <select@plt>
test   %eax,%eax
je     1210 <dtserv_run+0x80>
mov    %r13,%rdx
cmp    $0xffffffff,%eax
je     1205 <dtserv_run+0x75>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
jne    11e0 <dtserv_run+0x50>
jmp    1210 <dtserv_run+0x80>
mov    %ebx,%edi
mov    %eax,%esi
call   1050 <connect_process@plt>
addl   $0x1,0x8(%rsp)
xor    %edi,%edi
call   1060 <time@plt>
cmp    0x18(%rsp),%rax
jle    1210 <dtserv_run+0x80>
lea    0xd8a(%rip),%rdi
mov    0x8(%rsp),%esi
mov    %rax,0x8(%rsp)
call   1040 <printf@plt>
mov    0x8(%rsp),%rax
mov    %rax,0x18(%rsp)
jmp    11d0 <dtserv_run+0x40>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@.str = [7 x i8] c"select\00"
@.str.1 = [7 x i8] c"accept\00"
@.str.2 = [18 x i8] c"last sec qps: %d\0A\00"
define i32 @dtserv_run(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
store i32 %0, i32* %2
store i32 0, i32* %8
%12 = call i64 @time(i32 0)
store i64 %12, i64* %6
br label %13
13: ; preds = %60, %43, %36, %30, %24, %1
%14 = call i32 @FD_ZERO(i32* %9)
%15 = load i32, i32* %2
%16 = call i32 @FD_SET(i32 %15, i32* %9)
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 2, i32* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* %2
%20 = add nsw i32 %19, 1
%21 = call i32 @select(i32 %20, i32* %9, i32* null, i32* null, %struct.timeval* %11)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = load i32, i32* @errno
%26 = call i32 @error(i32 0, i32 %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
27: ; preds = %13
%28 = load i32, i32* %10
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
br label %13
31: ; preds = %27
br label %32
32: ; preds = %31
%33 = load i32, i32* %2
%34 = call i32 @FD_ISSET(i32 %33, i32* %9)
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %32
br label %13
37: ; preds = %32
store i32 4, i32* %5
%38 = load i32, i32* %2
%39 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%40 = call i32 @accept(i32 %38, %struct.sockaddr* %39, i32* %5)
store i32 %40, i32* %3
%41 = load i32, i32* %3
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %37
%44 = load i32, i32* @errno
%45 = call i32 @error(i32 0, i32 %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %13
46: ; preds = %37
%47 = load i32, i32* %2
%48 = load i32, i32* %3
%49 = call i32 @connect_process(i32 %47, i32 %48)
%50 = load i32, i32* %8
%51 = add nsw i32 %50, 1
store i32 %51, i32* %8
%52 = call i64 @time(i32 0)
store i64 %52, i64* %7
%53 = load i64, i64* %7
%54 = load i64, i64* %6
%55 = icmp sgt i64 %53, %54
br i1 %55, label %56, label %60
56: ; preds = %46
%57 = load i32, i32* %8
%58 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %57)
store i32 0, i32* %8
%59 = load i64, i64* %7
store i64 %59, i64* %6
br label %60
60: ; preds = %56, %46
br label %13
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@.str = [7 x i8] c"select\00"
@.str.1 = [7 x i8] c"accept\00"
@.str.2 = [18 x i8] c"last sec qps: %d\0A\00"
define i32 @dtserv_run(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.timeval* %5 to i8*
%10 = tail call i64 @time(i32 0)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%13 = add nsw i32 %0, 1
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %15
15: ; preds = %39, %1
%16 = phi i64 [ %37, %39 ], [ %10, %1 ]
br label %17
17: ; preds = %15, %34
%18 = phi i32 [ %36, %34 ], [ 0, %15 ]
br label %19
19: ; preds = %27, %17
%20 = call i32 @FD_ZERO(i32* %4)
%21 = call i32 @FD_SET(i32 %0, i32* %4)
store i32 2, i32* %11
store i64 0, i64* %12
%22 = call i32 @select(i32 %13, i32* %4, i32* null, i32* null, %struct.timeval* %5)
switch i32 %22, label %28 [
i32 -1, label %23
i32 0, label %27
]
23: ; preds = %19, %31
%24 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %31 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %19 ]
%25 = load i32, i32* @errno
%26 = call i32 @error(i32 0, i32 %25, i8* %24)
br label %27
27: ; preds = %23, %28, %19
br label %19
28: ; preds = %19
%29 = call i32 @FD_ISSET(i32 %0, i32* %4)
%30 = icmp eq i32 %29, 0
br i1 %30, label %27, label %31
31: ; preds = %28
store i32 4, i32* %3
%32 = call i32 @accept(i32 %0, %struct.sockaddr* %14, i32* %3)
%33 = icmp eq i32 %32, -1
br i1 %33, label %23, label %34
34: ; preds = %31
%35 = call i32 @connect_process(i32 %0, i32 %32)
%36 = add nuw nsw i32 %18, 1
%37 = call i64 @time(i32 0)
%38 = icmp sgt i64 %37, %16
br i1 %38, label %39, label %17
39: ; preds = %34
%40 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %15
}