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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int errno;
long getpagesize();
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, long);
void *mmap(int /*<<< orphan*/ *, long, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, int *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main() {
  int uiofd, intr_cnt = 0;
  void *map_addr;
  fd_set readset;
  uiofd = open("/dev/uio0", O_RDWR);
  if (uiofd < 0) {
    perror("uio open:");
    return errno;
  }
  long page_size = getpagesize();
  map_addr =
      mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, uiofd, 0);
  while (1) {
    FD_ZERO(&readset);
    FD_SET(uiofd, &readset);
    if (select(uiofd + 1, &readset, NULL, NULL, NULL) <= 0) {
      perror("select:");
      break;
    }
    if (FD_ISSET(uiofd, &readset)) {
      if (read(uiofd, &intr_cnt, 4) != 4) {
        perror("uio read:");
        break;
      }
      printf("intr count %d\n", intr_cnt);
      memset(map_addr, 0, page_size);
    }
  }
  munmap(map_addr, page_size);
  close(uiofd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    0xe0a(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1225 <main+0x55>
lea    0xdfb(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    132d <main+0x15d>
mov    $0x0,%al
call   10e0 <getpagesize@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    0x2d7c(%rip),%rax
or     (%rax),%edx
mov    0x2d9b(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10a0 <select@plt>
cmp    $0x0,%rax
jg     12ad <main+0xdd>
lea    0xd71(%rip),%rdi
call   10d0 <perror@plt>
jmp    1311 <main+0x141>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     130c <main+0x13c>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
je     12ee <main+0x11e>
lea    0xd38(%rip),%rdi
call   10d0 <perror@plt>
jmp    1311 <main+0x141>
mov    -0xc(%rbp),%esi
lea    0xd2e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1060 <memset@plt>
jmp    1263 <main+0x93>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10b0 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,(%rsp)
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0xe0e(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
jns    1216 <main+0x46>
lea    0xe08(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%r13d
jmp    1332 <main+0x162>
mov    %eax,%ebp
xor    %r13d,%r13d
xor    %eax,%eax
call   10e0 <getpagesize@plt>
mov    %rax,%r15
mov    0x2dcc(%rip),%rax
mov    0x2d8d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2daa(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   10f0 <FD_SET@plt>
mov    %ebp,%ebx
add    $0x1,%ebx
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
lea    0xd8d(%rip),%rdi
test   %rax,%rax
jle    131b <main+0x14b>
lea    0x4(%rsp),%r13
lea    0xd8a(%rip),%r14
jmp    12c8 <main+0xf8>
xchg   %ax,%ax
mov    %r13,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   10f0 <FD_SET@plt>
mov    %ebx,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %rax,%rax
jle    1305 <main+0x135>
mov    %ebp,%edi
mov    %r13,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12a0 <main+0xd0>
mov    %ebp,%edi
mov    %rsp,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
jne    1311 <main+0x141>
mov    (%rsp),%esi
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1060 <memset@plt>
jmp    12a0 <main+0xd0>
xor    %r13d,%r13d
lea    0xd05(%rip),%rdi
jmp    131b <main+0x14b>
lea    0xd04(%rip),%rdi
xor    %r13d,%r13d
call   10d0 <perror@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   10b0 <munmap@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/uio0\00"
@O_RDWR = global i32 0
@.str.1 = [10 x i8] c"uio open:\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [8 x i8] c"select:\00"
@.str.3 = [10 x i8] c"uio read:\00"
@.str.4 = [15 x i8] c"intr count %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @errno
store i32 %13, i32* %1
br label %56
14: ; preds = %0
%15 = call i64 (...) @getpagesize()
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_SHARED
%21 = load i32, i32* %2
%22 = call i8* @mmap(i32* null, i64 %16, i32 %19, i32 %20, i32 %21, i32 0)
store i8* %22, i8** %4
br label %23
23: ; preds = %14, %49
%24 = call i32 @FD_ZERO(i32* %5)
%25 = load i32, i32* %2
%26 = call i32 @FD_SET(i32 %25, i32* %5)
%27 = load i32, i32* %2
%28 = add nsw i32 %27, 1
%29 = call i64 @select(i32 %28, i32* %5, i32* null, i32* null, i32* null)
%30 = icmp sle i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %50
33: ; preds = %23
%34 = load i32, i32* %2
%35 = call i64 @FD_ISSET(i32 %34, i32* %5)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %33
%38 = load i32, i32* %2
%39 = call i32 @read(i32 %38, i32* %3, i32 4)
%40 = icmp ne i32 %39, 4
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %50
43: ; preds = %37
%44 = load i32, i32* %3
%45 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = load i8*, i8** %4
%47 = load i64, i64* %6
%48 = call i32 @memset(i8* %46, i32 0, i64 %47)
br label %49
49: ; preds = %43, %33
br label %23
50: ; preds = %41, %31
%51 = load i8*, i8** %4
%52 = load i64, i64* %6
%53 = call i32 @munmap(i8* %51, i64 %52)
%54 = load i32, i32* %2
%55 = call i32 @close(i32 %54)
store i32 0, i32* %1
br label %56
56: ; preds = %50, %11
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/uio0\00"
@O_RDWR = global i32 0
@.str.1 = [10 x i8] c"uio open:\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [8 x i8] c"select:\00"
@.str.3 = [10 x i8] c"uio read:\00"
@.str.4 = [15 x i8] c"intr count %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @errno
br label %43
11: ; preds = %0
%12 = tail call i64 (...) @getpagesize()
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i8* @mmap(i32* null, i64 %12, i32 %15, i32 %16, i32 %6, i32 0)
%18 = call i32 @FD_ZERO(i32* %2)
%19 = call i32 @FD_SET(i32 %6, i32* %2)
%20 = add nuw nsw i32 %6, 1
%21 = call i64 @select(i32 %20, i32* %2, i32* null, i32* null, i32* null)
%22 = icmp slt i64 %21, 1
br i1 %22, label %38, label %23
23: ; preds = %11, %33
%24 = call i64 @FD_ISSET(i32 %6, i32* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = call i32 @read(i32 %6, i32* %1, i32 4)
%28 = icmp eq i32 %27, 4
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i32, i32* %1
%31 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = call i32 @memset(i8* %17, i32 0, i64 %12)
br label %33
33: ; preds = %29, %23
%34 = call i32 @FD_ZERO(i32* %2)
%35 = call i32 @FD_SET(i32 %6, i32* %2)
%36 = call i64 @select(i32 %20, i32* %2, i32* null, i32* null, i32* null)
%37 = icmp slt i64 %36, 1
br i1 %37, label %38, label %23
38: ; preds = %26, %33, %11
%39 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %33 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %26 ]
%40 = call i32 @perror(i8* %39)
%41 = call i32 @munmap(i8* %17, i64 %12)
%42 = call i32 @close(i32 %6)
br label %43
43: ; preds = %38, %8
%44 = phi i32 [ %10, %8 ], [ 0, %38 ]
ret i32 %44
}