#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 */

/* Type definitions */
typedef long u_int64_t;
typedef int /*<<< orphan*/ ticks;
struct pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
int POLLIN;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ poll(struct pollfd *, int, int);
int read(int, long *, int);
int /*<<< orphan*/ stderr;
long waittmr(int tfd, int timeo) {
  u_int64_t ticks;
  struct pollfd pfd;
  pfd.fd = tfd;
  pfd.events = POLLIN;
  pfd.revents = 0;
  if (poll(&pfd, 1, timeo) < 0) {
    perror("poll");
    return -1;
  }
  if ((pfd.revents & POLLIN) == 0) {
    fprintf(stderr, "no ticks happened\n");
    return -1;
  }
  if (read(tfd, &ticks, sizeof(ticks)) != sizeof(ticks)) {
    perror("timerfd read");
    return -1;
  }
  return ticks;
}


x86 assembly (clang, -O0):
<waittmr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%edx
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
cmp    $0x0,%rax
jge    119b <waittmr+0x5b>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1210 <waittmr+0xd0>
mov    -0x20(%rbp),%eax
mov    0x2e43(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11d2 <waittmr+0x92>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1210 <waittmr+0xd0>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1030 <read@plt>
cltq
cmp    $0x8,%rax
je     1208 <waittmr+0xc8>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1210 <waittmr+0xd0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waittmr>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%edx
mov    %edi,%ebx
mov    %edi,(%rsp)
mov    0x2e93(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
test   %rax,%rax
js     11b3 <waittmr+0x73>
mov    (%r14),%eax
test   %eax,0x8(%rsp)
je     119c <waittmr+0x5c>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1030 <read@plt>
cmp    $0x8,%eax
jne    11bc <waittmr+0x7c>
mov    0x10(%rsp),%rax
jmp    11cf <waittmr+0x8f>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11c8 <waittmr+0x88>
lea    0xe46(%rip),%rdi
jmp    11c3 <waittmr+0x83>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffffffffffff,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@.str = [5 x i8] c"poll\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"no ticks happened\0A\00"
@.str.2 = [13 x i8] c"timerfd read\00"
define i64 @waittmr(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca %struct.pollfd
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @POLLIN
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 2
store i32 0, i32* %12
%13 = load i32, i32* %5
%14 = call i64 @poll(%struct.pollfd* %7, i32 1, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %3
br label %36
18: ; preds = %2
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 2
%20 = load i32, i32* %19
%21 = load i32, i32* @POLLIN
%22 = and i32 %20, %21
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i64 -1, i64* %3
br label %36
27: ; preds = %18
%28 = load i32, i32* %4
%29 = call i32 @read(i32 %28, i64* %6, i32 8)
%30 = sext i32 %29 to i64
%31 = icmp ne i64 %30, 8
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i64 -1, i64* %3
br label %36
34: ; preds = %27
%35 = load i64, i64* %6
store i64 %35, i64* %3
br label %36
36: ; preds = %34, %32, %24, %16
%37 = load i64, i64* %3
ret i64 %37
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@.str = [5 x i8] c"poll\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"no ticks happened\0A\00"
@.str.2 = [13 x i8] c"timerfd read\00"
define i64 @waittmr(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca %struct.pollfd
%5 = bitcast i64* %3 to i8*
%6 = bitcast %struct.pollfd* %4 to i8*
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %0, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
store i32 0, i32* %10
%11 = call i64 @poll(%struct.pollfd* %4, i32 1, i32 %1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %30
15: ; preds = %2
%16 = load i32, i32* %10
%17 = load i32, i32* @POLLIN
%18 = and i32 %17, %16
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %30
23: ; preds = %15
%24 = call i32 @read(i32 %0, i64* %3, i32 8)
%25 = icmp eq i32 %24, 8
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %30
28: ; preds = %23
%29 = load i64, i64* %3
br label %30
30: ; preds = %28, %26, %20, %13
%31 = phi i64 [ -1, %13 ], [ -1, %20 ], [ -1, %26 ], [ %29, %28 ]
ret i64 %31
}