#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 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;
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 /*<<< orphan*/ printf(char *, int);
int 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);
  uint64_t val;
  int ret;
  while (1) {
    ret = read(timerfd, &val, sizeof val);
    if (ret == -1 && errno == EINTR)
      continue;
    printf("ret = %d\n", ret);
    foo();
  }
  close(timerfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <timerfd_create@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d2 <main+0x52>
jmp    11b8 <main+0x38>
lea    0xe41(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   10a0 <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   1090 <timerfd_settime@plt>
cmp    $0xffffffff,%eax
jne    122a <main+0xaa>
jmp    1210 <main+0x90>
lea    0xdf8(%rip),%rdi
call   1080 <perror@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    122f <main+0xaf>
mov    -0x14(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    126c <main+0xec>
mov    0x2d8c(%rip),%rax
mov    (%rax),%rax
mov    0x2d72(%rip),%rcx
cmp    (%rcx),%rax
jne    126c <main+0xec>
jmp    122f <main+0xaf>
mov    -0x28(%rbp),%esi
lea    0xda9(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <foo@plt>
jmp    122f <main+0xaf>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <timerfd_create@plt>
cmp    $0xffffffff,%eax
je     1231 <main+0xb1>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movabs $0x300000001,%rax
mov    %rax,(%rsp)
mov    %ebx,%edi
xor    %esi,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1090 <timerfd_settime@plt>
cmp    $0xffffffff,%eax
je     123a <main+0xba>
lea    0xc(%rsp),%r14
mov    0x2df9(%rip),%r12
mov    0x2de2(%rip),%r13
lea    0xe2a(%rip),%r15
jmp    1211 <main+0x91>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1070 <foo@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    1200 <main+0x80>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1211 <main+0x91>
jmp    1200 <main+0x80>
lea    0xdc8(%rip),%rdi
jmp    1241 <main+0xc1>
lea    0xdce(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32 }
%struct.TYPE_3__ = type { i32 }
@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"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [10 x i8] c"ret = %d\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 i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @CLOCK_REALTIME
%11 = call i32 @timerfd_create(i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %2
br label %15
15: ; preds = %14
%16 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; No predecessors!
br label %20
20: ; preds = %19, %2
%21 = call i32 @memset(%struct.itimerspec* %7, i32 0, i32 8)
%22 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %7, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %22, i32 0, i32 0
store i32 3, i32* %23
%24 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %7, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %24, i32 0, i32 0
store i32 1, i32* %25
%26 = load i32, i32* %6
%27 = call i32 @timerfd_settime(i32 %26, i32 0, %struct.itimerspec* %7, i32* null)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %35
29: ; preds = %20
br label %30
30: ; preds = %29
%31 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; No predecessors!
br label %35
35: ; preds = %34, %20
br label %36
36: ; preds = %35, %45, %46
%37 = load i32, i32* %6
%38 = call i32 @read(i32 %37, i32* %8, i32 4)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINTR
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41
br label %36
46: ; preds = %41, %36
%47 = load i32, i32* %9
%48 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = call i32 (...) @foo()
br label %36
}

LLVM IR (-O2):
%struct.itimerspec = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32 }
%struct.TYPE_3__ = type { i32 }
@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"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [10 x i8] c"ret = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.itimerspec
%4 = alloca i32
%5 = load i32, i32* @CLOCK_REALTIME
%6 = tail call i32 @timerfd_create(i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = bitcast %struct.itimerspec* %3 to i8*
%14 = call i32 @memset(%struct.itimerspec* %3, i32 0, i32 8)
%15 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %3, i64 0, i32 1, i32 0
store i32 3, i32* %15
%16 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %3, i64 0, i32 0, i32 0
store i32 1, i32* %16
%17 = call i32 @timerfd_settime(i32 %6, i32 0, %struct.itimerspec* %3, i32* null)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %12
%24 = bitcast i32* %4 to i8*
br label %25
25: ; preds = %35, %23
%26 = call i32 @read(i32 %6, i32* %4, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %35, label %32
32: ; preds = %28, %25
%33 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%34 = call i32 (...) @foo()
br label %35
35: ; preds = %32, %28
br label %25
}