#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 int /*<<< orphan*/ data;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ RTC_IRQP_SET;
int /*<<< orphan*/ RTC_PIE_OFF;
int /*<<< orphan*/ RTC_PIE_ON;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errx(int, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, unsigned long *, int);
int time(int /*<<< orphan*/ *);
int main(void) {
  unsigned long i = 0;
  unsigned long data = 0;
  int fd = open("/dev/rtc", O_RDONLY);
  if (fd < 0)
    errx(1, "open() fail");
  if (ioctl(fd, RTC_IRQP_SET, 4) < 0)
    errx(1, "ioctl(RTC_IRQP_SET) fail");
  if (ioctl(fd, RTC_PIE_ON, 0) < 0)
    errx(1, "ioctl(RTC_PIE_ON)");
  for (i = 0; i < 100; i++) {
    if (read(fd, &data, sizeof(data)) < 0)
      errx(1, "read() error");
    printf("timer %d\n", time(NULL));
  }
  if (ioctl(fd, RTC_PIE_OFF, 0) < 0)
    errx(1, "ioctl(RTC_PIE_OFF)");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c2 <main+0x52>
mov    $0x1,%edi
lea    0xe4c(%rip),%rsi
call   1030 <errx@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11f3 <main+0x83>
mov    $0x1,%edi
lea    0xe27(%rip),%rsi
call   1030 <errx@plt>
mov    -0x1c(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1221 <main+0xb1>
mov    $0x1,%edi
lea    0xe12(%rip),%rsi
call   1030 <errx@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x64,-0x10(%rbp)
jae    1288 <main+0x118>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    1260 <main+0xf0>
mov    $0x1,%edi
lea    0xde5(%rip),%rsi
call   1030 <errx@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%esi
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1229 <main+0xb9>
mov    -0x1c(%rbp),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    12b6 <main+0x146>
mov    $0x1,%edi
lea    0xda6(%rip),%rsi
call   1030 <errx@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    11b2 <main+0x42>
lea    0xe61(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    $0x4,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    11de <main+0x6e>
lea    0xe41(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    1207 <main+0x97>
lea    0xe31(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    $0x64,%ebp
lea    0x8(%rsp),%r14
lea    0xe28(%rip),%r15
lea    0xe2e(%rip),%rbx
jmp    1247 <main+0xd7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1080 <time@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
add    $0xffffffffffffffff,%rbp
je     126b <main+0xfb>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1070 <read@plt>
test   %rax,%rax
jns    1230 <main+0xc0>
mov    $0x1,%edi
mov    %r15,%rsi
call   1030 <errx@plt>
jmp    1230 <main+0xc0>
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    1294 <main+0x124>
lea    0xdcd(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/rtc\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open() fail\00"
@RTC_IRQP_SET = global i32 0
@.str.2 = [25 x i8] c"ioctl(RTC_IRQP_SET) fail\00"
@RTC_PIE_ON = global i32 0
@.str.3 = [18 x i8] c"ioctl(RTC_PIE_ON)\00"
@.str.4 = [13 x i8] c"read() error\00"
@.str.5 = [10 x i8] c"timer %d\0A\00"
@RTC_PIE_OFF = global i32 0
@.str.6 = [19 x i8] c"ioctl(RTC_PIE_OFF)\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
store i64 0, i64* %2
store i64 0, i64* %3
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @errx(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* %4
%13 = load i32, i32* @RTC_IRQP_SET
%14 = call i64 @ioctl(i32 %12, i32 %13, i32 4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = load i32, i32* %4
%20 = load i32, i32* @RTC_PIE_ON
%21 = call i64 @ioctl(i32 %19, i32 %20, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
store i64 0, i64* %2
br label %26
26: ; preds = %38, %25
%27 = load i64, i64* %2
%28 = icmp ult i64 %27, 100
br i1 %28, label %29, label %41
29: ; preds = %26
%30 = load i32, i32* %4
%31 = call i64 @read(i32 %30, i64* %3, i32 8)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @errx(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = call i32 @time(i32* null)
%37 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35
%39 = load i64, i64* %2
%40 = add i64 %39, 1
store i64 %40, i64* %2
br label %26
41: ; preds = %26
%42 = load i32, i32* %4
%43 = load i32, i32* @RTC_PIE_OFF
%44 = call i64 @ioctl(i32 %42, i32 %43, i32 0)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %46, %41
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/rtc\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open() fail\00"
@RTC_IRQP_SET = global i32 0
@.str.2 = [25 x i8] c"ioctl(RTC_IRQP_SET) fail\00"
@RTC_PIE_ON = global i32 0
@.str.3 = [18 x i8] c"ioctl(RTC_PIE_ON)\00"
@.str.4 = [13 x i8] c"read() error\00"
@.str.5 = [10 x i8] c"timer %d\0A\00"
@RTC_PIE_OFF = global i32 0
@.str.6 = [19 x i8] c"ioctl(RTC_PIE_OFF)\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* @RTC_IRQP_SET
%10 = tail call i64 @ioctl(i32 %4, i32 %9, i32 4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = load i32, i32* @RTC_PIE_ON
%16 = tail call i64 @ioctl(i32 %4, i32 %15, i32 0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
br label %21
21: ; preds = %20, %27
%22 = phi i64 [ %30, %27 ], [ 0, %20 ]
%23 = call i64 @read(i32 %4, i64* %1, i32 8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = tail call i32 @time(i32* null)
%29 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %28)
%30 = add nuw nsw i64 %22, 1
%31 = icmp eq i64 %30, 100
br i1 %31, label %32, label %21
32: ; preds = %27
%33 = load i32, i32* @RTC_PIE_OFF
%34 = tail call i64 @ioctl(i32 %4, i32 %33, i32 0)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = tail call i32 @close(i32 %4)
ret i32 0
}