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

/* 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*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, unsigned long *, int);
int main(int argc, char *argv[]) {
  unsigned long i = 0;
  unsigned long data = 0;
  int retval = 0;
  int fd = open("/dev/rtc", O_RDONLY);
  if (fd < 0) {
    perror("open");
    exit(errno);
  }
  if (ioctl(fd, RTC_IRQP_SET, 1) < 0) {
    perror("ioctl(RTC_IRQP_SET)");
    close(fd);
    exit(errno);
  }
  if (ioctl(fd, RTC_PIE_ON, 0) < 0) {
    perror("ioctl(RTC_PIE_ON)");
    close(fd);
    exit(errno);
  }
  for (i = 0; i < 100; i++) {
    if (read(fd, &data, sizeof(unsigned long)) < 0) {
      perror("read");
      close(fd);
      exit(errno);
    }
    printf("timer\n");
  }
  ioctl(fd, RTC_PIE_OFF, 0);
  close(fd);
  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)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11d9 <main+0x69>
lea    0xe43(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    121b <main+0xab>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    125a <main+0xea>
lea    0xde3(%rip),%rdi
call   1080 <perror@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0x64,-0x18(%rbp)
jae    12c7 <main+0x157>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jge    12aa <main+0x13a>
lea    0xda5(%rip),%rdi
call   1080 <perror@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xd88(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1262 <main+0xf2>
mov    -0x28(%rbp),%edi
mov    0x2ce7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movq   $0x0,(%rsp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     125d <main+0xed>
mov    %eax,%r15d
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    $0x1,%edx
call   1040 <ioctl@plt>
test   %rax,%rax
js     126b <main+0xfb>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
test   %rax,%rax
js     1274 <main+0x104>
mov    $0x64,%ebp
mov    %rsp,%r14
lea    0xe50(%rip),%rbx
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
test   %rax,%rax
js     123b <main+0xcb>
mov    %rbx,%rdi
call   1030 <puts@plt>
add    $0xffffffffffffffff,%rbp
jne    11f0 <main+0x80>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xda5(%rip),%rdi
call   1080 <perror@plt>
jmp    124f <main+0xdf>
lea    0xd9c(%rip),%rdi
jmp    1242 <main+0xd2>
lea    0xda7(%rip),%rdi
jmp    1242 <main+0xd2>

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

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