#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 /*<<< orphan*/ uint64_t;
typedef int /*<<< orphan*/ timer_expiries;
struct timespec {
  int tv_sec;
  int tv_nsec;
};
struct TYPE_4__ {
  int tv_sec;
  scalar_t__ tv_nsec;
};
struct TYPE_3__ {
  int tv_sec;
  scalar_t__ tv_nsec;
};
struct itimerspec {
  TYPE_2__ it_interval;
  TYPE_1__ it_value;
};
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ its;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ close(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct itimerspec *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int /*<<< orphan*/ *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int signalfd(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int timerfd_create(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ timerfd_settime(int, int /*<<< orphan*/, struct itimerspec *,
                                   int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int timer_fd, signal_fd;
  int max_fd;
  fd_set read_fds;
  sigset_t sigmask;
  uint64_t timer_expiries;
  struct itimerspec its;
  int timer_expiry_sec;
  int iterations, iter = 0;
  unsigned long *ts_delta;
  struct timespec ts1, ts2;
  if (argc < 3) {
    printf("timerfd_test <secs> <iters>\n");
    return 1;
  }
  sscanf(argv[1], "%d", &timer_expiry_sec);
  sscanf(argv[2], "%d", &iterations);
  ts_delta = (unsigned long *)malloc(sizeof(unsigned long) * iterations);
  sigemptyset(&sigmask);
  sigaddset(&sigmask, SIGINT);
  sigaddset(&sigmask, SIGTERM);
  signal_fd = signalfd(-1, &sigmask, 0);
  if (signal_fd < 0) {
    perror("signalfd:");
    return 2;
  }
  sigprocmask(SIG_BLOCK, &sigmask, NULL);
  timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
  if (timer_fd < 0) {
    perror("timerfd_create:");
    return 3;
  }
  memset(&its, 0, sizeof(its));
  its.it_value.tv_sec = timer_expiry_sec;
  its.it_value.tv_nsec = 0;
  its.it_interval.tv_sec = timer_expiry_sec;
  its.it_interval.tv_nsec = 0;
  max_fd = timer_fd > signal_fd ? timer_fd : signal_fd;
  while (iter < iterations) {
    FD_ZERO(&read_fds);
    FD_SET(timer_fd, &read_fds);
    FD_SET(signal_fd, &read_fds);
    timerfd_settime(timer_fd, 0, &its, NULL);
    clock_gettime(CLOCK_MONOTONIC, &ts1);
    if (select(max_fd + 1, &read_fds, NULL, NULL, NULL) < 0) {
      perror("select:");
      break;
    }
    if (FD_ISSET(signal_fd, &read_fds)) {
      break;
    }
    if (FD_ISSET(timer_fd, &read_fds)) {
      read(timer_fd, &timer_expiries, sizeof(timer_expiries));
    }
    clock_gettime(CLOCK_MONOTONIC, &ts2);
    ts_delta[iter] = ts2.tv_sec * 1000000000 + ts2.tv_nsec -
                     ts1.tv_sec * 1000000000 - ts1.tv_nsec;
    printf("iter = %d ts_delta = %lu\n", iter, ts_delta[iter]);
    iter++;
  }
  printf("quiting\n");
  close(timer_fd);
  close(signal_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x54(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1264 <main+0x44>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    14f2 <main+0x2d2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdaa(%rip),%rsi
lea    -0x4c(%rbp),%rdx
call   1100 <sscanf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd92(%rip),%rsi
lea    -0x50(%rbp),%rdx
call   1100 <sscanf@plt>
movslq -0x50(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   10f0 <malloc@plt>
mov    %rax,-0x60(%rbp)
lea    -0x24(%rbp),%rdi
call   10b0 <sigemptyset@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1140 <sigaddset@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1140 <sigaddset@plt>
mov    $0xffffffff,%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1080 <signalfd@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1309 <main+0xe9>
lea    0xd28(%rip),%rdi
call   1110 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    14f2 <main+0x2d2>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   10c0 <timerfd_create@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1354 <main+0x134>
lea    0xce7(%rip),%rdi
call   1110 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    14f2 <main+0x2d2>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    1397 <main+0x177>
mov    -0x14(%rbp),%eax
mov    %eax,-0x74(%rbp)
jmp    139d <main+0x17d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x50(%rbp),%eax
jge    14cd <main+0x2ad>
lea    -0x20(%rbp),%rdi
call   10d0 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1130 <FD_SET@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1130 <FD_SET@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
lea    -0x48(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1120 <timerfd_settime@plt>
mov    0x2bef(%rip),%rax
mov    (%rax),%edi
lea    -0x68(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10e0 <select@plt>
cmp    $0x0,%rax
jge    1429 <main+0x209>
lea    0xc1b(%rip),%rdi
call   1110 <perror@plt>
jmp    14cd <main+0x2ad>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1444 <main+0x224>
jmp    14cd <main+0x2ad>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     146b <main+0x24b>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <read@plt>
mov    0x2b66(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1040 <clock_gettime@plt>
imul   $0x3b9aca00,-0x70(%rbp),%eax
add    -0x6c(%rbp),%eax
imul   $0x3b9aca00,-0x68(%rbp),%ecx
sub    %ecx,%eax
sub    -0x64(%rbp),%eax
movslq %eax,%rdx
mov    -0x60(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x54(%rbp),%esi
mov    -0x60(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xb8a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    13a3 <main+0x183>
lea    0xb88(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
cmp    $0x2,%edi
jg     1259 <main+0x29>
lea    0xdfd(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    149d <main+0x26d>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xd99(%rip),%rbp
lea    0x10(%rsp),%rdx
mov    %rbp,%rsi
call   1110 <sscanf@plt>
mov    0x10(%rbx),%rdi
lea    0xc(%rsp),%rdx
mov    %rbp,%rsi
call   1110 <sscanf@plt>
mov    0xc(%rsp),%edi
shl    $0x3,%edi
call   1100 <malloc@plt>
mov    %rax,0x28(%rsp)
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   10c0 <sigemptyset@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1150 <sigaddset@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1150 <sigaddset@plt>
mov    $0xffffffff,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <signalfd@plt>
test   %eax,%eax
js     144e <main+0x21e>
mov    %eax,%r13d
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
lea    0x14(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    0x2cdd(%rip),%r15
mov    (%r15),%edi
xor    %esi,%esi
call   10d0 <timerfd_create@plt>
test   %eax,%eax
js     1461 <main+0x231>
mov    %eax,%ebx
lea    0x38(%rsp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    0x10(%rsp),%eax
mov    %eax,0x48(%rsp)
movq   $0x0,0x50(%rsp)
mov    %eax,0x38(%rsp)
movq   $0x0,0x40(%rsp)
cmpl   $0x0,0xc(%rsp)
jle    1480 <main+0x250>
cmp    %r13d,%ebx
mov    %r13d,%r12d
cmova  %ebx,%r12d
add    $0x1,%r12d
lea    0x34(%rsp),%rbp
xor    %r14d,%r14d
jmp    13c4 <main+0x194>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%edi
lea    0x18(%rsp),%rsi
call   1050 <clock_gettime@plt>
mov    0x18(%rsp),%eax
mov    0x1c(%rsp),%ecx
sub    0x20(%rsp),%eax
sub    0x24(%rsp),%ecx
imul   $0x3b9aca00,%eax,%eax
add    %eax,%ecx
movslq %ecx,%rdx
mov    0x28(%rsp),%rax
mov    %rdx,(%rax,%r14,8)
lea    0xc7d(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%r14
movslq 0xc(%rsp),%rax
cmp    %rax,%r14
jge    1480 <main+0x250>
mov    %rbp,%rdi
call   10e0 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1140 <FD_SET@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
call   1140 <FD_SET@plt>
mov    %ebx,%edi
xor    %esi,%esi
lea    0x38(%rsp),%rdx
xor    %ecx,%ecx
call   1130 <timerfd_settime@plt>
mov    (%r15),%edi
lea    0x20(%rsp),%rsi
call   1050 <clock_gettime@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10f0 <select@plt>
test   %rax,%rax
js     1474 <main+0x244>
mov    %r13d,%edi
mov    %rbp,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
jne    1480 <main+0x250>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1370 <main+0x140>
mov    %ebx,%edi
lea    0x30(%rsp),%rsi
mov    $0x4,%edx
call   10b0 <read@plt>
jmp    1370 <main+0x140>
lea    0xbae(%rip),%rdi
call   1120 <perror@plt>
mov    $0x2,%eax
jmp    149d <main+0x26d>
lea    0xba5(%rip),%rdi
call   1120 <perror@plt>
mov    $0x3,%eax
jmp    149d <main+0x26d>
lea    0xba2(%rip),%rdi
call   1120 <perror@plt>
lea    0xbb8(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    %r13d,%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32, i64 }
%struct.TYPE_3__ = type { i32, i64 }
%struct.timespec = type { i32, i32 }
@.str = [29 x i8] c"timerfd_test <secs> <iters>\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@SIGINT = global i32 0
@SIGTERM = global i32 0
@.str.2 = [10 x i8] c"signalfd:\00"
@SIG_BLOCK = global i32 0
@CLOCK_MONOTONIC = global i32 0
@.str.3 = [16 x i8] c"timerfd_create:\00"
@.str.4 = [8 x i8] c"select:\00"
@.str.5 = [26 x i8] c"iter = %d ts_delta = %lu\0A\00"
@.str.6 = [9 x i8] c"quiting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.itimerspec
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i64*
%17 = alloca %struct.timespec
%18 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %15
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %146
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %13)
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @sscanf(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %14)
%32 = load i32, i32* %14
%33 = sext i32 %32 to i64
%34 = mul i64 8, %33
%35 = trunc i64 %34 to i32
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i64*
store i64* %37, i64** %16
%38 = call i32 @sigemptyset(i32* %10)
%39 = load i32, i32* @SIGINT
%40 = call i32 @sigaddset(i32* %10, i32 %39)
%41 = load i32, i32* @SIGTERM
%42 = call i32 @sigaddset(i32* %10, i32 %41)
%43 = call i32 @signalfd(i32 -1, i32* %10, i32 0)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %23
%47 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %146
48: ; preds = %23
%49 = load i32, i32* @SIG_BLOCK
%50 = call i32 @sigprocmask(i32 %49, i32* %10, i32* null)
%51 = load i32, i32* @CLOCK_MONOTONIC
%52 = call i32 @timerfd_create(i32 %51, i32 0)
store i32 %52, i32* %6
%53 = load i32, i32* %6
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %48
%56 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 3, i32* %3
br label %146
57: ; preds = %48
%58 = call i32 @memset(%struct.itimerspec* %12, i32 0, i32 32)
%59 = load i32, i32* %13
%60 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %12, i32 0, i32 1
%61 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %60, i32 0, i32 0
store i32 %59, i32* %61
%62 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %12, i32 0, i32 1
%63 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %62, i32 0, i32 1
store i64 0, i64* %63
%64 = load i32, i32* %13
%65 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %12, i32 0, i32 0
%66 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %65, i32 0, i32 0
store i32 %64, i32* %66
%67 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %12, i32 0, i32 0
%68 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %67, i32 0, i32 1
store i64 0, i64* %68
%69 = load i32, i32* %6
%70 = load i32, i32* %7
%71 = icmp sgt i32 %69, %70
br i1 %71, label %72, label %74
72: ; preds = %57
%73 = load i32, i32* %6
br label %76
74: ; preds = %57
%75 = load i32, i32* %7
br label %76
76: ; preds = %74, %72
%77 = phi i32 [ %73, %72 ], [ %75, %74 ]
store i32 %77, i32* %8
br label %78
78: ; preds = %110, %76
%79 = load i32, i32* %15
%80 = load i32, i32* %14
%81 = icmp slt i32 %79, %80
br i1 %81, label %82, label %140
82: ; preds = %78
%83 = call i32 @FD_ZERO(i32* %9)
%84 = load i32, i32* %6
%85 = call i32 @FD_SET(i32 %84, i32* %9)
%86 = load i32, i32* %7
%87 = call i32 @FD_SET(i32 %86, i32* %9)
%88 = load i32, i32* %6
%89 = call i32 @timerfd_settime(i32 %88, i32 0, %struct.itimerspec* %12, i32* null)
%90 = load i32, i32* @CLOCK_MONOTONIC
%91 = call i32 @clock_gettime(i32 %90, %struct.timespec* %17)
%92 = load i32, i32* %8
%93 = add nsw i32 %92, 1
%94 = call i64 @select(i32 %93, i32* %9, i32* null, i32* null, i32* null)
%95 = icmp slt i64 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %82
%97 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %140
98: ; preds = %82
%99 = load i32, i32* %7
%100 = call i64 @FD_ISSET(i32 %99, i32* %9)
%101 = icmp ne i64 %100, 0
br i1 %101, label %102, label %103
102: ; preds = %98
br label %140
103: ; preds = %98
%104 = load i32, i32* %6
%105 = call i64 @FD_ISSET(i32 %104, i32* %9)
%106 = icmp ne i64 %105, 0
br i1 %106, label %107, label %110
107: ; preds = %103
%108 = load i32, i32* %6
%109 = call i32 @read(i32 %108, i32* %11, i32 4)
br label %110
110: ; preds = %107, %103
%111 = load i32, i32* @CLOCK_MONOTONIC
%112 = call i32 @clock_gettime(i32 %111, %struct.timespec* %18)
%113 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 0
%114 = load i32, i32* %113
%115 = mul nsw i32 %114, 1000000000
%116 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 1
%117 = load i32, i32* %116
%118 = add nsw i32 %115, %117
%119 = getelementptr inbounds %struct.timespec, %struct.timespec* %17, i32 0, i32 0
%120 = load i32, i32* %119
%121 = mul nsw i32 %120, 1000000000
%122 = sub nsw i32 %118, %121
%123 = getelementptr inbounds %struct.timespec, %struct.timespec* %17, i32 0, i32 1
%124 = load i32, i32* %123
%125 = sub nsw i32 %122, %124
%126 = sext i32 %125 to i64
%127 = load i64*, i64** %16
%128 = load i32, i32* %15
%129 = sext i32 %128 to i64
%130 = getelementptr inbounds i64, i64* %127, i64 %129
store i64 %126, i64* %130
%131 = load i32, i32* %15
%132 = load i64*, i64** %16
%133 = load i32, i32* %15
%134 = sext i32 %133 to i64
%135 = getelementptr inbounds i64, i64* %132, i64 %134
%136 = load i64, i64* %135
%137 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %131, i64 %136)
%138 = load i32, i32* %15
%139 = add nsw i32 %138, 1
store i32 %139, i32* %15
br label %78
140: ; preds = %102, %96, %78
%141 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%142 = load i32, i32* %6
%143 = call i32 @close(i32 %142)
%144 = load i32, i32* %7
%145 = call i32 @close(i32 %144)
store i32 0, i32* %3
br label %146
146: ; preds = %140, %55, %46, %21
%147 = load i32, i32* %3
ret i32 %147
}

LLVM IR (-O2):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32, i64 }
%struct.TYPE_3__ = type { i32, i64 }
%struct.timespec = type { i32, i32 }
@.str.1 = [3 x i8] c"%d\00"
@SIGINT = global i32 0
@SIGTERM = global i32 0
@.str.2 = [10 x i8] c"signalfd:\00"
@SIG_BLOCK = global i32 0
@CLOCK_MONOTONIC = global i32 0
@.str.3 = [16 x i8] c"timerfd_create:\00"
@.str.4 = [8 x i8] c"select:\00"
@.str.5 = [26 x i8] c"iter = %d ts_delta = %lu\0A\00"
@str = [8 x i8] c"quiting\00"
@str.7 = [28 x i8] c"timerfd_test <secs> <iters>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.itimerspec
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timespec
%10 = alloca %struct.timespec
%11 = bitcast i32* %3 to i8*
%12 = bitcast i32* %4 to i8*
%13 = bitcast i32* %5 to i8*
%14 = bitcast %struct.itimerspec* %6 to i8*
%15 = bitcast i32* %7 to i8*
%16 = bitcast i32* %8 to i8*
%17 = bitcast %struct.timespec* %9 to i8*
%18 = bitcast %struct.timespec* %10 to i8*
%19 = icmp slt i32 %0, 3
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
br label %110
22: ; preds = %2
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
%26 = getelementptr inbounds i8*, i8** %1, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %8)
%29 = load i32, i32* %8
%30 = shl i32 %29, 3
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i64*
%33 = call i32 @sigemptyset(i32* %4)
%34 = load i32, i32* @SIGINT
%35 = call i32 @sigaddset(i32* %4, i32 %34)
%36 = load i32, i32* @SIGTERM
%37 = call i32 @sigaddset(i32* %4, i32 %36)
%38 = call i32 @signalfd(i32 -1, i32* %4, i32 0)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %22
%41 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %110
42: ; preds = %22
%43 = load i32, i32* @SIG_BLOCK
%44 = call i32 @sigprocmask(i32 %43, i32* %4, i32* null)
%45 = load i32, i32* @CLOCK_MONOTONIC
%46 = call i32 @timerfd_create(i32 %45, i32 0)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %110
50: ; preds = %42
%51 = call i32 @memset(%struct.itimerspec* %6, i32 0, i32 32)
%52 = load i32, i32* %7
%53 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %6, i64 0, i32 1, i32 0
store i32 %52, i32* %53
%54 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %6, i64 0, i32 1, i32 1
store i64 0, i64* %54
%55 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %6, i64 0, i32 0, i32 0
store i32 %52, i32* %55
%56 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %6, i64 0, i32 0, i32 1
store i64 0, i64* %56
%57 = load i32, i32* %8
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %106
59: ; preds = %50
%60 = icmp ugt i32 %46, %38
%61 = select i1 %60, i32 %46, i32 %38
%62 = add nuw nsw i32 %61, 1
%63 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i64 0, i32 0
%64 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i64 0, i32 1
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i64 0, i32 0
%66 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i64 0, i32 1
br label %67
67: ; preds = %59, %87
%68 = phi i64 [ 0, %59 ], [ %102, %87 ]
%69 = call i32 @FD_ZERO(i32* %3)
%70 = call i32 @FD_SET(i32 %46, i32* %3)
%71 = call i32 @FD_SET(i32 %38, i32* %3)
%72 = call i32 @timerfd_settime(i32 %46, i32 0, %struct.itimerspec* %6, i32* null)
%73 = load i32, i32* @CLOCK_MONOTONIC
%74 = call i32 @clock_gettime(i32 %73, %struct.timespec* %9)
%75 = call i64 @select(i32 %62, i32* %3, i32* null, i32* null, i32* null)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %67
%78 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %106
79: ; preds = %67
%80 = call i64 @FD_ISSET(i32 %38, i32* %3)
%81 = icmp eq i64 %80, 0
br i1 %81, label %82, label %106
82: ; preds = %79
%83 = call i64 @FD_ISSET(i32 %46, i32* %3)
%84 = icmp eq i64 %83, 0
br i1 %84, label %87, label %85
85: ; preds = %82
%86 = call i32 @read(i32 %46, i32* %5, i32 4)
br label %87
87: ; preds = %85, %82
%88 = load i32, i32* @CLOCK_MONOTONIC
%89 = call i32 @clock_gettime(i32 %88, %struct.timespec* %10)
%90 = load i32, i32* %63
%91 = load i32, i32* %64
%92 = load i32, i32* %65
%93 = load i32, i32* %66
%94 = sub i32 %90, %92
%95 = mul i32 %94, 1000000000
%96 = sub i32 %91, %93
%97 = add i32 %96, %95
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds i64, i64* %32, i64 %68
store i64 %98, i64* %99
%100 = trunc i64 %68 to i32
%101 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %100, i64 %98)
%102 = add nuw nsw i64 %68, 1
%103 = load i32, i32* %8
%104 = sext i32 %103 to i64
%105 = icmp slt i64 %102, %104
br i1 %105, label %67, label %106
106: ; preds = %87, %79, %50, %77
%107 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%108 = call i32 @close(i32 %46)
%109 = call i32 @close(i32 %38)
br label %110
110: ; preds = %106, %48, %40, %20
%111 = phi i32 [ 1, %20 ], [ 2, %40 ], [ 3, %48 ], [ 0, %106 ]
ret i32 %111
}