#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;
struct pollfd {
  int fd;
  scalar_t__ revents;
  scalar_t__ events;
};
struct TYPE_4__ {
  int tv_sec;
};
struct TYPE_3__ {
  int tv_sec;
};
struct itimerspec {
  TYPE_2__ it_interval;
  TYPE_1__ it_value;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ POLLIN;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ foo();
int /*<<< orphan*/ memset(struct itimerspec *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, int /*<<< orphan*/ *, int);
int timerfd_create(int /*<<< orphan*/, int /*<<< orphan*/);
int timerfd_settime(int, int /*<<< orphan*/, struct itimerspec *,
                    int /*<<< orphan*/ *);
int main(int argc, const char *argv[]) {
  int timerfd = timerfd_create(CLOCK_REALTIME, 0);
  if (timerfd == -1)
    do {
      perror("timerfd_create");
      exit(EXIT_FAILURE);
    } while (0);
  struct itimerspec tv;
  memset(&tv, 0, sizeof tv);
  tv.it_value.tv_sec = 3;
  tv.it_interval.tv_sec = 1;
  if (timerfd_settime(timerfd, 0, &tv, NULL) == -1)
    do {
      perror("timerfd_settime");
      exit(EXIT_FAILURE);
    } while (0);
  struct pollfd pfd;
  pfd.fd = timerfd;
  pfd.events = POLLIN;
  uint64_t val;
  int ret;
  while (1) {
    ret = poll(&pfd, 1, 5000);
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      do {
        perror("poll");
        exit(EXIT_FAILURE);
      } while (0);
    } else if (ret == 0) {
      printf("timeout\n");
    }
    if (pfd.revents == POLLIN) {
      read(timerfd, &val, sizeof val);
      foo();
    }
  }
  close(timerfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <timerfd_create@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e2 <main+0x52>
jmp    11c8 <main+0x38>
lea    0xe31(%rip),%rdi
call   1090 <perror@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movl   $0x3,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x14(%rbp),%edi
xor    %esi,%esi
lea    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10a0 <timerfd_settime@plt>
cmp    $0xffffffff,%eax
jne    123a <main+0xaa>
jmp    1220 <main+0x90>
lea    0xde8(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1080 <poll@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    12ac <main+0x11c>
mov    0x2d63(%rip),%rax
mov    (%rax),%rax
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
jne    128d <main+0xfd>
jmp    124e <main+0xbe>
jmp    1292 <main+0x102>
lea    0xd86(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x40(%rbp)
jne    12c2 <main+0x132>
lea    0xd67(%rip),%rdi
call   1030 <printf@plt>
jmp    12c7 <main+0x137>
mov    -0x30(%rbp),%rax
mov    0x2d16(%rip),%rcx
cmp    (%rcx),%rax
jne    12f3 <main+0x163>
mov    -0x14(%rbp),%edi
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    $0x0,%al
call   1070 <foo@plt>
jmp    124e <main+0xbe>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <timerfd_create@plt>
cmp    $0xffffffff,%eax
jne    11bf <main+0x2f>
lea    0xe46(%rip),%rdi
jmp    12a7 <main+0x117>
mov    %eax,%r12d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movabs $0x300000001,%rax
mov    %rax,(%rsp)
mov    %r12d,%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10a0 <timerfd_settime@plt>
cmp    $0xffffffff,%eax
jne    1202 <main+0x72>
lea    0xe12(%rip),%rdi
jmp    12a7 <main+0x117>
mov    %r12d,0x10(%rsp)
mov    0x2dda(%rip),%rbp
mov    0x0(%rbp),%rax
mov    %rax,0x20(%rsp)
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
mov    0x2db0(%rip),%r13
mov    0x2d99(%rip),%r14
jmp    1249 <main+0xb9>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%rax
cmp    (%r14),%rax
jne    12a0 <main+0x110>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1080 <poll@plt>
test   %eax,%eax
je     1270 <main+0xe0>
cmp    $0xffffffff,%eax
je     1240 <main+0xb0>
jmp    127c <main+0xec>
cs nopw 0x0(%rax,%rax,1)
lea    0xdad(%rip),%rdi
call   1030 <puts@plt>
mov    0x18(%rsp),%rax
cmp    0x0(%rbp),%rax
jne    1249 <main+0xb9>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
xor    %eax,%eax
call   1070 <foo@plt>
jmp    1249 <main+0xb9>
lea    0xd78(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32 }
%struct.TYPE_3__ = type { i32 }
%struct.pollfd = type { i32, i64, i64 }
@CLOCK_REALTIME = global i32 0
@.str = [15 x i8] c"timerfd_create\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"timerfd_settime\00"
@POLLIN = global i64 0
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [5 x i8] c"poll\00"
@.str.3 = [9 x i8] c"timeout\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.itimerspec
%8 = alloca %struct.pollfd
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @CLOCK_REALTIME
%12 = call i32 @timerfd_create(i32 %11, i32 0)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %2
br label %16
16: ; preds = %15
%17 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; No predecessors!
br label %21
21: ; preds = %20, %2
%22 = call i32 @memset(%struct.itimerspec* %7, i32 0, i32 8)
%23 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %7, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %23, i32 0, i32 0
store i32 3, i32* %24
%25 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %7, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %25, i32 0, i32 0
store i32 1, i32* %26
%27 = load i32, i32* %6
%28 = call i32 @timerfd_settime(i32 %27, i32 0, %struct.itimerspec* %7, i32* null)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %36
30: ; preds = %21
br label %31
31: ; preds = %30
%32 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; No predecessors!
br label %36
36: ; preds = %35, %21
%37 = load i32, i32* %6
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i64, i64* @POLLIN
%40 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 2
store i64 %39, i64* %40
br label %41
41: ; preds = %36, %49, %71
%42 = call i32 @poll(%struct.pollfd* %8, i32 1, i32 5000)
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %56
45: ; preds = %41
%46 = load i64, i64* @errno
%47 = load i64, i64* @EINTR
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45
br label %41
50: ; preds = %45
br label %51
51: ; preds = %50
%52 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; No predecessors!
br label %62
56: ; preds = %41
%57 = load i32, i32* %10
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %59, %56
br label %62
62: ; preds = %61, %55
%63 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 1
%64 = load i64, i64* %63
%65 = load i64, i64* @POLLIN
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = load i32, i32* %6
%69 = call i32 @read(i32 %68, i32* %9, i32 4)
%70 = call i32 (...) @foo()
br label %71
71: ; preds = %67, %62
br label %41
}

LLVM IR (-O2):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32 }
%struct.TYPE_3__ = type { i32 }
%struct.pollfd = type { i32, i64, i64 }
@CLOCK_REALTIME = global i32 0
@.str = [15 x i8] c"timerfd_create\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"timerfd_settime\00"
@POLLIN = global i64 0
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [5 x i8] c"poll\00"
@str = [8 x i8] c"timeout\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.itimerspec
%4 = alloca %struct.pollfd
%5 = alloca i32
%6 = load i32, i32* @CLOCK_REALTIME
%7 = tail call i32 @timerfd_create(i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = bitcast %struct.itimerspec* %3 to i8*
%15 = call i32 @memset(%struct.itimerspec* %3, i32 0, i32 8)
%16 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %3, i64 0, i32 1, i32 0
store i32 3, i32* %16
%17 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %3, i64 0, i32 0, i32 0
store i32 1, i32* %17
%18 = call i32 @timerfd_settime(i32 %7, i32 0, %struct.itimerspec* %3, i32* null)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %13
%25 = bitcast %struct.pollfd* %4 to i8*
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %7, i32* %26
%27 = load i64, i64* @POLLIN
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
store i64 %27, i64* %28
%29 = bitcast i32* %5 to i8*
%30 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
br label %31
31: ; preds = %47, %24
%32 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 5000)
switch i32 %32, label %43 [
i32 -1, label %33
i32 0, label %41
]
33: ; preds = %31
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %47, label %37
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %31
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %31, %41
%44 = load i64, i64* %30
%45 = load i64, i64* @POLLIN
%46 = icmp eq i64 %44, %45
br i1 %46, label %48, label %47
47: ; preds = %43, %48, %33
br label %31
48: ; preds = %43
%49 = call i32 @read(i32 %7, i32* %5, i32 4)
%50 = call i32 (...) @foo()
br label %47
}