#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 time_t;
struct timeval {
  int tv_sec;
  int /*<<< orphan*/ tv_usec;
};
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_value;
  TYPE_1__ it_interval;
};
typedef int /*<<< orphan*/ expirations;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ TFD_TIMER_ABSTIME;
int /*<<< orphan*/ close(int);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int timerfd_create(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ timerfd_settime(int, int /*<<< orphan*/, struct itimerspec *,
                           int /*<<< orphan*/ *);
int main() {
  struct itimerspec ts;
  struct timeval tv;
  uint64_t expirations;
  time_t seconds;
  int timerfd;
  printf("Getting a realtime timer\n");
  if ((timerfd = timerfd_create(CLOCK_REALTIME, 0)) == -1) {
    perror("timerfd_create");
    return 1;
  }
  printf("Getting the current time:\n");
  if (gettimeofday(&tv, NULL)) {
    perror("gettimeofday");
    return 1;
  }
  printf("%ld.%06ld\n", tv.tv_sec, tv.tv_usec);
  ts.it_interval.tv_sec = 60;
  ts.it_interval.tv_nsec = 0;
  ts.it_value.tv_nsec = 0;
  seconds = tv.tv_sec;
  seconds -= seconds % 60;
  seconds += 60;
  ts.it_value.tv_sec = seconds;
  ts.it_value.tv_sec = 60;
  printf("Setting the timer for the next full minute (%ld)\n",
         ts.it_value.tv_sec);
  if (timerfd_settime(timerfd, TFD_TIMER_ABSTIME, &ts, NULL)) {
    perror("timerfd_settime");
    return 1;
  }
  printf("Preparing to sleep\n");
  while (read(timerfd, &expirations, sizeof(expirations))) {
    printf("Timer expired %lu times, getting the current time again: ",
           expirations);
    if (gettimeofday(&tv, NULL)) {
      perror("gettimeofday");
      return 1;
    }
    printf("%ld.%06ld\n", tv.tv_sec, tv.tv_usec);
  }
  close(timerfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1070 <timerfd_create@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    11c1 <main+0x51>
lea    0xe6a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1335 <main+0x1c5>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmp    $0x0,%rax
je     11fe <main+0x8e>
lea    0xe57(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1335 <main+0x1c5>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x3c,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%eax
sub    %edx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x3c,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x3c,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x3c(%rbp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <timerfd_settime@plt>
cmp    $0x0,%rax
je     12a4 <main+0x134>
lea    0xdfb(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1335 <main+0x1c5>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x3c(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
je     1326 <main+0x1b6>
mov    -0x34(%rbp),%esi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmp    $0x0,%rax
je     130d <main+0x19d>
lea    0xd48(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1335 <main+0x1c5>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b2 <main+0x142>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
lea    0xf0f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1080 <timerfd_create@plt>
cmp    $0xffffffff,%eax
je     12e2 <main+0x162>
mov    %eax,%ebx
lea    0xf01(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
jne    12d9 <main+0x159>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x3c,0x20(%rsp)
movq   $0x0,0x28(%rsp)
movq   $0x0,0x18(%rsp)
movl   $0x3c,0x10(%rsp)
lea    0xe16(%rip),%rdi
mov    $0x3c,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
xor    %ecx,%ecx
call   10a0 <timerfd_settime@plt>
test   %rax,%rax
jne    12eb <main+0x16b>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <read@plt>
test   %rax,%rax
je     12c2 <main+0x142>
lea    0xe03(%rip),%r14
mov    %rsp,%r15
lea    0xdac(%rip),%r12
lea    0xc(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    0xc(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
jne    12d9 <main+0x159>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1070 <read@plt>
test   %rax,%rax
jne    1280 <main+0x100>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd2f(%rip),%rdi
jmp    12f2 <main+0x172>
lea    0xd17(%rip),%rdi
jmp    12f2 <main+0x172>
lea    0xd67(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    12cb <main+0x14b>

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.timeval = type { i32, i32 }
@.str = [26 x i8] c"Getting a realtime timer\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.1 = [15 x i8] c"timerfd_create\00"
@.str.2 = [27 x i8] c"Getting the current time:\0A\00"
@.str.3 = [13 x i8] c"gettimeofday\00"
@.str.4 = [11 x i8] c"%ld.%06ld\0A\00"
@.str.5 = [50 x i8] c"Setting the timer for the next full minute (%ld)\0A\00"
@TFD_TIMER_ABSTIME = global i32 0
@.str.6 = [16 x i8] c"timerfd_settime\00"
@.str.7 = [20 x i8] c"Preparing to sleep\0A\00"
@.str.8 = [58 x i8] c"Timer expired %lu times, getting the current time again: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.itimerspec
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @CLOCK_REALTIME
%9 = call i32 @timerfd_create(i32 %8, i32 0)
store i32 %9, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %76
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%15 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %76
19: ; preds = %13
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %21, i32 %23)
%25 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %25, i32 0, i32 0
store i32 60, i32* %26
%27 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %27, i32 0, i32 1
store i64 0, i64* %28
%29 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %29, i32 0, i32 1
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%32 = load i32, i32* %31
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = srem i32 %33, 60
%35 = load i32, i32* %5
%36 = sub nsw i32 %35, %34
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 60
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i32 0, i32 0
%41 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i32 0, i32 0
%43 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %42, i32 0, i32 0
store i32 60, i32* %43
%44 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %2, i32 0, i32 0
%45 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %6
%49 = load i32, i32* @TFD_TIMER_ABSTIME
%50 = call i64 @timerfd_settime(i32 %48, i32 %49, %struct.itimerspec* %2, i32* null)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %19
%53 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %76
54: ; preds = %19
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
br label %56
56: ; preds = %67, %54
%57 = load i32, i32* %6
%58 = call i64 @read(i32 %57, i32* %4, i32 4)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %73
60: ; preds = %56
%61 = load i32, i32* %4
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.8, i64 0, i64 0), i32 %61)
%63 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %60
%66 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %76
67: ; preds = %60
%68 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%69 = load i32, i32* %68
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%71 = load i32, i32* %70
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %69, i32 %71)
br label %56
73: ; preds = %56
%74 = load i32, i32* %6
%75 = call i32 @close(i32 %74)
store i32 0, i32* %1
br label %76
76: ; preds = %73, %65, %52, %17, %11
%77 = load i32, i32* %1
ret i32 %77
}

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.timeval = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str.1 = [15 x i8] c"timerfd_create\00"
@.str.3 = [13 x i8] c"gettimeofday\00"
@.str.4 = [11 x i8] c"%ld.%06ld\0A\00"
@.str.5 = [50 x i8] c"Setting the timer for the next full minute (%ld)\0A\00"
@TFD_TIMER_ABSTIME = global i32 0
@.str.6 = [16 x i8] c"timerfd_settime\00"
@.str.8 = [58 x i8] c"Timer expired %lu times, getting the current time again: \00"
@str = [25 x i8] c"Getting a realtime timer\00"
@str.9 = [26 x i8] c"Getting the current time:\00"
@str.10 = [19 x i8] c"Preparing to sleep\00"
define i32 @main() {
%1 = alloca %struct.itimerspec
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = bitcast %struct.itimerspec* %1 to i8*
%5 = bitcast %struct.timeval* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%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 = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %54
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.9, i64 0, i64 0))
%15 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %54
19: ; preds = %13
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%23 = load i32, i32* %22
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %21, i32 %23)
%25 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %1, i64 0, i32 1, i32 0
store i32 60, i32* %25
%26 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %1, i64 0, i32 1, i32 1
store i64 0, i64* %26
%27 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %1, i64 0, i32 0, i32 1
store i64 0, i64* %27
%28 = getelementptr inbounds %struct.itimerspec, %struct.itimerspec* %1, i64 0, i32 0, i32 0
store i32 60, i32* %28
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i32 60)
%30 = load i32, i32* @TFD_TIMER_ABSTIME
%31 = call i64 @timerfd_settime(i32 %9, i32 %30, %struct.itimerspec* %1, i32* null)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %19
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %54
35: ; preds = %19
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
%37 = call i64 @read(i32 %9, i32* %3, i32 4)
%38 = icmp eq i64 %37, 0
br i1 %38, label %52, label %39
39: ; preds = %35, %46
%40 = load i32, i32* %3
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.8, i64 0, i64 0), i32 %40)
%42 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %54
46: ; preds = %39
%47 = load i32, i32* %20
%48 = load i32, i32* %22
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %47, i32 %48)
%50 = call i64 @read(i32 %9, i32* %3, i32 4)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %39
52: ; preds = %46, %35
%53 = call i32 @close(i32 %9)
br label %54
54: ; preds = %52, %44, %33, %17, %11
%55 = phi i32 [ 1, %11 ], [ 1, %17 ], [ 1, %33 ], [ 1, %44 ], [ 0, %52 ]
ret i32 %55
}