#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 int /*<<< orphan*/ timestr;
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 /*<<< orphan*/ ssize_t;
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*/ close(int);
char *ctime(scalar_t__ *);
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 *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ time(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int dtserv_run(int listenfd) {
  int connfd;
  struct sockaddr_in clitaddr;
  socklen_t addrlen;
  char timestr[64];
  time_t tval, oldsec, cursec;
  ssize_t wlen;
  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;
    }
    tval = time(0);
    wlen = snprintf(timestr, sizeof(timestr), "%s", ctime(&tval));
    if (write(connfd, timestr, wlen) != wlen)
      error(0, errno, "write");
    close(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    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x80(%rbp)
xor    %edi,%edi
call   1090 <time@plt>
mov    %rax,-0x70(%rbp)
lea    -0x84(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x84(%rbp),%rsi
call   10e0 <FD_SET@plt>
movl   $0x2,-0x98(%rbp)
movq   $0x0,-0x90(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x84(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x98(%rbp),%r8
mov    %rcx,%rdx
call   10b0 <select@plt>
mov    %eax,-0x88(%rbp)
cmpl   $0xffffffff,-0x88(%rbp)
jne    125f <dtserv_run+0x9f>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xdab(%rip),%rdx
call   10c0 <error@plt>
jmp    11e0 <dtserv_run+0x20>
cmpl   $0x0,-0x88(%rbp)
jne    1271 <dtserv_run+0xb1>
jmp    11e0 <dtserv_run+0x20>
jmp    1276 <dtserv_run+0xb6>
mov    -0x4(%rbp),%edi
lea    -0x84(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1293 <dtserv_run+0xd3>
jmp    11e0 <dtserv_run+0x20>
movl   $0x4,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12d3 <dtserv_run+0x113>
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xd3e(%rip),%rdx
call   10c0 <error@plt>
jmp    11e0 <dtserv_run+0x20>
xor    %edi,%edi
call   1090 <time@plt>
mov    %rax,-0x68(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x68(%rbp),%rdi
call   1050 <ctime@plt>
mov    -0xa0(%rbp),%rdi
mov    %rax,%rcx
mov    $0x40,%esi
lea    0xd06(%rip),%rdx
call   1070 <snprintf@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    -0x7c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x7c(%rbp),%eax
je     133f <dtserv_run+0x17f>
mov    0x2cb1(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xcd7(%rip),%rdx
call   10c0 <error@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0x80(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80(%rbp)
xor    %edi,%edi
call   1090 <time@plt>
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
cmp    -0x70(%rbp),%rax
jle    1387 <dtserv_run+0x1c7>
mov    -0x80(%rbp),%esi
lea    0xca4(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x80(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,-0x70(%rbp)
jmp    11e0 <dtserv_run+0x20>

x86 assembly (clang, -O2):
<dtserv_run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %edi,%ebx
xor    %edi,%edi
call   1090 <time@plt>
mov    %rax,0x18(%rsp)
lea    0x1(%rbx),%r14d
lea    0x2c(%rsp),%rbp
lea    0x30(%rsp),%r15
lea    0xe0c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movl   $0x0,0xc(%rsp)
jmp    1245 <dtserv_run+0x85>
nopw   0x0(%rax,%rax,1)
movl   $0x4,0x14(%rsp)
mov    %ebx,%edi
lea    0x28(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   10d0 <accept@plt>
lea    0xdd7(%rip),%rdx
cmp    $0xffffffff,%eax
jne    129a <dtserv_run+0xda>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   10c0 <error@plt>
mov    %rbp,%rdi
call   10a0 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10e0 <FD_SET@plt>
movl   $0x2,0x30(%rsp)
movq   $0x0,0x38(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   10b0 <select@plt>
test   %eax,%eax
je     1245 <dtserv_run+0x85>
mov    %r12,%rdx
cmp    $0xffffffff,%eax
je     1235 <dtserv_run+0x75>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
jne    1210 <dtserv_run+0x50>
jmp    1245 <dtserv_run+0x85>
mov    %eax,%r13d
xor    %edi,%edi
call   1090 <time@plt>
mov    %rax,0x20(%rsp)
lea    0x20(%rsp),%rdi
call   1050 <ctime@plt>
lea    0x40(%rsp),%rdi
mov    $0x40,%esi
lea    0xd4a(%rip),%rdx
mov    %rax,%rcx
call   1070 <snprintf@plt>
mov    %eax,0x10(%rsp)
mov    %r13d,%edi
lea    0x40(%rsp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    0x10(%rsp),%eax
je     12fc <dtserv_run+0x13c>
mov    0x2cf4(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xd1a(%rip),%rdx
call   10c0 <error@plt>
mov    %r13d,%edi
call   1080 <close@plt>
addl   $0x1,0xc(%rsp)
xor    %edi,%edi
call   1090 <time@plt>
cmp    0x18(%rsp),%rax
jle    1245 <dtserv_run+0x85>
mov    %rax,%r13
lea    0xcf2(%rip),%rdi
mov    0xc(%rsp),%esi
call   1060 <printf@plt>
mov    %r13,0x18(%rsp)
jmp    1200 <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 = [3 x i8] c"%s\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [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 [64 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.timeval
store i32 %0, i32* %2
store i32 0, i32* %11
%15 = call i64 @time(i32 0)
store i64 %15, i64* %8
br label %16
16: ; preds = %76, %46, %39, %33, %27, %1
%17 = call i32 @FD_ZERO(i32* %12)
%18 = load i32, i32* %2
%19 = call i32 @FD_SET(i32 %18, i32* %12)
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
store i32 2, i32* %20
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
store i64 0, i64* %21
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
%24 = call i32 @select(i32 %23, i32* %12, i32* null, i32* null, %struct.timeval* %14)
store i32 %24, i32* %13
%25 = load i32, i32* %13
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = load i32, i32* @errno
%29 = call i32 @error(i32 0, i32 %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %16
30: ; preds = %16
%31 = load i32, i32* %13
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %16
34: ; preds = %30
br label %35
35: ; preds = %34
%36 = load i32, i32* %2
%37 = call i32 @FD_ISSET(i32 %36, i32* %12)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %35
br label %16
40: ; preds = %35
store i32 4, i32* %5
%41 = load i32, i32* %2
%42 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%43 = call i32 @accept(i32 %41, %struct.sockaddr* %42, i32* %5)
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* @errno
%48 = call i32 @error(i32 0, i32 %47, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %16
49: ; preds = %40
%50 = call i64 @time(i32 0)
store i64 %50, i64* %7
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%52 = call i8* @ctime(i64* %7)
%53 = call i32 @snprintf(i8* %51, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %52)
store i32 %53, i32* %10
%54 = load i32, i32* %3
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%56 = load i32, i32* %10
%57 = call i32 @write(i32 %54, i8* %55, i32 %56)
%58 = load i32, i32* %10
%59 = icmp ne i32 %57, %58
br i1 %59, label %60, label %63
60: ; preds = %49
%61 = load i32, i32* @errno
%62 = call i32 @error(i32 0, i32 %61, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %63
63: ; preds = %60, %49
%64 = load i32, i32* %3
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %11
%67 = add nsw i32 %66, 1
store i32 %67, i32* %11
%68 = call i64 @time(i32 0)
store i64 %68, i64* %9
%69 = load i64, i64* %9
%70 = load i64, i64* %8
%71 = icmp sgt i64 %69, %70
br i1 %71, label %72, label %76
72: ; preds = %63
%73 = load i32, i32* %11
%74 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %73)
store i32 0, i32* %11
%75 = load i64, i64* %9
store i64 %75, i64* %8
br label %76
76: ; preds = %72, %63
br label %16
}

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