#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int uint64_t;
struct timespec {
  int tv_sec;
  int tv_nsec;
};
struct TYPE_4__ {
  int tv_sec;
  int tv_nsec;
};
struct TYPE_3__ {
  int tv_sec;
  int tv_nsec;
};
struct itimerspec {
  TYPE_2__ it_value;
  TYPE_1__ it_interval;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ EXIT_SUCCESS;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, unsigned long long,
                          unsigned long long);
int read(int, int *, int);
int timerfd_create(int /*<<< orphan*/, int /*<<< orphan*/);
int timerfd_settime(int, int /*<<< orphan*/, struct itimerspec *,
                    int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  struct itimerspec ts;
  struct timespec start, now;
  int maxExp, fd, secs, nanosecs;
  uint64_t numExp, totalExp;
  ssize_t s;
  ts.it_interval.tv_sec = 2;
  ts.it_interval.tv_nsec = 10;
  ts.it_value.tv_sec = 1;
  ts.it_value.tv_nsec = 20;
  fd = timerfd_create(CLOCK_REALTIME, 0);
  if (fd == -1)
    perror("timerfd_create");
  if (timerfd_settime(fd, 0, &ts, NULL) == -1)
    perror("timerfd_settime");
  if (clock_gettime(CLOCK_MONOTONIC, &start) == -1)
    perror("clock_gettime");
  for (totalExp = 0; totalExp < maxExp;) {
    s = read(fd, &numExp, sizeof(uint64_t));
    if (s != sizeof(uint64_t))
      perror("read");
    totalExp += numExp;
    if (clock_gettime(CLOCK_MONOTONIC, &now) == -1)
      perror("clock_gettime");
    secs = now.tv_sec - start.tv_sec;
    nanosecs = now.tv_nsec - start.tv_nsec;
    if (nanosecs < 0) {
      secs--;
      nanosecs += 1000000000;
    }
    printf("%d.%03d: expirations read: %llu; total=%llu\n", secs,
           (nanosecs + 500000) / 1000000, (unsigned long long)numExp,
           (unsigned long long)totalExp);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x2,-0x18(%rbp)
movl   $0xa,-0x14(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x14,-0x1c(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <timerfd_create@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11cb <main+0x5b>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
mov    -0x38(%rbp),%edi
xor    %esi,%esi
lea    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <timerfd_settime@plt>
cmp    $0xffffffff,%eax
jne    11f2 <main+0x82>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    1219 <main+0xa9>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    12e8 <main+0x178>
mov    -0x38(%rbp),%edi
lea    -0x44(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x4c(%rbp)
movslq -0x4c(%rbp),%rax
cmp    $0x4,%rax
je     125a <main+0xea>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
mov    -0x44(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    128a <main+0x11a>
lea    0xd9a(%rip),%rdi
call   1070 <perror@plt>
mov    -0x30(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x2c(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    12ba <main+0x14a>
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%eax
add    $0x3b9aca00,%eax
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%esi
mov    -0x40(%rbp),%eax
add    $0x7a120,%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
movslq -0x44(%rbp),%rcx
movslq -0x48(%rbp),%r8
lea    0xd54(%rip),%rdi
call   1040 <printf@plt>
jmp    1220 <main+0xb0>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movaps 0xea4(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <timerfd_create@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1183 <main+0x33>
lea    0xe92(%rip),%rdi
call   1050 <perror@plt>
mov    %rsp,%rdx
mov    %ebx,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <timerfd_settime@plt>
cmp    $0xffffffff,%eax
jne    11a2 <main+0x52>
lea    0xe82(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0x18(%rsp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    11c6 <main+0x76>
lea    0xe6e(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32, i32 }
%struct.TYPE_3__ = type { i32, i32 }
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [15 x i8] c"timerfd_create\00"
@.str.1 = [16 x i8] c"timerfd_settime\00"
@CLOCK_MONOTONIC = global i32 0
@.str.2 = [14 x i8] c"clock_gettime\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [45 x i8] c"%d.%03d: expirations read: %llu; total=%llu\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.itimerspec
%7 = alloca %struct.timespec
%8 = alloca %struct.timespec
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %6, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %16, i32 0, i32 0
store i32 2, i32* %17
%18 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %6, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %18, i32 0, i32 1
store i32 10, i32* %19
%20 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %6, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %20, i32 0, i32 0
store i32 1, i32* %21
%22 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %6, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %22, i32 0, i32 1
store i32 20, i32* %23
%24 = load i32, i32* @CLOCK_REALTIME
%25 = call i32 @timerfd_create(i32 %24, i32 0)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %2
%29 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %2
%31 = load i32, i32* %10
%32 = call i32 @timerfd_settime(i32 %31, i32 0, %struct.itimerspec* %6, i32* null)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = load i32, i32* @CLOCK_MONOTONIC
%38 = call i32 @clock_gettime(i32 %37, %struct.timespec* %7)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
store i32 0, i32* %14
br label %43
43: ; preds = %82, %42
%44 = load i32, i32* %14
%45 = load i32, i32* %9
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %92
47: ; preds = %43
%48 = load i32, i32* %10
%49 = call i32 @read(i32 %48, i32* %13, i32 4)
store i32 %49, i32* %15
%50 = load i32, i32* %15
%51 = sext i32 %50 to i64
%52 = icmp ne i64 %51, 4
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
%56 = load i32, i32* %13
%57 = load i32, i32* %14
%58 = add nsw i32 %57, %56
store i32 %58, i32* %14
%59 = load i32, i32* @CLOCK_MONOTONIC
%60 = call i32 @clock_gettime(i32 %59, %struct.timespec* %8)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %55
%63 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %62, %55
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%66 = load i32, i32* %65
%67 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%68 = load i32, i32* %67
%69 = sub nsw i32 %66, %68
store i32 %69, i32* %11
%70 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%71 = load i32, i32* %70
%72 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%73 = load i32, i32* %72
%74 = sub nsw i32 %71, %73
store i32 %74, i32* %12
%75 = load i32, i32* %12
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %64
%78 = load i32, i32* %11
%79 = add nsw i32 %78, -1
store i32 %79, i32* %11
%80 = load i32, i32* %12
%81 = add nsw i32 %80, 1000000000
store i32 %81, i32* %12
br label %82
82: ; preds = %77, %64
%83 = load i32, i32* %11
%84 = load i32, i32* %12
%85 = add nsw i32 %84, 500000
%86 = sdiv i32 %85, 1000000
%87 = load i32, i32* %13
%88 = sext i32 %87 to i64
%89 = load i32, i32* %14
%90 = sext i32 %89 to i64
%91 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %83, i32 %86, i64 %88, i64 %90)
br label %43
92: ; preds = %43
%93 = load i32, i32* @EXIT_SUCCESS
%94 = call i32 @exit(i32 %93)
unreachable
}

LLVM IR (-O2):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32, i32 }
%struct.TYPE_3__ = type { i32, i32 }
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [15 x i8] c"timerfd_create\00"
@.str.1 = [16 x i8] c"timerfd_settime\00"
@CLOCK_MONOTONIC = global i32 0
@.str.2 = [14 x i8] c"clock_gettime\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca <4 x i32>
%4 = bitcast <4 x i32>* %3 to %struct.itimerspec*
%5 = alloca %struct.timespec
%6 = bitcast <4 x i32>* %3 to i8*
%7 = bitcast %struct.timespec* %5 to i8*
store <4 x i32> <i32 1, i32 20, i32 2, i32 10>, <4 x i32>* %3
%8 = load i32, i32* @CLOCK_REALTIME
%9 = tail call i32 @timerfd_create(i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = call i32 @timerfd_settime(i32 %9, i32 0, %struct.itimerspec* %4, i32* null)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load i32, i32* @CLOCK_MONOTONIC
%20 = call i32 @clock_gettime(i32 %19, %struct.timespec* %5)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i32, i32* @EXIT_SUCCESS
%26 = call i32 @exit(i32 %25)
unreachable
}