#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ clitaddr;

/* Variables and functions */
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 /*<<< 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;
  oldsec = time(0);
  for (;;) {
    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    $0x90,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x80(%rbp)
xor    %edi,%edi
call   1080 <time@plt>
mov    %rax,-0x70(%rbp)
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    11e0 <dtserv_run+0x60>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xe2a(%rip),%rdx
call   1090 <error@plt>
jmp    11a0 <dtserv_run+0x20>
xor    %edi,%edi
call   1080 <time@plt>
mov    %rax,-0x68(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x88(%rbp)
lea    -0x68(%rbp),%rdi
call   1040 <ctime@plt>
mov    -0x88(%rbp),%rdi
mov    %rax,%rcx
mov    $0x40,%esi
lea    0xdf2(%rip),%rdx
call   1060 <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     124c <dtserv_run+0xcc>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xdc3(%rip),%rdx
call   1090 <error@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x80(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80(%rbp)
xor    %edi,%edi
call   1080 <time@plt>
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
cmp    -0x70(%rbp),%rax
jle    1294 <dtserv_run+0x114>
mov    -0x80(%rbp),%esi
lea    0xd90(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x80(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,-0x70(%rbp)
jmp    11a0 <dtserv_run+0x20>

x86 assembly (clang, -O2):
<dtserv_run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %edi,%r14d
xor    %edi,%edi
call   1080 <time@plt>
mov    %rax,0x10(%rsp)
lea    0x4(%rsp),%r15
lea    0x18(%rsp),%r12
mov    %r14d,(%rsp)
jmp    11c6 <dtserv_run+0x46>
nopl   (%rax)
mov    %rax,%rbp
lea    0xe56(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    %rbp,0x10(%rsp)
xor    %ebx,%ebx
jmp    11f1 <dtserv_run+0x71>
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
call   1070 <close@plt>
mov    0xc(%rsp),%ebx
add    $0x1,%ebx
xor    %edi,%edi
call   1080 <time@plt>
cmp    0x10(%rsp),%rax
mov    (%rsp),%r14d
jg     11b0 <dtserv_run+0x30>
mov    %ebx,0xc(%rsp)
movl   $0x4,0x4(%rsp)
mov    %r14d,%edi
lea    0x8(%rsp),%rsi
mov    %r15,%rdx
call   10a0 <accept@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1261 <dtserv_run+0xe1>
mov    0x2dc4(%rip),%rbx
mov    %r15,%r12
lea    0x8(%rsp),%r15
lea    0xdd5(%rip),%r14
mov    (%rsp),%ebp
xchg   %ax,%ax
mov    (%rbx),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1090 <error@plt>
movl   $0x4,0x4(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
je     1230 <dtserv_run+0xb0>
mov    %eax,%r13d
mov    %r12,%r15
lea    0x18(%rsp),%r12
xor    %edi,%edi
call   1080 <time@plt>
mov    %rax,0x18(%rsp)
mov    %r12,%rdi
call   1040 <ctime@plt>
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0x40,%esi
lea    0xd7e(%rip),%rdx
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %eax,%ebp
mov    %r13d,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11d0 <dtserv_run+0x50>
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xd50(%rip),%rdx
call   1090 <error@plt>
jmp    11d0 <dtserv_run+0x50>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@.str = [7 x i8] c"accept\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [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 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%9 = bitcast i64* %5 to i8*
%10 = tail call i64 @time(i32 0)
%11 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %12
12: ; preds = %38, %1
%13 = phi i64 [ %36, %38 ], [ %10, %1 ]
br label %14
14: ; preds = %12, %33
%15 = phi i32 [ %35, %33 ], [ 0, %12 ]
store i32 4, i32* %3
%16 = call i32 @accept(i32 %0, %struct.sockaddr* %11, i32* %3)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %14, %18
%19 = load i32, i32* @errno
%20 = call i32 @error(i32 0, i32 %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 4, i32* %3
%21 = call i32 @accept(i32 %0, %struct.sockaddr* %11, i32* %3)
%22 = icmp eq i32 %21, -1
br i1 %22, label %18, label %23
23: ; preds = %18, %14
%24 = phi i32 [ %16, %14 ], [ %21, %18 ]
%25 = call i64 @time(i32 0)
store i64 %25, i64* %5
%26 = call i8* @ctime(i64* %5)
%27 = call i32 @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @write(i32 %24, i8* %8, i32 %27)
%29 = icmp eq i32 %28, %27
br i1 %29, label %33, label %30
30: ; preds = %23
%31 = load i32, i32* @errno
%32 = call i32 @error(i32 0, i32 %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %30, %23
%34 = call i32 @close(i32 %24)
%35 = add nuw nsw i32 %15, 1
%36 = call i64 @time(i32 0)
%37 = icmp sgt i64 %36, %13
br i1 %37, label %38, label %14
38: ; preds = %33
%39 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %12
}